]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
Enable OK/Apply buttons when resetting to class defaults.
[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 <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<int, 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         // ignore Debug::NONE and Debug::ANY
89         int const level_count = Debug::levelCount() - 1;
90         DebugVector dmap;
91         for (int i = 1 ; i < level_count; i++) {
92                 Debug::Type const level = Debug::value(i);
93                 QString const desc =
94                         toqstr(from_ascii(Debug::name(level) + " - "))
95                         + qt_(Debug::description(level));
96                 dmap.push_back(DebugMap(level, desc));
97         }
98         sort(dmap.begin(), dmap.end(), DebugSorter);
99
100         widget_->debugMessagesTW->setColumnCount(2);
101         widget_->debugMessagesTW->headerItem()->setText(0, qt_("Debug Level"));
102         widget_->debugMessagesTW->headerItem()->setText(1, qt_("Set"));
103
104         DebugVector::const_iterator dit = dmap.begin();
105         DebugVector::const_iterator const den = dmap.end();
106         for (; dit != den; ++dit) {
107                 QTreeWidgetItem * item = new QTreeWidgetItem(widget_->debugMessagesTW);
108                 item->setText(0, dit->second);
109                 item->setData(0, Qt::UserRole, int(dit->first));
110                 item->setText(1, qt_("No"));
111         }
112         widget_->debugMessagesTW->resizeColumnToContents(0);
113         widget_->debugMessagesTW->resizeColumnToContents(1);
114         connect(widget_->debugMessagesTW,
115                 SIGNAL(itemActivated(QTreeWidgetItem *, int)),
116                 this, SLOT(debugMessageActivated(QTreeWidgetItem *, int)));
117
118         GuiProgress * progress =
119                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
120
121         if (progress) {
122                 connect(progress, SIGNAL(processStarted(QString const &)),
123                         this, SLOT(appendText(QString const &)));
124                 //connect(progress, SIGNAL(processFinished(QString const &)),
125                 //      this, SLOT(appendText(QString const &)));
126                 connect(progress, SIGNAL(appendMessage(QString const &)),
127                         this, SLOT(appendText(QString const &)));
128                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
129                         this, SLOT(appendLyXErrText(QString const &)), Qt::QueuedConnection);
130                 connect(progress, SIGNAL(appendError(QString const &)),
131                         this, SLOT(appendText(QString const &)));
132                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
133                 progress->lyxerrConnect();
134         }
135 }
136
137
138 void GuiProgressView::debugMessageActivated(QTreeWidgetItem * item, int)
139 {
140         if (item == 0)
141                 return;
142
143         QString const no = qt_("No");
144         QString const yes = qt_("Yes");
145
146         bool selected = (item->text(1) == yes);
147         item->setText(1, selected ? no : yes);
148
149         levelChanged();
150 }
151
152
153 void GuiProgressView::levelChanged()
154 {
155         unsigned int level = Debug::NONE;
156         QTreeWidgetItemIterator it(widget_->debugMessagesTW);
157         while (*it) {
158                 if ((*it)->text(1) == qt_("Yes"))
159                         level |= (*it)->data(0, Qt::UserRole).toInt();
160                 ++it;
161         }
162         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
163 }
164
165
166 void GuiProgressView::debugSelectionChanged()
167 {
168         unsigned int level = Debug::NONE;
169         if (widget_->debugAnyRB->isChecked())
170                 level = Debug::ANY;
171         else if (widget_->debugSelectedRB->isChecked()) {
172                 widget_->debugMessagesTW->setEnabled(true);
173                 levelChanged();
174                 return;
175         }
176         QTreeWidgetItemIterator it(widget_->debugMessagesTW);
177         while (*it) {
178                 (*it)->setText(1, level == Debug::NONE ?
179                                 qt_("No") : qt_("Yes"));
180                 ++it;
181         }
182         widget_->debugMessagesTW->setEnabled(false);
183         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
184 }
185
186
187 void GuiProgressView::clearText()
188 {
189         if (widget_->autoClearCB->isChecked()){
190                 widget_->outTE->clear();
191                 eol_last_ = true;
192         }
193 }
194
195
196 void GuiProgressView::appendLyXErrText(QString const & text)
197 {
198         // Skip verbose messages meant for the terminal
199         if (text.startsWith("\nRunning:"))
200                 return;
201
202         widget_->outTE->moveCursor(QTextCursor::End);
203         widget_->outTE->insertPlainText(text);
204         widget_->outTE->ensureCursorVisible();
205         eol_last_ = false;
206         // Give the user a chance to disable debug messages because
207         // showing Debug::ANY messages completely blocks the GUI.
208         // Text is not always send as the whole line, so we must be
209         // careful about eolns.
210         // WARNING: processing events could cause crashes!
211         // TODO: find a better solution
212         if (text.endsWith("\n")) {
213                 eol_last_ = true;
214                 QApplication::processEvents();
215         }
216 }
217
218
219 void GuiProgressView::appendText(QString const & text)
220 {
221         if (text.isEmpty() || !widget_->sbarCB->isChecked())
222                 return;
223         QString str = GuiProgress::currentTime();
224         str += ": " + text;
225         if (!eol_last_)
226                 str = "\n" + str;
227         eol_last_ = text.endsWith("\n");
228
229         widget_->outTE->moveCursor(QTextCursor::End);
230         widget_->outTE->insertPlainText(str);
231         widget_->outTE->ensureCursorVisible();
232 }
233
234
235 void GuiProgressView::saveSession(QSettings & settings) const
236 {
237         Dialog::saveSession(settings);
238         settings.setValue(
239                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
240         settings.setValue(
241                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
242 }
243
244
245 void GuiProgressView::restoreSession()
246 {
247         DockView::restoreSession();
248         QSettings settings;
249         widget_->autoClearCB->setChecked(
250                 settings.value(sessionKey() + "/autoclear", true).toBool());
251         widget_->sbarCB->setChecked(
252                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
253 }
254
255
256 void GuiProgressView::showEvent(QShowEvent*)
257 {
258         ProgressInterface::instance()->lyxerrConnect();
259 }
260
261
262 void GuiProgressView::hideEvent(QHideEvent*)
263 {
264         ProgressInterface::instance()->lyxerrDisconnect();
265 }
266
267
268 Dialog * createGuiProgressView(GuiView & guiview)
269 {
270         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
271 }
272
273
274
275 } // namespace frontend
276 } // namespace lyx
277
278 #include "moc_GuiProgressView.cpp"