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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/system-monitor/temperature.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
 *   Copyright (C) 2008 Marco Martin <notmart@gmail.com>
 
4
 *   Copyright (C) 2010 Michel Lafon-Puyo <michel.lafonpuyo@gmail.com>
 
5
 *   Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
 
6
 *
 
7
 *   This program is free software; you can redistribute it and/or modify
 
8
 *   it under the terms of the GNU Library General Public License version 2 as
 
9
 *   published by the Free Software Foundation
 
10
 *
 
11
 *   This program is distributed in the hope that it will be useful,
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *   GNU General Public License for more details
 
15
 *
 
16
 *   You should have received a copy of the GNU Library General Public
 
17
 *   License along with this program; if not, write to the
 
18
 *   Free Software Foundation, Inc.,
 
19
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
20
 */
 
21
 
 
22
#include "temperature.h"
 
23
#include <Plasma/Meter>
 
24
#include <Plasma/Containment>
 
25
#include <Plasma/Theme>
 
26
#include <KConfigDialog>
 
27
#include <KUnitConversion/Converter>
 
28
#include <KUnitConversion/Value>
 
29
#include <QGraphicsLinearLayout>
 
30
#include <QTimer>
 
31
#include <cmath>
 
32
#include "plotter.h"
 
33
#include "temperature-offset-delegate.h"
 
34
 
 
35
using namespace KUnitConversion;
 
36
 
 
37
Temperature::Temperature(QObject *parent, const QVariantList &args)
 
38
    : SM::Applet(parent, args)
 
39
    , m_tempModel(0)
 
40
    , m_rx(".*temp.*", Qt::CaseInsensitive)
 
41
{
 
42
    setHasConfigurationInterface(true);
 
43
    resize(215 + 20 + 23, 109 + 20 + 25);
 
44
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
 
45
    m_sourceTimer.setSingleShot(true);
 
46
    connect(&m_sourceTimer, SIGNAL(timeout()), this, SLOT(sourcesAdded()));
 
47
}
 
48
 
 
49
Temperature::~Temperature()
 
50
{
 
51
}
 
52
 
 
53
void Temperature::init()
 
54
{
 
55
    KGlobal::locale()->insertCatalog("plasma_applet_system-monitor");
 
56
    setEngine(dataEngine("systemmonitor"));
 
57
    setTitle(i18n("Temperature"));
 
58
 
 
59
    /* At the time this method is running, not all source may be connected. */
 
60
    connect(engine(), SIGNAL(sourceAdded(const QString&)), this, SLOT(sourceAdded(const QString&)));
 
61
    foreach (const QString& source, engine()->sources()) {
 
62
        sourceAdded(source);
 
63
    }
 
64
}
 
65
 
 
66
void Temperature::configChanged()
 
67
{
 
68
    KConfigGroup cg = config();
 
69
    setInterval(cg.readEntry("interval", 2.0) * 1000.0);
 
70
    setSources(cg.readEntry("temps", m_sources.mid(0, 5)));
 
71
    connectToEngine();
 
72
}
 
73
 
 
74
void Temperature::sourceAdded(const QString& name)
 
75
{
 
76
    if (m_rx.indexIn(name) != -1) {
 
77
        //kDebug() << m_rx.cap(1);
 
78
        m_sources << name;
 
79
        if (!m_sourceTimer.isActive()) {
 
80
            m_sourceTimer.start(0);
 
81
        }
 
82
    }
 
83
}
 
84
 
 
85
void Temperature::sourcesAdded()
 
86
{
 
87
    configChanged();
 
88
}
 
89
 
 
90
void Temperature::createConfigurationInterface(KConfigDialog *parent)
 
91
{
 
92
    QWidget *widget = new QWidget();
 
93
    ui.setupUi(widget);
 
94
    m_tempModel.clear();
 
95
    m_tempModel.setHorizontalHeaderLabels(QStringList() << i18n("Sensor")
 
96
                                                        << i18n("Name")
 
97
                                                        << i18n("Offset"));
 
98
 
 
99
    QStandardItem *parentItem = m_tempModel.invisibleRootItem();
 
100
    foreach (const QString& temp, m_sources) {
 
101
        QStandardItem *item1 = new QStandardItem(temp);
 
102
        item1->setEditable(false);
 
103
        item1->setCheckable(true);
 
104
        if (sources().contains(temp)) {
 
105
            item1->setCheckState(Qt::Checked);
 
106
        }
 
107
        QStandardItem *item2 = new QStandardItem(temperatureTitle(temp));
 
108
        item2->setEditable(true);
 
109
        QStandardItem *item3 = new QStandardItem(
 
110
                KGlobal::locale()->formatNumber(temperatureOffset(temp), 1));
 
111
        item3->setEditable(true);
 
112
        parentItem->appendRow(QList<QStandardItem *>() << item1 << item2 << item3);
 
113
    }
 
114
    ui.treeView->setModel(&m_tempModel);
 
115
    ui.treeView->resizeColumnToContents(0);
 
116
    ui.treeView->setItemDelegateForColumn(2, new TemperatureOffsetDelegate());
 
117
 
 
118
    ui.intervalSpinBox->setValue(interval() / 1000.0);
 
119
    ui.intervalSpinBox->setSuffix(i18nc("second", " s"));
 
120
    parent->setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
 
121
    parent->addPage(widget, i18n("Temperature"), "media-flash");
 
122
 
 
123
    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
 
124
    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
 
125
    connect(ui.treeView, SIGNAL(clicked(QModelIndex)), parent, SLOT(settingsModified()));
 
126
    connect(ui.intervalSpinBox, SIGNAL(valueChanged(QString)), parent, SLOT(settingsModified()));
 
127
}
 
