]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiAlert.cpp
Fix also nomenclature dialog which was refered in #3852
[lyx.git] / src / frontends / qt4 / GuiAlert.cpp
1 /**
2  * \file qt4/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
17 #include "frontends/Application.h"
18
19 #include "qt_helpers.h"
20 #include "LyX.h" // for lyx::use_gui
21 #include "ui_AskForTextUi.h"
22 #include "ui_ToggleWarningUi.h"
23 #include "support/gettext.h"
24
25 #include "support/debug.h"
26 #include "support/docstring.h"
27 #include "support/lstrings.h"
28
29 #include <QApplication>
30 #include <QCheckBox>
31 #include <QMessageBox>
32 #include <QLineEdit>
33 #include <QInputDialog>
34 #include <QSettings>
35
36 #include <iomanip>
37 #include <iostream>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43 namespace frontend {
44
45
46 class GuiToggleWarningDialog : public QDialog, public Ui::ToggleWarningUi
47 {
48 public:
49         GuiToggleWarningDialog(QWidget * parent) : QDialog(parent)
50         {
51                 Ui::ToggleWarningUi::setupUi(this);
52                 QDialog::setModal(true);
53         }
54 };
55
56
57 static docstring const formatted(docstring const & text)
58 {
59         const int w = 80;
60         docstring sout;
61
62         if (text.empty())
63                 return sout;
64
65         size_t curpos = 0;
66         docstring line;
67
68         while (true) {
69                 size_t const nxtpos1 = text.find(' ',  curpos);
70                 size_t const nxtpos2 = text.find('\n', curpos);
71                 size_t const nxtpos = min(nxtpos1, nxtpos2);
72
73                 docstring const word =
74                         nxtpos == docstring::npos ?
75                         text.substr(curpos) :
76                         text.substr(curpos, nxtpos - curpos);
77
78                 bool const newline = (nxtpos2 != docstring::npos &&
79                                       nxtpos2 < nxtpos1);
80
81                 docstring const line_plus_word =
82                         line.empty() ? word : line + char_type(' ') + word;
83
84                 // FIXME: make w be size_t
85                 if (int(line_plus_word.length()) >= w) {
86                         sout += line + char_type('\n');
87                         if (newline) {
88                                 sout += word + char_type('\n');
89                                 line.erase();
90                         } else {
91                                 line = word;
92                         }
93
94                 } else if (newline) {
95                         sout += line_plus_word + char_type('\n');
96                         line.erase();
97
98                 } else {
99                         if (!line.empty())
100                                 line += char_type(' ');
101                         line += word;
102                 }
103
104                 if (nxtpos == docstring::npos) {
105                         if (!line.empty())
106                                 sout += line;
107                         break;
108                 }
109
110                 curpos = nxtpos + 1;
111         }
112
113         return sout;
114 }
115
116
117 void toggleWarning(docstring const & title, docstring const & msg)
118 {
119         if (!use_gui)
120                 return;
121
122         QSettings settings;
123         if (settings.value("hidden_warnings/" + toqstr(msg), false).toBool())
124                 return;
125
126         GuiToggleWarningDialog * dlg =
127                 new GuiToggleWarningDialog(qApp->focusWidget());
128
129         dlg->setWindowTitle(toqstr(title));
130         dlg->messageLA->setText(toqstr(formatted(msg)));
131         dlg->dontShowAgainCB->setChecked(false);
132
133         if (dlg->exec() == QDialog::Accepted)
134                 if (dlg->dontShowAgainCB->isChecked())
135                         settings.setValue("hidden_warnings/"
136                                 + toqstr(msg), true);
137 }
138
139
140 namespace Alert {
141
142 int prompt(docstring const & title0, docstring const & question,
143                   int default_button, int cancel_button,
144                   docstring const & b1, docstring const & b2, docstring const & b3)
145 {
146         //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
147         if (!use_gui || lyxerr.debugging()) {
148                 lyxerr << title0 << '\n'
149                        << "----------------------------------------\n"
150                        << question << endl;
151
152                 lyxerr << "Assuming answer is ";
153                 switch (default_button) {
154                 case 0: lyxerr << b1 << endl;
155                 case 1: lyxerr << b2 << endl;
156                 case 2: lyxerr << b3 << endl;
157                 }
158                 if (!use_gui)
159                         return default_button;
160         }
161
162         docstring const title = bformat(_("LyX: %1$s"), title0);
163
164         // For some reason, sometimes Qt uses an hourglass or watch cursor when
165         // displaying the alert. Hence, we ask for the standard cursor shape.
166         // This call has no effect if the cursor has not been overridden.
167         qApp->changeOverrideCursor(Qt::ArrowCursor);
168
169         // FIXME replace that with guiApp->currentView()
170         //LYXERR0("FOCUS: " << qApp->focusWidget());
171         int res = QMessageBox::information(qApp->focusWidget(),
172                                            toqstr(title),
173                                            toqstr(formatted(question)),
174                                            toqstr(b1),
175                                            toqstr(b2),
176                                            b3.empty() ? QString::null : toqstr(b3),
177                                            default_button, cancel_button);
178
179         // Qt bug: can return -1 on cancel or WM close, despite the docs.
180         if (res == -1)
181                 res = cancel_button;
182         return res;
183 }
184
185
186 void warning(docstring const & title0, docstring const & message,
187              bool const & askshowagain)
188 {
189         lyxerr << "Warning: " << title0 << '\n'
190                << "----------------------------------------\n"
191                << message << endl;
192
193         if (!use_gui)
194                 return;
195
196         docstring const title = bformat(_("LyX: %1$s"), title0);
197
198         if (theApp() == 0) {
199                 int argc = 1;
200                 char * argv[1];
201                 QApplication app(argc, argv);
202                 QMessageBox::warning(0,
203                         toqstr(title),
204                         toqstr(formatted(message)));
205                 return;
206         }
207         if (!askshowagain)
208                 QMessageBox::warning(qApp->focusWidget(),
209                                 toqstr(title),
210                                 toqstr(formatted(message)));
211         else
212                 toggleWarning(title, message);
213 }
214
215
216 int argc = 1;
217 char * argv[1];
218
219 void error(docstring const & title0, docstring const & message)
220 {
221         lyxerr << "Error: " << title0 << '\n'
222                << "----------------------------------------\n"
223                << message << endl;
224
225         if (!use_gui)
226                 return;
227
228         docstring const title = bformat(_("LyX: %1$s"), title0);
229         if (theApp() == 0) {
230                 QApplication app(argc, argv);
231                 QMessageBox::critical(0,
232                         toqstr(title),
233                         toqstr(formatted(message)));
234                 return;
235         }
236         QMessageBox::critical(qApp->focusWidget(),
237                               toqstr(title),
238                               toqstr(formatted(message)));
239 }
240
241
242 void information(docstring const & title0, docstring const & message)
243 {
244         if (!use_gui || lyxerr.debugging())
245                 lyxerr << title0 << '\n'
246                        << "----------------------------------------\n"
247                        << message << endl;
248
249         if (!use_gui)
250                 return;
251
252         docstring const title = bformat(_("LyX: %1$s"), title0);
253         QMessageBox::information(qApp->focusWidget(),
254                                  toqstr(title),
255                                  toqstr(formatted(message)));
256 }
257
258
259 bool askForText(docstring & response, docstring const & msg,
260         docstring const & dflt)
261 {
262         if (!use_gui || lyxerr.debugging()) {
263                 lyxerr << "----------------------------------------\n"
264                        << msg << '\n'
265                        << "Assuming answer is " << dflt << '\n'
266                        << "----------------------------------------" << endl;
267                 if (!use_gui) {
268                         response = dflt;
269                         return true;
270                 }
271         }
272
273         docstring const title = bformat(_("LyX: %1$s"), msg);
274
275         bool ok;
276         QString text = QInputDialog::getText(qApp->focusWidget(),
277                 toqstr(title),
278                 toqstr(char_type('&') + msg),
279                 QLineEdit::Normal,
280                 toqstr(dflt), &ok);
281
282         if (ok) {
283                 response = qstring_to_ucs4(text);
284                 return true;
285         }
286         response.clear();
287         return false;
288 }
289
290
291 } // namespace Alert
292 } // namespace frontend
293 } // namespace lyx