]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiAlert.cpp
#12818 amend last change b924db72c5 - use c++ 11 compiler compatible code
[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         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);
118         if (!b2.empty())
119                 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
120         if (!b3.empty())
121                 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
122         if (!b4.empty())
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]));
128         msg_box.exec();
129         const QAbstractButton * button = msg_box.clickedButton();
130
131         qApp->restoreOverrideCursor();
132
133         if (long_op)
134                 theApp()->startLongOperation();
135
136         size_t res = cancel_button;
137
138         if (button == nullptr)
139                 return res;
140         else {
141                 // Convert selection of the button into an integer
142                 for (size_t i = 0; i < numbuttons; i++) {
143                         if (button == b[i]) {
144                                 res = i;
145                                 break;
146                         }
147                 }
148         }
149
150         return res;
151 }
152
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)
157 {
158 #ifdef EXPORT_in_THREAD
159         return InGuiThread<int>().call(&doPrompt,
160 #else
161         return doPrompt(
162 #endif
163                                 title, question, default_button,
164                                 cancel_button, b0, b1, b2, b3);
165 }
166
167 void doWarning(docstring const & title, docstring const & message,
168              bool askshowagain)
169 {
170         lyxerr << "Warning: " << toPlainText(title) << '\n'
171                << "----------------------------------------\n"
172                << toPlainText(message) << endl;
173
174         if (!use_gui)
175                 return;
176
177         if (theApp() == 0) {
178                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
179                 return;
180         }
181
182         /// Long operation in progress prevents user from Ok-ing the error dialog
183         bool long_op = theApp()->longOperationStarted();
184         if (long_op)
185                 theApp()->stopLongOperation();
186
187         // Don't use a hourglass cursor while displaying the alert
188         qApp->setOverrideCursor(Qt::ArrowCursor);
189
190         if (!askshowagain) {
191                 ProgressInterface::instance()->warning(
192                                 toqstr(title),
193                                 toqstr(message));
194         } else {
195                 ProgressInterface::instance()->toggleWarning(
196                                 toqstr(title),
197                                 toqstr(message),
198                                 toqstr(message));
199         }
200
201         qApp->restoreOverrideCursor();
202
203         if (long_op)
204                 theApp()->startLongOperation();
205 }
206
207 void warning(docstring const & title, docstring const & message,
208              bool askshowagain)
209 {
210 #ifdef EXPORT_in_THREAD
211         InGuiThread<void>().call(&doWarning,
212 #else
213         doWarning(
214 #endif
215                                 title, message, askshowagain);
216 }
217
218 void doError(docstring const & title, docstring const & message, bool backtrace)
219 {
220         lyxerr << "Error: " << toPlainText(title) << '\n'
221                << "----------------------------------------\n"
222                << toPlainText(message) << endl;
223
224         QString details;
225         if (backtrace)
226                 details = toqstr(printCallStack());
227
228         if (!use_gui)
229                 return;
230
231         if (theApp() == 0) {
232                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
233                 return;
234         }
235
236         /// Long operation in progress prevents user from Ok-ing the error dialog
237         bool long_op = theApp()->longOperationStarted();
238         if (long_op)
239                 theApp()->stopLongOperation();
240
241         // Don't use a hourglass cursor while displaying the alert
242         qApp->setOverrideCursor(Qt::ArrowCursor);
243
244         ProgressInterface::instance()->error(
245                 toqstr(title),
246                 toqstr(message),
247                 details);
248
249         qApp->restoreOverrideCursor();
250
251         if (long_op)
252                 theApp()->startLongOperation();
253 }
254
255 void error(docstring const & title, docstring const & message, bool backtrace)
256 {
257 #ifdef EXPORT_in_THREAD
258         InGuiThread<void>().call(&doError,
259 #else
260         doError(
261 #endif
262                                 title, message, backtrace);
263 }
264
265 void doInformation(docstring const & title, docstring const & message)
266 {
267         if (!use_gui || lyxerr.debugging())
268                 lyxerr << toPlainText(title) << '\n'
269                        << "----------------------------------------\n"
270                        << toPlainText(message) << endl;
271
272         if (!use_gui)
273                 return;
274
275         if (theApp() == 0) {
276                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
277                 return;
278         }
279
280         /// Long operation in progress prevents user from Ok-ing the error dialog
281         bool long_op = theApp()->longOperationStarted();
282         if (long_op)
283                 theApp()->stopLongOperation();
284
285         // Don't use a hourglass cursor while displaying the alert
286         qApp->setOverrideCursor(Qt::ArrowCursor);
287
288         ProgressInterface::instance()->information(
289                 toqstr(title),
290                 toqstr(message));
291
292         qApp->restoreOverrideCursor();
293
294         if (long_op)
295                 theApp()->startLongOperation();
296 }
297
298 void information(docstring const & title, docstring const & message)
299 {
300 #ifdef EXPORT_in_THREAD
301         InGuiThread<void>().call(&doInformation,
302 #else
303         doInformation(
304 #endif
305                                 title, message);
306 }
307
308 bool doAskForText(docstring & response, docstring const & msg,
309         docstring const & dflt)
310 {
311         if (!use_gui || lyxerr.debugging()) {
312                 lyxerr << "----------------------------------------\n"
313                        << toPlainText(msg) << '\n'
314                        << "Assuming answer is " << dflt << '\n'
315                        << "----------------------------------------" << endl;
316                 if (!use_gui) {
317                         response = dflt;
318                         return true;
319                 }
320         }
321
322         docstring const title = bformat(from_utf8("%1$s"), msg);
323
324         /// Long operation in progress prevents user from Ok-ing the error dialog
325         bool long_op = theApp()->longOperationStarted();
326         if (long_op)
327                 theApp()->stopLongOperation();
328
329         bool ok;
330         QString text = QInputDialog::getText(qApp->focusWidget(),
331                 toqstr(title),
332                 toqstr(char_type('&') + msg),
333                 QLineEdit::Normal,
334                 toqstr(dflt), &ok);
335
336         if (long_op)
337                 theApp()->startLongOperation();
338
339         if (ok) {
340                 response = qstring_to_ucs4(text);
341                 return true;
342         }
343         response.clear();
344         return false;
345 }
346
347 bool askForText(docstring & response, docstring const & msg,
348         docstring const & dflt)
349 {
350 #ifdef EXPORT_in_THREAD
351         return InGuiThread<bool>().call(&doAskForText,
352 #else
353         return doAskForText(
354 #endif
355                                 response, msg, dflt);
356 }
357
358 } // namespace Alert
359 } // namespace frontend
360 } // namespace lyx