• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

cpu.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2008 Petri Damsten <damu@iki.fi>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017  */
00018 
00019 #include "cpu.h"
00020 #include <Plasma/SignalPlotter>
00021 #include <Plasma/Theme>
00022 #include <KConfigDialog>
00023 #include <QTimer>
00024 #include <QGraphicsLinearLayout>
00025 
00026 SM::Cpu::Cpu(QObject *parent, const QVariantList &args)
00027     : SM::Applet(parent, args)
00028 {
00029     setHasConfigurationInterface(true);
00030     resize(234 + 20 + 23, 135 + 20 + 25);
00031     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00032     connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeChanged()));
00033 }
00034 
00035 SM::Cpu::~Cpu()
00036 {
00037 }
00038 
00039 void SM::Cpu::init()
00040 {
00041     KConfigGroup cg = config();
00042     setEngine(dataEngine("systemmonitor"));
00043     setInterval(cg.readEntry("interval", 2) * 1000);
00044     setTitle(i18n("CPU"));
00045     if (engine()->sources().count() == 0) {
00046         connect(engine(), SIGNAL(sourceAdded(QString)), this, SLOT(initLater(const QString)));
00047     } else {
00048         parseSources();
00049     }
00050 }
00051 
00052 void SM::Cpu::parseSources()
00053 {
00054     QRegExp rx("cpu/(\\w+)/TotalLoad");
00055 
00056     foreach (const QString& s, engine()->sources()) {
00057         if (rx.indexIn(s) != -1) {
00058             //kDebug() << rx.cap(1);
00059             m_cpus << s;
00060         }
00061     }
00062     KConfigGroup cg = config();
00063     setItems(cg.readEntry("cpus", QStringList() << "cpu/system/TotalLoad"));
00064     connectToEngine();
00065 }
00066 
00067 void SM::Cpu::initLater(const QString &name)
00068 {
00069     // How we know all (cpu) sources are ready???
00070     if (name == "system/uptime") {
00071         QTimer::singleShot(0, this, SLOT(parseSources()));
00072     }
00073 }
00074 
00075 bool SM::Cpu::addMeter(const QString& source)
00076 {
00077     QStringList l = source.split('/');
00078     if (l.count() < 3) {
00079         return false;
00080     }
00081     QString cpu = l[2];
00082     Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00083     Plasma::SignalPlotter *plotter = new Plasma::SignalPlotter(this);
00084     plotter->addPlot(theme->color(Plasma::Theme::TextColor));
00085     plotter->setUseAutoRange(false);
00086     plotter->setVerticalRange(0.0, 100.0);
00087     plotter->setThinFrame(false);
00088     plotter->setShowLabels(false);
00089     plotter->setShowTopBar(false);
00090     plotter->setShowVerticalLines(false);
00091     plotter->setShowHorizontalLines(false);
00092     plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
00093     QFont font = theme->font(Plasma::Theme::DefaultFont);
00094     font.setPointSize(8);
00095     plotter->setFont(font);
00096     plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00097     plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00098     plotter->setHorizontalLinesCount(4);
00099     //plotter->setSvgBackground("widgets/plot-background");
00100     plotter->setTitle(cpu);
00101     plotter->setUnit("%");
00102     appendPlotter(source, plotter);
00103     mainLayout()->addItem(plotter);
00104     setPreferredItemHeight(80);
00105     return true;
00106 }
00107 
00108 void SM::Cpu::themeChanged()
00109 {
00110     Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00111     foreach (Plasma::SignalPlotter *plotter, plotters().values()) {
00112         plotter->removePlot(0);
00113         plotter->addPlot(theme->color(Plasma::Theme::TextColor));
00114         plotter->setFontColor(theme->color(Plasma::Theme::HighlightColor));
00115         plotter->setHorizontalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00116         plotter->setVerticalLinesColor(theme->color(Plasma::Theme::HighlightColor));
00117     }
00118 }
00119 
00120 void SM::Cpu::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
00121 {
00122     Plasma::SignalPlotter *plotter = plotters()[source];
00123     if (plotter) {
00124         plotter->addSample(QList<double>() << data["value"].toDouble());
00125     }
00126 }
00127 
00128 void SM::Cpu::createConfigurationInterface(KConfigDialog *parent)
00129 {
00130    QWidget *widget = new QWidget();
00131    ui.setupUi(widget);
00132    m_model.clear();
00133    m_model.setHorizontalHeaderLabels(QStringList() << i18n("CPU"));
00134    QStandardItem *parentItem = m_model.invisibleRootItem();
00135    QRegExp rx("cpu/(\\w+)/TotalLoad");
00136 
00137 
00138    foreach (const QString& cpu, m_cpus) {
00139       if (rx.indexIn(cpu) != -1) {
00140          QStandardItem *item1 = new QStandardItem(rx.cap(1));
00141          item1->setEditable(false);
00142          item1->setCheckable(true);
00143          item1->setData(cpu);
00144          if (items().contains(cpu)) {
00145             item1->setCheckState(Qt::Checked);
00146          }
00147          parentItem->appendRow(QList<QStandardItem *>() << item1);
00148       }
00149    }
00150    ui.treeView->setModel(&m_model);
00151    ui.treeView->resizeColumnToContents(0);
00152    ui.intervalSpinBox->setValue(interval() / 1000);
00153 
00154    parent->addPage(widget, i18n("CPUs"), "cpu");
00155    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
00156    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
00157 }
00158 
00159 void SM::Cpu::configAccepted()
00160 {
00161     KConfigGroup cg = config();
00162     QStandardItem *parentItem = m_model.invisibleRootItem();
00163 
00164     clearItems();
00165     for (int i = 0; i < parentItem->rowCount(); ++i) {
00166         QStandardItem *item = parentItem->child(i, 0);
00167         if (item) {
00168             if (item->checkState() == Qt::Checked) {
00169                 appendItem(item->data().toString());
00170             }
00171         }
00172     }
00173     cg.writeEntry("cpus", items());
00174 
00175     uint interval = ui.intervalSpinBox->value();
00176     cg.writeEntry("interval", interval);
00177     interval *= 1000;
00178     setInterval(interval);
00179 
00180     emit configNeedsSaving();
00181     connectToEngine();
00182 }
00183 
00184 void SM::Cpu::setDetail(Detail detail)
00185 {
00186     foreach (const QString& key, plotters().keys()) {
00187         plotters().value(key)->setShowLabels(detail == SM::Applet::High);
00188         //plotters().value(key)->setShowTopBar(detail == SM::Applet::High);
00189         //plotters().value(key)->setShowVerticalLines(detail == SM::Applet::High);
00190         plotters().value(key)->setShowHorizontalLines(detail == SM::Applet::High);
00191     }
00192 }
00193 
00194 #include "cpu.moc"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal