]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
Use QFontMetrics information for underlines (and friends) width and position
[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 <QCheckBox>
28 #include <QDebug>
29 #include <QSettings>
30 #include <QTime>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36 namespace frontend {
37
38
39 ProgressViewWidget::ProgressViewWidget()
40 {
41         setupUi(this);
42 }
43
44
45 GuiProgressView::~GuiProgressView()
46 {
47         delete widget_;
48 }
49
50
51 namespace{
52 typedef pair<int, QString> DebugMap;
53 typedef vector<DebugMap> DebugVector;
54
55 bool DebugSorter(DebugMap const & a, DebugMap const & b)
56 {
57         return a.second < b.second;
58 }
59 }
60
61
62 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area, 
63         Qt::WindowFlags flags)
64         : DockView(parent, "progress", qt_("Progress/Debug Messages"), area, flags)
65 {
66         eol_last_ = true;
67         widget_ = new ProgressViewWidget;
68         widget_->setMinimumHeight(150);
69         widget_->debugMessagesTW->setSizePolicy(QSizePolicy::Ignored,
70                                                 QSizePolicy::Expanding);
71         widget_->adjustSize();
72         setWidget(widget_);
73
74         QFont font(guiApp->typewriterFontName());
75         font.setFixedPitch(true);
76         font.setStyleHint(QFont::TypeWriter);
77         widget_->outTE->setFont(font);
78         widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
79
80         connect(widget_->debugNoneRB, SIGNAL(clicked()),
81                 this, SLOT(debugSelectionChanged()));
82         connect(widget_->debugSelectedRB, SIGNAL(clicked()),
83                 this, SLOT(debugSelectionChanged()));
84         connect(widget_->debugAnyRB, SIGNAL(clicked()),
85                 this, SLOT(debugSelectionChanged()));
86         widget_->debugMessagesTW->setEnabled(false);
87         widget_->debugNoneRB->setChecked(true);
88
89         // ignore Debug::NONE and Debug::ANY
90         int const level_count = Debug::levelCount() - 1;
91         DebugVector dmap;
92         for (int i = 1 ; i < level_count; i++) {
93                 Debug::Type const level = Debug::value(i);
94                 QString const desc = qt_(Debug::description(level));
95                 dmap.push_back(DebugMap(level, desc));
96         }
97         sort(dmap.begin(), dmap.end(), DebugSorter);
98
99         QTreeWidgetItem * item = 0;
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                 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         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         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         widget_->outTE->moveCursor(QTextCursor::End);
199         widget_->outTE->insertPlainText(text);
200         widget_->outTE->ensureCursorVisible();
201         eol_last_ = false;
202         // Give the user a chance to disable debug messages because
203         // showing Debug::ANY messages completely blocks the GUI.
204         // Text is not always send as the whole line, so we must be
205         // careful about eolns.
206         // WARNING: processing events could cause crashes!
207         // TODO: find a better solution
208         if (text.endsWith("\n")) {
209                 eol_last_ = true;
210                 QApplication::processEvents();
211         }
212 }
213
214
215 void GuiProgressView::appendText(QString const & text)
216 {
217         if (text.isEmpty() || !widget_->sbarCB->isChecked())
218                 return;
219         QString str = GuiProgress::currentTime();
220         str += ": " + text;
221         if (!eol_last_)
222                 str = "\n" + str;
223         eol_last_ = text.endsWith("\n");
224
225         widget_->outTE->moveCursor(QTextCursor::End);
226         widget_->outTE->insertPlainText(str);
227         widget_->outTE->ensureCursorVisible();
228 }
229
230
231 void GuiProgressView::saveSession() const
232 {
233         Dialog::saveSession();
234         QSettings settings;
235         settings.setValue(
236                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
237         settings.setValue(
238                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
239 }
240
241
242 void GuiProgressView::restoreSession()
243 {
244         DockView::restoreSession();
245         QSettings settings;
246         widget_->autoClearCB->setChecked(
247                 settings.value(sessionKey() + "/autoclear", true).toBool());
248         widget_->sbarCB->setChecked(
249                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
250 }
251
252
253 void GuiProgressView::showEvent(QShowEvent*)
254 {
255         ProgressInterface::instance()->lyxerrConnect();
256 }
257
258
259 void GuiProgressView::hideEvent(QHideEvent*)
260 {
261         ProgressInterface::instance()->lyxerrDisconnect();
262 }
263
264
265 Dialog * createGuiProgressView(GuiView & guiview)
266 {
267         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
268 }
269
270
271
272 } // namespace frontend
273 } // namespace lyx
274
275 #include "moc_GuiProgressView.cpp"