~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/system-monitor/system-monitor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Petri Damsten <damu@iki.fi>
 
3
 *
 
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
 
7
 *
 
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
 
12
 *
 
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.
 
17
 */
 
18
 
 
19
#include "system-monitor.h"
 
20
#include "monitorbutton.h"
 
21
#include "applet.h"
 
22
#include <QGraphicsLinearLayout>
 
23
#include <KDebug>
 
24
#include <KPushButton>
 
25
#include <Plasma/Containment>
 
26
#include <Plasma/Corona>
 
27
#include <Plasma/ToolTipManager>
 
28
 
 
29
SystemMonitor::SystemMonitor(QObject *parent, const QVariantList &args)
 
30
    : Plasma::PopupApplet(parent, args), m_layout(0), m_buttons(0), m_widget(0)
 
31
{
 
32
    setAspectRatioMode(Plasma::IgnoreAspectRatio);
 
33
}
 
34
 
 
35
SystemMonitor::~SystemMonitor()
 
36
{
 
37
}
 
38
 
 
39
void SystemMonitor::saveState(KConfigGroup &group) const
 
40
{
 
41
    QStringList appletNames;
 
42
    foreach (SM::Applet *applet, m_applets) {
 
43
        applet->saveConfig(group);
 
44
        appletNames << applet->objectName();
 
45
    }
 
46
 
 
47
    group.writeEntry("applets", appletNames);
 
48
}
 
49
 
 
50
void SystemMonitor::createConfigurationInterface(KConfigDialog *parent)
 
51
{
 
52
    foreach (Plasma::Applet *applet, m_applets) {
 
53
        applet->createConfigurationInterface(parent);
 
54
    }
 
55
}
 
56
 
 
57
void SystemMonitor::init()
 
58
{
 
59
    KConfigGroup cg = config();
 
60
    QStringList appletNames = cg.readEntry("applets", QStringList());
 
61
 
 
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);
 
68
 
 
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);
 
74
        }
 
75
    }
 
76
 
 
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);
 
88
        }
 
89
        connect(button, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
 
90
        m_buttons->addItem(button);
 
91
        m_monitorButtons << button;
 
92
        // this does not work
 
93
        KGlobal::locale()->insertCatalog(pluginInfo.pluginName());
 
94
    }
 
95
 
 
96
    m_layout->addItem(m_buttons);
 
97
    foreach (const QString& appletName, appletNames) {
 
98
        if (appletsFound.contains(appletName)) {
 
99
            Applet * applet = addApplet(appletName);
 
100
 
 
101
            if (applet) {
 
102
                Plasma::Constraints constraints(Plasma::ImmutableConstraint |
 
103
                                                Plasma::StartupCompletedConstraint);
 
104
                applet->updateConstraints(constraints);
 
105
                applet->flushPendingConstraintsEvents();
 
106
            }
 
107
        }
 
108
    }
 
109
 
 
110
    m_widget->setLayout(m_layout);
 
111
    checkGeometry();
 
112
 
 
113
    setPopupIcon("utilities-system-monitor");
 
114
}
 
115
 
 
116
void SystemMonitor::toggled(bool toggled)
 
117
{
 
118
    removeApplet(sender()->objectName());
 
119
 
 
120
    if (toggled) {
 
121
        SM::Applet * applet = addApplet(sender()->objectName());
 
122
 
 
123
        if (applet) {
 
124
            Plasma::Constraints constraints(Plasma::ImmutableConstraint |
 
125
                                            Plasma::StartupCompletedConstraint);
 
126
            applet->updateConstraints(constraints);
 
127
            applet->flushPendingConstraintsEvents();
 
128
        }
 
129
    }
 
130
}
 
131
 
 
132
SM::Applet *SystemMonitor::addApplet(const QString &name)
 
