summaryrefslogtreecommitdiffstats
path: root/knetload/statpopup.h
blob: f98d48b48f97dce12480ce474ff051fa9dc7f51a (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

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef __STATPOPUP_H
#define __STATPOPUP_H

#include <qwidget.h>

class KAction;
class KActionCollection;
class KConfig;
class KPopupMenu;
class KRadioAction;
class KSelectAction;
class KToggleAction;
class QTimer;
class StatDock;

/**
 * A small pop-up window that contains a continually updating display of
 * statistics.
 *
 * This pop-up will be sticky and stay-on-top by default.
 *
 * Up to two windows docked in the system tray (of class StatDock) can
 * be maintained by this pop-up.  There should only be one StatPopup
 * per application.
 *
 * Note that initDock() and cleanUpDock() \e must be called when a
 * system tray window is created or closed respectively, though
 * initDock() is generally called automatically from the StatDock
 * constructor.
 *
 * Note also that the constructor for this class does \e not call
 * setupActions(), readPopupState() or startUpdates(); these must all be
 * called by other routines (such as subclass constructors), since they
 * require either subclass initialisation or an existing system tray
 * window.
 */
class StatPopup : public QWidget {
    Q_OBJECT

public:
    /**
     * Colour constants.
     */
    static const QColor colorBorder;

public:
    /**
     * Constructors and destructors.
     *
     * This constructor will set the config variable but will not read
     * any configuration information.  See the general class notes also
     * for a list of other routines this constructor will not call.
     */
    StatPopup(bool useSupportSplit, QWidget *parent = 0, const char *name = 0);
    ~StatPopup();

    /**
     * Geometric pop-up state management.
     */
    void readPopupState();
    void savePopupState();

    /**
     * Initialises the given system tray window and its context menu.
     *
     * All global application properties (i.e., properties held by this
     * pop-up) will be propagated to the system tray window, and all
     * global application actions will be added to the given menu.
     *
     * Parameter whichDock must be either 0 or 1 to specify whether the
     * given system tray window will become window dock[0] or dock[1].
     *
     * This routine \e must be called when a system tray window is first
     * created, though in general this will be done automatically by the
     * StatDock constructor.
     */
    virtual void initDock(StatDock* target, KPopupMenu* menu, int whichDock);

    /**
     * Performs any cleaning up required before the given system tray
     * window is closed.
     *
     * This routine \e must be called when a system tray is about to be
     * closed.
     *
     * Parameter whichDock must be either 0 or 1 to specify whether
     * window dock[0] or dock[1] is to be closed.
     */
    virtual void cleanUpDock(int whichDock);

    /**
     * Is this application currently active (i.e., taking periodic
     * readings and displaying them)?
     */
    bool isActive() const;

    /**
     * Are split diagrams currently enabled?
     */
    bool isSplit() const;

    /**
     * Returns the frequency of updates in milliseconds.
     */
    int getSpeed() const;

public slots:
    /**
     * Slots for menu items.
     */
    void setActive(bool);
    void clearHistory();
    void selectSpeed();
    void setSplit(bool);
    void setFillLines();
    void setFillBars();
    void setFillShaded();
    void setSoft(bool);
    void setLabelled(bool);
    void setGrid(bool);
    void selectColor0();
    void selectColor1();

protected:
    /**
     * Return a human-readable name and default diagram colour for each
     * system tray window.
     * The argument given will be either 0 or 1, referring to window
     * dock[0] or dock[1] respectively.
     */
    virtual QString dockName(int which) const = 0;
    virtual QColor defaultDockColor(int which) const = 0;

    /**
     * Take a fresh reading.  Subclasses must override this routine.
     *
     * This routine must update the array upper.
     * If split readings are enabled, it must also update the array lower.
     * If this pop-up is visible, it must also update the string fullReading.
     */
    virtual void takeReadingInternal() = 0;

    /**
     * Sets up any actions specific to subclasses of StatPopup.
     * The global configuration should be read at this point to set the
     * initial states of these actions.
     *
     * This routine will be called during setupActions().
     */
    virtual void setupCustomActions() {}

    /**
     * Inserts any menu items specific to subclasses of StatPopup into
     * the given menu.  If any items are inserted, a separator should
     * be inserted after them.
     *
     * This routine will be called during initDock().
     */
    virtual void insertCustomItems(KPopupMenu*) {}

    /**
     * Propagates any properties specific to subclasses of StatPopup to
     * the given system tray window during its initialisation.
     *
     * This routine will be called during initDock().
     */
    virtual void setCustomProperties(StatDock*) {}

    /**
     * Returns the first non-null pointer out of dock[0] and dock[1], or
     * returns null if both these pointers are null.
     */
    StatDock* firstDock();

    /**
     * Start and stop the periodic taking of readings.
     */
    void startUpdates();
    void stopUpdates();

    /**
     * Set up the collection of appliation-wide actions.
     * The global configuration will be read at this point to set the
     * initial states of these actions.
     */
    virtual void setupActions();

    /**
     * Request that this pop-up be resized at the next reading according
     * to its text contents.  The pop-up will be resized \e after the
     * reading is taken.
     */
    void requestResize();

    /**
     * Custom painting routine.
     */
    void paintEvent(QPaintEvent *);

    /**
     * Overrides for dragging support.
     */
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);

    /**
     * Overrides for saving the popup state.
     */
    void closeEvent(QCloseEvent *);
    void hideEvent(QHideEvent *);
    void showEvent(QShowEvent *);

protected slots:
    /**
     * Take a fresh reading and update all visual elements accordingly.
     */
    void takeReading();

protected:
    /**
     * Configuration and GUI elements.
     */
    KConfig* config;
        /**< The configuration for the underlying application. */
    StatDock* dock[2];
        /**< The two system tray windows maintained by this pop-up.
             Either of these pointers may be null. */
    KActionCollection* coll;
        /**< The collection of all application-level actions. */

    /**
     * Last readings taken.
     */
    int upper[2];
        /**< The upper readings taken during the last reading,
             one for each system tray window. */
    int lower[2];
        /**< The lower readings taken during the last reading,
             one for each system tray window. */
    QString fullReading;
        /**< A full string describing the last reading taken,
             to be displayed in this pop-up. */

private:
    /**
     * Resize this pop-up according to its text contents (i.e., the
     * variable fullReading).  A little extra room will be added to
     * accomodate potential minor variations in the text.
     */
    void resizeToText();

private:
    /**
     * Update support.
     */
    QTimer* timer;
        /**< The timer handling periodic updates. */
    int speed;
        /**< The frequency of updates in milliseconds. */

    /**
     * Diagram support.
     */
    QColor color[2];
        /**< The colours used in the two system tray diagrams. */
    int fillStyle;
        /**< The fill style used in the system tray diagrams. */
    bool supportSplit;
        /**< Does this application support split diagrams? */

    /**
     * Pop-up support.
     */
    int relX;
        /**< The X coordinate of this pop-up at the beginning of a drag
             operation. */
    int relY;
        /**< The Y coordinate of this pop-up at the beginning of a drag
             operation. */
    bool isDragged;
        /**< Are we in the middle of a drag operation? */
    bool closing;
        /**< Have we received a close event? */
    bool resizeRequested;
        /**< Has a pop-up resize been requested? */

    /**
     * Actions
     */
    KToggleAction* actActive;
    KAction* actClearHistory;
    KAction* actSpeed;
    KToggleAction* actSplit;
    KToggleAction* actFillLines;
    KToggleAction* actFillBars;
    KToggleAction* actFillShaded;
    KToggleAction* actSoft;
    KToggleAction* actLabelled;
    KToggleAction* actGrid;
    KAction* actColor0;
    KAction* actColor1;
};

inline int StatPopup::getSpeed() const {
    return speed;
}

inline void StatPopup::requestResize() {
    resizeRequested = true;
}

#endif