3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "FileDialog.h"
14 #include "GuiApplication.h"
15 #include "GuiDialog.h"
17 #include "qt_helpers.h"
19 #include "support/debug.h"
20 #include "support/filetools.h"
22 #include <QCloseEvent>
23 #include <QDialogButtonBox>
24 #include <QColorDialog>
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)
35 connect(&lv, SIGNAL(bufferViewChanged()),
36 this, SLOT(onBufferViewChanged()));
38 // remove question marks from Windows dialogs
39 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
43 void GuiDialog::closeEvent(QCloseEvent * ev)
50 void GuiDialog::setButtonsValid(bool valid)
56 void GuiDialog::slotApply()
58 setApplyStopped(false);
66 void GuiDialog::slotAutoApply()
73 void GuiDialog::slotOK()
76 setApplyStopped(false);
86 void GuiDialog::slotClose()
93 void GuiDialog::slotRestore()
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().
103 void GuiDialog::slotButtonBox(QAbstractButton * button)
105 QDialogButtonBox * bbox = qobject_cast<QDialogButtonBox*>(sender());
106 switch (bbox->standardButton(button)) {
107 case QDialogButtonBox::Ok:
110 case QDialogButtonBox::Apply:
113 case QDialogButtonBox::Cancel:
114 case QDialogButtonBox::Close:
117 case QDialogButtonBox::Reset:
120 case QDialogButtonBox::RestoreDefaults:
121 slotRestoreDefaults();
129 void GuiDialog::changed()
133 bc().setValid(isValid());
137 void GuiDialog::enableView(bool enable)
140 bc().setReadOnly(true);
141 bc().setValid(false);
143 Dialog::enableView(enable);
147 void GuiDialog::updateView()
149 setUpdatesEnabled(false);
151 bc().setReadOnly(isBufferReadonly());
152 // protect the BC from unwarranted state transitions
156 // The widgets may not be valid, so refresh the button controller
159 setUpdatesEnabled(true);
162 QString GuiDialog::browseFile(QString const & filename,
163 QString const & title,
164 QStringList const & filters,
166 QString const & label1,
167 QString const & dir1,
168 QString const & label2,
169 QString const & dir2,
170 QString const & fallback_dir)
172 QString lastPath = ".";
173 if (!filename.isEmpty())
174 lastPath = onlyPath(filename);
175 else if(!fallback_dir.isEmpty())
176 lastPath = fallback_dir;
178 FileDialog dlg(title);
179 dlg.setButton1(label1, dir1);
180 dlg.setButton2(label2, dir2);
182 FileDialog::Result result;
185 result = dlg.save(lastPath, filters, onlyFileName(filename));
187 result = dlg.open(lastPath, filters, onlyFileName(filename));
189 if (guiApp->platformName() == "cocoa") {
190 QWidget * dialog = asQWidget();
192 dialog->activateWindow();
195 return result.second;
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.
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)
211 QString lastPath = ".";
212 if (!pathname.isEmpty())
213 lastPath = onlyPath(pathname);
215 FileDialog dlg(title);
216 dlg.setButton1(label1, dir1);
217 dlg.setButton2(label2, dir2);
219 FileDialog::Result const result =
220 dlg.opendir(lastPath, onlyFileName(pathname));
222 if (guiApp->platformName() == "cocoa") {
223 QWidget * dialog = asQWidget();
225 dialog->activateWindow();
228 return result.second;
231 QString GuiDialog::browseRelToParent(
232 QString const & filename,
233 QString const & relpath,
234 QString const & title,
235 QStringList const & filters,
237 QString const & label1,
238 QString const & dir1,
239 QString const & label2,
240 QString const & dir2)
242 QString const fname = makeAbsPath(filename, relpath);
244 QString const outname =
245 browseFile(fname, title, filters, save, label1, dir1, label2, dir2);
247 QString const reloutname =
248 toqstr(support::makeRelPath(qstring_to_ucs4(outname), qstring_to_ucs4(relpath)));
250 if (reloutname.startsWith("../"))
257 QString GuiDialog::browseRelToSub(
258 QString const & filename,
259 QString const & relpath,
260 QString const & title,
261 QStringList const & filters,
263 QString const & label1,
264 QString const & dir1,
265 QString const & label2,
266 QString const & dir2)
268 QString const fname = makeAbsPath(filename, relpath);
270 QString const outname =
271 browseFile(fname, title, filters, save, label1, dir1, label2, dir2);
273 QString const reloutname =
274 toqstr(support::makeRelPath(qstring_to_ucs4(outname), qstring_to_ucs4(relpath)));
276 QString testname = reloutname;
277 testname.remove(QRegularExpression("^(\\.\\./)+"));
279 if (testname.contains("/"))
286 QColor GuiDialog::getColor(const QColor &initial, QWidget *parent)
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
294 dialog->activateWindow();
299 QColor GuiDialog::getColor(const QColor &initial)
301 return getColor(initial, asQWidget());
304 } // namespace frontend
307 #include "moc_GuiDialog.cpp"