]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiDialog.cpp
Fix readability
[lyx.git] / src / frontends / qt / GuiDialog.cpp
1 /**
2  * \file Dialog.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "FileDialog.h"
14 #include "GuiApplication.h"
15 #include "GuiDialog.h"
16 #include "GuiView.h"
17 #include "qt_helpers.h"
18
19 #include "support/debug.h"
20 #include "support/filetools.h"
21
22 #include <QCloseEvent>
23 #include <QDialogButtonBox>
24 #include <QColorDialog>
25
26 using namespace std;
27
28 namespace lyx {
29 namespace frontend {
30
31 GuiDialog::GuiDialog(GuiView & lv, QString const & name, QString const & title)
32         : QDialog(&lv), Dialog(lv, name, title), updating_(false),
33       is_closing_(false), apply_stopped_(false)
34 {
35         connect(&lv, SIGNAL(bufferViewChanged()),
36                 this, SLOT(onBufferViewChanged()));
37
38         // remove question marks from Windows dialogs
39         setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
40 }
41
42
43 void GuiDialog::closeEvent(QCloseEvent * ev)
44 {
45         slotClose();
46         ev->accept();
47 }
48
49
50 void GuiDialog::setButtonsValid(bool valid)
51 {
52         bc().setValid(valid);
53 }
54
55
56 void GuiDialog::slotApply()
57 {
58         setApplyStopped(false);
59         apply();
60         if (applyStopped())
61                 return;
62         bc().apply();
63 }
64
65
66 void GuiDialog::slotAutoApply()
67 {
68         apply();
69         bc().autoApply();
70 }
71
72
73 void GuiDialog::slotOK()
74 {
75         is_closing_ = true;
76         setApplyStopped(false);
77         apply();
78         if (applyStopped())
79                 return;
80         is_closing_ = false;
81         hideView();
82         bc().ok();
83 }
84
85
86 void GuiDialog::slotClose()
87 {
88         hideView();
89         bc().cancel();
90 }
91
92
93 void GuiDialog::slotRestore()
94 {
95         // Tell the controller that a request to refresh the dialog's contents
96         // has been received. It's up to the controller to supply the necessary
97         // info by calling GuiDialog::updateView().
98         updateDialog();
99         bc().restore();
100 }
101
102
103 void GuiDialog::slotButtonBox(QAbstractButton * button)
104 {
105         QDialogButtonBox * bbox = qobject_cast<QDialogButtonBox*>(sender());
106         switch (bbox->standardButton(button)) {
107         case QDialogButtonBox::Ok:
108                 slotOK();
109                 break;
110         case QDialogButtonBox::Apply:
111                 slotApply();
112                 break;
113         case QDialogButtonBox::Cancel:
114         case QDialogButtonBox::Close:
115                 slotClose();
116                 break;
117         case QDialogButtonBox::Reset:
118                 slotRestore();
119                 break;
120         case QDialogButtonBox::RestoreDefaults:
121                 slotRestoreDefaults();
122                 break;
123         default:
124                 break;
125         }
126 }
127
128
129 void GuiDialog::changed()
130 {
131         if (updating_)
132                 return;
133         bc().setValid(isValid());
134 }
135
136
137 void GuiDialog::enableView(bool enable)
138 {
139         if (!enable) {
140                 bc().setReadOnly(true);
141                 bc().setValid(false);
142         }
143         Dialog::enableView(enable);
144 }
145
146
147 void GuiDialog::updateView()
148 {
149         setUpdatesEnabled(false);
150
151         bc().setReadOnly(isBufferReadonly());
152         // protect the BC from unwarranted state transitions
153         updating_ = true;
154         updateContents();
155         updating_ = false;
156         // The widgets may not be valid, so refresh the button controller
157         bc().refresh();
158
159         setUpdatesEnabled(true);
160 }
161
162 QString GuiDialog::browseFile(QString const & filename,
163         QString const & title,
164         QStringList const & filters,
165         bool save,
166         QString const & label1,
167         QString const & dir1,
168         QString const & label2,
169         QString const & dir2,
170         QString const & fallback_dir)
171 {
172         QString lastPath = ".";
173         if (!filename.isEmpty())
174                 lastPath = onlyPath(filename);
175         else if(!fallback_dir.isEmpty())
176                 lastPath = fallback_dir;
177
178         FileDialog dlg(title);
179         dlg.setButton1(label1, dir1);
180         dlg.setButton2(label2, dir2);
181
182         FileDialog::Result result;
183
184         if (save)
185                 result = dlg.save(lastPath, filters, onlyFileName(filename));
186         else
187                 result = dlg.open(lastPath, filters, onlyFileName(filename));
188
189         if (guiApp->platformName() == "cocoa") {
190                 QWidget * dialog = asQWidget();
191                 dialog->raise();
192                 dialog->activateWindow();
193         }
194
195         return result.second;
196 }
197
198
199 /** Launch a file dialog and return the chosen directory.
200         pathname: a suggested pathname.
201         title: the title of the dialog.
202         dir1 = (name, dir), dir2 = (name, dir): extra buttons on the dialog.
203 */
204 QString GuiDialog::browseDir(QString const & pathname,
205         QString const & title,
206         QString const & label1,
207         QString const & dir1,
208         QString const & label2,
209         QString const & dir2)
210 {
211         QString lastPath = ".";
212         if (!pathname.isEmpty())
213                 lastPath = onlyPath(pathname);
214
215         FileDialog dlg(title);
216         dlg.setButton1(label1, dir1);
217         dlg.setButton2(label2, dir2);
218
219         FileDialog::Result const result =
220                 dlg.opendir(lastPath, onlyFileName(pathname));
221         
222         if (guiApp->platformName() == "cocoa") {
223                 QWidget * dialog = asQWidget();
224                 dialog->raise();
225                 dialog->activateWindow();
226         }
227
228         return result.second;
229 }
230
231 QString GuiDialog::browseRelToParent(
232         QString const & filename,
233         QString const & relpath,
234         QString const & title,
235         QStringList const & filters,
236         bool save,
237         QString const & label1,
238         QString const & dir1,
239         QString const & label2,
240         QString const & dir2)
241 {
242         QString const fname = makeAbsPath(filename, relpath);
243
244         QString const outname =
245                 browseFile(fname, title, filters, save, label1, dir1, label2, dir2);
246
247         QString const reloutname =
248                 toqstr(support::makeRelPath(qstring_to_ucs4(outname), qstring_to_ucs4(relpath)));
249
250         if (reloutname.startsWith("../"))
251                 return outname;
252         else
253                 return reloutname;
254 }
255
256
257 QString GuiDialog::browseRelToSub(
258         QString const & filename,
259         QString const & relpath,
260         QString const & title,
261         QStringList const & filters,
262         bool save,
263         QString const & label1,
264         QString const & dir1,
265         QString const & label2,
266         QString const & dir2)
267 {
268         QString const fname = makeAbsPath(filename, relpath);
269
270         QString const outname =
271                 browseFile(fname, title, filters, save, label1, dir1, label2, dir2);
272
273         QString const reloutname =
274                 toqstr(support::makeRelPath(qstring_to_ucs4(outname), qstring_to_ucs4(relpath)));
275
276         QString testname = reloutname;
277         testname.remove(QRegularExpression("^(\\.\\./)+"));
278
279         if (testname.contains("/"))
280                 return outname;
281         else
282                 return reloutname;
283 }
284
285
286 QColor GuiDialog::getColor(const QColor &initial, QWidget *parent)
287 {
288         const QColor color = QColorDialog::getColor(initial, parent);
289         if (guiApp->platformName() == "cocoa") {
290                 QWidget * dialog = parent->window();
291                 // On Mac explicitly activate the parents top-level widget
292                 // See #10740
293                 dialog->raise();
294                 dialog->activateWindow();
295         }
296         return color;
297 }
298
299 QColor GuiDialog::getColor(const QColor &initial)
300 {
301         return getColor(initial, asQWidget());
302 }
303
304 } // namespace frontend
305 } // namespace lyx
306
307 #include "moc_GuiDialog.cpp"