Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / shell / statusbar.cpp
blob5d56ff2a2fb15ba579b52d4dcd5067159f4a9342
1 /* This file is part of the KDE project
2 Copyright 2007 Hamish Rodda <rodda@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "statusbar.h"
22 #include <QTimer>
23 #include <QProgressBar>
24 #include <QLabel>
25 #include <QSignalMapper>
27 #include <KColorScheme>
28 #include <KDebug>
30 #include <interfaces/istatus.h>
31 #include <interfaces/ilanguagecontroller.h>
32 #include <language/backgroundparser/backgroundparser.h>
34 #include <sublime/view.h>
36 #include "plugincontroller.h"
37 #include "core.h"
39 namespace KDevelop
42 StatusBar::StatusBar(QWidget* parent)
43 : KStatusBar(parent)
44 , m_timer(new QTimer(this))
45 , m_currentView(0)
46 , m_errorRemovalMapper(new QSignalMapper(this))
48 m_timer->setSingleShot(true);
49 connect(m_timer, SIGNAL(timeout()), SLOT(slotTimeout()));
50 connect(Core::self()->pluginController(), SIGNAL(pluginLoaded(KDevelop::IPlugin*)), SLOT(pluginLoaded(KDevelop::IPlugin*)));
52 foreach (IPlugin* plugin, Core::self()->pluginControllerInternal()->allPluginsForExtension("IStatus", QStringList()))
53 registerStatus(plugin);
55 registerStatus(Core::self()->languageController()->backgroundParser());
57 insertPermanentItem(i18n("No View Selected"), 0);
58 connect(m_errorRemovalMapper, SIGNAL(mapped(QWidget*)), SLOT(removeError(QWidget*)));
61 void StatusBar::removeError(QWidget* w)
63 removeWidget(w);
64 w->deleteLater();
67 void StatusBar::viewChanged(Sublime::View* view)
69 if (m_currentView)
70 m_currentView->disconnect(this);
72 m_currentView = view;
74 if (view) {
75 connect(view, SIGNAL(statusChanged(Sublime::View*)), this, SLOT(viewStatusChanged(Sublime::View*)));
76 changeItem(view->viewStatus(), 0);
78 } else {
79 changeItem(i18n("No View Selected"), 0);
83 void StatusBar::viewStatusChanged(Sublime::View* view)
85 changeItem(view->viewStatus(), 0);
88 void StatusBar::pluginLoaded(IPlugin* plugin)
90 if (qobject_cast<IStatus*>(plugin))
91 registerStatus(plugin);
94 void StatusBar::registerStatus(QObject* status)
96 Q_ASSERT(qobject_cast<IStatus*>(status));
97 connect(status, SIGNAL(clearMessage()), SLOT(clearMessage()));
98 connect(status, SIGNAL(showMessage(const QString&, int)), SLOT(showMessage(const QString&, int)));
99 connect(status, SIGNAL(hideProgress()), SLOT(hideProgress()));
100 connect(status, SIGNAL(showProgress(int, int, int)), SLOT(showProgress(int, int, int)));
101 connect(status, SIGNAL(showErrorMessage(const QString&, int)), SLOT(showErrorMessage(const QString&, int)));
104 QWidget* errorMessage(QWidget* parent, const QString& text)
106 QLabel* label = new QLabel(parent);
107 KStatefulBrush red(KColorScheme::Window, KColorScheme::NegativeText);
108 QPalette pal = label->palette();
109 pal.setBrush(QPalette::WindowText, red.brush(label));
110 label->setPalette(pal);
111 label->setAlignment(Qt::AlignRight);
112 label->setText(text);
113 return label;
116 QTimer* StatusBar::errorTimeout(QWidget* error, int timeout)
118 QTimer* timer = new QTimer(error);
119 timer->setSingleShot(true);
120 timer->setInterval(1000*timeout);
121 m_errorRemovalMapper->setMapping(timer, error);
122 connect(timer, SIGNAL(timeout()), m_errorRemovalMapper, SLOT(map()));
123 return timer;
126 void StatusBar::showErrorMessage(const QString& message, int timeout)
128 QWidget* error = errorMessage(this, message);
129 QTimer* timer = errorTimeout(error, timeout);
130 addWidget(error);
131 timer->start(); // triggers removeError()
134 void StatusBar::slotTimeout()
136 QMutableMapIterator<IStatus*, Message> it = m_messages;
138 while (it.hasNext()) {
139 it.next();
140 if (it.value().timeout) {
141 it.value().timeout -= m_timer->interval();
142 if (it.value().timeout == 0)
143 it.remove();
147 updateMessage();
150 void StatusBar::updateMessage()
152 if (m_timer->isActive()) {
153 m_timer->stop();
154 m_timer->setInterval(m_time.elapsed());
155 slotTimeout();
158 QString ret;
159 int timeout = 0;
161 foreach (const Message& m, m_messages) {
162 if (!ret.isEmpty())
163 ret += "; ";
165 ret += m.text;
167 if (timeout)
168 timeout = qMin(timeout, m.timeout);
169 else
170 timeout = m.timeout;
173 if (!ret.isEmpty())
174 KStatusBar::showMessage(ret);
175 else
176 KStatusBar::clearMessage();
178 if (timeout) {
179 m_time.start();
180 m_timer->start(timeout);
184 void StatusBar::clearMessage()
186 IStatus* status = qobject_cast<IStatus*>(sender());
188 if (m_messages.contains(status)) {
189 m_messages.remove(status);
190 updateMessage();
194 void StatusBar::showMessage(const QString & message, int timeout)
196 IStatus* status = qobject_cast<IStatus*>(sender());
198 Message m;
199 m.text = message;
200 m.timeout = timeout;
202 m_messages.insert(status, m);
204 updateMessage();
207 void StatusBar::hideProgress()
209 IStatus* status = qobject_cast<IStatus*>(sender());
211 if (m_progressBars.contains(status)) {
212 delete m_progressBars[status];
213 m_progressBars.remove(status);
217 void StatusBar::showProgress(int minimum, int maximum, int value)
219 IStatus* status = qobject_cast<IStatus*>(sender());
221 QProgressBar* bar;
223 if (m_progressBars.contains(status)) {
224 bar = m_progressBars[status];
225 if (bar->minimum() != minimum)
226 bar->setMinimum(minimum);
227 if (bar->maximum() != maximum)
228 bar->setMaximum(maximum);
229 if (bar->value() != value)
230 bar->setValue(value);
232 } else {
233 bar = new QProgressBar(this);
234 bar->setRange(minimum, maximum);
235 bar->setValue(value);
236 bar->setMaximumWidth(300);
237 bar->setMaximumHeight((height()*2)/3);
238 m_progressBars.insert(status, bar);
240 addPermanentWidget(bar);
246 #include "statusbar.moc"