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