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"
23 #include <QProgressBar>
25 #include <QSignalMapper>
27 #include <KColorScheme>
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"
42 StatusBar::StatusBar(QWidget
* parent
)
44 , m_timer(new QTimer(this))
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
)
67 void StatusBar::viewChanged(Sublime::View
* view
)
70 m_currentView
->disconnect(this);
75 connect(view
, SIGNAL(statusChanged(Sublime::View
*)), this, SLOT(viewStatusChanged(Sublime::View
*)));
76 changeItem(view
->viewStatus(), 0);
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
);
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()));
126 void StatusBar::showErrorMessage(const QString
& message
, int timeout
)
128 QWidget
* error
= errorMessage(this, message
);
129 QTimer
* timer
= errorTimeout(error
, timeout
);
131 timer
->start(); // triggers removeError()
134 void StatusBar::slotTimeout()
136 QMutableMapIterator
<IStatus
*, Message
> it
= m_messages
;
138 while (it
.hasNext()) {
140 if (it
.value().timeout
) {
141 it
.value().timeout
-= m_timer
->interval();
142 if (it
.value().timeout
== 0)
150 void StatusBar::updateMessage()
152 if (m_timer
->isActive()) {
154 m_timer
->setInterval(m_time
.elapsed());
161 foreach (const Message
& m
, m_messages
) {
168 timeout
= qMin(timeout
, m
.timeout
);
174 KStatusBar::showMessage(ret
);
176 KStatusBar::clearMessage();
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
);
194 void StatusBar::showMessage(const QString
& message
, int timeout
)
196 IStatus
* status
= qobject_cast
<IStatus
*>(sender());
202 m_messages
.insert(status
, m
);
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());
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
);
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"