not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / system-monitor / cpu.cpp
blob0a64b498880b0568b30800e904a584f39003b3b8
1 /*
2 * Copyright (C) 2008 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 "cpu.h"
20 #include <Plasma/SignalPlotter>
21 #include <Plasma/Theme>
22 #include <KConfigDialog>
23 #include <QTimer>
24 #include <QGraphicsLinearLayout>
26 SM::Cpu::Cpu(QObject *parent, const QVariantList &args)
27 : SM::Applet(parent, args)
29 setHasConfigurationInterface(true);
30 resize(234 + 20 + 23, 135 + 20 + 25);
31 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
32 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
35 SM::Cpu::~Cpu()
39 void SM::Cpu::init()
41 KConfigGroup cg = config();
42 setEngine(dataEngine("systemmonitor"));
43 setInterval(cg.readEntry("interval", 2) * 1000);
44 setTitle(i18n("CPU"));
45 if (engine()->sources().count() == 0) {
46 connect(engine(), SIGNAL(sourceAdded(QString)), this, SLOT(initLater(const QString)));
47 } else {
48 parseSources();
52 void SM::Cpu::parseSources()
54 QRegExp rx("cpu/(\\w+)/TotalLoad");
56 foreach (const QString& s, engine()->sources()) {
57 if (rx.indexIn(s) != -1) {
58 //kDebug() << rx.cap(1);
59 m_cpus << s;
62 KConfigGroup cg = config();
63 setItems(cg.readEntry("cpus", QStringList() << "cpu/system/TotalLoad"));
64 connectToEngine();
67 void SM::Cpu::initLater(const QString &name)
69 // How we know all (cpu) sources are ready???
70 if (name == "system/uptime") {
71 QTimer::singleShot(0, this, SLOT(parseSources()));
75 bool SM::Cpu::addMeter(const QString& source)
77 QStringList l = source.split('/');
78 if (l.count() < 3) {
79 return false;
81 QString cpu = l[2];
82 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
83 Plasma::SignalPlotter *plotter = new Plasma::SignalPlotter(this);
84 plotter->addPlot(theme->color(Plasma::Theme::TextColor));
85 plotter->setUseAutoRange(false);
86 plotter->setVerticalRange(0.0, 100.0);
87 plotter->setThinFrame(false);
88 plotter->setShowLabels(false);
89 plotter->setShowTopBar(false);
90 plotter->setShowVerticalLines(false);
91 plotter->setShowHorizontalLines(false);
92 plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
93 QFont font = theme->font(Plasma::Theme::DefaultFont);
94 font.setPointSize(8);
95 plotter->setFont(font);
96 plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
97 plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
98 plotter->setHorizontalLinesCount(4);
99 //plotter->setSvgBackground("widgets/plot-background");
100 plotter->setTitle(cpu);
101 plotter->setUnit("%");
102 appendPlotter(source, plotter);
103 mainLayout()->addItem(plotter);
104 setPreferredItemHeight(80);
105 return true;
108 void SM::Cpu::themeChanged()
110 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
111 foreach (Plasma::SignalPlotter *plotter, plotters().values()) {
112 plotter->removePlot(0);
113 plotter->addPlot(theme->color(Plasma::Theme::TextColor));
114 plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
115 plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
116 plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
120 void SM::Cpu::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
122 Plasma::SignalPlotter *plotter = plotters()[source];
123 if (plotter) {
124 plotter->addSample(QList<double>() << data["value"].toDouble());
128 void SM::Cpu::createConfigurationInterface(KConfigDialog *parent)
130 QWidget *widget = new QWidget();
131 ui.setupUi(widget);
132 m_model.clear();
133 m_model.setHorizontalHeaderLabels(QStringList() << i18n("CPU"));
134 QStandardItem *parentItem = m_model.invisibleRootItem();
135 QRegExp rx("cpu/(\\w+)/TotalLoad");
138 foreach (const QString& cpu, m_cpus) {
139 if (rx.indexIn(cpu) != -1) {
140 QStandardItem *item1 = new QStandardItem(rx.cap(1));
141 item1->setEditable(false);
142 item1->setCheckable(true);
143 item1->setData(cpu);
144 if (items().contains(cpu)) {
145 item1->setCheckState(Qt::Checked);
147 parentItem->appendRow(QList<QStandardItem *>() << item1);
150 ui.treeView->setModel(&m_model);
151 ui.treeView->resizeColumnToContents(0);
152 ui.intervalSpinBox->setValue(interval() / 1000);
154 parent->addPage(widget, i18n("CPUs"), "cpu");
155 connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
156 connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
159 void SM::Cpu::configAccepted()
161 KConfigGroup cg = config();
162 QStandardItem *parentItem = m_model.invisibleRootItem();
164 clearItems();
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 appendItem(item->data().toString());
173 cg.writeEntry("cpus", items());
175 uint interval = ui.intervalSpinBox->value();
176 cg.writeEntry("interval", interval);
177 interval *= 1000;
178 setInterval(interval);
180 emit configNeedsSaving();
181 connectToEngine();
184 void SM::Cpu::setDetail(Detail detail)
186 foreach (const QString& key, plotters().keys()) {
187 plotters().value(key)->setShowLabels(detail == SM::Applet::High);
188 //plotters().value(key)->setShowTopBar(detail == SM::Applet::High);
189 //plotters().value(key)->setShowVerticalLines(detail == SM::Applet::High);
190 plotters().value(key)->setShowHorizontalLines(detail == SM::Applet::High);
194 #include "cpu.moc"