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