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 // Don't use a hourglass cursor while displaying the alert
165 qApp->setOverrideCursor(Qt::ArrowCursor);
168 ProgressInterface::instance()->warning(
172 ProgressInterface::instance()->toggleWarning(
178 qApp->restoreOverrideCursor();
181 void warning(docstring const & title0, docstring const & message,
182 bool const & askshowagain)
184 #ifdef EXPORT_in_THREAD
185 InGuiThread<void>().call(&doWarning,
189 title0, message, askshowagain);
192 void doError(docstring const & title0, docstring const & message)
194 lyxerr << "Error: " << title0 << '\n'
195 << "----------------------------------------\n"
201 docstring const title = bformat(_("LyX: %1$s"), title0);
204 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
208 /// Long operation in progress prevents user from Ok-ing the error dialog
209 bool long_op = theApp()->longOperationStarted();
211 theApp()->stopLongOperation();
213 // Don't use a hourglass cursor while displaying the alert
214 qApp->setOverrideCursor(Qt::ArrowCursor);
216 ProgressInterface::instance()->error(
220 qApp->restoreOverrideCursor();
223 theApp()->startLongOperation();
226 void error(docstring const & title0, docstring const & message)
228 #ifdef EXPORT_in_THREAD
229 InGuiThread<void>().call(&doError,
236 void doInformation(docstring const & title0, docstring const & message)
238 if (!use_gui || lyxerr.debugging())
239 lyxerr << title0 << '\n'
240 << "----------------------------------------\n"
246 docstring const title = bformat(_("LyX: %1$s"), title0);
249 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
253 // Don't use a hourglass cursor while displaying the alert
254 qApp->setOverrideCursor(Qt::ArrowCursor);
256 ProgressInterface::instance()->information(
260 qApp->restoreOverrideCursor();
263 void information(docstring const & title0, docstring const & message)
265 #ifdef EXPORT_in_THREAD
266 InGuiThread<void>().call(&doInformation,
273 bool doAskForText(docstring & response, docstring const & msg,
274 docstring const & dflt)
276 if (!use_gui || lyxerr.debugging()) {
277 lyxerr << "----------------------------------------\n"
279 << "Assuming answer is " << dflt << '\n'
280 << "----------------------------------------" << endl;
287 docstring const title = bformat(_("LyX: %1$s"), msg);
290 QString text = QInputDialog::getText(qApp->focusWidget(),
292 toqstr(char_type('&') + msg),
297 response = qstring_to_ucs4(text);
304 bool askForText(docstring & response, docstring const & msg,
305 docstring const & dflt)
307 #ifdef EXPORT_in_THREAD
308 return InGuiThread<bool>().call(&doAskForText,
312 response, msg, dflt);
316 } // namespace frontend