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