]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
Speed up exit time
[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  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiProgressView.h"
17
18 #include "GuiApplication.h"
19 #include "GuiProgress.h"
20 #include "qt_helpers.h"
21
22 #include "FuncRequest.h"
23
24 #include "support/convert.h"
25 #include "support/debug.h"
26
27 #include <QCheckBox>
28 #include <QDebug>
29 #include <QSettings>
30 #include <QTime>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36 namespace frontend {
37
38
39 ProgressViewWidget::ProgressViewWidget()
40 {
41         setupUi(this);
42 }
43
44
45 GuiProgressView::~GuiProgressView()
46 {
47         delete widget_;
48 }
49
50
51 namespace{
52 typedef pair<int, QString> DebugMap;
53 typedef vector<DebugMap> DebugVector;
54
55 bool DebugSorter(DebugMap const & a, DebugMap const & b)
56 {
57         return a.second < b.second;
58 }
59 }
60
61
62 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area, 
63         Qt::WindowFlags flags)
64         : DockView(parent, "progress", qt_("Progress/Debug Messages"), area, flags)
65 {
66         eol_last_ = true;
67         widget_ = new ProgressViewWidget;
68         widget_->setMinimumHeight(150);
69         widget_->debugMessagesTW->setSizePolicy(QSizePolicy::Ignored,
70                                                 QSizePolicy::Expanding);
71         widget_->adjustSize();
72         setWidget(widget_);
73
74         widget_->outTE->setFont(guiApp->typewriterSystemFont());
75         widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
76
77         connect(widget_->debugNoneRB, SIGNAL(clicked()),
78                 this, SLOT(debugSelectionChanged()));
79         connect(widget_->debugSelectedRB, SIGNAL(clicked()),
80                 this, SLOT(debugSelectionChanged()));
81         connect(widget_->debugAnyRB, SIGNAL(clicked()),
82                 this, SLOT(debugSelectionChanged()));
83         widget_->debugMessagesTW->setEnabled(false);
84         widget_->debugNoneRB->setChecked(true);
85
86         // ignore Debug::NONE and Debug::ANY
87         int const level_count = Debug::levelCount() - 1;
88         DebugVector dmap;
89         for (int i = 1 ; i < level_count; i++) {
90                 Debug::Type const level = Debug::value(i);
91                 QString const desc = qt_(Debug::description(level));
92                 dmap.push_back(DebugMap(level, desc));
93         }
94         sort(dmap.begin(), dmap.end(), DebugSorter);
95
96         widget_->debugMessagesTW->setColumnCount(2);
97         widget_->debugMessagesTW->headerItem()->setText(0, qt_("Debug Level"));
98         widget_->debugMessagesTW->headerItem()->setText(1, qt_("Set"));
99
100         DebugVector::const_iterator dit = dmap.begin();
101         DebugVector::const_iterator const den = dmap.end();
102         for (; dit != den; ++dit) {
103                 QTreeWidgetItem * item = new QTreeWidgetItem(widget_->debugMessagesTW);
104                 item->setText(0, dit->second);
105                 item->setData(0, Qt::UserRole, int(dit->first));
106                 item->setText(1, qt_("No"));
107         }
108         widget_->debugMessagesTW->resizeColumnToContents(0);
109         widget_->debugMessagesTW->resizeColumnToContents(1);
110         connect(widget_->debugMessagesTW,
111                 SIGNAL(itemActivated(QTreeWidgetItem *, int)),
112                 this, SLOT(debugMessageActivated(QTreeWidgetItem *, int)));
113   
114         GuiProgress * progress =
115                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
116
117         if (progress) {
118                 connect(progress, SIGNAL(processStarted(QString const &)),
119                         this, SLOT(appendText(QString const &)));
120                 //connect(progress, SIGNAL(processFinished(QString const &)),
121                 //      this, SLOT(appendText(QString const &)));
122                 connect(progress, SIGNAL(appendMessage(QString const &)),
123                         this, SLOT(appendText(QString const &)));
124                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
125                         this, SLOT(appendLyXErrText(QString const &)), Qt::QueuedConnection);
126                 connect(progress, SIGNAL(appendError(QString const &)),
127                         this, SLOT(appendText(QString const &)));
128                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
129                 progress->lyxerrConnect();
130         }
131 }
132
133
134 void GuiProgressView::debugMessageActivated(QTreeWidgetItem * item, int)
135 {
136         if (item == 0)
137                 return;
138
139         QString const no = qt_("No");
140         QString const yes = qt_("Yes");
141
142         bool selected = (item->text(1) == yes);
143         item->setText(1, selected ? no : yes);
144
145         levelChanged();
146 }
147
148
149 void GuiProgressView::levelChanged()
150 {
151         unsigned int level = Debug::NONE;
152         QTreeWidgetItemIterator it(widget_->debugMessagesTW);
153         while (*it) {
154                 if ((*it)->text(1) == qt_("Yes"))
155                         level |= (*it)->data(0, Qt::UserRole).toInt();
156                 ++it;
157         }
158         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
159 }
160
161
162 void GuiProgressView::debugSelectionChanged()
163 {
164         unsigned int level = Debug::NONE;
165         if (widget_->debugAnyRB->isChecked())
166                 level = Debug::ANY;
167         else if (widget_->debugSelectedRB->isChecked()) {
168                 widget_->debugMessagesTW->setEnabled(true);
169                 levelChanged();
170                 return;
171         }
172         QTreeWidgetItemIterator it(widget_->debugMessagesTW);
173         while (*it) {
174                 (*it)->setText(1, level == Debug::NONE ?
175                                 qt_("No") : qt_("Yes"));
176                 ++it;
177         }
178         widget_->debugMessagesTW->setEnabled(false);
179         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
180 }
181
182
183 void GuiProgressView::clearText()
184 {
185         if (widget_->autoClearCB->isChecked()){
186                 widget_->outTE->clear();
187                 eol_last_ = true;
188         }
189 }
190
191
192 void GuiProgressView::appendLyXErrText(QString const & text)
193 {
194         widget_->outTE->moveCursor(QTextCursor::End);
195         widget_->outTE->insertPlainText(text);
196         widget_->outTE->ensureCursorVisible();
197         eol_last_ = false;
198         // Give the user a chance to disable debug messages because
199         // showing Debug::ANY messages completely blocks the GUI.
200         // Text is not always send as the whole line, so we must be
201         // careful about eolns.
202         // WARNING: processing events could cause crashes!
203         // TODO: find a better solution
204         if (text.endsWith("\n")) {
205                 eol_last_ = true;
206                 QApplication::processEvents();
207         }
208 }
209
210
211 void GuiProgressView::appendText(QString const & text)
212 {
213         if (text.isEmpty() || !widget_->sbarCB->isChecked())
214                 return;
215         QString str = GuiProgress::currentTime();
216         str += ": " + text;
217         if (!eol_last_)
218                 str = "\n" + str;
219         eol_last_ = text.endsWith("\n");
220
221         widget_->outTE->moveCursor(QTextCursor::End);
222         widget_->outTE->insertPlainText(str);
223         widget_->outTE->ensureCursorVisible();
224 }
225
226
227 void GuiProgressView::saveSession(QSettings & settings) const
228 {
229         Dialog::saveSession(settings);
230         settings.setValue(
231                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
232         settings.setValue(
233                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
234 }
235
236
237 void GuiProgressView::restoreSession()
238 {
239         DockView::restoreSession();
240         QSettings settings;
241         widget_->autoClearCB->setChecked(
242                 settings.value(sessionKey() + "/autoclear", true).toBool());
243         widget_->sbarCB->setChecked(
244                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
245 }
246
247
248 void GuiProgressView::showEvent(QShowEvent*)
249 {
250         ProgressInterface::instance()->lyxerrConnect();
251 }
252
253
254 void GuiProgressView::hideEvent(QHideEvent*)
255 {
256         ProgressInterface::instance()->lyxerrDisconnect();
257 }
258
259
260 Dialog * createGuiProgressView(GuiView & guiview)
261 {
262         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
263 }
264
265
266
267 } // namespace frontend
268 } // namespace lyx
269
270 #include "moc_GuiProgressView.cpp"