]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiAlert.cpp
#12818 correct evaluation of message box result info
[lyx.git] / src / frontends / qt / GuiAlert.cpp
1 /**
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.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "alert.h"
16 #include "InGuiThread.h"
17
18 #include "frontends/Application.h"
19
20 #include "qt_helpers.h"
21 #include "LyX.h" // for lyx::use_gui
22
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"
29
30 #include <QApplication>
31 #include <QCheckBox>
32 #include <QMessageBox>
33 #include <QLineEdit>
34 #include <QInputDialog>
35 #include <QPushButton>
36 #include <QSettings>
37
38 #include <iomanip>
39 #include <iostream>
40
41
42 // sync with GuiView.cpp
43 #define EXPORT_in_THREAD 1
44
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace lyx {
50 namespace frontend {
51
52
53 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
54 {
55         int argc = 1;
56         const char *argv[] = { "lyx", 0 };
57
58         QApplication app(argc, (char**)argv);
59         switch (mode)
60         {
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;
64                 default: break;
65         }
66 }
67
68
69 namespace Alert {
70
71
72 docstring toPlainText(docstring const & msg)
73 {
74         return qstring_to_ucs4(qtHtmlToPlainText(toqstr(msg)));
75 }
76
77
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)
82 {
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;
88
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;
95                 }
96                 if (!use_gui)
97                         return default_button;
98         }
99
100         /// Long operation in progress prevents user from Ok-ing the error dialog
101         bool long_op = theApp()->longOperationStarted();
102         if (long_op)
103                 theApp()->stopLongOperation();
104
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);
108
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);
117         if (!b2.empty())
118                 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
119         if (!b3.empty())
120                 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
121         if (!b4.empty())
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]));
127         msg_box.exec();
128         const QAbstractButton * button = msg_box.clickedButton();
129
130         qApp->restoreOverrideCursor();
131
132         if (long_op)
133                 theApp()->startLongOperation();
134
135         size_t res = cancel_button;
136
137         if (button == nullptr)
138                 return res;
139         else {
140                 // Convert selection of the button into an integer
141                 for (size_t i = 0; i < size(b); i++) {
142                         if (button == b[i]) {
143                                 res = i;
144                                 break;
145                         }
146                 }
147         }
148
149         return res;
150 }
151
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)
156 {
157 #ifdef EXPORT_in_THREAD
158         return InGuiThread<int>().call(&doPrompt,
159 #else
160         return doPrompt(
161 #endif
162                                 title, question, default_button,
163                                 cancel_button, b0, b1, b2, b3);
164 }
165
166 void doWarning(docstring const & title, docstring const & message,
167              bool askshowagain)
168 {
169         lyxerr << "Warning: " << toPlainText(title) << '\n'
170                << "----------------------------------------\n"
171                << toPlainText(message) << endl;
172
173         if (!use_gui)
174                 return;
175
176         if (theApp() == 0) {
177                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
178                 return;
179         }
180
181         /// Long operation in progress prevents user from Ok-ing the error dialog
182         bool long_op = theApp()->longOperationStarted();
183         if (long_op)
184                 theApp()->stopLongOperation();
185
186         // Don't use a hourglass cursor while displaying the alert
187         qApp->setOverrideCursor(Qt::ArrowCursor);
188
189         if (!askshowagain) {
190                 ProgressInterface::instance()->warning(
191                                 toqstr(title),
192                                 toqstr(message));
193         } else {
194                 ProgressInterface::instance()->toggleWarning(
195                                 toqstr(title),
196                                 toqstr(message),
197                                 toqstr(message));
198         }
199
200         qApp->restoreOverrideCursor();
201
202         if (long_op)
203                 theApp()->startLongOperation();
204 }
205
206 void warning(docstring const & title, docstring const & message,
207              bool askshowagain)
208 {
209 #ifdef EXPORT_in_THREAD
210         InGuiThread<void>().call(&doWarning,
211 #else
212         doWarning(
213 #endif
214                                 title, message, askshowagain);
215 }
216
217 void doError(docstring const & title, docstring const & message, bool backtrace)
218 {
219         lyxerr << "Error: " << toPlainText(title) << '\n'
220                << "----------------------------------------\n"
221                << toPlainText(message) << endl;
222
223         QString details;
224         if (backtrace)
225                 details = toqstr(printCallStack());
226
227         if (!use_gui)
228                 return;
229
230         if (theApp() == 0) {
231                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
232                 return;
233         }
234
235         /// Long operation in progress prevents user from Ok-ing the error dialog
236         bool long_op = theApp()->longOperationStarted();
237         if (long_op)
238                 theApp()->stopLongOperation();
239
240         // Don't use a hourglass cursor while displaying the alert
241         qApp->setOverrideCursor(Qt::ArrowCursor);
242
243         ProgressInterface::instance()->error(
244                 toqstr(title),
245                 toqstr(message),
246                 details);
247
248         qApp->restoreOverrideCursor();
249
250         if (long_op)
251                 theApp()->startLongOperation();
252 }
253
254 void error(docstring const & title, docstring const & message, bool backtrace)
255 {
256 #ifdef EXPORT_in_THREAD
257         InGuiThread<void>().call(&doError,
258 #else
259         doError(
260 #endif
261                                 title, message, backtrace);
262 }
263
264 void doInformation(docstring const & title, docstring const & message)
265 {
266         if (!use_gui || lyxerr.debugging())
267                 lyxerr << toPlainText(title) << '\n'
268                        << "----------------------------------------\n"
269                        << toPlainText(message) << endl;
270
271         if (!use_gui)
272                 return;
273
274         if (theApp() == 0) {
275                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
276                 return;
277         }
278
279         /// Long operation in progress prevents user from Ok-ing the error dialog
280         bool long_op = theApp()->longOperationStarted();
281         if (long_op)
282                 theApp()->stopLongOperation();
283
284         // Don't use a hourglass cursor while displaying the alert
285         qApp->setOverrideCursor(Qt::ArrowCursor);
286
287         ProgressInterface::instance()->information(
288                 toqstr(title),
289                 toqstr(message));
290
291         qApp->restoreOverrideCursor();
292
293         if (long_op)
294                 theApp()->startLongOperation();
295 }
296
297 void information(docstring const & title, docstring const & message)
298 {
299 #ifdef EXPORT_in_THREAD
300         InGuiThread<void>().call(&doInformation,
301 #else
302         doInformation(
303 #endif
304                                 title, message);
305 }
306
307 bool doAskForText(docstring & response, docstring const & msg,
308         docstring const & dflt)
309 {
310         if (!use_gui || lyxerr.debugging()) {
311                 lyxerr << "----------------------------------------\n"
312                        << toPlainText(msg) << '\n'
313                        << "Assuming answer is " << dflt << '\n'
314                        << "----------------------------------------" << endl;
315                 if (!use_gui) {
316                         response = dflt;
317                         return true;
318                 }
319         }
320
321         docstring const title = bformat(from_utf8("%1$s"), msg);
322
323         /// Long operation in progress prevents user from Ok-ing the error dialog
324         bool long_op = theApp()->longOperationStarted();
325         if (long_op)
326                 theApp()->stopLongOperation();
327
328         bool ok;
329         QString text = QInputDialog::getText(qApp->focusWidget(),
330                 toqstr(title),
331                 toqstr(char_type('&') + msg),
332                 QLineEdit::Normal,
333                 toqstr(dflt), &ok);
334
335         if (long_op)
336                 theApp()->startLongOperation();
337
338         if (ok) {
339                 response = qstring_to_ucs4(text);
340                 return true;
341         }
342         response.clear();
343         return false;
344 }
345
346 bool askForText(docstring & response, docstring const & msg,
347         docstring const & dflt)
348 {
349 #ifdef EXPORT_in_THREAD
350         return InGuiThread<bool>().call(&doAskForText,
351 #else
352         return doAskForText(
353 #endif
354                                 response, msg, dflt);
355 }
356
357 } // namespace Alert
358 } // namespace frontend
359 } // namespace lyx