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