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 buttonid doPrompt(docstring const & title, docstring const & question,
79 buttonid default_button, buttonid 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] = { nullptr, nullptr, nullptr, nullptr };
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 if (default_button < size(b) && nullptr != b[default_button])
124 msg_box.setDefaultButton(b[default_button]);
125 if (cancel_button < size(b) && nullptr != b[cancel_button])
126 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
128 const QAbstractButton * button = msg_box.clickedButton();
130 qApp->restoreOverrideCursor();
133 theApp()->startLongOperation();
135 size_t res = cancel_button;
137 if (button == nullptr)
140 // Convert selection of the button into an integer
141 for (size_t i = 0; i < size(b); i++) {
142 if (button == b[i]) {
152 buttonid prompt(docstring const & title, docstring const & question,
153 buttonid default_button, buttonid cancel_button,
154 docstring const & b0, docstring const & b1,
155 docstring const & b2, docstring const & b3)
157 #ifdef EXPORT_in_THREAD
158 return InGuiThread<int>().call(&doPrompt,
162 title, question, default_button,
163 cancel_button, b0, b1, b2, b3);
166 void doWarning(docstring const & title, docstring const & message,
169 lyxerr << "Warning: " << toPlainText(title) << '\n'
170 << "----------------------------------------\n"
171 << toPlainText(message) << endl;
177 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
181 /// Long operation in progress prevents user from Ok-ing the error dialog
182 bool long_op = theApp()->longOperationStarted();
184 theApp()->stopLongOperation();
186 // Don't use a hourglass cursor while displaying the alert
187 qApp->setOverrideCursor(Qt::ArrowCursor);
190 ProgressInterface::instance()->warning(
194 ProgressInterface::instance()->toggleWarning(
200 qApp->restoreOverrideCursor();
203 theApp()->startLongOperation();
206 void warning(docstring const & title, docstring const & message,
209 #ifdef EXPORT_in_THREAD
210 InGuiThread<void>().call(&doWarning,
214 title, message, askshowagain);
217 void doError(docstring const & title, docstring const & message, bool backtrace)
219 lyxerr << "Error: " << toPlainText(title) << '\n'
220 << "----------------------------------------\n"
221 << toPlainText(message) << endl;
225 details = toqstr(printCallStack());
231 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
235 /// Long operation in progress prevents user from Ok-ing the error dialog
236 bool long_op = theApp()->longOperationStarted();
238 theApp()->stopLongOperation();
240 // Don't use a hourglass cursor while displaying the alert
241 qApp->setOverrideCursor(Qt::ArrowCursor);
243 ProgressInterface::instance()->error(
248 qApp->restoreOverrideCursor();
251 theApp()->startLongOperation();
254 void error(docstring const & title, docstring const & message, bool backtrace)
256 #ifdef EXPORT_in_THREAD
257 InGuiThread<void>().call(&doError,
261 title, message, backtrace);
264 void doInformation(docstring const & title, docstring const & message)
266 if (!use_gui || lyxerr.debugging())
267 lyxerr << toPlainText(title) << '\n'
268 << "----------------------------------------\n"
269 << toPlainText(message) << endl;
275 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
279 /// Long operation in progress prevents user from Ok-ing the error dialog
280 bool long_op = theApp()->longOperationStarted();
282 theApp()->stopLongOperation();
284 // Don't use a hourglass cursor while displaying the alert
285 qApp->setOverrideCursor(Qt::ArrowCursor);
287 ProgressInterface::instance()->information(
291 qApp->restoreOverrideCursor();
294 theApp()->startLongOperation();
297 void information(docstring const & title, docstring const & message)
299 #ifdef EXPORT_in_THREAD
300 InGuiThread<void>().call(&doInformation,
307 bool doAskForText(docstring & response, docstring const & msg,
308 docstring const & dflt)
310 if (!use_gui || lyxerr.debugging()) {
311 lyxerr << "----------------------------------------\n"
312 << toPlainText(msg) << '\n'
313 << "Assuming answer is " << dflt << '\n'
314 << "----------------------------------------" << endl;
321 docstring const title = bformat(from_utf8("%1$s"), msg);
323 /// Long operation in progress prevents user from Ok-ing the error dialog
324 bool long_op = theApp()->longOperationStarted();
326 theApp()->stopLongOperation();
329 QString text = QInputDialog::getText(qApp->focusWidget(),
331 toqstr(char_type('&') + msg),
336 theApp()->startLongOperation();
339 response = qstring_to_ucs4(text);
346 bool askForText(docstring & response, docstring const & msg,
347 docstring const & dflt)
349 #ifdef EXPORT_in_THREAD
350 return InGuiThread<bool>().call(&doAskForText,
354 response, msg, dflt);
358 } // namespace frontend