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