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

« back to all changes in this revision

Viewing changes to plasma/generic/applets/system-monitor/ram.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 "ram.h"
 
21
#include <Plasma/Theme>
 
22
#include <Plasma/ToolTipManager>
 
23
#include <KConfigDialog>
 
24
#include <QTimer>
 
25
#include <QGraphicsLinearLayout>
 
26
#include "plotter.h"
 
27
 
 
28
/* All sources we are interested in. */
 
29
static const char phys_source[] = "mem/physical/application";
 
30
static const char swap_source[] = "mem/swap/used";
 
31
 
 
32
SM::Ram::Ram(QObject *parent, const QVariantList &args)
 
33
    : SM::Applet(parent, args)
 
34
{
 
35
    setHasConfigurationInterface(true);
 
36
    resize(234 + 20 + 23, 135 + 20 + 25);
 
37
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
38
}
 
39
 
 
40
SM::Ram::~Ram()
 
41
{
 
42
}
 
43
 
 
44
void SM::Ram::init()
 
45
{
 
46
    KGlobal::locale()->insertCatalog("plasma_applet_system-monitor");
 
47
    setEngine(dataEngine("systemmonitor"));
 
48
    setTitle(i18n("RAM"));
 
49
 
 
50
    /* At the time this method is running, not all source may be connected. */
 
51
    connect(engine(), SIGNAL(sourceAdded(const QString&)), this, SLOT(sourceAdded(const QString&)));
 
52
    foreach (const QString& source, engine()->sources()) {
 
53
        sourceAdded(source);
 
54
    }
 
55
}
 
56
 
 
57
void SM::Ram::configChanged()
 
58
{
 
59
    KConfigGroup cg = config();
 
60
    setInterval(cg.readEntry("interval", 2.0) * 1000.0);
 
61
    setSources(cg.readEntry("memories", m_memories));
 
62
    m_max.clear();
 
63
    connectToEngine();
 
64
}
 
65
 
 
66
void SM::Ram::sourceAdded(const QString& name)
 
67
{
 
68
    if ((name == phys_source || name == swap_source) && !m_memories.contains(name)) {
 
69
        m_memories << name;
 
70
        if (m_memories.count() == 2) {
 
71
            // all sources are ready
 
72
            QTimer::singleShot(0, this, SLOT(sourcesAdded()));
 
73
        }
 
74
    }
 
75
}
 
76
 
 
77
void SM::Ram::sourcesAdded()
 
78
{
 
79
    configChanged();
 
80
}
 
81
 
 
82
bool SM::Ram::addVisualization(const QString& source)
 
83
{
 
84
    QStringList l = source.split('/');
 
85
    if (l.count() < 3) {
 
86
        return false;
 
87
    }
 
88
    QString ram = l[1];
 
89
    SM::Plotter *plotter = new SM::Plotter(this);
 
90
    plotter->setTitle(ram);
 
91
    plotter->setUnit("B");
 
92
    appendVisualization(source, plotter);
 
93
    setPreferredItemHeight(80);
 
94
    return true;
 
95
}
 
96
 
 
97
void SM::Ram::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
 
98
{
 
99
    SM::Plotter *plotter = qobject_cast<SM::Plotter*>(visualization(source));
 
100
    if (plotter) {
 
101
        /* A factor to convert from default units to bytes.
 
102
         * If units is not "KB", assume it is bytes. */
 
103
        const double factor = (data["units"].toString() == "KB") ? 1024.0 : 1.0;
 
104
        const double value_b = data["value"].toDouble() * factor;
 
105
        const double max_b = data["max"].toDouble() * factor;
 
106
        static const QStringList units = QStringList() << "B" << "KiB" << "MiB" << "GiB" << "TiB";
 
107
        if (value_b > m_max[source]) {
 
108
            m_max[source] = max_b;
 
109
            plotter->setMinMax(0.0, max_b);
 
110
            qreal scale = 1.0;
 
111
            int i = 0;
 
112
            while (max_b / scale > 1024.0 && i < units.size()) {
 
113
                scale *= 1024.0;
 
114
                ++i;
 
115
            }
 
116
            plotter->setUnit(units[i]);
 
117
            plotter->setScale(scale);
 
118
        }
 
119
 
 
120
        plotter->addSample(QList<double>() << value_b);
 
121
        QString temp = KGlobal::locale()->formatByteSize(value_b);
 
122
        if (mode() == SM::Applet::Panel) {
 
123
            setToolTip(source, QString("<tr><td>%1</td><td>%2</td><td>of</td><td>%3</td></tr>")
 
124
                                      .arg(plotter->title())
 
125
                                      .arg(temp)
 
126
                                      .arg(KGlobal::locale()->formatByteSize(m_max[source])));
 
127
        }
 
128
    }
 
129
}
 
130
 
 
131
void SM::Ram::createConfigurationInterface(KConfigDialog *parent)
 
132
{
 
133
    QWidget *widget = new QWidget();
 
134
    ui.setupUi(widget);
 
135
    m_model.clear();
 
136
    m_model.setHorizontalHeaderLabels(QStringList() << i18n("RAM"));
 
137
    QStandardItem *parentItem = m_model.invisibleRootItem();
 
138
    QRegExp rx("mem/(\\w+)/.*");
 
139
 
 
140
    foreach (const QString& ram, m_memories) {
 
141
        if (rx.indexIn(ram) != -1) {
 
142
            QStandardItem *item1 = new QStandardItem(rx.cap(1));
 
143
            item1->setEditable(false);
 
144
            item1->setCheckable(true);
 
145
            item1->setData(ram);
 
146
            if (sources().contains(ram)) {
 
147
                item1->setCheckState(Qt::Checked);
 
148
            }
 
149
            parentItem->appendRow(QList<QStandardItem *>() << item1);
 
150
        }
 
151
    }
 
152
    ui.treeView->setModel(&m_model);
 
153
    ui.treeView->resizeColumnToContents(0);
 
154
    ui.intervalSpinBox->setValue(interval() / 1000.0);
 
155
    ui.intervalSpinBox->setSuffix(i18nc("second", " s"));
 
156
    parent->addPage(widget, i18n("RAM"), "media-flash-memory-stick");
 
157
 
 
158
    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
 
159
    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
 
160
    connect(ui.treeView, SIGNAL(clicked(QModelIndex)), parent, SLOT(settingsModified()));
 
161
    connect(ui.intervalSpinBox, SIGNAL(valueChanged(QString)), parent, SLOT(settingsModified()));
 
162
}
 
163
 
 
164
void SM::Ram::configAccepted()
 
165
{
 
166
    KConfigGroup cg = config();
 
167
    QStandardItem *parentItem = m_model.invisibleRootItem();
 
168
 
 
169
    clear();
 
170
 
 
171
    for (int i = 0; i < parentItem->rowCount(); ++i) {
 
172
        QStandardItem *item = parentItem->child(i, 0);
 
173
        if (item) {
 
174
            if (item->checkState() == Qt::Checked) {
 
175
                appendSource(item->data().toString());
 
176
            }
 
177
        }
 
178
    }
 
179
    cg.writeEntry("memories", sources());
 
180
 
 
181
    double interval = ui.intervalSpinBox->value();
 
182
    cg.writeEntry("interval", interval);
 
183
 
 
184
    emit configNeedsSaving();
 
185
}
 
186
 
 
187
#include "ram.moc"