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 widget_->outTE->setFont(guiApp->typewriterSystemFont());
75 widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
77 connect(widget_->debugNoneRB, SIGNAL(clicked()),
78 this, SLOT(debugSelectionChanged()));
79 connect(widget_->debugSelectedRB, SIGNAL(clicked()),
80 this, SLOT(debugSelectionChanged()));
81 connect(widget_->debugAnyRB, SIGNAL(clicked()),
82 this, SLOT(debugSelectionChanged()));
83 widget_->debugMessagesTW->setEnabled(false);
84 widget_->debugNoneRB->setChecked(true);
86 // ignore Debug::NONE and Debug::ANY
87 int const level_count = Debug::levelCount() - 1;
89 for (int i = 1 ; i < level_count; i++) {
90 Debug::Type const level = Debug::value(i);
92 toqstr(from_ascii(Debug::name(level) + " - "))
93 + qt_(Debug::description(level));
94 dmap.push_back(DebugMap(level, desc));
96 sort(dmap.begin(), dmap.end(), DebugSorter);
98 widget_->debugMessagesTW->setColumnCount(2);
99 widget_->debugMessagesTW->headerItem()->setText(0, qt_("Debug Level"));
100 widget_->debugMessagesTW->headerItem()->setText(1, qt_("Set"));
102 DebugVector::const_iterator dit = dmap.begin();
103 DebugVector::const_iterator const den = dmap.end();
104 for (; dit != den; ++dit) {
105 QTreeWidgetItem * item = new QTreeWidgetItem(widget_->debugMessagesTW);
106 item->setText(0, dit->second);
107 item->setData(0, Qt::UserRole, int(dit->first));
108 item->setText(1, qt_("No"));
110 widget_->debugMessagesTW->resizeColumnToContents(0);
111 widget_->debugMessagesTW->resizeColumnToContents(1);
112 connect(widget_->debugMessagesTW,
113 SIGNAL(itemActivated(QTreeWidgetItem *, int)),
114 this, SLOT(debugMessageActivated(QTreeWidgetItem *, int)));
116 GuiProgress * progress =
117 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
120 connect(progress, SIGNAL(processStarted(QString const &)),
121 this, SLOT(appendText(QString const &)));
122 //connect(progress, SIGNAL(processFinished(QString const &)),
123 // this, SLOT(appendText(QString const &)));
124 connect(progress, SIGNAL(appendMessage(QString const &)),
125 this, SLOT(appendText(QString const &)));
126 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
127 this, SLOT(appendLyXErrText(QString const &)), Qt::QueuedConnection);
128 connect(progress, SIGNAL(appendError(QString const &)),
129 this, SLOT(appendText(QString const &)));
130 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
131 progress->lyxerrConnect();
136 void GuiProgressView::debugMessageActivated(QTreeWidgetItem * item, int)
141 QString const no = qt_("No");
142 QString const yes = qt_("Yes");
144 bool selected = (item->text(1) == yes);
145 item->setText(1, selected ? no : yes);
151 void GuiProgressView::levelChanged()
153 int level = Debug::NONE;
154 QTreeWidgetItemIterator it(widget_->debugMessagesTW);
156 if ((*it)->text(1) == qt_("Yes"))
157 level |= (*it)->data(0, Qt::UserRole).toInt();
160 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
164 void GuiProgressView::debugSelectionChanged()
166 int level = Debug::NONE;
167 if (widget_->debugAnyRB->isChecked())
169 else if (widget_->debugSelectedRB->isChecked()) {
170 widget_->debugMessagesTW->setEnabled(true);
174 QTreeWidgetItemIterator it(widget_->debugMessagesTW);
176 (*it)->setText(1, level == Debug::NONE ?
177 qt_("No") : qt_("Yes"));
180 widget_->debugMessagesTW->setEnabled(false);
181 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
185 void GuiProgressView::clearText()
187 if (widget_->autoClearCB->isChecked()){
188 widget_->outTE->clear();
194 void GuiProgressView::appendLyXErrText(QString const & text)
196 widget_->outTE->moveCursor(QTextCursor::End);
197 widget_->outTE->insertPlainText(text);
198 widget_->outTE->ensureCursorVisible();
200 // Give the user a chance to disable debug messages because
201 // showing Debug::ANY messages completely blocks the GUI.
202 // Text is not always send as the whole line, so we must be
203 // careful about eolns.
204 // WARNING: processing events could cause crashes!
205 // TODO: find a better solution
206 if (text.endsWith("\n")) {
208 QApplication::processEvents();
213 void GuiProgressView::appendText(QString const & text)
215 if (text.isEmpty() || !widget_->sbarCB->isChecked())
217 QString str = GuiProgress::currentTime();
221 eol_last_ = text.endsWith("\n");
223 widget_->outTE->moveCursor(QTextCursor::End);
224 widget_->outTE->insertPlainText(str);
225 widget_->outTE->ensureCursorVisible();
229 void GuiProgressView::saveSession() const
231 Dialog::saveSession();
234 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
236 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
240 void GuiProgressView::restoreSession()
242 DockView::restoreSession();
244 widget_->autoClearCB->setChecked(
245 settings.value(sessionKey() + "/autoclear", true).toBool());
246 widget_->sbarCB->setChecked(
247 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
251 void GuiProgressView::showEvent(QShowEvent*)
253 ProgressInterface::instance()->lyxerrConnect();
257 void GuiProgressView::hideEvent(QHideEvent*)
259 ProgressInterface::instance()->lyxerrDisconnect();
263 Dialog * createGuiProgressView(GuiView & guiview)
265 return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
270 } // namespace frontend
273 #include "moc_GuiProgressView.cpp"