Fix rotation
[system_status.git] / applet / system_monitor.cpp
bloba6ac52e2d3302b43999234ed41074da72187ca90
1 /***********************************************************************************
2 * System Monitor: Plasmoid and data engines to monitor CPU/Memory/Swap Usage.
3 * Copyright (C) 2008 Matthew Dawson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 ***********************************************************************************/
21 #include "system_monitor.h"
23 #include <QPainter>
24 #include <KConfigDialog>
26 systemMonitor::systemMonitor(QObject *parent, const QVariantList &args)
27 : Plasma::Applet(parent, args),
28 m_user(0),
29 m_sys(0),
30 m_nice(0),
31 m_idle(0),
32 m_disk(0){
34 setHasConfigurationInterface(true);
35 resize(100, 100);
38 systemMonitor::~systemMonitor(){
40 if(hasFailedToLaunch()){
42 }else {
48 void systemMonitor::init(){
50 readConfig();
52 Plasma::DataEngine *cpu_mon = dataEngine("cpumonitor");
53 Plasma::DataEngine *mem_mon = dataEngine("memmonitor");
54 cpu_mon->connectSource("Average CPU Usage", this, 500);
55 mem_mon->connectSource("RAM", this, 500);
56 mem_mon->connectSource("Swap", this, 500);
60 void systemMonitor::constraintsEvent(Plasma::Constraints constraints){
62 if(constraints.testFlag(Plasma::FormFactorConstraint)){
64 if(formFactor() == Plasma::Planar || formFactor() == Plasma::MediaCenter){
65 setAspectRatioMode(Plasma::IgnoreAspectRatio);
66 }else {
67 setAspectRatioMode(Plasma::ConstrainedSquare);
70 //If only StandardBackground is set, it should be completely transparent. (it gets
71 //set in Plasma::Applet on certain conditions.
72 if(backgroundHints() == StandardBackground){
73 setBackgroundHints(NoBackground);
80 void systemMonitor::createConfigurationInterface(KConfigDialog *parent){
82 QWidget *widGeneral = new QWidget();
83 uiGeneral.setupUi(widGeneral);
85 uiGeneral.chkIsVertical->setChecked(m_isVertical);
86 uiGeneral.chkUseOxygen->setChecked(m_useOxygen);
87 uiGeneral.kcmbBackground->setItemData(0, DefaultBackground);
88 uiGeneral.kcmbBackground->setItemData(1, TranslucentBackground);
89 uiGeneral.kcmbBackground->setItemData(2, NoBackground);
91 switch( (int)backgroundHints() ){
93 case (int)DefaultBackground:
94 uiGeneral.kcmbBackground->setCurrentIndex(0);
95 break;
96 //StandardBackground may get set. Doesn't effect drawing, so this just makes sure we catch it.
97 case (int)TranslucentBackground:
98 case (int)TranslucentBackground | StandardBackground:
99 uiGeneral.kcmbBackground->setCurrentIndex(1);
100 break;
101 case (int)NoBackground:
102 uiGeneral.kcmbBackground->setCurrentIndex(2);
103 break;
104 default:
105 Q_ASSERT("Unknown background hints!");
106 break;
110 QWidget *widColours = new QWidget();
111 uiColours.setupUi(widColours);
113 uiColours.kcbCpuUser->setColor(this->m_cpuUserColour);
114 uiColours.kcbCpuNice->setColor(this->m_cpuNiceColour);
115 uiColours.kcbCpuDisk->setColor(this->m_cpuDiskColour);
116 uiColours.kcbCpuSystem->setColor(this->m_cpuSysColour);
117 uiColours.kcbRamBuffers->setColor(this->m_ramBuffersColour);
118 uiColours.kcbRamCached->setColor(this->m_ramCachedColour);
119 uiColours.kcbRamUser->setColor(this->m_ramUsedColour);
120 uiColours.kcbSwap->setColor(this->m_swapUsedColour);
122 parent->setButtons (KDialog::Ok | KDialog::Cancel | KDialog::Apply );
123 connect(parent, SIGNAL(applyClicked()), this, SLOT(configUpdated()));
124 connect(parent, SIGNAL(okClicked()), this, SLOT(configUpdated()));
126 parent->addPage(widGeneral, "General", icon(), QString(), false);
127 parent->addPage(widColours, "Colours", icon(), QString(), false);
129 parent->setFaceType(KConfigDialog::Tabbed);
133 void systemMonitor::configUpdated(){
135 KConfigGroup cg = config();
137 if(uiGeneral.kcmbBackground->itemData(uiGeneral.kcmbBackground->currentIndex()).toInt() != (int)this->backgroundHints()){
138 this->setBackgroundHints(QFlag(uiGeneral.kcmbBackground->itemData(uiGeneral.kcmbBackground->currentIndex()).toInt()));
139 cg.writeEntry("background", (int)this->backgroundHints());
142 if(uiGeneral.chkIsVertical->isChecked() != this->m_isVertical){
143 this->m_isVertical = uiGeneral.chkIsVertical->isChecked();
144 cg.writeEntry("vertical", this->m_isVertical);
146 if(uiGeneral.chkUseOxygen->isChecked() != this->m_useOxygen){
147 this->m_useOxygen = uiGeneral.chkUseOxygen->isChecked();
148 cg.writeEntry("use_oxygen", this->m_useOxygen);
150 if(uiColours.kcbCpuUser->color() != this->m_cpuUserColour){
151 this->m_cpuUserColour = uiColours.kcbCpuUser->color();
152 cg.writeEntry("colour_cpu_user", this->m_cpuUserColour.name());
154 if(uiColours.kcbCpuNice->color() != this->m_cpuNiceColour){
155 this->m_cpuNiceColour = uiColours.kcbCpuNice->color();
156 cg.writeEntry("colour_cpu_nice", this->m_cpuNiceColour.name());
158 if(uiColours.kcbCpuDisk->color() != this->m_cpuDiskColour){
159 this->m_cpuDiskColour = uiColours.kcbCpuDisk->color();
160 cg.writeEntry("colour_cpu_disk", this->m_cpuDiskColour.name());
162 if(uiColours.kcbCpuSystem->color() != this->m_cpuSysColour){
163 this->m_cpuSysColour = uiColours.kcbCpuSystem->color();
164 cg.writeEntry("colour_cpu_sys", this->m_cpuSysColour.name());
166 if(uiColours.kcbRamCached->color() != this->m_ramCachedColour){
167 this->m_ramCachedColour = uiColours.kcbRamCached->color();
168 cg.writeEntry("colour_ram_cached", this->m_ramCachedColour.name());
170 if(uiColours.kcbRamBuffers->color() != this->m_ramBuffersColour){
171 this->m_ramBuffersColour = uiColours.kcbRamBuffers->color();
172 cg.writeEntry("colour_ram_buffers", this->m_ramBuffersColour.name());
174 if(uiColours.kcbRamUser->color() != this->m_ramUsedColour){
175 this->m_ramUsedColour = uiColours.kcbRamUser->color();
176 cg.writeEntry("colour_ram_used", this->m_ramUsedColour.name());
178 if(uiColours.kcbSwap->color() != this->m_swapUsedColour){
179 this->m_swapUsedColour = uiColours.kcbSwap->color();
180 cg.writeEntry("colour_swap_used", this->m_swapUsedColour.name());
183 emit configNeedsSaving();
184 update();
188 void systemMonitor::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data){
190 if(source == "0" || source == "Average CPU Usage"){
192 m_user = data["User"].toDouble();
193 m_sys = data["Sys"].toDouble();
194 m_nice = data["Nice"].toDouble();
195 m_idle = data["Idle"].toDouble();
196 m_disk = data["Disk"].toDouble();
198 }else if(source == "RAM"){
200 m_ramfree = data["Free"].toDouble();
201 m_ramused = data["Used"].toDouble();
202 m_rambuffers = data["Buffers"].toDouble();
203 m_ramcached = data["Cached"].toDouble();
205 }else if(source == "Swap"){
207 m_swapfree = data["Free"].toDouble();
208 m_swapused = data["Used"].toDouble();
213 update();
217 void systemMonitor::paintCPUUsage(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect& contentsRect){
219 Q_UNUSED(option)
221 p->save();
223 p->translate(0, contentsRect.height() * m_idle);
226 p->setBrush(this->m_cpuUserColour);
227 p->setPen(this->m_cpuUserColour);
229 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_user));
230 p->translate(0, contentsRect.height() * m_user);
233 p->setBrush(this->m_cpuNiceColour);
234 p->setPen(this->m_cpuNiceColour);
236 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_nice));
237 p->translate(0, contentsRect.height() * m_nice);
240 p->setBrush(this->m_cpuDiskColour);
241 p->setPen(this->m_cpuDiskColour);
243 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_disk));
244 p->translate(0, contentsRect.height() * m_disk);
247 p->setBrush(this->m_cpuSysColour);
248 p->setPen(this->m_cpuSysColour);
250 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_sys));
252 p->restore();
256 void systemMonitor::paintSwapUsage(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect& contentsRect){
258 Q_UNUSED(option)
260 p->save();
262 p->translate(0, contentsRect.height() * m_swapfree);
265 p->setBrush(this->m_swapUsedColour);
266 p->setPen(this->m_swapUsedColour);
268 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_swapused));
270 p->restore();
274 void systemMonitor::paintRAMUsage(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect& contentsRect){
276 Q_UNUSED(option)
278 p->save();
280 p->translate(0, contentsRect.height() * m_ramfree);
283 p->setBrush(this->m_ramCachedColour);
284 p->setPen(this->m_ramCachedColour);
286 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_ramcached));
287 p->translate(0, contentsRect.height() * m_ramcached);
290 p->setBrush(this->m_ramBuffersColour);
291 p->setPen(this->m_ramBuffersColour);
293 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_rambuffers));
294 p->translate(0, contentsRect.height() * m_rambuffers);
297 p->setBrush(this->m_ramUsedColour);
298 p->setPen(this->m_ramUsedColour);
300 p->drawRect(QRectF(contentsRect.left(), contentsRect.top(), contentsRect.width(), contentsRect.height() * m_ramused));
302 p->restore();
306 void systemMonitor::paintFrontGlass(QPainter* p, const QStyleOptionGraphicsItem *option, const QRect& contentsRect) {
308 Q_UNUSED(option);
310 QLinearGradient grad(0,contentsRect.top(),0,contentsRect.bottom());
311 grad.setColorAt(0,QColor(255,255,255, 30));
312 grad.setColorAt(1,QColor( 0, 0, 0,150));
313 p->setBrush(grad);
314 p->setPen(QColor(0,0,0,100));
315 p->drawRect(contentsRect);
319 void systemMonitor::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect& contentsRect){
321 p->setRenderHint(QPainter::SmoothPixmapTransform);
322 p->setRenderHint(QPainter::Antialiasing);
324 QRect rotatedContentsRect(contentsRect);
327 if(!this->m_isVertical && (this->formFactor() == Plasma::Horizontal || this->formFactor() == Plasma::Vertical) ){
328 p->setWorldTransform(QTransform().rotate(90).translate(0, -(contentsRect.width() + contentsRect.x() * 2)));
329 rotatedContentsRect.setWidth(contentsRect.height());
330 rotatedContentsRect.setHeight(contentsRect.width());
333 int origWidth = rotatedContentsRect.width();
335 if(m_useOxygen){
336 rotatedContentsRect.setWidth(rotatedContentsRect.width() * 0.325);
337 rotatedContentsRect.moveLeft(rotatedContentsRect.left() + origWidth * 0.009);
338 }else{
339 rotatedContentsRect.setWidth(rotatedContentsRect.width() * 0.33);
342 paintCPUUsage(p, option, rotatedContentsRect);
343 if(m_useOxygen){
344 paintFrontGlass(p, option, rotatedContentsRect);
346 rotatedContentsRect.moveLeft(rotatedContentsRect.left() + 0.33*origWidth);
348 paintRAMUsage(p, option, rotatedContentsRect);
349 if(m_useOxygen){
350 paintFrontGlass(p, option, rotatedContentsRect);
352 rotatedContentsRect.moveLeft(rotatedContentsRect.left() + 0.33*origWidth);
354 paintSwapUsage(p, option, rotatedContentsRect);
355 if(m_useOxygen){
356 paintFrontGlass(p, option, rotatedContentsRect);
362 void systemMonitor::readConfig(){
364 KConfigGroup cg = config();
366 this->m_isVertical = cg.readEntry("vertical", true);
367 this->m_useOxygen = cg.readEntry("use_oxygen", false);
369 this->m_cpuUserColour = QColor( cg.readEntry("colour_cpu_user", QString("#0000FF")) );
370 this->m_cpuNiceColour = QColor( cg.readEntry("colour_cpu_nice", QString("#FFFF00")) );
371 this->m_cpuDiskColour = QColor( cg.readEntry("colour_cpu_disk", QString("#006400")) );
372 this->m_cpuSysColour = QColor( cg.readEntry("colour_cpu_sys", QString("#FF0000")) );
373 this->m_ramCachedColour = QColor( cg.readEntry("colour_ram_cached", QString("#007800")) );
374 this->m_ramBuffersColour = QColor( cg.readEntry("colour_ram_buffers", QString("#FFFF00")) );
375 this->m_ramUsedColour = QColor( cg.readEntry("colour_ram_used", QString("#0000B1")) );
376 this->m_swapUsedColour = QColor( cg.readEntry("colour_swap_used", QString("#00CDCD")) );
378 this->setBackgroundHints(QFlag(cg.readEntry("background", (int)(TranslucentBackground))));
383 #include "system_monitor.moc"