3 * \file GuiProgressView.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
16 #include "GuiProgressView.h"
18 #include "GuiApplication.h"
19 #include "GuiProgress.h"
20 #include "qt_helpers.h"
22 #include "FuncRequest.h"
24 #include "support/convert.h"
25 #include "support/debug.h"
35 using namespace lyx::support;
41 ProgressViewWidget::ProgressViewWidget()
47 GuiProgressView::~GuiProgressView()
54 typedef pair<int, QString> DebugMap;
55 typedef vector<DebugMap> DebugVector;
57 bool DebugSorter(DebugMap const & a, DebugMap const & b)
59 return a.second < b.second;
64 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area,
65 Qt::WindowFlags flags)
66 : DockView(parent, "progress", qt_("Progress/Debug Messages"), area, flags)
69 widget_ = new ProgressViewWidget;
70 widget_->setMinimumHeight(150);
71 widget_->debugMessagesTW->setSizePolicy(QSizePolicy::Ignored,
72 QSizePolicy::Expanding);
73 widget_->adjustSize();
76 widget_->outTE->setFont(guiApp->typewriterSystemFont());
77 widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
79 connect(widget_->debugNoneRB, SIGNAL(clicked()),
80 this, SLOT(debugSelectionChanged()));
81 connect(widget_->debugSelectedRB, SIGNAL(clicked()),
82 this, SLOT(debugSelectionChanged()));
83 connect(widget_->debugAnyRB, SIGNAL(clicked()),
84 this, SLOT(debugSelectionChanged()));
85 widget_->debugMessagesTW->setEnabled(false);
86 widget_->debugNoneRB->setChecked(true);
88 // ignore Debug::NONE and Debug::ANY
89 int const level_count = Debug::levelCount() - 1;
91 for (int i = 1 ; i < level_count; i++) {
92 Debug::Type const level = Debug::value(i);
94 toqstr(from_ascii(Debug::name(level) + " - "))
95 + qt_(Debug::description(level));
96 dmap.push_back(DebugMap(level, desc));
98 sort(dmap.begin(), dmap.end(), DebugSorter);
100 widget_->debugMessagesTW->setColumnCount(2);
101 widget_->debugMessagesTW->headerItem()->setText(0, qt_("Debug Level"));
102 widget_->debugMessagesTW->headerItem()->setText(1, qt_("Set"));
104 DebugVector::const_iterator dit = dmap.begin();
105 DebugVector::const_iterator const den = dmap.end();
106 for (; dit != den; ++dit) {
107 QTreeWidgetItem * 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"));
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)));
118 GuiProgress * progress =
119 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
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();
138 void GuiProgressView::debugMessageActivated(QTreeWidgetItem * item, int)
143 QString const no = qt_("No");
144 QString const yes = qt_("Yes");
146 bool selected = (item->text(1) == yes);
147 item->setText(1, selected ? no : yes);
153 void GuiProgressView::levelChanged()
155 unsigned int level = Debug::NONE;
156 QTreeWidgetItemIterator it(widget_->debugMessagesTW);
158 if ((*it)->text(1) == qt_("Yes"))
159 level |= (*it)->data(0, Qt::UserRole).toInt();
162 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
166 void GuiProgressView::debugSelectionChanged()
168 Debug::Type level = Debug::NONE;
169 if (widget_->debugAnyRB->isChecked())
171 else if (widget_->debugSelectedRB->isChecked()) {
172 widget_->debugMessagesTW->setEnabled(true);
176 QTreeWidgetItemIterator it(widget_->debugMessagesTW);
178 (*it)->setText(1, level == Debug::NONE ?
179 qt_("No") : qt_("Yes"));
182 widget_->debugMessagesTW->setEnabled(false);
183 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>((int)level)));
187 void GuiProgressView::clearText()
189 if (widget_->autoClearCB->isChecked()){
190 widget_->outTE->clear();
196 void GuiProgressView::appendLyXErrText(QString const & text)
198 // Skip verbose messages meant for the terminal
199 if (text.startsWith("\nRunning:"))
202 widget_->outTE->moveCursor(QTextCursor::End);
203 widget_->outTE->insertPlainText(text);
204 widget_->outTE->ensureCursorVisible();
206 // Give the user a chance to disable debug messages because
207 // showing Debug::ANY messages completely blocks the GUI.
208 // Text is not always send as the whole line, so we must be
209 // careful about eolns.
210 // WARNING: processing events could cause crashes!
211 // TODO: find a better solution
212 if (text.endsWith("\n")) {
214 QApplication::processEvents();
219 void GuiProgressView::appendText(QString const & text)
221 if (text.isEmpty() || !widget_->sbarCB->isChecked())
223 QString str = GuiProgress::currentTime();
227 eol_last_ = text.endsWith("\n");
229 widget_->outTE->moveCursor(QTextCursor::End);
230 widget_->outTE->insertPlainText(str);
231 widget_->outTE->ensureCursorVisible();
235 void GuiProgressView::saveSession(QSettings & settings) const
237 Dialog::saveSession(settings);
239 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
241 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
245 void GuiProgressView::restoreSession()
247 DockView::restoreSession();
249 widget_->autoClearCB->setChecked(
250 settings.value(sessionKey() + "/autoclear", true).toBool());
251 widget_->sbarCB->setChecked(
252 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
256 void GuiProgressView::showEvent(QShowEvent*)
258 ProgressInterface::instance()->lyxerrConnect();
262 void GuiProgressView::hideEvent(QHideEvent*)
264 ProgressInterface::instance()->lyxerrDisconnect();
268 } // namespace frontend
271 #include "moc_GuiProgressView.cpp"