1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2  * (c)LGPL2+
3  *
4  * LXQt - a lightweight, Qt based, desktop toolset
5  * https://lxqt.org
6  *
7  * Copyright: 2012 Razor team
8  * Authors:
9  *   Alexander Sokoloff <sokoloff.a@gmail.com>
10  *
11  * This program or library is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20 
21  * You should have received a copy of the GNU Lesser General
22  * Public License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301 USA
25  *
26  * END_COMMON_COPYRIGHT_HEADER */
27 
28 #include "lxqtcpuload.h"
29 #include "../panel/ilxqtpanelplugin.h"
30 #include "../panel/pluginsettings.h"
31 #include <QtCore>
32 #include <QPainter>
33 #include <QLinearGradient>
34 #include <QHBoxLayout>
35 
36 extern "C" {
37 #include <statgrab.h>
38 }
39 
40 #ifdef __sg_public
41 // since libstatgrab 0.90 this macro is defined, so we use it for version check
42 #define STATGRAB_NEWER_THAN_0_90 	1
43 #endif
44 
45 #define BAR_ORIENT_BOTTOMUP "bottomUp"
46 #define BAR_ORIENT_TOPDOWN "topDown"
47 #define BAR_ORIENT_LEFTRIGHT "leftRight"
48 #define BAR_ORIENT_RIGHTLEFT "rightLeft"
49 
50 
LXQtCpuLoad(ILXQtPanelPlugin * plugin,QWidget * parent)51 LXQtCpuLoad::LXQtCpuLoad(ILXQtPanelPlugin* plugin, QWidget* parent):
52     QFrame(parent),
53     mPlugin(plugin),
54     m_avg(0),
55     m_showText(false),
56     m_barWidth(20),
57     m_barOrientation(TopDownBar),
58     m_timerID(-1)
59 {
60     setObjectName(QStringLiteral("LXQtCpuLoad"));
61 
62     QHBoxLayout *layout = new QHBoxLayout(this);
63     layout->setSpacing(0);
64     layout->setContentsMargins(0, 0, 0, 0);
65     layout->addWidget(&m_stuff);
66 
67     /* Initialise statgrab */
68 #ifdef STATGRAB_NEWER_THAN_0_90
69     sg_init(0);
70 #else
71     sg_init();
72 #endif
73 
74     /* Drop setuid/setgid privileges. */
75     if (sg_drop_privileges() != 0) {
76         perror("Error. Failed to drop privileges");
77     }
78 
79     m_font.setPointSizeF(8);
80 
81     settingsChanged();
82 }
83 
~LXQtCpuLoad()84 LXQtCpuLoad::~LXQtCpuLoad()
85 {
86   sg_shutdown();
87 }
88 
setSizes()89 void LXQtCpuLoad::setSizes()
90 {
91     if (m_barOrientation == RightToLeftBar || m_barOrientation == LeftToRightBar)
92     {
93         m_stuff.setFixedHeight(m_barWidth);
94         m_stuff.setMinimumWidth(24);
95     }
96     else
97     {
98         m_stuff.setFixedWidth(m_barWidth);
99         m_stuff.setMinimumHeight(24);
100     }
101 }
102 
resizeEvent(QResizeEvent *)103 void LXQtCpuLoad::resizeEvent(QResizeEvent *)
104 {
105     setSizes();
106     update();
107 }
108 
109 
getLoadCpu() const110 double LXQtCpuLoad::getLoadCpu() const
111 {
112 #ifdef STATGRAB_NEWER_THAN_0_90
113     size_t count;
114     sg_cpu_percents* cur = sg_get_cpu_percents(&count);
115 #else
116     sg_cpu_percents* cur = sg_get_cpu_percents();
117 #endif
118     return (cur->user + cur->kernel + cur->nice);
119 }
120 
timerEvent(QTimerEvent *)121 void LXQtCpuLoad::timerEvent(QTimerEvent * /*event*/)
122 {
123     double avg = getLoadCpu();
124     if ( qAbs(m_avg-avg)>1 )
125     {
126         m_avg = avg;
127         setToolTip(tr("CPU load %1%").arg(m_avg));
128         update();
129     }
130 }
131 
paintEvent(QPaintEvent *)132 void LXQtCpuLoad::paintEvent ( QPaintEvent * )
133 {
134     QPainter p(this);
135     QPen pen;
136     pen.setWidth(2);
137     p.setPen(pen);
138     p.setRenderHint(QPainter::Antialiasing, true);
139 
140     p.setFont(m_font);
141     QRectF r = rect();
142 
143     QRectF r1;
144     QLinearGradient shade(0,0,1,1);
145     if (m_barOrientation == RightToLeftBar || m_barOrientation == LeftToRightBar)
146     {
147         float vo = (r.height() - static_cast<double>(m_barWidth))/2.0;
148         float ho = r.width()*(1-m_avg*0.01);
149 
150         if (m_barOrientation == RightToLeftBar)
151         {
152             r1.setRect(r.left()+ho, r.top()+vo, r.width()-ho, r.height()-2*vo );
153         }
154         else // LeftToRightBar
155         {
156             r1.setRect(r.left(), r.top()+vo, r.width()-ho, r.height()-2*vo );
157         }
158         shade.setFinalStop(0, r1.height());
159     }
160     else // BottomUpBar || TopDownBar
161     {
162         float vo = r.height()*(1-m_avg*0.01);
163         float ho = (r.width() - static_cast<double>(m_barWidth) )/2.0;
164 
165         if (m_barOrientation == TopDownBar)
166         {
167             r1.setRect(r.left()+ho, r.top(), r.width()-2*ho, r.height()-vo );
168         }
169         else // BottomUpBar
170         {
171             r1.setRect(r.left()+ho, r.top()+vo, r.width()-2*ho, r.height()-vo );
172         }
173         shade.setFinalStop(r1.width(), 0);
174     }
175 
176     shade.setSpread(QLinearGradient::ReflectSpread);
177     shade.setColorAt(0, QColor(0, 196, 0, 128));
178     shade.setColorAt(0.5, QColor(0, 128, 0, 255) );
179     shade.setColorAt(1, QColor(0, 196, 0 , 128));
180 
181     p.fillRect(r1, shade);
182 
183     if (m_showText)
184     {
185         p.setPen(fontColor);
186         p.drawText(rect(), Qt::AlignCenter, QString::number(m_avg));
187     }
188 }
189 
190 
settingsChanged()191 void LXQtCpuLoad::settingsChanged()
192 {
193     if (m_timerID != -1)
194         killTimer(m_timerID);
195 
196     m_showText = mPlugin->settings()->value(QStringLiteral("showText"), false).toBool();
197     m_barWidth = mPlugin->settings()->value(QStringLiteral("barWidth"), 20).toInt();
198     m_updateInterval = mPlugin->settings()->value(QStringLiteral("updateInterval"), 1000).toInt();
199 
200     QString barOrientation = mPlugin->settings()->value(QStringLiteral("barOrientation"), QStringLiteral(BAR_ORIENT_BOTTOMUP)).toString();
201     if (barOrientation == QLatin1String(BAR_ORIENT_RIGHTLEFT))
202         m_barOrientation = RightToLeftBar;
203     else if (barOrientation == QLatin1String(BAR_ORIENT_LEFTRIGHT))
204         m_barOrientation = LeftToRightBar;
205     else if (barOrientation == QLatin1String(BAR_ORIENT_TOPDOWN))
206         m_barOrientation = TopDownBar;
207     else
208         m_barOrientation = BottomUpBar;
209 
210     m_timerID = startTimer(m_updateInterval);
211     setSizes();
212     update();
213 }
214