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