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.
7 * \author Jürgen Spitzmüller
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
17 #include "frontends/Application.h"
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"
25 #include "support/debug.h"
26 #include "support/docstring.h"
27 #include "support/lstrings.h"
29 #include <QApplication>
31 #include <QMessageBox>
33 #include <QInputDialog>
40 using namespace lyx::support;
46 class GuiToggleWarningDialog : public QDialog, public Ui::ToggleWarningUi
49 GuiToggleWarningDialog(QWidget * parent) : QDialog(parent)
51 Ui::ToggleWarningUi::setupUi(this);
52 QDialog::setModal(true);
57 static docstring const formatted(docstring const & text)
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);
73 docstring const word =
74 nxtpos == docstring::npos ?
76 text.substr(curpos, nxtpos - curpos);
78 bool const newline = (nxtpos2 != docstring::npos &&
81 docstring const line_plus_word =
82 line.empty() ? word : line + char_type(' ') + word;
84 // FIXME: make w be size_t
85 if (int(line_plus_word.length()) >= w) {
86 sout += line + char_type('\n');
88 sout += word + char_type('\n');
95 sout += line_plus_word + char_type('\n');
100 line += char_type(' ');
104 if (nxtpos == docstring::npos) {
117 void toggleWarning(docstring const & title, docstring const & msg)
123 if (settings.value("hidden_warnings/" + toqstr(msg), false).toBool())
126 GuiToggleWarningDialog * dlg =
127 new GuiToggleWarningDialog(qApp->focusWidget());
129 dlg->setWindowTitle(toqstr(title));
130 dlg->messageLA->setText(toqstr(formatted(msg)));
131 dlg->dontShowAgainCB->setChecked(false);
133 if (dlg->exec() == QDialog::Accepted)
134 if (dlg->dontShowAgainCB->isChecked())
135 settings.setValue("hidden_warnings/"
136 + toqstr(msg), true);
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)
146 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
147 if (!use_gui || lyxerr.debugging()) {
148 lyxerr << title0 << '\n'
149 << "----------------------------------------\n"
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;
159 return default_button;
162 docstring const title = bformat(_("LyX: %1$s"), title0);
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);
169 // FIXME replace that with guiApp->currentView()
170 //LYXERR0("FOCUS: " << qApp->focusWidget());
171 int res = QMessageBox::information(qApp->focusWidget(),
173 toqstr(formatted(question)),
176 b3.empty() ? QString::null : toqstr(b3),
177 default_button, cancel_button);
179 // Qt bug: can return -1 on cancel or WM close, despite the docs.
186 void warning(docstring const & title0, docstring const & message,
187 bool const & askshowagain)
189 lyxerr << "Warning: " << title0 << '\n'
190 << "----------------------------------------\n"
196 docstring const title = bformat(_("LyX: %1$s"), title0);
201 QApplication app(argc, argv);
202 QMessageBox::warning(0,
204 toqstr(formatted(message)));
208 QMessageBox::warning(qApp->focusWidget(),
210 toqstr(formatted(message)));
212 toggleWarning(title, message);
219 void error(docstring const & title0, docstring const & message)
221 lyxerr << "Error: " << title0 << '\n'
222 << "----------------------------------------\n"
228 docstring const title = bformat(_("LyX: %1$s"), title0);
230 QApplication app(argc, argv);
231 QMessageBox::critical(0,
233 toqstr(formatted(message)));
236 QMessageBox::critical(qApp->focusWidget(),
238 toqstr(formatted(message)));
242 void information(docstring const & title0, docstring const & message)
244 if (!use_gui || lyxerr.debugging())
245 lyxerr << title0 << '\n'
246 << "----------------------------------------\n"
252 docstring const title = bformat(_("LyX: %1$s"), title0);
253 QMessageBox::information(qApp->focusWidget(),
255 toqstr(formatted(message)));
259 bool askForText(docstring & response, docstring const & msg,
260 docstring const & dflt)
262 if (!use_gui || lyxerr.debugging()) {
263 lyxerr << "----------------------------------------\n"
265 << "Assuming answer is " << dflt << '\n'
266 << "----------------------------------------" << endl;
273 docstring const title = bformat(_("LyX: %1$s"), msg);
276 QString text = QInputDialog::getText(qApp->focusWidget(),
278 toqstr(char_type('&') + msg),
283 response = qstring_to_ucs4(text);
292 } // namespace frontend