You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
486 lines
14 KiB
486 lines
14 KiB
15 years ago
|
|
||
|
/***************************************************************************
|
||
|
* *
|
||
|
* 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"
|
||
|
|
||
14 years ago
|
#include <tqpainter.h>
|
||
|
#include <tqtimer.h>
|
||
12 years ago
|
#include <tdeaction.h>
|
||
15 years ago
|
#include <kcolordialog.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdeglobal.h>
|
||
15 years ago
|
#include <khelpmenu.h>
|
||
|
#include <kiconloader.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
12 years ago
|
#include <tdepopupmenu.h>
|
||
13 years ago
|
#include <twin.h>
|
||
15 years ago
|
#include <cstdlib>
|
||
|
|
||
|
#define DEFAULT_SPEED 1000
|
||
|
#define TEXT_EXPANSION_HORIZONTAL 10
|
||
|
#define TEXT_EXPANSION_VERTICAL 3
|
||
|
|
||
14 years ago
|
const TQColor StatPopup::colorBorder(0, 0, 0);
|
||
15 years ago
|
|
||
|
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);
|
||
12 years ago
|
actColor = new TDEAction(i18n("Color (%1)...").arg(popup->dockName(which)),
|
||
14 years ago
|
"color", 0, TQT_TQOBJECT(popup), TQT_SLOT(selectColor()), popup->coll, colorid);
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
13 years ago
|
StatPopup::StatPopup(bool useSupportSplit, TQWidget *parent, const char *name) :
|
||
|
TQWidget(parent, name, TQt::WStyle_Customize | TQt::WStyle_NoBorder |
|
||
14 years ago
|
TQt::WStyle_StaysOnTop | TQt::WDestructiveClose | TQt::WType_TopLevel),
|
||
15 years ago
|
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.
|
||
12 years ago
|
config = TDEGlobal::config();
|
||
12 years ago
|
coll = new TDEActionCollection(this);
|
||
15 years ago
|
|
||
|
// Set up a timer for our periodic updates.
|
||
14 years ago
|
timer = new TQTimer(this);
|
||
|
connect(timer, TQT_SIGNAL(timeout()), TQT_TQOBJECT(this), TQT_SLOT(takeReading()));
|
||
15 years ago
|
}
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
12 years ago
|
void StatPopup::initDock(StatDock* target, TDEPopupMenu* menu, int whichDock) {
|
||
15 years ago
|
// Initialise the menus.
|
||
|
actActive->plug(menu);
|
||
|
actClearHistory->plug(menu);
|
||
|
menu->insertSeparator();
|
||
|
actSpeed->plug(menu);
|
||
|
if (supportSplit)
|
||
|
actSplit->plug(menu);
|
||
|
menu->insertSeparator();
|
||
|
|
||
|
insertCustomItems(menu);
|
||
|
|
||
12 years ago
|
TDEPopupMenu* fillMenu = new TDEPopupMenu(menu);
|
||
15 years ago
|
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"),
|
||
12 years ago
|
(new KHelpMenu(0, TDEGlobal::instance()->aboutData(), false))->menu());
|
||
15 years ago
|
|
||
|
// 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;
|
||
14 years ago
|
if (sscanf(TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name(), "Color%d", &whichDock) != 1)
|
||
15 years ago
|
return;
|
||
|
if (whichDock < 0 || whichDock >= r.size())
|
||
|
return;
|
||
|
|
||
|
if (r[whichDock].dock) {
|
||
14 years ago
|
TQColor ans;
|
||
15 years ago
|
if (KColorDialog::getColor(ans, r[whichDock].color, firstDock()) ==
|
||
14 years ago
|
TQDialog::Accepted) {
|
||
15 years ago
|
r[whichDock].color = ans;
|
||
|
r[whichDock].dock->setColor(ans);
|
||
|
|
||
|
config->setGroup("General Options");
|
||
14 years ago
|
TQString n;
|
||
15 years ago
|
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);
|
||
12 years ago
|
actActive = new TDEToggleAction(i18n("&Active"), 0, coll, "active");
|
||
15 years ago
|
actActive->setChecked(bVal);
|
||
14 years ago
|
connect(actActive, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setActive(bool)));
|
||
15 years ago
|
|
||
10 years ago
|
actClearHistory = new TDEAction(i18n("&Clear"), "edit-delete", 0,
|
||
14 years ago
|
TQT_TQOBJECT(this), TQT_SLOT(clearHistory()), coll, "clear");
|
||
15 years ago
|
|
||
|
speed = config->readNumEntry("Speed", DEFAULT_SPEED);
|
||
12 years ago
|
actSpeed = new TDEAction(i18n("&Speed..."), "speedarrow", 0,
|
||
14 years ago
|
TQT_TQOBJECT(this), TQT_SLOT(selectSpeed()), coll, "speed");
|
||
15 years ago
|
|
||
|
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);
|
||
14 years ago
|
connect(actSplit, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setSplit(bool)));
|
||
15 years ago
|
}
|
||
|
|
||
|
fillStyle = config->readNumEntry("StyleID", StatDock::fillShaded);
|
||
|
actFillLines = new IconToggleAction(i18n("&Lines"), "lines", "lineson", 0,
|
||
14 years ago
|
TQT_TQOBJECT(this), TQT_SLOT(setFillLines()), coll, "filllines");
|
||
15 years ago
|
actFillLines->setChecked(fillStyle == StatDock::fillLines);
|
||
|
actFillBars = new IconToggleAction(i18n("&Bars"), "bars", "barson", 0,
|
||
14 years ago
|
TQT_TQOBJECT(this), TQT_SLOT(setFillBars()), coll, "fillbars");
|
||
15 years ago
|
actFillBars->setChecked(fillStyle == StatDock::fillBars);
|
||
|
actFillShaded = new IconToggleAction(i18n("&Shaded"), "shaded", "shadedon",
|
||
14 years ago
|
0, TQT_TQOBJECT(this), TQT_SLOT(setFillShaded()), coll, "fillshaded");
|
||
15 years ago
|
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);
|
||
14 years ago
|
connect(actSoft, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setSoft(bool)));
|
||
15 years ago
|
|
||
|
bVal = config->readBoolEntry("Labelled", true);
|
||
|
actLabelled= new IconToggleAction(i18n("Show &Labels"), "labels",
|
||
|
i18n("&Labels Enabled"), "labelson", 0, coll, "labelled");
|
||
|
actLabelled->setChecked(bVal);
|
||
14 years ago
|
connect(actLabelled, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setLabelled(bool)));
|
||
15 years ago
|
|
||
|
bVal = config->readBoolEntry("Grid", true);
|
||
|
actGrid = new IconToggleAction(i18n("Show &Grid"), "grid",
|
||
|
i18n("&Grid Enabled"), "gridon", 0, coll, "grid");
|
||
|
actGrid->setChecked(bVal);
|
||
14 years ago
|
connect(actGrid, TQT_SIGNAL(toggled(bool)), TQT_TQOBJECT(this), TQT_SLOT(setGrid(bool)));
|
||
15 years ago
|
|
||
|
setupCustomActions();
|
||
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::paintEvent(TQPaintEvent*) {
|
||
|
TQPainter p(this);
|
||
15 years ago
|
|
||
|
// 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());
|
||
13 years ago
|
p.setPen(colorGroup().foreground());
|
||
15 years ago
|
p.drawText(rect(), AlignHCenter | AlignVCenter, fullReading);
|
||
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::mousePressEvent(TQMouseEvent* e) {
|
||
|
if(e->button() == Qt::RightButton) {
|
||
15 years ago
|
// Hide the pop-up.
|
||
|
hide();
|
||
|
} else {
|
||
|
// Begin a drag operation.
|
||
|
isDragged = true;
|
||
|
relX = e->x();
|
||
|
relY = e->y();
|
||
13 years ago
|
repaint();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::mouseMoveEvent(TQMouseEvent* e) {
|
||
15 years ago
|
// In the middle of a drag operation.
|
||
|
move(e->globalX() - relX, e->globalY() - relY);
|
||
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::mouseReleaseEvent(TQMouseEvent* e) {
|
||
15 years ago
|
// The end of a drag operation.
|
||
|
move(e->globalX() - relX, e->globalY() - relY);
|
||
|
isDragged = false;
|
||
13 years ago
|
repaint();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::closeEvent(TQCloseEvent* e) {
|
||
15 years ago
|
// We're about to close. Save the current state for the last time.
|
||
|
savePopupState();
|
||
|
closing = true;
|
||
14 years ago
|
TQWidget::closeEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::hideEvent(TQHideEvent* e) {
|
||
15 years ago
|
// We're about to hide. Save the current state if we're not
|
||
|
// closing altogether.
|
||
|
if (! closing)
|
||
|
savePopupState();
|
||
14 years ago
|
TQWidget::hideEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void StatPopup::showEvent(TQShowEvent* e) {
|
||
15 years ago
|
// 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);
|
||
|
|
||
14 years ago
|
TQWidget::showEvent(e);
|
||
15 years ago
|
}
|
||
|
|
||
|
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();
|
||
|
|
||
13 years ago
|
repaint();
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void StatPopup::resizeToText() {
|
||
|
resizeRequested = false;
|
||
|
|
||
14 years ago
|
TQSize size = fontMetrics().size(0, fullReading);
|
||
15 years ago
|
resize(size.width() + 2 * TEXT_EXPANSION_HORIZONTAL,
|
||
|
size.height() + 2 * TEXT_EXPANSION_VERTICAL);
|
||
13 years ago
|
repaint();
|
||
15 years ago
|
}
|
||
|
|
||
|
#include "statpopup.moc"
|