2 * \file qt4/GuiAlert.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
16 #include "InGuiThread.h"
18 #include "frontends/Application.h"
20 #include "qt_helpers.h"
21 #include "LyX.h" // for lyx::use_gui
23 #include "support/gettext.h"
24 #include "support/debug.h"
25 #include "support/docstring.h"
26 #include "support/lstrings.h"
27 #include "support/ProgressInterface.h"
29 #include <QApplication>
31 #include <QMessageBox>
33 #include <QInputDialog>
34 #include <QPushButton>
41 // sync with GuiView.cpp
42 #define EXPORT_in_THREAD 1
46 using namespace lyx::support;
52 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
55 const char *argv[] = { "lyx", 0 };
57 QApplication app(argc, (char**)argv);
60 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
61 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
62 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
71 int doPrompt(docstring const & title0, docstring const & question,
72 int default_button, int cancel_button,
73 docstring const & b1, docstring const & b2,
74 docstring const & b3, docstring const & b4)
76 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
77 if (!use_gui || lyxerr.debugging()) {
78 lyxerr << title0 << '\n'
79 << "----------------------------------------\n"
82 lyxerr << "Assuming answer is ";
83 switch (default_button) {
84 case 0: lyxerr << b1 << endl;
85 case 1: lyxerr << b2 << endl;
86 case 2: lyxerr << b3 << endl;
87 case 3: lyxerr << b4 << endl;
90 return default_button;
93 docstring const title = bformat(_("LyX: %1$s"), title0);
95 /// Long operation in progress prevents user from Ok-ing the error dialog
96 bool long_op = theApp()->longOperationStarted();
98 theApp()->stopLongOperation();
100 // For some reason, sometimes Qt uses a hourglass or watch cursor when
101 // displaying the alert. Hence, we ask for the standard cursor shape.
102 qApp->setOverrideCursor(Qt::ArrowCursor);
104 // FIXME replace that with guiApp->currentView()
105 //LYXERR0("FOCUS: " << qApp->focusWidget());
106 QPushButton * b[4] = { 0, 0, 0, 0 };
107 QMessageBox msg_box(QMessageBox::Information,
108 toqstr(title), toqstr(question),
109 QMessageBox::NoButton, qApp->focusWidget());
110 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
111 QMessageBox::ActionRole);
113 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
115 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
117 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
118 msg_box.setDefaultButton(b[default_button]);
119 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
120 int res = msg_box.exec();
122 qApp->restoreOverrideCursor();
125 theApp()->startLongOperation();
127 // Qt bug: can return -1 on cancel or WM close, despite the docs.
133 int prompt(docstring const & title0, docstring const & question,
134 int default_button, int cancel_button,
135 docstring const & b1, docstring const & b2,
136 docstring const & b3, docstring const & b4)
138 #ifdef EXPORT_in_THREAD
139 return InGuiThread<int>().call(&doPrompt,
143 title0, question, default_button,
144 cancel_button, b1, b2, b3, b4);
147 void doWarning(docstring const & title0, docstring const & message,
148 bool const & askshowagain)
150 lyxerr << "Warning: " << title0 << '\n'
151 << "----------------------------------------\n"
157 docstring const title = bformat(_("LyX: %1$s"), title0);
160 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
164 /// Long operation in progress prevents user from Ok-ing the error dialog
165 bool long_op = theApp()->longOperationStarted();
167 theApp()->stopLongOperation();
169 // Don't use a hourglass cursor while displaying the alert
170 qApp->setOverrideCursor(Qt::ArrowCursor);
173 ProgressInterface::instance()->warning(
177 ProgressInterface::instance()->toggleWarning(
183 qApp->restoreOverrideCursor();
186 theApp()->startLongOperation();
189 void warning(docstring const & title0, docstring const & message,
190 bool const & askshowagain)
192 #ifdef EXPORT_in_THREAD
193 InGuiThread<void>().call(&doWarning,
197 title0, message, askshowagain);
200 void doError(docstring const & title0, docstring const & message)
202 lyxerr << "Error: " << title0 << '\n'
203 << "----------------------------------------\n"
209 docstring const title = bformat(_("LyX: %1$s"), title0);
212 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
216 /// Long operation in progress prevents user from Ok-ing the error dialog
217 bool long_op = theApp()->longOperationStarted();
219 theApp()->stopLongOperation();
221 // Don't use a hourglass cursor while displaying the alert
222 qApp->setOverrideCursor(Qt::ArrowCursor);
224 ProgressInterface::instance()->error(
228 qApp->restoreOverrideCursor();
231 theApp()->startLongOperation();
234 void error(docstring const & title0, docstring const & message)
236 #ifdef EXPORT_in_THREAD
237 InGuiThread<void>().call(&doError,
244 void doInformation(docstring const & title0, docstring const & message)
246 if (!use_gui || lyxerr.debugging())
247 lyxerr << title0 << '\n'
248 << "----------------------------------------\n"
254 docstring const title = bformat(_("LyX: %1$s"), title0);
257 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
261 /// Long operation in progress prevents user from Ok-ing the error dialog
262 bool long_op = theApp()->longOperationStarted();
264 theApp()->stopLongOperation();
266 // Don't use a hourglass cursor while displaying the alert
267 qApp->setOverrideCursor(Qt::ArrowCursor);
269 ProgressInterface::instance()->information(
273 qApp->restoreOverrideCursor();
276 theApp()->startLongOperation();
279 void information(docstring const & title0, docstring const & message)
281 #ifdef EXPORT_in_THREAD
282 InGuiThread<void>().call(&doInformation,
289 bool doAskForText(docstring & response, docstring const & msg,
290 docstring const & dflt)
292 if (!use_gui || lyxerr.debugging()) {
293 lyxerr << "----------------------------------------\n"
295 << "Assuming answer is " << dflt << '\n'
296 << "----------------------------------------" << endl;
303 docstring const title = bformat(_("LyX: %1$s"), msg);
305 /// Long operation in progress prevents user from Ok-ing the error dialog
306 bool long_op = theApp()->longOperationStarted();
308 theApp()->stopLongOperation();
311 QString text = QInputDialog::getText(qApp->focusWidget(),
313 toqstr(char_type('&') + msg),
318 theApp()->startLongOperation();
321 response = qstring_to_ucs4(text);
328 bool askForText(docstring & response, docstring const & msg,
329 docstring const & dflt)
331 #ifdef EXPORT_in_THREAD
332 return InGuiThread<bool>().call(&doAskForText,
336 response, msg, dflt);
340 } // namespace frontend