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/lassert.h"
28 #include "support/ProgressInterface.h"
30 #include <QApplication>
32 #include <QMessageBox>
34 #include <QInputDialog>
35 #include <QPushButton>
42 // sync with GuiView.cpp
43 #define EXPORT_in_THREAD 1
47 using namespace lyx::support;
53 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
56 const char *argv[] = { "lyx", 0 };
58 QApplication app(argc, (char**)argv);
61 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
62 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
63 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
72 int doPrompt(docstring const & title0, docstring const & question,
73 int default_button, int cancel_button,
74 docstring const & b1, docstring const & b2,
75 docstring const & b3, docstring const & b4)
77 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
78 if (!use_gui || lyxerr.debugging()) {
79 lyxerr << title0 << '\n'
80 << "----------------------------------------\n"
83 lyxerr << "Assuming answer is ";
84 switch (default_button) {
85 case 0: lyxerr << b1 << endl;
86 case 1: lyxerr << b2 << endl;
87 case 2: lyxerr << b3 << endl;
88 case 3: lyxerr << b4 << endl;
91 return default_button;
94 docstring const title = bformat(_("LyX: %1$s"), title0);
96 /// Long operation in progress prevents user from Ok-ing the error dialog
97 bool long_op = theApp()->longOperationStarted();
99 theApp()->stopLongOperation();
101 // For some reason, sometimes Qt uses a hourglass or watch cursor when
102 // displaying the alert. Hence, we ask for the standard cursor shape.
103 qApp->setOverrideCursor(Qt::ArrowCursor);
105 // FIXME replace that with guiApp->currentView()
106 //LYXERR0("FOCUS: " << qApp->focusWidget());
107 QPushButton * b[4] = { 0, 0, 0, 0 };
108 QMessageBox msg_box(QMessageBox::Information,
109 toqstr(title), toqstr(question),
110 QMessageBox::NoButton, qApp->focusWidget());
111 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
112 QMessageBox::ActionRole);
114 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
116 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
118 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
119 msg_box.setDefaultButton(b[default_button]);
120 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
121 int res = msg_box.exec();
123 qApp->restoreOverrideCursor();
126 theApp()->startLongOperation();
128 // Qt bug: can return -1 on cancel or WM close, despite the docs.
134 int prompt(docstring const & title0, docstring const & question,
135 int default_button, int cancel_button,
136 docstring const & b1, docstring const & b2,
137 docstring const & b3, docstring const & b4)
139 #ifdef EXPORT_in_THREAD
140 return InGuiThread<int>().call(&doPrompt,
144 title0, question, default_button,
145 cancel_button, b1, b2, b3, b4);
148 void doWarning(docstring const & title0, docstring const & message,
149 bool const & askshowagain)
151 lyxerr << "Warning: " << title0 << '\n'
152 << "----------------------------------------\n"
158 docstring const title = bformat(_("LyX: %1$s"), title0);
161 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
165 /// Long operation in progress prevents user from Ok-ing the error dialog
166 bool long_op = theApp()->longOperationStarted();
168 theApp()->stopLongOperation();
170 // Don't use a hourglass cursor while displaying the alert
171 qApp->setOverrideCursor(Qt::ArrowCursor);
174 ProgressInterface::instance()->warning(
178 ProgressInterface::instance()->toggleWarning(
184 qApp->restoreOverrideCursor();
187 theApp()->startLongOperation();
190 void warning(docstring const & title0, docstring const & message,
191 bool const & askshowagain)
193 #ifdef EXPORT_in_THREAD
194 InGuiThread<void>().call(&doWarning,
198 title0, message, askshowagain);
201 void doError(docstring const & title0, docstring const & message, bool backtrace)
203 lyxerr << "Error: " << title0 << '\n'
204 << "----------------------------------------\n"
209 details = QString::fromLocal8Bit(to_local8bit(printCallStack()).c_str());
215 docstring const title = bformat(_("LyX: %1$s"), title0);
218 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
222 /// Long operation in progress prevents user from Ok-ing the error dialog
223 bool long_op = theApp()->longOperationStarted();
225 theApp()->stopLongOperation();
227 // Don't use a hourglass cursor while displaying the alert
228 qApp->setOverrideCursor(Qt::ArrowCursor);
230 ProgressInterface::instance()->error(
235 qApp->restoreOverrideCursor();
238 theApp()->startLongOperation();
241 void error(docstring const & title0, docstring const & message, bool backtrace)
243 #ifdef EXPORT_in_THREAD
244 InGuiThread<void>().call(&doError,
248 title0, message, backtrace);
251 void doInformation(docstring const & title0, docstring const & message)
253 if (!use_gui || lyxerr.debugging())
254 lyxerr << title0 << '\n'
255 << "----------------------------------------\n"
261 docstring const title = bformat(_("LyX: %1$s"), title0);
264 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
268 /// Long operation in progress prevents user from Ok-ing the error dialog
269 bool long_op = theApp()->longOperationStarted();
271 theApp()->stopLongOperation();
273 // Don't use a hourglass cursor while displaying the alert
274 qApp->setOverrideCursor(Qt::ArrowCursor);
276 ProgressInterface::instance()->information(
280 qApp->restoreOverrideCursor();
283 theApp()->startLongOperation();
286 void information(docstring const & title0, docstring const & message)
288 #ifdef EXPORT_in_THREAD
289 InGuiThread<void>().call(&doInformation,
296 bool doAskForText(docstring & response, docstring const & msg,
297 docstring const & dflt)
299 if (!use_gui || lyxerr.debugging()) {
300 lyxerr << "----------------------------------------\n"
302 << "Assuming answer is " << dflt << '\n'
303 << "----------------------------------------" << endl;
310 docstring const title = bformat(_("LyX: %1$s"), msg);
312 /// Long operation in progress prevents user from Ok-ing the error dialog
313 bool long_op = theApp()->longOperationStarted();
315 theApp()->stopLongOperation();
318 QString text = QInputDialog::getText(qApp->focusWidget(),
320 toqstr(char_type('&') + msg),
325 theApp()->startLongOperation();
328 response = qstring_to_ucs4(text);
335 bool askForText(docstring & response, docstring const & msg,
336 docstring const & dflt)
338 #ifdef EXPORT_in_THREAD
339 return InGuiThread<bool>().call(&doAskForText,
343 response, msg, dflt);
347 } // namespace frontend