]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
2da0aea88079c869ddceddb7c0dc1612acd95817
[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 ((levels == Debug::ANY) && (levels == level))
79                         box->setChecked(true);
80                 else
81                         if ((level != Debug::ANY) && (levels & level))
82                                 box->setChecked(true);
83
84                 level_buttons << box;
85                 connect(box, SIGNAL(stateChanged(int)), this, SLOT(levelChanged()));
86         }
87         widget_->settingsLayout->activate();
88
89         GuiProgress * progress =
90                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
91
92         if (progress) {
93                 connect(progress, SIGNAL(processStarted(QString const &)),
94                         this, SLOT(appendText(QString const &)));
95                 //connect(progress, SIGNAL(processFinished(QString const &)),
96                 //      this, SLOT(appendText(QString const &)));
97                 connect(progress, SIGNAL(appendMessage(QString const &)),
98                         this, SLOT(appendText(QString const &)));
99                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
100                         this, SLOT(appendLyXErrText(QString const &)));
101                 connect(progress, SIGNAL(appendError(QString const &)),
102                         this, SLOT(appendText(QString const &)));
103                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
104                 progress->lyxerrConnect();
105         }
106 }
107
108
109 void GuiProgressView::levelChanged()
110 {
111         int level = Debug::NONE;
112         Q_FOREACH(const LevelButton* button, level_buttons) {
113                 if (button->isChecked()) {
114                         // Debug::NONE overwrites other levels
115                         if (button->level == Debug::NONE) {
116                                 level = Debug::NONE;
117                                 break;
118                         } else {
119                                 level |= button->level;
120                         }
121                 }
122         }
123         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
124 }
125
126
127 void GuiProgressView::clearText()
128 {
129         if (widget_->autoClearCB->isChecked())
130                 widget_->outTE->clear();
131 }
132
133
134 void GuiProgressView::appendLyXErrText(QString const & text)
135 {
136         widget_->outTE->insertPlainText(text);
137         widget_->outTE->ensureCursorVisible();
138
139         // Give the user a chance to disable debug messages because
140         // showing Debug::ANY messages completely blocks the GUI.
141         // Text is not always send as the whole line, so we must be
142         // careful about eolns.
143         if (text.endsWith("\n"))
144                 QApplication::processEvents();
145 }
146
147
148 void GuiProgressView::appendText(QString const & text)
149 {
150         if (text.isEmpty() || !widget_->sbarCB->isChecked())
151                 return;
152         QString str = QTime::currentTime().toString();
153         str += ": " + text;
154         if (!text.endsWith("\n"))
155                 str += "\n";
156
157         widget_->outTE->insertPlainText(str);
158         widget_->outTE->ensureCursorVisible();
159 }
160
161
162 void GuiProgressView::saveSession() const
163 {
164         Dialog::saveSession();
165         QSettings settings;
166         settings.setValue(
167                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
168         settings.setValue(
169                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
170 }
171
172
173 void GuiProgressView::restoreSession()
174 {
175         DockView::restoreSession();
176         QSettings settings;
177         widget_->autoClearCB->setChecked(
178                 settings.value(sessionKey() + "/autoclear", true).toBool());
179         widget_->sbarCB->setChecked(
180                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
181 }
182
183
184 void GuiProgressView::showEvent(QShowEvent*)
185 {
186         ProgressInterface::instance()->lyxerrConnect();
187 }
188
189
190 void GuiProgressView::hideEvent(QHideEvent*)
191 {
192         ProgressInterface::instance()->lyxerrDisconnect();
193 }
194
195
196
197
198 Dialog * createGuiProgressView(GuiView & guiview)
199 {
200 #ifdef Q_WS_MACX
201         return new GuiProgressView(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
202 #else
203         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
204 #endif
205 }
206
207
208
209 } // namespace frontend
210 } // namespace lyx
211
212 #include "moc_GuiProgressView.cpp"