2 * \file qt/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 docstring toPlainText(docstring const & msg)
74 return qstring_to_ucs4(qtHtmlToPlainText(toqstr(msg)));
78 int doPrompt(docstring const & title, docstring const & question,
79 int default_button, int cancel_button,
80 docstring const & b1, docstring const & b2,
81 docstring const & b3, docstring const & b4)
83 //lyxerr << "PROMPT" << title << "FOCUS: " << qApp->focusWidget() << endl;
84 if (!use_gui || lyxerr.debugging()) {
85 lyxerr << toPlainText(title) << '\n'
86 << "----------------------------------------\n"
87 << toPlainText(question) << endl;
89 lyxerr << "Assuming answer is ";
90 switch (default_button) {
91 case 0: lyxerr << b1 << endl; break;
92 case 1: lyxerr << b2 << endl; break;
93 case 2: lyxerr << b3 << endl; break;
94 case 3: lyxerr << b4 << endl;
97 return default_button;
100 /// Long operation in progress prevents user from Ok-ing the error dialog
101 bool long_op = theApp()->longOperationStarted();
103 theApp()->stopLongOperation();
105 // For some reason, sometimes Qt uses a hourglass or watch cursor when
106 // displaying the alert. Hence, we ask for the standard cursor shape.
107 qApp->setOverrideCursor(Qt::ArrowCursor);
109 // FIXME replace that with guiApp->currentView()
110 //LYXERR0("FOCUS: " << qApp->focusWidget());
111 QPushButton * b[4] = { 0, 0, 0, 0 };
112 QMessageBox msg_box(QMessageBox::Information,
113 toqstr(title), toqstr(question),
114 QMessageBox::NoButton, qApp->focusWidget());
115 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
116 QMessageBox::ActionRole);
118 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
120 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
122 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
123 msg_box.setDefaultButton(b[default_button]);
124 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
125 int res = msg_box.exec();
127 qApp->restoreOverrideCursor();
130 theApp()->startLongOperation();
132 // Qt bug: can return -1 on cancel or WM close, despite the docs.
138 int prompt(docstring const & title, docstring const & question,
139 int default_button, int cancel_button,
140 docstring const & b0, docstring const & b1,
141 docstring const & b2, docstring const & b3)
143 #ifdef EXPORT_in_THREAD
144 return InGuiThread<int>().call(&doPrompt,
148 title, question, default_button,
149 cancel_button, b0, b1, b2, b3);
152 void doWarning(docstring const & title, docstring const & message,
153 bool const & askshowagain)
155 lyxerr << "Warning: " << toPlainText(title) << '\n'
156 << "----------------------------------------\n"
157 << toPlainText(message) << endl;
163 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
167 /// Long operation in progress prevents user from Ok-ing the error dialog
168 bool long_op = theApp()->longOperationStarted();
170 theApp()->stopLongOperation();
172 // Don't use a hourglass cursor while displaying the alert
173 qApp->setOverrideCursor(Qt::ArrowCursor);
176 ProgressInterface::instance()->warning(
180 ProgressInterface::instance()->toggleWarning(
186 qApp->restoreOverrideCursor();
189 theApp()->startLongOperation();
192 void warning(docstring const & title, docstring const & message,
193 bool const & askshowagain)
195 #ifdef EXPORT_in_THREAD
196 InGuiThread<void>().call(&doWarning,
200 title, message, askshowagain);
203 void doError(docstring const & title, docstring const & message, bool backtrace)
205 lyxerr << "Error: " << toPlainText(title) << '\n'
206 << "----------------------------------------\n"
207 << toPlainText(message) << endl;
211 details = toqstr(printCallStack());
217 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
221 /// Long operation in progress prevents user from Ok-ing the error dialog
222 bool long_op = theApp()->longOperationStarted();
224 theApp()->stopLongOperation();
226 // Don't use a hourglass cursor while displaying the alert
227 qApp->setOverrideCursor(Qt::ArrowCursor);
229 ProgressInterface::instance()->error(
234 qApp->restoreOverrideCursor();
237 theApp()->startLongOperation();
240 void error(docstring const & title, docstring const & message, bool backtrace)
242 #ifdef EXPORT_in_THREAD
243 InGuiThread<void>().call(&doError,
247 title, message, backtrace);
250 void doInformation(docstring const & title, docstring const & message)
252 if (!use_gui || lyxerr.debugging())
253 lyxerr << toPlainText(title) << '\n'
254 << "----------------------------------------\n"
255 << toPlainText(message) << endl;
261 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
265 /// Long operation in progress prevents user from Ok-ing the error dialog
266 bool long_op = theApp()->longOperationStarted();
268 theApp()->stopLongOperation();
270 // Don't use a hourglass cursor while displaying the alert
271 qApp->setOverrideCursor(Qt::ArrowCursor);
273 ProgressInterface::instance()->information(
277 qApp->restoreOverrideCursor();
280 theApp()->startLongOperation();
283 void information(docstring const & title, docstring const & message)
285 #ifdef EXPORT_in_THREAD
286 InGuiThread<void>().call(&doInformation,
293 bool doAskForText(docstring & response, docstring const & msg,
294 docstring const & dflt)
296 if (!use_gui || lyxerr.debugging()) {
297 lyxerr << "----------------------------------------\n"
298 << toPlainText(msg) << '\n'
299 << "Assuming answer is " << dflt << '\n'
300 << "----------------------------------------" << endl;
307 docstring const title = bformat(from_utf8("%1$s"), msg);
309 /// Long operation in progress prevents user from Ok-ing the error dialog
310 bool long_op = theApp()->longOperationStarted();
312 theApp()->stopLongOperation();
315 QString text = QInputDialog::getText(qApp->focusWidget(),
317 toqstr(char_type('&') + msg),
322 theApp()->startLongOperation();
325 response = qstring_to_ucs4(text);
332 bool askForText(docstring & response, docstring const & msg,
333 docstring const & dflt)
335 #ifdef EXPORT_in_THREAD
336 return InGuiThread<bool>().call(&doAskForText,
340 response, msg, dflt);
344 } // namespace frontend