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