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 const size_t numbuttons = sizeof(b)/sizeof(b[0]);
113 QMessageBox msg_box(QMessageBox::Information,
114 toqstr(title), toqstr(question),
115 QMessageBox::NoButton, qApp->focusWidget());
116 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
117 QMessageBox::ActionRole);
119 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
121 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
123 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
124 if (default_button < numbuttons && nullptr != b[default_button])
125 msg_box.setDefaultButton(b[default_button]);
126 if (cancel_button < numbuttons && nullptr != b[cancel_button])
127 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
129 const QAbstractButton * button = msg_box.clickedButton();
131 qApp->restoreOverrideCursor();
134 theApp()->startLongOperation();
136 size_t res = cancel_button;
138 if (button == nullptr)
141 // Convert selection of the button into an integer
142 for (size_t i = 0; i < numbuttons; i++) {
143 if (button == b[i]) {
153 buttonid prompt(docstring const & title, docstring const & question,
154 buttonid default_button, buttonid cancel_button,
155 docstring const & b0, docstring const & b1,
156 docstring const & b2, docstring const & b3)
158 #ifdef EXPORT_in_THREAD
159 return InGuiThread<int>().call(&doPrompt,
163 title, question, default_button,
164 cancel_button, b0, b1, b2, b3);
167 void doWarning(docstring const & title, docstring const & message,
170 lyxerr << "Warning: " << toPlainText(title) << '\n'
171 << "----------------------------------------\n"
172 << toPlainText(message) << endl;
178 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
182 /// Long operation in progress prevents user from Ok-ing the error dialog
183 bool long_op = theApp()->longOperationStarted();
185 theApp()->stopLongOperation();
187 // Don't use a hourglass cursor while displaying the alert
188 qApp->setOverrideCursor(Qt::ArrowCursor);
191 ProgressInterface::instance()->warning(
195 ProgressInterface::instance()->toggleWarning(
201 qApp->restoreOverrideCursor();
204 theApp()->startLongOperation();
207 void warning(docstring const & title, docstring const & message,
210 #ifdef EXPORT_in_THREAD
211 InGuiThread<void>().call(&doWarning,
215 title, message, askshowagain);
218 void doError(docstring const & title, docstring const & message, bool backtrace)
220 lyxerr << "Error: " << toPlainText(title) << '\n'
221 << "----------------------------------------\n"
222 << toPlainText(message) << endl;
226 details = toqstr(printCallStack());
232 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
236 /// Long operation in progress prevents user from Ok-ing the error dialog
237 bool long_op = theApp()->longOperationStarted();
239 theApp()->stopLongOperation();
241 // Don't use a hourglass cursor while displaying the alert
242 qApp->setOverrideCursor(Qt::ArrowCursor);
244 ProgressInterface::instance()->error(
249 qApp->restoreOverrideCursor();
252 theApp()->startLongOperation();
255 void error(docstring const & title, docstring const & message, bool backtrace)
257 #ifdef EXPORT_in_THREAD
258 InGuiThread<void>().call(&doError,
262 title, message, backtrace);
265 void doInformation(docstring const & title, docstring const & message)
267 if (!use_gui || lyxerr.debugging())
268 lyxerr << toPlainText(title) << '\n'
269 << "----------------------------------------\n"
270 << toPlainText(message) << endl;
276 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
280 /// Long operation in progress prevents user from Ok-ing the error dialog
281 bool long_op = theApp()->longOperationStarted();
283 theApp()->stopLongOperation();
285 // Don't use a hourglass cursor while displaying the alert
286 qApp->setOverrideCursor(Qt::ArrowCursor);
288 ProgressInterface::instance()->information(
292 qApp->restoreOverrideCursor();
295 theApp()->startLongOperation();
298 void information(docstring const & title, docstring const & message)
300 #ifdef EXPORT_in_THREAD
301 InGuiThread<void>().call(&doInformation,
308 bool doAskForText(docstring & response, docstring const & msg,
309 docstring const & dflt)
311 if (!use_gui || lyxerr.debugging()) {
312 lyxerr << "----------------------------------------\n"
313 << toPlainText(msg) << '\n'
314 << "Assuming answer is " << dflt << '\n'
315 << "----------------------------------------" << endl;
322 docstring const title = bformat(from_utf8("%1$s"), msg);
324 /// Long operation in progress prevents user from Ok-ing the error dialog
325 bool long_op = theApp()->longOperationStarted();
327 theApp()->stopLongOperation();
330 QString text = QInputDialog::getText(qApp->focusWidget(),
332 toqstr(char_type('&') + msg),
337 theApp()->startLongOperation();
340 response = qstring_to_ucs4(text);
347 bool askForText(docstring & response, docstring const & msg,
348 docstring const & dflt)
350 #ifdef EXPORT_in_THREAD
351 return InGuiThread<bool>().call(&doAskForText,
355 response, msg, dflt);
359 } // namespace frontend