2 * Copyright (C) 2007 Petri Damsten <damu@iki.fi>
3 * Copyright (C) 2008 Marco Martin <notmart@gmail.com>
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
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
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.
20 #include "temperature.h"
21 #include <Plasma/SignalPlotter>
22 #include <Plasma/Meter>
23 #include <Plasma/Containment>
24 #include <Plasma/Theme>
25 #include <KConfigDialog>
26 #include <QGraphicsLinearLayout>
30 Temperature::Temperature(QObject
*parent
, const QVariantList
&args
)
31 : SM::Applet(parent
, args
), m_showPlotters(false), m_tempModel(0)
33 setHasConfigurationInterface(true);
34 resize(215 + 20 + 23, 109 + 20 + 25);
35 setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Preferred
);
36 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
39 Temperature::~Temperature()
43 void Temperature::init()
45 KConfigGroup cg
= persistentConfig();
46 setEngine(dataEngine("systemmonitor"));
47 m_showPlotters
= cg
.readEntry("showPlotters", true);
48 setInterval(cg
.readEntry("interval", 2) * 1000);
50 setTitle(i18n("Temperature"));
51 if (engine()->sources().count() == 0) {
52 connect(engine(), SIGNAL(sourceAdded(QString
)), this, SLOT(initLater(const QString
)));
58 void Temperature::parseSources()
60 KConfigGroup cg
= persistentConfig();
61 setItems(cg
.readEntry("temps", engine()->sources().filter(QRegExp(".*temp.*", Qt::CaseInsensitive
))));
65 void Temperature::initLater(const QString
&name
)
67 // How we know all (cpu) sources are ready???
68 if (name
== "system/uptime") {
69 QTimer::singleShot(0, this, SLOT(parseSources()));
73 void Temperature::createConfigurationInterface(KConfigDialog
*parent
)
75 QWidget
*widget
= new QWidget();
78 m_tempModel
.setHorizontalHeaderLabels(QStringList() << i18n("Sensor")
80 Plasma::DataEngine
*engine
= dataEngine("systemmonitor");
81 QStringList temps
= engine
->sources().filter(QRegExp(".*temp.*", Qt::CaseInsensitive
));
83 QStandardItem
*parentItem
= m_tempModel
.invisibleRootItem();
84 foreach (const QString
& temp
, temps
) {
85 QStandardItem
*item1
= new QStandardItem(temp
);
86 item1
->setEditable(false);
87 item1
->setCheckable(true);
88 if (items().contains(temp
)) {
89 item1
->setCheckState(Qt::Checked
);
91 QStandardItem
*item2
= new QStandardItem(title(temp
));
92 item2
->setEditable(true);
93 parentItem
->appendRow(QList
<QStandardItem
*>() << item1
<< item2
);
95 ui
.treeView
->setModel(&m_tempModel
);
96 ui
.treeView
->resizeColumnToContents(0);
98 ui
.showPlotters
->setChecked(m_showPlotters
);
99 ui
.intervalSpinBox
->setValue(interval() / 1000);
101 parent
->setButtons(KDialog::Ok
| KDialog::Cancel
| KDialog::Apply
);
102 parent
->addPage(widget
, i18n("Temperature"), "cpu");
103 connect(parent
, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
104 connect(parent
, SIGNAL(okClicked()), this, SLOT(configAccepted()));
107 void Temperature::configAccepted()
109 KConfigGroup cg
= persistentConfig();
110 QStandardItem
*parentItem
= m_tempModel
.invisibleRootItem();
113 for (int i
= 0; i
< parentItem
->rowCount(); ++i
) {
114 QStandardItem
*item
= parentItem
->child(i
, 0);
116 cg
.writeEntry(item
->text(),
117 parentItem
->child(i
, 1)->text());
118 if (item
->checkState() == Qt::Checked
) {
119 appendItem(item
->text());
123 cg
.writeEntry("temps", items());
125 m_showPlotters
= ui
.showPlotters
->isChecked();
126 cg
.writeEntry("showPlotters", m_showPlotters
);
127 uint interval
= ui
.intervalSpinBox
->value();
128 cg
.writeEntry("interval", interval
);
130 setInterval(interval
);
132 emit
configNeedsSaving();
136 QString
Temperature::title(const QString
& source
)
138 KConfigGroup cg
= persistentConfig();
139 return cg
.readEntry(source
, source
.mid(source
.lastIndexOf('/') + 1));
142 bool Temperature::addMeter(const QString
& source
)
144 Plasma::DataEngine
*engine
= dataEngine("systemmonitor");
145 Plasma::DataEngine::Data data
;
146 Plasma::Theme
* theme
= Plasma::Theme::defaultTheme();
151 data
= engine
->query(source
);
152 QGraphicsLinearLayout
*layout
= new QGraphicsLinearLayout(Qt::Horizontal
);
153 layout
->setContentsMargins(3, 3, 3, 3);
154 layout
->setSpacing(5);
156 Plasma::Meter
*meter
= new Plasma::Meter(this);
157 meter
->setMeterType(Plasma::Meter::AnalogMeter
);
158 meter
->setLabel(0, title(source
));
159 meter
->setLabelColor(0, theme
->color(Plasma::Theme::TextColor
));
160 meter
->setLabel(1, QString());
161 meter
->setLabelColor(1, QColor("#000"));
162 meter
->setLabelAlignment(1, Qt::AlignCenter
);
163 QFont font
= theme
->font(Plasma::Theme::DefaultFont
);
164 font
.setPointSize(7);
165 meter
->setLabelFont(0, font
);
166 meter
->setLabelFont(1, font
);
167 meter
->setMinimum(0);
168 meter
->setMaximum(110);
169 layout
->addItem(meter
);
170 appendMeter(source
, meter
);
171 appendKeepRatio(meter
);
173 if (mode() != SM::Applet::Panel
&& m_showPlotters
) {
174 Plasma::SignalPlotter
*plotter
= new Plasma::SignalPlotter(this);
175 plotter
->addPlot(theme
->color(Plasma::Theme::TextColor
));
176 plotter
->setUseAutoRange(true);
177 plotter
->setThinFrame(false);
178 plotter
->setShowLabels(false);
179 plotter
->setShowTopBar(false);
180 plotter
->setShowVerticalLines(false);
181 plotter
->setShowHorizontalLines(false);
182 plotter
->setSvgBackground("widgets/plot-background");
183 layout
->addItem(plotter
);
184 appendPlotter(source
, plotter
);
185 setRatioOrientation(Qt::Horizontal
);
189 mainLayout()->addItem(layout
);
191 dataUpdated(source
, data
);
192 //setPreferredItemHeight(layout->preferredSize().height());
193 setPreferredItemHeight(80);
197 void Temperature::themeChanged()
199 Plasma::Theme
* theme
= Plasma::Theme::defaultTheme();
200 foreach (Plasma::Meter
*w
, meters().values()) {
201 w
->setLabelColor(0, theme
->color(Plasma::Theme::TextColor
));
202 QFont font
= theme
->font(Plasma::Theme::DefaultFont
);
203 font
.setPointSize(7);
204 w
->setLabelFont(0, font
);
205 w
->setLabelFont(1, font
);
207 foreach (Plasma::SignalPlotter
*plotter
, plotters().values()) {
208 plotter
->removePlot(0);
209 plotter
->addPlot(theme
->color(Plasma::Theme::TextColor
));
213 void Temperature::dataUpdated(const QString
& source
,
214 const Plasma::DataEngine::Data
&data
)
216 if (!items().contains(source
)) {
219 Plasma::Meter
*w
= meters().value(source
);
223 w
->setValue(data
["value"].toDouble());
225 qreal celsius
= ((qreal
)data
["value"].toDouble());
227 if (KGlobal::locale()->measureSystem() == KLocale::Metric
) {
228 w
->setLabel(1, i18n("%1 °C", celsius
));
230 w
->setLabel(1, i18n("%1 °F", (celsius
* 1.8) + 32));
233 if (m_showPlotters
) {
234 Plasma::SignalPlotter
*plotter
= plotters().value(source
);
236 plotter
->addSample(QList
<double>() << data
["value"].toDouble());
241 #include "temperature.moc"