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