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