2
* Copyright (C) 2007 Petri Damsten <damu@iki.fi>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License version 2 as
6
* published by the Free Software Foundation
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details
13
* You should have received a copy of the GNU Library General Public
14
* License along with this program; if not, write to the
15
* Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include "system-monitor.h"
20
#include "monitorbutton.h"
22
#include <QGraphicsLinearLayout>
24
#include <KPushButton>
25
#include <Plasma/Containment>
26
#include <Plasma/Corona>
27
#include <Plasma/ToolTipManager>
29
SystemMonitor::SystemMonitor(QObject *parent, const QVariantList &args)
30
: Plasma::PopupApplet(parent, args), m_layout(0), m_buttons(0), m_widget(0)
32
setAspectRatioMode(Plasma::IgnoreAspectRatio);
35
SystemMonitor::~SystemMonitor()
39
void SystemMonitor::saveState(KConfigGroup &group) const
41
QStringList appletNames;
42
foreach (SM::Applet *applet, m_applets) {
43
applet->saveConfig(group);
44
appletNames << applet->objectName();
47
group.writeEntry("applets", appletNames);
50
void SystemMonitor::createConfigurationInterface(KConfigDialog *parent)
52
foreach (Plasma::Applet *applet, m_applets) {
53
applet->createConfigurationInterface(parent);
57
void SystemMonitor::init()
59
KConfigGroup cg = config();
60
QStringList appletNames = cg.readEntry("applets", QStringList());
62
m_widget = new QGraphicsWidget(this);
63
m_layout = new QGraphicsLinearLayout(Qt::Vertical);
64
m_layout->setContentsMargins(0, 0, 0, 0);
65
m_buttons = new QGraphicsLinearLayout(Qt::Horizontal);
66
m_buttons->setContentsMargins(0, 0, 0, 0);
67
m_buttons->setSpacing(5);
69
QMap<QString, KPluginInfo> appletsFound;
70
KPluginInfo::List appletList = listAppletInfo("System Information");
71
foreach (const KPluginInfo &pluginInfo, appletList) {
72
if (pluginInfo.pluginName().startsWith("sm_") && !pluginInfo.isHidden()) {
73
appletsFound.insert(pluginInfo.pluginName(), pluginInfo);
77
foreach (const KPluginInfo &pluginInfo, appletsFound) {
78
MonitorButton *button = new MonitorButton(m_widget);
79
button->setObjectName(pluginInfo.pluginName());
80
Plasma::ToolTipContent data;
81
data.setMainText(pluginInfo.name());
82
data.setImage(KIcon(pluginInfo.icon()).pixmap(IconSize(KIconLoader::Desktop)));
83
Plasma::ToolTipManager::self()->setContent(button, data);
84
button->setCheckable(true);
85
button->setImage(pluginInfo.icon());
86
if (appletNames.contains(pluginInfo.pluginName())) {
87
button->setChecked(true);
89
connect(button, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
90
m_buttons->addItem(button);
91
m_monitorButtons << button;
93
KGlobal::locale()->insertCatalog(pluginInfo.pluginName());
96
m_layout->addItem(m_buttons);
97
foreach (const QString& appletName, appletNames) {
98
if (appletsFound.contains(appletName)) {
99
Applet * applet = addApplet(appletName);
102
Plasma::Constraints constraints(Plasma::ImmutableConstraint |
103
Plasma::StartupCompletedConstraint);
104
applet->updateConstraints(constraints);
105
applet->flushPendingConstraintsEvents();
110
m_widget->setLayout(m_layout);
113
setPopupIcon("utilities-system-monitor");
116
void SystemMonitor::toggled(bool toggled)
118
removeApplet(sender()->objectName());
121
SM::Applet * applet = addApplet(sender()->objectName());
124
Plasma::Constraints constraints(Plasma::ImmutableConstraint |
125
Plasma::StartupCompletedConstraint);
126
applet->updateConstraints(constraints);
127
applet->flushPendingConstraintsEvents();
132
SM::Applet *SystemMonitor::addApplet(const QString &name)
134
if (name.isEmpty()) {
138
Plasma::Applet* plasmaApplet = Plasma::Applet::load(name, 0, QVariantList() << "SM");
139
SM::Applet* applet = qobject_cast<SM::Applet*>(plasmaApplet);
141
applet->setParentItem(m_widget);
142
m_applets.append(applet);
143
connect(applet, SIGNAL(geometryChecked()), this, SLOT(checkGeometry()));
144
connect(applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletRemoved(QObject*)));
145
applet->setFlag(QGraphicsItem::ItemIsMovable, false);
146
applet->setBackgroundHints(Plasma::Applet::NoBackground);
147
applet->setObjectName(name);
148
connect(applet, SIGNAL(configNeedsSaving()), this, SIGNAL(configNeedsSaving()));
149
m_layout->addItem(applet);
152
KConfigGroup cg = config();
154
emit configNeedsSaving();
155
} else if (plasmaApplet) {
162
void SystemMonitor::removeApplet(const QString &name)
164
foreach (SM::Applet *applet, m_applets) {
165
if (applet->objectName() == name) {
171
void SystemMonitor::appletRemoved(QObject *object)
173
SM::Applet *applet = static_cast<SM::Applet*>(object);
175
foreach (SM::Applet *a, m_applets) {
177
m_layout->removeItem(applet);
178
m_applets.removeAll(applet);
181
KConfigGroup cg = config();
183
emit configNeedsSaving();
187
// sanity check the buttons
188
QSet<QString> running;
189
foreach (SM::Applet *a, m_applets) {
190
running << a->objectName();
193
foreach (MonitorButton* button, m_monitorButtons) {
194
if (!running.contains(button->objectName())) {
195
kDebug() << "unchecking" << button->objectName();
196
button->setChecked(false);
201
void SystemMonitor::checkGeometry()
203
QSizeF margins = size() - contentsRect().size();
204
qreal minHeight = m_buttons->minimumHeight();
205
//kDebug() << minHeight;
207
foreach (SM::Applet *applet, m_applets) {
208
//kDebug() << applet->minSize() << applet->minimumSize()
209
// << applet->metaObject()->className() << applet->size() - applet->contentsRect().size();
210
minHeight += applet->preferredSize().height() + m_layout->spacing();
216
kDebug() << m_widget->size().height() << m_layout->geometry().height();
217
foreach (SM::Applet *applet, m_applets) {
218
kDebug() << applet->metaObject()->className() << applet->size().height();
220
for (int i = 0; i < m_layout->count(); ++i) {
221
kDebug() << m_layout->itemAt(i)->geometry().top() << m_layout->itemAt(i)->geometry().height();
226
QGraphicsWidget *SystemMonitor::graphicsWidget()
231
void SystemMonitor::constraintsEvent(Plasma::Constraints constraints)
233
Plasma::Constraints passOn = Plasma::NoConstraint;
235
if (constraints & Plasma::ImmutableConstraint) {
236
foreach (MonitorButton* button, m_monitorButtons) {
237
button->setEnabled(immutability() == Plasma::Mutable);
240
passOn |= Plasma::ImmutableConstraint;
243
if (constraints & Plasma::StartupCompletedConstraint) {
244
passOn |= Plasma::StartupCompletedConstraint;
247
if (passOn != Plasma::NoConstraint) {
248
foreach (Plasma::Applet *applet, m_applets) {
249
applet->updateConstraints(passOn);
250
if (passOn & Plasma::StartupCompletedConstraint) {
251
applet->flushPendingConstraintsEvents();
256
PopupApplet::constraintsEvent(constraints);
259
#include "system-monitor.moc"