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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/system-monitor/cpu.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) 2008 Petri Damsten <damu@iki.fi>
 
3
 *   Copyright (C) 2010 Michel Lafon-Puyo <michel.lafonpuyo@gmail.com>
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU Library General Public License version 2 as
 
7
 *   published by the Free Software Foundation
 
8
 *
 
9
 *   This program 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
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "cpu.h"
 
21
#include <KDebug>
 
22
#include <Plasma/Theme>
 
23
#include <KConfigDialog>
 
24
#include <QTimer>
 
25
#include <QGraphicsLinearLayout>
 
26
#include "plotter.h"
 
27
 
 
28
SM::Cpu::Cpu(QObject *parent, const QVariantList &args)
 
29
    : SM::Applet(parent, args)
 
30
    , m_rx("^cpu/(\\w+)/TotalLoad$")
 
31
{
 
32
    setHasConfigurationInterface(true);
 
33
    resize(234 + 20 + 23, 135 + 20 + 25);
 
34
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
35
    m_sourceTimer.setSingleShot(true);
 
36
    connect(&m_sourceTimer, SIGNAL(timeout()), this, SLOT(sourcesChanged()));
 
37
}
 
38
 
 
39
SM::Cpu::~Cpu()
 
40
{
 
41
}
 
42
 
 
43
void SM::Cpu::init()
 
44
{
 
45
    KGlobal::locale()->insertCatalog("plasma_applet_system-monitor");
 
46
    setEngine(dataEngine("systemmonitor"));
 
47
    setTitle(i18n("CPU"));
 
48
 
 
49
    /* At the time this method is running, not all source may be connected. */
 
50
    connect(engine(), SIGNAL(sourceAdded(const QString&)), this, SLOT(sourceChanged(const QString&)));
 
51
    connect(engine(), SIGNAL(sourceRemoved(const QString&)), this, SLOT(sourceChanged(const QString&)));
 
52
    foreach (const QString& source, engine()->sources()) {
 
53
        sourceChanged(source);
 
54
    }
 
55
}
 
56
 
 
57
void SM::Cpu::sourceChanged(const QString& name)
 
58
{
 
59
    if (m_rx.indexIn(name) != -1) {
 
60
        //kDebug() << m_rx.cap(1);
 
61
        //kWarning() << name; // debug
 
62
        m_cpus << name;
 
63
        if (!m_sourceTimer.isActive()) {
 
64
            m_sourceTimer.start(0);
 
65
        }
 
66
    }
 
67
}
 
68
 
 
69
void SM::Cpu::sourcesChanged()
 
70
{
 
71
    configChanged();
 
72
}
 
73
 
 
74
void SM::Cpu::configChanged()
 
75
{
 
76
    KConfigGroup cg = config();
 
77
    QStringList default_cpus;
 
78
 
 
79
    if(m_cpus.contains("cpu/system/TotalLoad"))
 
80
        default_cpus << "cpu/system/TotalLoad";
 
81
    else
 
82
        default_cpus = m_cpus;
 
83
    setInterval(cg.readEntry("interval", 2.0) * 1000.0);
 
84
    setSources(cg.readEntry("cpus", default_cpus));
 
85
    connectToEngine();
 
86
}
 
87
 
 
88
QString SM::Cpu::cpuTitle(const QString &name)
 
89
{
 
90
    if (name == "system") {
 
91
        return i18n("total");
 
92
    }
 
93
    return name;
 
94
}
 
95
 
 
96
bool SM::Cpu::addVisualization(const QString& source)
 
97
{
 
98
    QStringList l = source.split('/');
 
99
    if (l.count() < 3) {
 
100
        return false;
 
101
    }
 
102
    QString cpu = l[1];
 
103
    SM::Plotter *plotter = new SM::Plotter(this);
 
104
    plotter->setMinMax(0.0, 100.0);
 
105
    plotter->setTitle(cpuTitle(cpu));
 
106
    plotter->setUnit("%");
 
107
    appendVisualization(source, plotter);
 
108
    setPreferredItemHeight(80);
 
109
    return true;
 
110
}
 
111
 
 
112
void SM::Cpu::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
 
113
{
 
114
    SM::Plotter *plotter = qobject_cast<SM::Plotter*>(visualization(source));
 
115
    if (plotter) {
 
116
        double value = data["value"].toDouble();
 
117
        QString temp = KGlobal::locale()->formatNumber(value, 1);
 
118
        plotter->addSample(QList<double>() << value);
 
119
        if (mode() == SM::Applet::Panel) {
 
120
            setToolTip(source, QString("<tr><td>%1&nbsp;</td><td>%2%</td></tr>")
 
121
                                      .arg(plotter->title()).arg(temp));
 
122
        }
 
123
    }
 
124
}
 
125
 
 
126
void SM::Cpu::createConfigurationInterface(KConfigDialog *parent)
 
127
{
 
128
    QWidget *widget = new QWidget();
 
129
    ui.setupUi(widget);
 
130
    m_model.clear();
 
131
    m_model.setHorizontalHeaderLabels(QStringList() << i18n("CPU"));
 
132
    QStandardItem *parentItem = m_model.invisibleRootItem();
 
133
 
 
134
    foreach (const QString& cpu, m_cpus) {
 
135
        if (m_rx.indexIn(cpu) != -1) {
 
136
            QStandardItem *item1 = new QStandardItem(cpuTitle(m_rx.cap(1)));
 
137
            item1->setEditable(false);
 
138
            item1->setCheckable(true);
 
139
            item1->setData(cpu);
 
140
            if (sources().contains(cpu)) {
 
141
                item1->setCheckState(Qt::Checked);
 
142
            }
 
143
            parentItem->appendRow(QList<QStandardItem *>() << item1);
 
144
        }
 
145
    }
 
146
    ui.treeView->setModel(&m_model);
 
147
    ui.treeView->resizeColumnToContents(0);
 
148
    ui.intervalSpinBox->setValue(interval() / 1000.0);
 
149
    ui.intervalSpinBox->setSuffix(i18nc("second", " s"));
 
150
    parent->addPage(widget, i18n("CPUs"), "cpu");
 
151
 
 
152
    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
 
153
    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
 
154
    connect(ui.treeView, SIGNAL(clicked(QModelIndex)), parent, SLOT(settingsModified()));
 
155
    connect(ui.intervalSpinBox, SIGNAL(valueChanged(QString)), parent, SLOT(settingsModified()));
 
156
}
 
157
 
 
158
void SM::Cpu::configAccepted()
 
159
{
 
160
    KConfigGroup cg = config();
 
161
    QStandardItem *parentItem = m_model.invisibleRootItem();
 
162
 
 
163
    clear();
 
164
 
 
165
    for (int i = 0; i < parentItem->rowCount(); ++i) {
 
166
        QStandardItem *item = parentItem->child(i, 0);
 
167
        if (item) {
 
168
            if (item->checkState() == Qt::Checked) {
 
169
                appendSource(item->data().toString());
 
170
            }
 
171
        }
 
172
    }
 
173
    cg.writeEntry("cpus", sources());
 
174
 
 
175
    double interval = ui.intervalSpinBox->value();
 
176
    cg.writeEntry("interval", interval);
 
177
 
 
178
    emit configNeedsSaving();
 
179
}
 
180
 
 
181
#include "cpu.moc"