128
 
 
129
void Temperature::configAccepted()
 
130
{
 
131
    KConfigGroup cg = config();
 
132
    KConfigGroup cgGlobal = globalConfig();
 
133
    QStandardItem *parentItem = m_tempModel.invisibleRootItem();
 
134
 
 
135
    clear();
 
136
 
 
137
    for (int i = 0; i < parentItem->rowCount(); ++i) {
 
138
        QStandardItem *item = parentItem->child(i, 0);
 
139
        if (item) {
 
140
            cgGlobal.writeEntry(item->text(),
 
141
                                parentItem->child(i, 1)->text());
 
142
            cgGlobal.writeEntry(item->text() + "_offset", QString::number(
 
143
                                    parentItem->child(i, 2)->data(Qt::EditRole).toDouble(), 'f', 1));
 
144
            if (item->checkState() == Qt::Checked) {
 
145
                appendSource(item->text());
 
146
            }
 
147
        }
 
148
    }
 
149
    cg.writeEntry("temps", sources());
 
150
    uint interval = ui.intervalSpinBox->value();
 
151
    cg.writeEntry("interval", interval);
 
152
 
 
153
    emit configNeedsSaving();
 
154
}
 
155
 
 
156
QString Temperature::temperatureTitle(const QString& source)
 
157
{
 
158
    KConfigGroup cg = globalConfig();
 
159
    return cg.readEntry(source, source.mid(source.lastIndexOf('/') + 1).replace('_',' '));
 
160
}
 
161
 
 
162
double Temperature::temperatureOffset(const QString& source)
 
163
{
 
164
    KConfigGroup cg = globalConfig();
 
165
    return cg.readEntry(source + "_offset", 0.0);
 
166
}
 
167
 
 
168
bool Temperature::addVisualization(const QString& source)
 
169
{
 
170
    Plasma::DataEngine *engine = dataEngine("systemmonitor");
 
171
 
 
172
    if (!engine) {
 
173
        return false;
 
174
    }
 
175
 
 
176
    SM::Plotter *plotter = new SM::Plotter(this);
 
177
    plotter->setTitle(temperatureTitle(source));
 
178
    plotter->setAnalog(mode() != SM::Applet::Panel);
 
179
 
 
180
    if (KGlobal::locale()->measureSystem() == KLocale::Metric) {
 
181
        plotter->setMinMax(0, 110);
 
182
        plotter->setUnit(QString::fromUtf8("°C"));
 
183
    } else {
 
184
        plotter->setMinMax(32, 230);
 
185
        plotter->setUnit(QString::fromUtf8("°F"));
 
186
    }
 
187
    appendVisualization(source, plotter);
 
188
 
 
189
    Plasma::DataEngine::Data data = engine->query(source);
 
190
    dataUpdated(source, data);
 
191
    setPreferredItemHeight(80);
 
192
    return true;
 
193
}
 
194
 
 
195
void Temperature::dataUpdated(const QString& source,
 
196
                              const Plasma::DataEngine::Data &data)
 
197
{
 
198
    if (!sources().contains(source)) {
 
199
        return;
 
200
    }
 
201
    SM::Plotter *plotter = qobject_cast<SM::Plotter*>(visualization(source));
 
202
    QString temp;
 
203
    QString unit = data["units"].toString();
 
204
    double doubleValue = data["value"].toDouble() + temperatureOffset(source);
 
205
    Value value = Value(doubleValue, unit);
 
206
 
 
207
    if (KGlobal::locale()->measureSystem() == KLocale::Metric) {
 
208
        value = value.convertTo(Celsius);
 
209
    } else {
 
210
        value = value.convertTo(Fahrenheit);
 
211
    }
 
212
 
 
213
    value.round(1);
 
214
    if (plotter) {
 
215
        plotter->addSample(QList<double>() << value.number());
 
216
    }
 
217
 
 
218
    temp = value.toSymbolString();
 
219
 
 
220
    if (mode() == SM::Applet::Panel) {
 
221
        setToolTip(source,
 
222
                   QString("<tr><td>%1</td><td>%2</td></tr>").arg(temperatureTitle(source)).arg(temp));
 
223
    }
 
224
}
 
225
 
 
226
#include "temperature.moc"