]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
568e2111ee863ac3dcdc074658dda5ab0a67a96d
[lyx.git] / src / frontends / qt4 / GuiProgressView.cpp
1 // -*- C++ -*-
2 /**
3  * \file GuiProgressView.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 "GuiProgressView.h"
16 #include "GuiApplication.h"
17
18 #include "qt_helpers.h"
19 #include "FuncRequest.h"
20
21 #include "support/debug.h"
22 #include "support/convert.h"
23
24 #include <QSettings>
25 #include <QTime>
26
27 #include <QCheckBox>
28 #include <QDebug>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 struct LevelButton : QCheckBox
38 {
39         LevelButton(const QString& name) : QCheckBox(name) {}
40         Debug::Type level;
41 };
42
43
44 ProgressViewWidget::ProgressViewWidget()
45 {
46         setupUi(this);
47
48 }
49
50
51 GuiProgressView::~GuiProgressView()
52 {
53         delete widget_;
54 }
55
56
57 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area, 
58         Qt::WindowFlags flags) : DockView(parent, "progress", "Debug/Progress window", area, flags)
59 {
60         widget_ = new ProgressViewWidget();
61         setWidget(widget_);
62
63         QFont font(guiApp->typewriterFontName());
64         font.setKerning(false);
65         font.setFixedPitch(true);
66         font.setStyleHint(QFont::TypeWriter);
67         widget_->outTE->setFont(font);
68         widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
69
70         toggle_button = new LevelButton("Toggle ALL messages");
71         toggle_button->level = Debug::ANY;
72         toggle_button->setTristate(true);
73         toggle_button->setCheckState(Qt::PartiallyChecked);
74         widget_->settingsLayout->addWidget(toggle_button);
75         connect(toggle_button, SIGNAL(stateChanged(int)), this, SLOT(tristateChanged(int)));
76
77         // ignore Debug::NONE and Debug::ANY
78         int const level_count = Debug::levelCount() - 1;
79         for (int i = 1 ; i < level_count; i++) {
80                 Debug::Type const level = Debug::value(i);
81                 LevelButton * box = new LevelButton(toqstr(Debug::description(level)));
82                 box->level = level;
83                 widget_->settingsLayout->addWidget(box, (i + 3) % 10, (i + 3) / 10);
84                 box->setChecked(false);
85                 level_buttons << box;
86                 connect(box, SIGNAL(stateChanged(int)), this, SLOT(levelChanged()));
87         }
88         widget_->settingsLayout->activate();
89
90         GuiProgress * progress =
91                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
92
93         if (progress) {
94                 connect(progress, SIGNAL(processStarted(QString const &)),
95                         this, SLOT(appendText(QString const &)));
96                 //connect(progress, SIGNAL(processFinished(QString const &)),
97                 //      this, SLOT(appendText(QString const &)));
98                 connect(progress, SIGNAL(appendMessage(QString const &)),
99                         this, SLOT(appendText(QString const &)));
100                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
101                         this, SLOT(appendLyXErrText(QString const &)));
102                 connect(progress, SIGNAL(appendError(QString const &)),
103                         this, SLOT(appendText(QString const &)));
104                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
105                 progress->lyxerrConnect();
106         }
107 }
108
109
110 void GuiProgressView::levelChanged()
111 {
112         int level = Debug::NONE;
113         checked_buttons.clear();
114         Q_FOREACH(LevelButton* button, level_buttons) {
115                 if (button->isChecked()) {
116                         level |= button->level;
117                         checked_buttons << button;
118                 }
119         }
120         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
121         
122         toggle_button->blockSignals(true);
123         toggle_button->setCheckState (Qt::PartiallyChecked);
124         toggle_button->blockSignals(false);
125 }
126
127
128 void GuiProgressView::tristateChanged(int state)
129 {
130         if (state != Qt::PartiallyChecked) {
131                 Q_FOREACH(LevelButton* button, level_buttons) {
132                         button->blockSignals(true);
133                         button->setChecked(toggle_button->checkState());
134                         button->blockSignals(false);
135                 }
136                 int level = (state == Qt::Checked ? Debug::ANY : Debug::NONE);
137                 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
138         } else {
139                 Q_FOREACH(LevelButton* button, checked_buttons) {
140                         button->blockSignals(true);
141                         button->setChecked(true);
142                         button->blockSignals(false);
143                 }
144                 levelChanged();
145         }
146 }
147
148
149 void GuiProgressView::clearText()
150 {
151         if (widget_->autoClearCB->isChecked())
152                 widget_->outTE->clear();
153 }
154
155
156 void GuiProgressView::appendLyXErrText(QString const & text)
157 {
158         widget_->outTE->insertPlainText(text);
159         widget_->outTE->ensureCursorVisible();
160
161         // Give the user a chance to disable debug messages because
162         // showing Debug::ANY messages completely blocks the GUI.
163         // Text is not always send as the whole line, so we must be
164         // careful about eolns.
165         if (text.endsWith("\n"))
166                 QApplication::processEvents();
167 }
168
169
170 void GuiProgressView::appendText(QString const & text)
171 {
172         if (text.isEmpty() || !widget_->sbarCB->isChecked())
173                 return;
174         QString str = QTime::currentTime().toString();
175         str += ": " + text;
176         if (!text.endsWith("\n"))
177                 str += "\n";
178
179         widget_->outTE->insertPlainText(str);
180         widget_->outTE->ensureCursorVisible();
181 }
182
183
184 void GuiProgressView::saveSession() const
185 {
186         Dialog::saveSession();
187         QSettings settings;
188         settings.setValue(
189                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
190         settings.setValue(
191                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
192 }
193
194
195 void GuiProgressView::restoreSession()
196 {
197         DockView::restoreSession();
198         QSettings settings;
199         widget_->autoClearCB->setChecked(
200                 settings.value(sessionKey() + "/autoclear", true).toBool());
201         widget_->sbarCB->setChecked(
202                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
203 }
204
205
206 void GuiProgressView::showEvent(QShowEvent*)
207 {
208         ProgressInterface::instance()->lyxerrConnect();
209 }
210
211
212 void GuiProgressView::hideEvent(QHideEvent*)
213 {
214         ProgressInterface::instance()->lyxerrDisconnect();
215 }
216
217
218
219
220 Dialog * createGuiProgressView(GuiView & guiview)
221 {
222 #ifdef Q_WS_MACX
223         return new GuiProgressView(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
224 #else
225         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
226 #endif
227 }
228
229
230
231 } // namespace frontend
232 } // namespace lyx
233
234 #include "moc_GuiProgressView.cpp"