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());
117 msg_box.setWindowModality(Qt::WindowModal);
119 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
120 QMessageBox::ActionRole);
122 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
124 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
126 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
127 if (default_button < numbuttons && nullptr != b[default_button])
128 msg_box.setDefaultButton(b[default_button]);
129 if (cancel_button < numbuttons && nullptr != b[cancel_button])
130 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
132 const QAbstractButton * button = msg_box.clickedButton();
134 qApp->restoreOverrideCursor();
137 theApp()->startLongOperation();
139 size_t res = cancel_button;
141 if (button == nullptr)
144 // Convert selection of the button into an integer
145 for (size_t i = 0; i < numbuttons; i++) {
146 if (button == b[i]) {
156 buttonid prompt(docstring const & title, docstring const & question,
157 buttonid default_button, buttonid cancel_button,
158 docstring const & b0, docstring const & b1,
159 docstring const & b2, docstring const & b3)
161 #ifdef EXPORT_in_THREAD
162 return InGuiThread<int>().call(&doPrompt,
166 title, question, default_button,
167 cancel_button, b0, b1, b2, b3);
170 void doWarning(docstring const & title, docstring const & message,
173 lyxerr << "Warning: " << toPlainText(title) << '\n'
174 << "----------------------------------------\n"
175 << toPlainText(message) << endl;
181 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
185 /// Long operation in progress prevents user from Ok-ing the error dialog
186 bool long_op = theApp()->longOperationStarted();
188 theApp()->stopLongOperation();
190 // Don't use a hourglass cursor while displaying the alert
191 qApp->setOverrideCursor(Qt::ArrowCursor);
194 ProgressInterface::instance()->warning(
198 ProgressInterface::instance()->toggleWarning(
204 qApp->restoreOverrideCursor();
207 theApp()->startLongOperation();
210 void warning(docstring const & title, docstring const & message,
213 #ifdef EXPORT_in_THREAD
214 InGuiThread<void>().call(&doWarning,
218 title, message, askshowagain);
221 void doError(docstring const & title, docstring const & message, bool backtrace)
223 lyxerr << "Error: " << toPlainText(title) << '\n'
224 << "----------------------------------------\n"
225 << toPlainText(message) << endl;
229 details = toqstr(printCallStack());
235 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
239 /// Long operation in progress prevents user from Ok-ing the error dialog
240 bool long_op = theApp()->longOperationStarted();
242 theApp()->stopLongOperation();
244 // Don't use a hourglass cursor while displaying the alert
245 qApp->setOverrideCursor(Qt::ArrowCursor);
247 ProgressInterface::instance()->error(
252 qApp->restoreOverrideCursor();
255 theApp()->startLongOperation();
258 void error(docstring const & title, docstring const & message, bool backtrace)
260 #ifdef EXPORT_in_THREAD
261 InGuiThread<void>().call(&doError,
265 title, message, backtrace);
268 void doInformation(docstring const & title, docstring const & message)
270 if (!use_gui || lyxerr.debugging())
271 lyxerr << toPlainText(title) << '\n'
272 << "----------------------------------------\n"
273 << toPlainText(message) << endl;
279 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
283 /// Long operation in progress prevents user from Ok-ing the error dialog
284 bool long_op = theApp()->longOperationStarted();
286 theApp()->stopLongOperation();
288 // Don't use a hourglass cursor while displaying the alert
289 qApp->setOverrideCursor(Qt::ArrowCursor);
291 ProgressInterface::instance()->information(
295 qApp->restoreOverrideCursor();
298 theApp()->startLongOperation();
301 void information(docstring const & title, docstring const & message)
303 #ifdef EXPORT_in_THREAD
304 InGuiThread<void>().call(&doInformation,
311 bool doAskForText(docstring & response, docstring const & msg,
312 docstring const & dflt)
314 if (!use_gui || lyxerr.debugging()) {
315 lyxerr << "----------------------------------------\n"
316 << toPlainText(msg) << '\n'
317 << "Assuming answer is " << dflt << '\n'
318 << "----------------------------------------" << endl;
325 docstring const title = bformat(from_utf8("%1$s"), msg);
327 /// Long operation in progress prevents user from Ok-ing the error dialog
328 bool long_op = theApp()->longOperationStarted();
330 theApp()->stopLongOperation();
333 QString text = QInputDialog::getText(qApp->focusWidget(),
335 toqstr(char_type('&') + msg),
340 theApp()->startLongOperation();
343 response = qstring_to_ucs4(text);
350 bool askForText(docstring & response, docstring const & msg,
351 docstring const & dflt)
353 #ifdef EXPORT_in_THREAD
354 return InGuiThread<bool>().call(&doAskForText,
358 response, msg, dflt);
362 } // namespace frontend