File: lxqtcpuload.cpp

package info (click to toggle)
lxqt-panel 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,680 kB
  • sloc: cpp: 30,052; xml: 1,152; makefile: 19
file content (212 lines) | stat: -rw-r--r-- 5,788 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2012 Razor team
 * Authors:
 *   Alexander Sokoloff <sokoloff.a@gmail.com>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "lxqtcpuload.h"
#include "../panel/ilxqtpanelplugin.h"
#include "../panel/pluginsettings.h"
#include <QPainter>
#include <QLinearGradient>
#include <QHBoxLayout>

extern "C" {
#include <statgrab.h>
}

#ifdef __sg_public
// since libstatgrab 0.90 this macro is defined, so we use it for version check
#define STATGRAB_NEWER_THAN_0_90 	1
#endif

#define BAR_ORIENT_BOTTOMUP "bottomUp"
#define BAR_ORIENT_TOPDOWN "topDown"
#define BAR_ORIENT_LEFTRIGHT "leftRight"
#define BAR_ORIENT_RIGHTLEFT "rightLeft"


LXQtCpuLoad::LXQtCpuLoad(ILXQtPanelPlugin* plugin, QWidget* parent):
    QFrame(parent),
    mPlugin(plugin),
    m_avg(0),
    m_showText(false),
    m_barWidth(20),
    m_barOrientation(TopDownBar),
    m_timerID(-1)
{
    setObjectName(QStringLiteral("LXQtCpuLoad"));

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(&m_stuff);

    /* Initialise statgrab */
#ifdef STATGRAB_NEWER_THAN_0_90
    sg_init(0);
#else
    sg_init();
#endif

    /* Drop setuid/setgid privileges. */
    if (sg_drop_privileges() != 0) {
        perror("Error. Failed to drop privileges");
    }

    m_font.setPointSizeF(8);

    settingsChanged();
}

LXQtCpuLoad::~LXQtCpuLoad()
{
  sg_shutdown();
}

void LXQtCpuLoad::setSizes()
{
    if (m_barOrientation == RightToLeftBar || m_barOrientation == LeftToRightBar)
    {
        m_stuff.setFixedHeight(m_barWidth);
        m_stuff.setMinimumWidth(24);
    }
    else
    {
        m_stuff.setFixedWidth(m_barWidth);
        m_stuff.setMinimumHeight(24);
    }
}

void LXQtCpuLoad::resizeEvent(QResizeEvent *)
{
    setSizes();
    update();
}


double LXQtCpuLoad::getLoadCpu() const
{
#ifdef STATGRAB_NEWER_THAN_0_90
    size_t count;
    sg_cpu_percents* cur = sg_get_cpu_percents(&count);
#else
    sg_cpu_percents* cur = sg_get_cpu_percents();
#endif
    return (cur->user + cur->kernel + cur->nice);
}

void LXQtCpuLoad::timerEvent(QTimerEvent * /*event*/)
{
    double avg = getLoadCpu();
    if ( qAbs(m_avg-avg)>1 )
    {
        m_avg = avg;
        setToolTip(tr("CPU load %1%").arg(m_avg));
        update();
    }
}

void LXQtCpuLoad::paintEvent ( QPaintEvent * )
{
    QPainter p(this);
    QPen pen;
    pen.setWidth(2);
    p.setPen(pen);
    p.setRenderHint(QPainter::Antialiasing, true);

    p.setFont(m_font);
    QRectF r = rect();

    QRectF r1;
    QLinearGradient shade(0, 0, 1, 1);
    if (m_barOrientation == RightToLeftBar || m_barOrientation == LeftToRightBar)
    {
        float vo = (r.height() - static_cast<double>(m_barWidth))/2.0;
        float ho = r.width()*(1-m_avg*0.01);

        if (m_barOrientation == RightToLeftBar)
        {
            r1.setRect(r.left()+ho, r.top()+vo, r.width()-ho, r.height()-2*vo );
        }
        else // LeftToRightBar
        {
            r1.setRect(r.left(), r.top()+vo, r.width()-ho, r.height()-2*vo );
        }
        shade.setFinalStop(0, r1.height());
    }
    else // BottomUpBar || TopDownBar
    {
        float vo = r.height()*(1-m_avg*0.01);
        float ho = (r.width() - static_cast<double>(m_barWidth) )/2.0;

        if (m_barOrientation == TopDownBar)
        {
            r1.setRect(r.left()+ho, r.top(), r.width()-2*ho, r.height()-vo );
        }
        else // BottomUpBar
        {
            r1.setRect(r.left()+ho, r.top()+vo, r.width()-2*ho, r.height()-vo );
        }
        shade.setFinalStop(r1.width(), 0);
    }

    shade.setSpread(QLinearGradient::ReflectSpread);
    shade.setColorAt(0, QColor(0, 196, 0, 128));
    shade.setColorAt(0.5, QColor(0, 128, 0, 255) );
    shade.setColorAt(1, QColor(0, 196, 0 , 128));

    p.fillRect(r1, shade);

    if (m_showText)
    {
        p.setPen(fontColor);
        p.drawText(rect(), Qt::AlignCenter, QString::number(m_avg));
    }
}


void LXQtCpuLoad::settingsChanged()
{
    if (m_timerID != -1)
        killTimer(m_timerID);

    m_showText = mPlugin->settings()->value(QStringLiteral("showText"), false).toBool();
    m_barWidth = mPlugin->settings()->value(QStringLiteral("barWidth"), 20).toInt();
    m_updateInterval = mPlugin->settings()->value(QStringLiteral("updateInterval"), 1000).toInt();

    QString barOrientation = mPlugin->settings()->value(QStringLiteral("barOrientation"), QStringLiteral(BAR_ORIENT_BOTTOMUP)).toString();
    if (barOrientation == QLatin1String(BAR_ORIENT_RIGHTLEFT))
        m_barOrientation = RightToLeftBar;
    else if (barOrientation == QLatin1String(BAR_ORIENT_LEFTRIGHT))
        m_barOrientation = LeftToRightBar;
    else if (barOrientation == QLatin1String(BAR_ORIENT_TOPDOWN))
        m_barOrientation = TopDownBar;
    else
        m_barOrientation = BottomUpBar;

    m_timerID = startTimer(m_updateInterval);
    setSizes();
    update();
}