]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
091cbdc2f5c2745458a02072cb2ea135a1d2c9d1
[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 #if (QT_VERSION >= 0x040500)
70         QFont font(guiApp->typewriterFontName());
71         font.setKerning(false);
72         font.setFixedPitch(true);
73         font.setStyleHint(QFont::TypeWriter);
74         widget_->outTE->setFont(font);
75         widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
76
77         toggle_button = new LevelButton("Toggle ALL messages");
78         toggle_button->level = Debug::ANY;
79         toggle_button->setTristate(true);
80         toggle_button->setCheckState(Qt::PartiallyChecked);
81         widget_->settingsLayout->addWidget(toggle_button);
82         connect(toggle_button, SIGNAL(stateChanged(int)), this, SLOT(tristateChanged(int)));
83
84         // ignore Debug::NONE and Debug::ANY
85         int const level_count = Debug::levelCount() - 1;
86         for (int i = 1 ; i < level_count; i++) {
87                 Debug::Type const level = Debug::value(i);
88                 LevelButton * box = new LevelButton(toqstr(Debug::description(level)));
89                 box->level = level;
90                 widget_->settingsLayout->addWidget(box, (i + 3) % 10, (i + 3) / 10);
91                 box->setChecked(false);
92                 level_buttons << box;
93                 connect(box, SIGNAL(stateChanged(int)), this, SLOT(levelChanged()));
94         }
95         widget_->settingsLayout->activate();
96 #endif
97         
98         GuiProgress * progress =
99                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
100
101         if (progress) {
102                 connect(progress, SIGNAL(processStarted(QString const &)),
103                         this, SLOT(appendText(QString const &)));
104                 //connect(progress, SIGNAL(processFinished(QString const &)),
105                 //      this, SLOT(appendText(QString const &)));
106                 connect(progress, SIGNAL(appendMessage(QString const &)),
107                         this, SLOT(appendText(QString const &)));
108                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
109                         this, SLOT(appendLyXErrText(QString const &)));
110                 connect(progress, SIGNAL(appendError(QString const &)),
111                         this, SLOT(appendText(QString const &)));
112                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
113                 progress->lyxerrConnect();
114         }
115 }
116
117
118 void GuiProgressView::levelChanged()
119 {
120         int level = Debug::NONE;
121         checked_buttons.clear();
122         Q_FOREACH(LevelButton* button, level_buttons) {
123                 if (button->isChecked()) {
124                         level |= button->level;
125                         checked_buttons << button;
126                 }
127         }
128         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
129         
130         toggle_button->setCheckStatusSilent (Qt::PartiallyChecked);
131 }
132
133
134 void GuiProgressView::tristateChanged(int state)
135 {
136         if (state != Qt::PartiallyChecked) {
137                 Q_FOREACH(LevelButton* button, level_buttons) {
138                         button->setCheckStatusSilent(toggle_button->checkState());
139                 }
140                 int level = (state == Qt::Checked ? Debug::ANY : Debug::NONE);
141                 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
142         } else {
143                 Q_FOREACH(LevelButton* button, checked_buttons) {
144                         button->setCheckStatusSilent(Qt::Checked);
145                 }
146                 levelChanged();
147         }
148 }
149
150
151 void GuiProgressView::clearText()
152 {
153         if (widget_->autoClearCB->isChecked())
154                 widget_->outTE->clear();
155 }
156
157
158 void GuiProgressView::appendLyXErrText(QString const & text)
159 {
160         widget_->outTE->insertPlainText(text);
161         widget_->outTE->ensureCursorVisible();
162
163         // Give the user a chance to disable debug messages because
164         // showing Debug::ANY messages completely blocks the GUI.
165         // Text is not always send as the whole line, so we must be
166         // careful about eolns.
167         if (text.endsWith("\n"))
168                 QApplication::processEvents();
169 }
170
171
172 void GuiProgressView::appendText(QString const & text)
173 {
174         if (text.isEmpty() || !widget_->sbarCB->isChecked())
175                 return;
176         QString str = QTime::currentTime().toString();
177         str += ": " + text;
178         if (!text.endsWith("\n"))
179                 str += "\n";
180
181         widget_->outTE->insertPlainText(str);
182         widget_->outTE->ensureCursorVisible();
183 }
184
185
186 void GuiProgressView::saveSession() const
187 {
188         Dialog::saveSession();
189         QSettings settings;
190         settings.setValue(
191                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
192         settings.setValue(
193                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
194 }
195
196
197 void GuiProgressView::restoreSession()
198 {
199         DockView::restoreSession();
200         QSettings settings;
201         widget_->autoClearCB->setChecked(
202                 settings.value(sessionKey() + "/autoclear", true).toBool());
203         widget_->sbarCB->setChecked(
204                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
205 }
206
207
208 void GuiProgressView::showEvent(QShowEvent*)
209 {
210 #if (QT_VERSION >= 0x040500)
211         ProgressInterface::instance()->lyxerrConnect();
212 #else
213         hide();
214 #endif
215 }
216
217
218 void GuiProgressView::hideEvent(QHideEvent*)
219 {
220         ProgressInterface::instance()->lyxerrDisconnect();
221 }
222
223
224
225
226 Dialog * createGuiProgressView(GuiView & guiview)
227 {
228 #ifdef Q_WS_MACX
229         return new GuiProgressView(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
230 #else
231         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
232 #endif
233 }
234
235
236
237 } // namespace frontend
238 } // namespace lyx
239
240 #include "moc_GuiProgressView.cpp"