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