133
{
 
134
    if (name.isEmpty()) {
 
135
        return 0;
 
136
    }
 
137
 
 
138
    Plasma::Applet* plasmaApplet = Plasma::Applet::load(name, 0, QVariantList() << "SM");
 
139
    SM::Applet* applet = qobject_cast<SM::Applet*>(plasmaApplet);
 
140
    if (applet) {
 
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);
 
150
        applet->init();
 
151
 
 
152
        KConfigGroup cg = config();
 
153
        saveState(cg);
 
154
        emit configNeedsSaving();
 
155
    } else if (plasmaApplet) {
 
156
        delete plasmaApplet;
 
157
    }
 
158
 
 
159
    return applet;
 
160
}
 
161
 
 
162
void SystemMonitor::removeApplet(const QString &name)
 
163
{
 
164
    foreach (SM::Applet *applet, m_applets) {
 
165
        if (applet->objectName() == name) {
 
166
            applet->destroy();
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
void SystemMonitor::appletRemoved(QObject *object)
 
172
{
 
173
    SM::Applet *applet = static_cast<SM::Applet*>(object);
 
174
 
 
175
    foreach (SM::Applet *a, m_applets) {
 
176
        if (a == applet) {
 
177
            m_layout->removeItem(applet);
 
178
            m_applets.removeAll(applet);
 
179
            checkGeometry();
 
180
 
 
181
            KConfigGroup cg = config();
 
182
            saveState(cg);
 
183
            emit configNeedsSaving();
 
184
        }
 
185
    }
 
186
 
 
187
    // sanity check the buttons
 
188
    QSet<QString> running;
 
189
    foreach (SM::Applet *a, m_applets) {
 
190
        running << a->objectName();
 
191
    }
 
192
 
 
193
    foreach (MonitorButton* button, m_monitorButtons) {
 
194
        if (!running.contains(button->objectName())) {
 
195
            kDebug() << "unchecking" << button->objectName();
 
196
            button->setChecked(false);
 
197
        }
 
198
    }
 
199
}
 
200
 
 
201
void SystemMonitor::checkGeometry()
 
202
{
 
203
    QSizeF margins = size() - contentsRect().size();
 
204
    qreal minHeight = m_buttons->minimumHeight();
 
205
    //kDebug() << minHeight;
 
206
 
 
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();
 
211
    }
 
212
 
 
213
 
 
214
    update();
 
215
    /*
 
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();
 
219
    }
 
220
    for (int i = 0; i < m_layout->count(); ++i) {
 
221
        kDebug() << m_layout->itemAt(i)->geometry().top() << m_layout->itemAt(i)->geometry().height();
 
222
    }
 
223
    */
 
224
}
 
225
 
 
226
QGraphicsWidget *SystemMonitor::graphicsWidget()
 
227
{
 
228
    return m_widget;
 
229
}
 
230
 
 
231
void SystemMonitor::constraintsEvent(Plasma::Constraints constraints)
 
232
{
 
233
    Plasma::Constraints passOn = Plasma::NoConstraint;
 
234
 
 
235
    if (constraints & Plasma::ImmutableConstraint) {
 
236
        foreach (MonitorButton* button, m_monitorButtons) {
 
237
            button->setEnabled(immutability() == Plasma::Mutable);
 
238
        }
 
239
 
 
240
        passOn |= Plasma::ImmutableConstraint;
 
241
    }
 
242
 
 
243
    if (constraints & Plasma::StartupCompletedConstraint) {
 
244
        passOn |= Plasma::StartupCompletedConstraint;
 
245
    }
 
246
 
 
247
    if (passOn != Plasma::NoConstraint) {
 
248
        foreach (Plasma::Applet *applet, m_applets) {
 
249
            applet->updateConstraints(passOn);
 
250
            if (passOn & Plasma::StartupCompletedConstraint) {
 
251
                applet->flushPendingConstraintsEvents();
 
252
            }
 
253
        }
 
254
    }
 
255
 
 
256
    PopupApplet::constraintsEvent(constraints);
 
257
}
 
258
 
 
259
#include "system-monitor.moc"