summaryrefslogtreecommitdiffstats
path: root/kcpuload/kcpuload/statpopup.cpp
blob: ae6cccd5c3a4675fa6dfc452cef1ef6554e77b24 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

/***************************************************************************
 *                                                                         *
 *   KCPULoad and KNetLoad are copyright (c) 1999-2000, Markus Gustavsson  *
 *                                       (c) 2002, Ben Burton              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "icontoggleaction.h"
#include "speeddialog.h"
#include "statdock.h"
#include "statpopup.h"

#include <tqpainter.h>
#include <tqtimer.h>
#include <kaction.h>
#include <kcolordialog.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <khelpmenu.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <twin.h>
#include <cstdlib>

#define DEFAULT_SPEED 1000
#define TEXT_EXPANSION_HORIZONTAL 10
#define TEXT_EXPANSION_VERTICAL 3

const TQColor StatPopup::colorBorder(0, 0, 0);

StatPopup::Reading::Reading() :
        dock(0),
        actColor(0)
{ }

StatPopup::Reading::~Reading()
{
    delete actColor;
    delete dock;
}

void StatPopup::Reading::Init(int which, StatPopup* popup)
{
    char colorid[30];
    sprintf(colorid, "Color%d", which);

    color = popup->defaultDockColor(which);
    color = popup->config->readColorEntry(colorid, &color);
    actColor = new KAction(i18n("Color (%1)...").arg(popup->dockName(which)),
        "color", 0, TQT_TQOBJECT(popup), TQT_SLOT(selectColor()), popup->coll, colorid);
}


StatPopup::StatPopup(bool useSupportSplit, TQWidget *parent, const char *name) :
        TQWidget(parent, name, TQt::WStyle_Customize | TQt::WStyle_NoBorder |
        TQt::WStyle_StaysOnTop | TQt::WDestructiveClose | TQt::WType_TopLevel),
        supportSplit(useSupportSplit) {
    // Window management.
    KWin::setState(winId(), NET::SkipTaskbar | NET::SkipPager);

    // TODO: Get all-desktops working, even after a hide/reshow.
    //KWin::setOnAllDesktops(winId(), true);

    // Basic variable initialisation.
    relX = relY = 0;
    isDragged = closing = false;

    // Initialise the text contents of this pop-up.
    fullReading = i18n("Inactive.");
    resizeToText();

    // Prepare for actions and the config file, but don't actually add
    // any actions or read the configuration.
    config = TDEGlobal::config();
    coll = new KActionCollection(this);

    // Set up a timer for our periodic updates.
    timer = new TQTimer(this);
    connect(timer, TQT_SIGNAL(timeout()), TQT_TQOBJECT(this), TQT_SLOT(takeReading()));
}

StatPopup::~StatPopup() {
}

void StatPopup::readPopupState() {
    config->setGroup("Popup Options");

    // Read the position.
    int useX = config->readNumEntry("PopupX", 0);
    int useY = config->readNumEntry("PopupY", 0);
    if (useX || useY)
        move(useX, useY);

    // Show the pop-up if appropriate.
    if (config->readBoolEntry("PopupActive", false))
        show();
    else
        hide();
}

void StatPopup::savePopupState() {
    config->setGroup("Popup Options");
    config->writeEntry("PopupActive", isVisible());
    config->writeEntry("PopupX", x());
    config->writeEntry("PopupY", y());
    config->sync();
}

void StatPopup::initDock(StatDock* target, KPopupMenu* menu, int whichDock) {
    // Initialise the menus.
    actActive->plug(menu);
    actClearHistory->plug(menu);
    menu->insertSeparator();
    actSpeed->plug(menu);
    if (supportSplit)
        actSplit->plug(menu);
    menu->insertSeparator();

    insertCustomItems(menu);

    KPopupMenu* fillMenu = new KPopupMenu(menu);
    actFillLines->plug(fillMenu);
    actFillBars->plug(fillMenu);
    actFillShaded->plug(fillMenu);
    menu->insertItem(SmallIcon("style"), i18n("St&yle"), fillMenu);

    actSoft->plug(menu);
    actLabelled->plug(menu);
    actGrid->plug(menu);
    r[whichDock].actColor->plug(menu);
    menu->insertSeparator();
    menu->insertItem(SmallIcon("help"), i18n("&Help"),
        (new KHelpMenu(0, TDEGlobal::instance()->aboutData(), false))->menu());

    // Set up display properties for the dock.
    target->setActive(actActive->isChecked());
    target->setSplit(isSplit());
    target->setFill(fillStyle);
    target->setSoft(actSoft->isChecked());
    target->setLabelled(actLabelled->isChecked());
    target->setGrid(actGrid->isChecked());
    target->setColor(r[whichDock].color);
    setCustomProperties(target);
}

bool StatPopup::isActive() const {
    return actActive->isChecked();
}

bool StatPopup::isSplit() const {
    return (supportSplit ? actSplit->isChecked() : false);
}

void StatPopup::setActive(bool set) {
    if (set)
        startUpdates();
    else
        stopUpdates();

    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setActive(set);
    }

    config->setGroup("General Options");
    config->writeEntry("Active", set);
    config->sync();
}

void StatPopup::clearHistory() {
    for (int i = 0; i < r.size(); i++) {
        r[i].dock->clearHistory();
    }
}

void StatPopup::selectSpeed() {
    SpeedDialog dlg(speed, firstDock());
    if (dlg.exec()) {
        int newSpeed = dlg.getSpeed();
        if (newSpeed > 0) {
            speed = newSpeed;
            if (timer->isActive())
                timer->changeInterval(speed);

            config->setGroup("General Options");
            config->writeEntry("Speed", speed);
            config->sync();
        }
    }
}

void StatPopup::setSplit(bool set) {
    if (! supportSplit)
        return;

    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setSplit(set);
    }

    requestResize();

    config->setGroup("General Options");
    config->writeEntry("Split", set);
    config->sync();
}

void StatPopup::setFillLines() {
    fillStyle = StatDock::fillLines;

    actFillLines->setChecked(true);
    actFillBars->setChecked(false);
    actFillShaded->setChecked(false);

    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setFill(StatDock::fillLines);
    }

    config->setGroup("General Options");
    config->writeEntry("StyleID", StatDock::fillLines);
    config->sync();
}

void StatPopup::setFillBars() {
    fillStyle = StatDock::fillBars;

    actFillLines->setChecked(false);
    actFillBars->setChecked(true);
    actFillShaded->setChecked(false);

    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setFill(StatDock::fillBars);
    }

    config->setGroup("General Options");
    config->writeEntry("StyleID", StatDock::fillBars);
    config->sync();
}

void StatPopup::setFillShaded() {
    fillStyle = StatDock::fillShaded;

    actFillLines->setChecked(false);
    actFillBars->setChecked(false);
    actFillShaded->setChecked(true);

    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setFill(StatDock::fillShaded);
    }

    config->setGroup("General Options");
    config->writeEntry("StyleID", StatDock::fillShaded);
    config->sync();
}

void StatPopup::setSoft(bool set) {
    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setSoft(set);
    }

    config->setGroup("General Options");
    config->writeEntry("Soft", set);
    config->sync();
}

void StatPopup::setLabelled(bool set) {
    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setLabelled(set);
    }

    config->setGroup("General Options");
    config->writeEntry("Labelled", set);
    config->sync();
}

void StatPopup::setGrid(bool set) {
    for (int i = 0; i < r.size(); i++) {
        r[i].dock->setGrid(set);
    }

    config->setGroup("General Options");
    config->writeEntry("Grid", set);
    config->sync();
}

void StatPopup::selectColor() {
    // which color?
    int whichDock;
    if (sscanf(TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name(), "Color%d", &whichDock) != 1)
        return;
    if (whichDock < 0 || whichDock >= r.size())
        return;

    if (r[whichDock].dock) {
        TQColor ans;
        if (KColorDialog::getColor(ans, r[whichDock].color, firstDock()) ==
                TQDialog::Accepted) {
            r[whichDock].color = ans;
            r[whichDock].dock->setColor(ans);

            config->setGroup("General Options");
            TQString n;
            n.sprintf("Color%d", whichDock);
            config->writeEntry(n, ans);
            config->sync();
        }
    }
}

StatDock* StatPopup::firstDock() {
    return r.size() > 0 ? r[0].dock : 0;
}

void StatPopup::startUpdates() {
    takeReading();
    timer->start(speed);
}

void StatPopup::stopUpdates() {
    timer->stop();
    fullReading = i18n("Inactive.");
}

void StatPopup::setupActions() {
    config->setGroup("General Options");
    bool bVal;

    bVal = config->readBoolEntry("Active", true);
    actActive = new KToggleAction(i18n("&Active"), 0, coll, "active");
    actActive->setChecked(bVal);
    connect(actActive, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setActive(bool)));

    actClearHistory = new KAction(i18n("&Clear"), "editdelete", 0,
        TQT_TQOBJECT(this), TQT_SLOT(clearHistory()), coll, "clear");

    speed = config->readNumEntry("Speed", DEFAULT_SPEED);
    actSpeed = new KAction(i18n("&Speed..."), "speedarrow", 0,
        TQT_TQOBJECT(this), TQT_SLOT(selectSpeed()), coll, "speed");

    if (supportSplit) {
        bVal = config->readBoolEntry("Split", true);
        actSplit = new IconToggleAction(i18n("Sp&lit Graph"), "split",
            i18n("Graph Sp&litting Enabled"), "spliton", 0, coll, "split");
        actSplit->setChecked(bVal);
        connect(actSplit, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setSplit(bool)));
    }

    fillStyle = config->readNumEntry("StyleID", StatDock::fillShaded);
    actFillLines = new IconToggleAction(i18n("&Lines"), "lines", "lineson", 0,
        TQT_TQOBJECT(this), TQT_SLOT(setFillLines()), coll, "filllines");
    actFillLines->setChecked(fillStyle == StatDock::fillLines);
    actFillBars = new IconToggleAction(i18n("&Bars"), "bars", "barson", 0,
        TQT_TQOBJECT(this), TQT_SLOT(setFillBars()), coll, "fillbars");
    actFillBars->setChecked(fillStyle == StatDock::fillBars);
    actFillShaded = new IconToggleAction(i18n("&Shaded"), "shaded", "shadedon",
        0, TQT_TQOBJECT(this), TQT_SLOT(setFillShaded()), coll, "fillshaded");
    actFillShaded->setChecked(fillStyle == StatDock::fillShaded);

    bVal = config->readBoolEntry("Soft", false);
    actSoft = new IconToggleAction(i18n("So&ft Curves"), "soft",
        i18n("So&ft Curves Enabled"), "softon", 0, coll, "soft");
    actSoft->setChecked(bVal);
    connect(actSoft, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setSoft(bool)));

    bVal = config->readBoolEntry("Labelled", true);
    actLabelled= new IconToggleAction(i18n("Show &Labels"), "labels",
        i18n("&Labels Enabled"), "labelson", 0, coll, "labelled");
    actLabelled->setChecked(bVal);
    connect(actLabelled, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setLabelled(bool)));

    bVal = config->readBoolEntry("Grid", true);
    actGrid = new IconToggleAction(i18n("Show &Grid"), "grid",
        i18n("&Grid Enabled"), "gridon", 0, coll, "grid");
    actGrid->setChecked(bVal);
    connect(actGrid, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setGrid(bool)));

    setupCustomActions();
}

void StatPopup::paintEvent(TQPaintEvent*) {
    TQPainter p(this);

    // Draw the border.
    p.setPen(colorBorder);
    p.drawLine(0, 0, width(), 0);
    p.drawLine(0, 1, width(), 1);
    p.drawLine(0, 2, 0, height());
    p.drawLine(1, 2, 1, height());
    p.drawLine(width() - 2, 2, width() - 2, height());
    p.drawLine(width() - 1, 2, width() - 1, height());
    p.drawLine(2, height() - 2, width() - 2, height() - 2);
    p.drawLine(2, height() - 1, width() - 2, height() - 1);

    // Draw the text.
    p.setFont(font());
    p.setPen(colorGroup().foreground());
    p.drawText(rect(), AlignHCenter | AlignVCenter, fullReading);
}

void StatPopup::mousePressEvent(TQMouseEvent* e) {
    if(e->button() == Qt::RightButton) {
        // Hide the pop-up.
        hide();
    } else {
        // Begin a drag operation.
        isDragged = true;
        relX = e->x();
        relY = e->y();
        repaint();
    }
}

void StatPopup::mouseMoveEvent(TQMouseEvent* e) {
    // In the middle of a drag operation.
    move(e->globalX() - relX, e->globalY() - relY);
}

void StatPopup::mouseReleaseEvent(TQMouseEvent* e) {
    // The end of a drag operation.
    move(e->globalX() - relX, e->globalY() - relY);
    isDragged = false;
    repaint();
}

void StatPopup::closeEvent(TQCloseEvent* e) {
    // We're about to close.  Save the current state for the last time.
    savePopupState();
    closing = true;
    TQWidget::closeEvent(e);
}

void StatPopup::hideEvent(TQHideEvent* e) {
    // We're about to hide.  Save the current state if we're not
    // closing altogether.
    if (! closing)
        savePopupState();
    TQWidget::hideEvent(e);
}

void StatPopup::showEvent(TQShowEvent* e) {
    // Make sure we're up-to-date and properly resized.
    if (isActive())
        takeReading();
    else
        resizeToText();

    // Window management - fix so a taskbar button doesn't appear
    KWin::setState(winId(), NET::SkipTaskbar | NET::SkipPager);

    TQWidget::showEvent(e);
}

void StatPopup::takeReading() {
    takeReadingInternal();

    for (int i = 0; i < r.size(); i++) {
        r[i].dock->addPercentReading(r[i].upper, r[i].lower);
    }

    if (isVisible()) {
        // Only resize if the pop-up is visible; otherwise fullReading
        // might be stale anyway so we'll do it when we show the pop-up.
        if (resizeRequested)
            resizeToText();

        repaint();
    }
}

void StatPopup::resizeToText() {
    resizeRequested = false;

    TQSize size = fontMetrics().size(0, fullReading);
    resize(size.width() + 2 * TEXT_EXPANSION_HORIZONTAL,
        size.height() + 2 * TEXT_EXPANSION_VERTICAL);
    repaint();
}

#include "statpopup.moc"