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