]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
f69258f9cd38d5d8549a2fef2077c56f648bf8d4
[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         void setCheckStatusSilent(Qt::CheckState state) {
43                 blockSignals(true);
44                 setCheckState(state);
45                 blockSignals(false);    
46         }
47 };
48
49
50 ProgressViewWidget::ProgressViewWidget()
51 {
52         setupUi(this);
53
54 }
55
56
57 GuiProgressView::~GuiProgressView()
58 {
59         delete widget_;
60 }
61
62
63 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area, 
64         Qt::WindowFlags flags) : DockView(parent, "progress", "Debug/Progress window", area, flags)
65 {
66         widget_ = new ProgressViewWidget();
67         setWidget(widget_);
68
69         QFont font(guiApp->typewriterFontName());
70         font.setKerning(false);
71         font.setFixedPitch(true);
72         font.setStyleHint(QFont::TypeWriter);
73         widget_->outTE->setFont(font);
74         widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
75
76         toggle_button = new LevelButton("Toggle ALL messages");
77         toggle_button->level = Debug::ANY;
78         toggle_button->setTristate(true);
79         toggle_button->setCheckState(Qt::PartiallyChecked);
80         widget_->settingsLayout->addWidget(toggle_button);
81         connect(toggle_button, SIGNAL(stateChanged(int)), this, SLOT(tristateChanged(int)));
82
83         // ignore Debug::NONE and Debug::ANY
84         int const level_count = Debug::levelCount() - 1;
85         for (int i = 1 ; i < level_count; i++) {
86                 Debug::Type const level = Debug::value(i);
87                 LevelButton * box = new LevelButton(toqstr(Debug::description(level)));
88                 box->level = level;
89                 widget_->settingsLayout->addWidget(box, (i + 3) % 10, (i + 3) / 10);
90                 box->setChecked(false);
91                 level_buttons << box;
92                 connect(box, SIGNAL(stateChanged(int)), this, SLOT(levelChanged()));
93         }
94         widget_->settingsLayout->activate();
95
96         GuiProgress * progress =
97                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
98
99         if (progress) {
100                 connect(progress, SIGNAL(processStarted(QString const &)),
101                         this, SLOT(appendText(QString const &)));
102                 //connect(progress, SIGNAL(processFinished(QString const &)),
103                 //      this, SLOT(appendText(QString const &)));
104                 connect(progress, SIGNAL(appendMessage(QString const &)),
105                         this, SLOT(appendText(QString const &)));
106                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
107                         this, SLOT(appendLyXErrText(QString const &)));
108                 connect(progress, SIGNAL(appendError(QString const &)),
109                         this, SLOT(appendText(QString const &)));
110                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
111                 progress->lyxerrConnect();
112         }
113 }
114
115
116 void GuiProgressView::levelChanged()
117 {
118         int level = Debug::NONE;
119         checked_buttons.clear();
120         Q_FOREACH(LevelButton* button, level_buttons) {
121                 if (button->isChecked()) {
122                         level |= button->level;
123                         checked_buttons << button;
124                 }
125         }
126         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
127         
128         toggle_button->setCheckStatusSilent (Qt::PartiallyChecked);
129 }
130
131
132 void GuiProgressView::tristateChanged(int state)
133 {
134         if (state != Qt::PartiallyChecked) {
135                 Q_FOREACH(LevelButton* button, level_buttons) {
136                         button->setCheckStatusSilent(toggle_button->checkState());
137                 }
138                 int level = (state == Qt::Checked ? Debug::ANY : Debug::NONE);
139                 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
140         } else {
141                 Q_FOREACH(LevelButton* button, checked_buttons) {
142                         button->setCheckStatusSilent(Qt::Checked);
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"