]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiProgressView.cpp
Really fix F&R ui for older Qt versions.
[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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiProgressView.h"
16 #include "GuiApplication.h"
17
18 #include "qt_helpers.h"
19 #include "FuncRequest.h"
20
21 #include "support/debug.h"
22 #include "support/convert.h"
23
24 #include <QSettings>
25 #include <QTime>
26
27 #include <QCheckBox>
28 #include <QDebug>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 struct LevelButton : QCheckBox
38 {
39         LevelButton(const QString& name) : QCheckBox(name) {}
40         Debug::Type level;
41         
42         void setCheckStatusSilent(Qt::CheckState state) {
43                 blockSignals(true);
44                 setCheckState(state);
45                 blockSignals(false);    
46         }
47 };
48
49
50 ProgressViewWidget::ProgressViewWidget()
51 {
52         setupUi(this);
53
54 }
55
56
57 GuiProgressView::~GuiProgressView()
58 {
59         delete widget_;
60 }
61
62
63 GuiProgressView::GuiProgressView(GuiView & parent, Qt::DockWidgetArea area, 
64         Qt::WindowFlags flags) : DockView(parent, "progress", "Debug/Progress window", area, flags)
65 {
66         widget_ = new ProgressViewWidget();
67 #if QT_VERSION < 0x040300
68         widget_->scrollArea->setWidget(widget_->scrollAreaWidgetContents);
69 #endif
70         setWidget(widget_);
71
72         QFont font(guiApp->typewriterFontName());
73         font.setKerning(false);
74         font.setFixedPitch(true);
75         font.setStyleHint(QFont::TypeWriter);
76         widget_->outTE->setFont(font);
77         widget_->tabWidget->widget(0)->setContentsMargins(-5, -7, 0, -7);
78
79         toggle_button = new LevelButton("Toggle ALL messages");
80         toggle_button->level = Debug::ANY;
81         toggle_button->setTristate(true);
82         toggle_button->setCheckState(Qt::PartiallyChecked);
83         widget_->settingsLayout->addWidget(toggle_button);
84         connect(toggle_button, SIGNAL(stateChanged(int)), this, SLOT(tristateChanged(int)));
85
86         // ignore Debug::NONE and Debug::ANY
87         int const level_count = Debug::levelCount() - 1;
88         for (int i = 1 ; i < level_count; i++) {
89                 Debug::Type const level = Debug::value(i);
90                 LevelButton * box = new LevelButton(toqstr(Debug::description(level)));
91                 box->level = level;
92                 widget_->settingsLayout->addWidget(box, (i + 3) % 10, (i + 3) / 10);
93                 box->setChecked(false);
94                 level_buttons << box;
95                 connect(box, SIGNAL(stateChanged(int)), this, SLOT(levelChanged()));
96         }
97         widget_->settingsLayout->activate();
98         
99         GuiProgress * progress =
100                 dynamic_cast<GuiProgress *>(ProgressInterface::instance());
101
102         if (progress) {
103                 connect(progress, SIGNAL(processStarted(QString const &)),
104                         this, SLOT(appendText(QString const &)));
105                 //connect(progress, SIGNAL(processFinished(QString const &)),
106                 //      this, SLOT(appendText(QString const &)));
107                 connect(progress, SIGNAL(appendMessage(QString const &)),
108                         this, SLOT(appendText(QString const &)));
109                 connect(progress, SIGNAL(appendLyXErrMessage(QString const &)),
110                         this, SLOT(appendLyXErrText(QString const &)));
111                 connect(progress, SIGNAL(appendError(QString const &)),
112                         this, SLOT(appendText(QString const &)));
113                 connect(progress, SIGNAL(clearMessages()), this, SLOT(clearText()));
114                 progress->lyxerrConnect();
115         }
116 }
117
118
119 void GuiProgressView::levelChanged()
120 {
121         int level = Debug::NONE;
122         checked_buttons.clear();
123         Q_FOREACH(LevelButton* button, level_buttons) {
124                 if (button->isChecked()) {
125                         level |= button->level;
126                         checked_buttons << button;
127                 }
128         }
129         dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
130         
131         toggle_button->setCheckStatusSilent (Qt::PartiallyChecked);
132 }
133
134
135 void GuiProgressView::tristateChanged(int state)
136 {
137         if (state != Qt::PartiallyChecked) {
138                 Q_FOREACH(LevelButton* button, level_buttons) {
139                         button->setCheckStatusSilent(toggle_button->checkState());
140                 }
141                 int level = (state == Qt::Checked ? Debug::ANY : Debug::NONE);
142                 dispatch(FuncRequest(LFUN_DEBUG_LEVEL_SET, convert<string>(level)));
143         } else {
144                 Q_FOREACH(LevelButton* button, checked_buttons) {
145                         button->setCheckStatusSilent(Qt::Checked);
146                 }
147                 levelChanged();
148         }
149 }
150
151
152 void GuiProgressView::clearText()
153 {
154         if (widget_->autoClearCB->isChecked())
155                 widget_->outTE->clear();
156 }
157
158
159 void GuiProgressView::appendLyXErrText(QString const & text)
160 {
161         widget_->outTE->insertPlainText(text);
162         widget_->outTE->ensureCursorVisible();
163
164         // Give the user a chance to disable debug messages because
165         // showing Debug::ANY messages completely blocks the GUI.
166         // Text is not always send as the whole line, so we must be
167         // careful about eolns.
168         if (text.endsWith("\n"))
169                 QApplication::processEvents();
170 }
171
172
173 void GuiProgressView::appendText(QString const & text)
174 {
175         if (text.isEmpty() || !widget_->sbarCB->isChecked())
176                 return;
177         QString str = QTime::currentTime().toString();
178         str += ": " + text;
179         if (!text.endsWith("\n"))
180                 str += "\n";
181
182         widget_->outTE->insertPlainText(str);
183         widget_->outTE->ensureCursorVisible();
184 }
185
186
187 void GuiProgressView::saveSession() const
188 {
189         Dialog::saveSession();
190         QSettings settings;
191         settings.setValue(
192                 sessionKey() + "/autoclear", widget_->autoClearCB->isChecked());
193         settings.setValue(
194                 sessionKey() + "/statusbarmsgs", widget_->sbarCB->isChecked());
195 }
196
197
198 void GuiProgressView::restoreSession()
199 {
200         DockView::restoreSession();
201         QSettings settings;
202         widget_->autoClearCB->setChecked(
203                 settings.value(sessionKey() + "/autoclear", true).toBool());
204         widget_->sbarCB->setChecked(
205                 settings.value(sessionKey() + "/statusbarmsgs", true).toBool());
206 }
207
208
209 void GuiProgressView::showEvent(QShowEvent*)
210 {
211         ProgressInterface::instance()->lyxerrConnect();
212 }
213
214
215 void GuiProgressView::hideEvent(QHideEvent*)
216 {
217         ProgressInterface::instance()->lyxerrDisconnect();
218 }
219
220
221
222
223 Dialog * createGuiProgressView(GuiView & guiview)
224 {
225 #ifdef Q_WS_MACX
226         return new GuiProgressView(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
227 #else
228         return new GuiProgressView(guiview, Qt::BottomDockWidgetArea);
229 #endif
230 }
231
232
233
234 } // namespace frontend
235 } // namespace lyx
236
237 #include "moc_GuiProgressView.cpp"