]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgress.cpp
Use QMessageBox for toggleWarning if possible
[lyx.git] / src / frontends / qt4 / GuiProgress.cpp
1 // -*- C++ -*-
2 /**
3  * \file GuiProgress.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Peter Kümmel
8  * \author Pavel Sanda
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiProgress.h"
16 #include "ui_ToggleWarningUi.h"
17
18 #include "qt_helpers.h"
19
20 #include "frontends/alert.h"
21
22 #include "support/debug.h"
23 #include "support/Systemcall.h"
24
25 #include <QApplication>
26 #include <QTime>
27 #include <QMessageBox>
28 #include <QSettings>
29
30
31 namespace lyx {
32 namespace frontend {
33
34
35 // This dialog is only a fallback for Qt < 5.2, which does not feature
36 // QMessageBox::setCheckBox() yet. Note that it has issues with line
37 // breaking and size, in particular with html.
38 #if QT_VERSION < 0x050200
39 class GuiToggleWarningDialog : public QDialog, public Ui::ToggleWarningUi
40 {
41 public:
42         GuiToggleWarningDialog(QWidget * parent) : QDialog(parent)
43         {
44                 Ui::ToggleWarningUi::setupUi(this);
45                 QDialog::setModal(true);
46         }
47 };
48 #endif
49
50
51 GuiProgress::GuiProgress()
52 {
53         connect(this, SIGNAL(processStarted(QString const &)), SLOT(doProcessStarted(QString const &)));
54         connect(this, SIGNAL(processFinished(QString const &)), SLOT(doProcessFinished(QString const &)));
55         connect(this, SIGNAL(appendMessage(QString const &)), SLOT(doAppendMessage(QString const &)));
56         connect(this, SIGNAL(appendError(QString const &)), SLOT(doAppendError(QString const &)));
57         connect(this, SIGNAL(clearMessages()), SLOT(doClearMessages()));
58
59         // Alert interface
60         connect(this, SIGNAL(warning(QString const &, QString const &)),
61                 SLOT(doWarning(QString const &, QString const &)));
62         connect(this, SIGNAL(toggleWarning(QString const &, QString const &, QString const &)),
63                 SLOT(doToggleWarning(QString const &, QString const &, QString const &)));
64         connect(this, SIGNAL(error(QString const &, QString const &, QString const &)),
65                 SLOT(doError(QString const &, QString const &, QString const &)));
66         connect(this, SIGNAL(information(QString const &, QString const &)),
67                 SLOT(doInformation(QString const &, QString const &)));
68         connect(this, SIGNAL(triggerFlush()),
69                 SLOT(startFlushing()));
70
71         flushDelay_.setInterval(200);
72         flushDelay_.setSingleShot(true);
73         connect(&flushDelay_, SIGNAL(timeout()), this, SLOT(updateWithLyXErr()));
74 }
75
76
77 int GuiProgress::prompt(docstring const & title, docstring const & question,
78                         int default_button, int cancel_button,
79                         docstring const & b1, docstring const & b2)
80 {
81         return Alert::prompt(title, question, default_button, cancel_button, b1, b2);
82 }
83
84
85 QString GuiProgress::currentTime()
86 {
87         return QTime::currentTime().toString("hh:mm:ss.zzz");
88 }
89
90
91 void GuiProgress::doProcessStarted(QString const & cmd)
92 {
93         appendText(currentTime() + ": <" + cmd + "> started");
94 }
95
96
97 void GuiProgress::doProcessFinished(QString const & cmd)
98 {
99         appendText(currentTime() + ": <" + cmd + "> done");
100 }
101
102
103 void GuiProgress::doAppendMessage(QString const & msg)
104 {
105         appendText(msg);
106 }
107
108
109 void GuiProgress::doAppendError(QString const & msg)
110 {
111         appendText(msg);
112 }
113
114
115 void GuiProgress::doClearMessages()
116 {
117         clearMessageText();
118 }
119
120
121 void GuiProgress::startFlushing()
122 {
123         flushDelay_.start();
124 }
125
126
127 void GuiProgress::lyxerrFlush()
128 {
129         triggerFlush();
130 }
131
132
133 void GuiProgress::updateWithLyXErr()
134 {
135         appendLyXErrMessage(toqstr(lyxerr_stream_.str()));
136         lyxerr_stream_.str("");
137 }
138
139
140 void GuiProgress::lyxerrConnect()
141 {
142         lyxerr.setSecondStream(&lyxerr_stream_);
143 }
144
145
146 void GuiProgress::lyxerrDisconnect()
147 {
148         lyxerr.setSecondStream(0);
149 }
150
151
152 GuiProgress::~GuiProgress()
153 {
154         lyxerrDisconnect();
155 }
156
157
158 void GuiProgress::appendText(QString const & text)
159 {
160         if (!text.isEmpty())
161                 updateStatusBarMessage(text);
162 }
163
164
165 void GuiProgress::doWarning(QString const & title, QString const & message)
166 {
167         QMessageBox::warning(qApp->focusWidget(), title, message);
168 }
169
170
171 void GuiProgress::doToggleWarning(QString const & title, QString const & msg, QString const & formatted)
172 {
173         QSettings settings;
174         if (settings.value("hidden_warnings/" + msg, false).toBool())
175                         return;
176
177 // Qt < 5.2 does not feature QMessageBox::setCheckBox() yet,
178 // so we roll our own dialog.
179 #if QT_VERSION < 0x050200
180         GuiToggleWarningDialog * dlg =
181                 new GuiToggleWarningDialog(qApp->focusWidget());
182
183         dlg->setWindowTitle(title);
184         dlg->messageLA->setText(formatted);
185         dlg->dontShowAgainCB->setChecked(false);
186
187         if (dlg->exec() == QDialog::Accepted)
188                 if (dlg->dontShowAgainCB->isChecked())
189                         settings.setValue("hidden_warnings/"
190                                 + msg, true);
191 #else
192         QCheckBox * dontShowAgainCB = new QCheckBox();
193         dontShowAgainCB->setText(qt_("&Do not show this warning again!"));
194         dontShowAgainCB->setToolTip(qt_("If you check this, LyX will not warn you again in the given case."));
195         QMessageBox box(QMessageBox::Warning, title, formatted,
196                         QMessageBox::Ok, qApp->focusWidget());
197         box.setCheckBox(dontShowAgainCB);
198         if (box.exec() == QMessageBox::Ok)
199                 if (dontShowAgainCB->isChecked())
200                         settings.setValue("hidden_warnings/"
201                                 + msg, true);
202 #endif
203 }
204
205
206 void GuiProgress::doError(QString const & title, QString const & message, QString const & details)
207 {
208         QMessageBox box(QMessageBox::Critical, title, message, QMessageBox::Ok, qApp->focusWidget());
209         if (!details.isEmpty()) {
210                 box.setDetailedText(details);
211         }
212         box.exec();
213 }
214
215
216 void GuiProgress::doInformation(QString const & title, QString const & message)
217 {
218         QMessageBox::information(qApp->focusWidget(), title, message);
219 }
220
221
222 } // namespace frontend
223 } // namespace lyx
224
225 #include "moc_GuiProgress.cpp"