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"
33 using namespace lyx::support;
39 ProgressViewWidget::ProgressViewWidget()
45 GuiProgressView::~GuiProgressView()
52 typedef pair<int, QString> DebugMap;
53 typedef vector<DebugMap> DebugVector;
55 bool DebugSorter(DebugMap const & a, DebugMap const & b)
57 return a.second < b.second;
62 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area,
63 Qt::WindowFlags flags)
64 : DockView(parent, "progress", qt_("Progress/Debug Messages"), area, flags)
67 widget_ = new ProgressViewWidget;
68 widget_->setMinimumHeight(150);
69 widget_->debugMessagesTW->setSizePolicy(QSizePolicy::Ignored,
70 QSizePolicy::Expanding);
71 widget_->adjustSize();
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);
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);
89 // ignore Debug::NONE and Debug::ANY
90 int const level_count = Debug::levelCount() - 1;
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));
97 sort(dmap.begin(), dmap.end(), DebugSorter);
99 widget_->debugMessagesTW->setColumnCount(2);
100 widget_->debugMessagesTW->headerItem()->setText(0, qt_("Debug Level"));
101 widget_->debugMessagesTW->headerItem()->setText(1, qt_("Set"));
103 DebugVector::const_iterator dit = dmap.begin();
104 DebugVector::const_iterator const den = dmap.end();
105 for (; dit != den; ++dit) {
106 QTreeWidgetItem * item = new QTreeWidgetItem(widget_->debugMessagesTW);
107 item->setText(0, dit->second);
108 item->setData(0, Qt::UserRole, int(dit->first));
109 item->setText(1, qt_("No"));
111 widget_->debugMessagesTW->resizeColumnToContents(0);
112 widget_->debugMessagesTW->resizeColumnToContents(1);
113 connect(widget_->debugMessagesTW,
114 SIGNAL(itemActivated(QTreeWidgetItem *, int)),
115 this, SLOT(debugMessageActivated(QTreeWidgetItem *, int)));
117 GuiProgress * progress =
118 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
121 connect(progress, SIGNAL(processStarted(QString const &)),
122 this, SLOT(appendText(QString const &)));
123 //connect(progress, SIGNAL(processFinished(QString const &)),
124 // this, SLOT(appendText(QString const &)));
125 connect(progress, SIGNAL(appendMessage(QString const &)),
126 this, SLOT(appendText(QString const &)));
127 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
128 this, SLOT(appendLyXErrText(QString const &)), Qt::QueuedConnection);
129 connect(progress, SIGNAL(appendError(QString const &)),
130 this, SLOT(appendText(QString const &)));
131 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
132 progress->lyxerrConnect();
137 void GuiProgressView::debugMessageActivated(QTreeWidgetItem * item, int)
142 QString const no = qt_("No");
143 QString const yes = qt_("Yes");
145 bool selected = (item->text(1) == yes);
146 item->setText(1, selected ? no : yes);
152 void GuiProgressView::levelChanged()
154 int level = Debug::NONE;
155 QTreeWidgetItemIterator it(widget_->debugMessagesTW);
157 if ((*it)->text(1) == qt_("Yes"))
158 level |= (*it)->data(0, Qt::UserRole).toInt();
161 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
165 void GuiProgressView::debugSelectionChanged()
167 int level = Debug::NONE;
168 if (widget_->debugAnyRB->isChecked())
170 else if (widget_->debugSelectedRB->isChecked()) {
171 widget_->debugMessagesTW->setEnabled(true);
175 QTreeWidgetItemIterator it(widget_->debugMessagesTW);
177 (*it)->setText(1, level == Debug::NONE ?
178 qt_("No") : qt_("Yes"));
181 widget_->debugMessagesTW->setEnabled(false);
182 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
186 void GuiProgressView::clearText()
188 if (widget_->autoClearCB->isChecked()){
189 widget_->outTE->clear();
195 void GuiProgressView::appendLyXErrText(QString const & text)
197 widget_->outTE->moveCursor(QTextCursor::End);
198 widget_->outTE->insertPlainText(text);
199 widget_->outTE->ensureCursorVisible();
201 // Give the user a chance to disable debug messages because
202 // showing Debug::ANY messages completely blocks the GUI.
203 // Text is not always send as the whole line, so we must be
204 // careful about eolns.
205 // WARNING: processing events could cause crashes!
206 // TODO: find a better solution
207 if (text.endsWith("\n")) {
209 QApplication::processEvents();
214 void GuiProgressView::appendText(QString const & text)
216 if (text.isEmpty() || !widget_->sbarCB->isChecked())
218 QString str = GuiProgress::currentTime();
222 eol_last_ = text.endsWith("\n");
224 widget_->outTE->moveCursor(QTextCursor::End);
225 widget_->outTE->insertPlainText(str);
226 widget_->outTE->ensureCursorVisible();
230 void GuiProgressView::saveSession() const
232 Dialog::saveSession();
235 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
237 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
241 void GuiProgressView::restoreSession()
243 DockView::restoreSession();
245 widget_->autoClearCB->setChecked(
246 settings.value(sessionKey() + "/autoclear", true).toBool());
247 widget_->sbarCB->setChecked(
248 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
252 void GuiProgressView::showEvent(QShowEvent*)
254 ProgressInterface::instance()->lyxerrConnect();
258 void GuiProgressView::hideEvent(QHideEvent*)
260 ProgressInterface::instance()->lyxerrDisconnect();
264 Dialog * createGuiProgressView(GuiView & guiview)
266 return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
271 } // namespace frontend
274 #include "moc_GuiProgressView.cpp"