~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorDisplayLib/MultiMeter.cc

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
   
 
4
        Copyright (c) 1999, 2000, 2001 Chris Schlaeger <cs@kde.org>
 
5
    
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of version 2 of the GNU General Public
 
8
    License as published by the Free Software Foundation.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 
 
19
        KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
 
20
        Please do not commit any changes without consulting me first. Thanks!
 
21
 
 
22
        $Id: MultiMeter.cc,v 1.5 2003/05/04 14:16:48 nhasan Exp $
 
23
*/
 
24
 
 
25
#include <math.h>
 
26
#include <stdlib.h>
 
27
 
 
28
#include <qdom.h>
 
29
#include <qlcdnumber.h>
 
30
#include <qtooltip.h>
 
31
 
 
32
#include <kdebug.h>
 
33
 
 
34
#include <ksgrd/ColorPicker.h>
 
35
#include <ksgrd/SensorManager.h>
 
36
#include <ksgrd/StyleEngine.h>
 
37
 
 
38
#include "MultiMeter.moc"
 
39
#include "MultiMeterSettings.h"
 
40
 
 
41
MultiMeter::MultiMeter(QWidget* parent, const char* name,
 
42
                                   const QString& title, double, double, bool nf)
 
43
        : KSGRD::SensorDisplay(parent, name, title)
 
44
{
 
45
        setShowUnit( true );
 
46
        lowerLimit = upperLimit = 0;
 
47
        lowerLimitActive = upperLimitActive = false;
 
48
        setNoFrame( nf );
 
49
 
 
50
        normalDigitColor = KSGRD::Style->firstForegroundColor();
 
51
        alarmDigitColor = KSGRD::Style->alarmColor();
 
52
        if (noFrame())
 
53
                lcd = new QLCDNumber(this, "meterLCD");
 
54
        else
 
55
                lcd = new QLCDNumber(frame(), "meterLCD");
 
56
        Q_CHECK_PTR(lcd);
 
57
        lcd->setSegmentStyle(QLCDNumber::Filled);
 
58
        setDigitColor(KSGRD::Style->backgroundColor());
 
59
        lcd->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
 
60
                                           QSizePolicy::Expanding, false));
 
61
 
 
62
        setBackgroundColor(KSGRD::Style->backgroundColor());
 
63
        /* All RMB clicks to the lcd widget will be handled by 
 
64
         * SensorDisplay::eventFilter. */
 
65
        lcd->installEventFilter(this);
 
66
 
 
67
        setPlotterWidget(lcd);
 
68
 
 
69
        setMinimumSize(16, 16);
 
70
        setModified(false);
 
71
}
 
72
 
 
73
bool
 
74
MultiMeter::addSensor(const QString& hostName, const QString& sensorName,
 
75
                                        const QString& sensorType, const QString& title)
 
76
{
 
77
        if (sensorType != "integer" && sensorType != "float")
 
78
                return (false);
 
79
 
 
80
        registerSensor(new KSGRD::SensorProperties(hostName, sensorName, sensorType, title));
 
81
 
 
82
        /* To differentiate between answers from value requests and info
 
83
         * requests we use 100 for info requests. */
 
84
        sendRequest(hostName, sensorName + "?", 100);
 
85
 
 
86
        QToolTip::remove(lcd);
 
87
        QToolTip::add(lcd, QString("%1:%2").arg(hostName).arg(sensorName));
 
88
 
 
89
        setModified(true);
 
90
        return (true);
 
91
}
 
92
 
 
93
void
 
94
MultiMeter::answerReceived(int id, const QString& answer)
 
95
{
 
96
        /* We received something, so the sensor is probably ok. */
 
97
        sensorError(id, false);
 
98
 
 
99
        if (id == 100)
 
100
        {
 
101
                KSGRD::SensorIntegerInfo info(answer);
 
102
                setUnit(KSGRD::SensorMgr->translateUnit(info.unit()));
 
103
        }
 
104
        else
 
105
        {
 
106
                double val = answer.toDouble();
 
107
                int digits = (int) log10(val) + 1;
 
108
 
 
109
                if (noFrame())
 
110
                        lcd->setNumDigits(2);
 
111
                else
 
112
                {
 
113
                        if (digits > 5)
 
114
                                lcd->setNumDigits(digits);
 
115
                        else
 
116
                                lcd->setNumDigits(5);
 
117
                }
 
118
 
 
119
                lcd->display(val);
 
120
                if (lowerLimitActive && val < lowerLimit)
 
121
                {
 
122
                        setDigitColor(alarmDigitColor); 
 
123
                }
 
124
                else if (upperLimitActive && val > upperLimit)
 
125
                {
 
126
                        setDigitColor(alarmDigitColor);
 
127
                }
 
128
                else
 
129
                        setDigitColor(normalDigitColor);
 
130
        }
 
131
}
 
132
 
 
133
void
 
134
MultiMeter::resizeEvent(QResizeEvent*)
 
135
{
 
136
        if (noFrame())
 
137
                lcd->setGeometry(0, 0, width() - 1, height() - 1);
 
138
        else
 
139
                frame()->setGeometry(0, 0, width(), height());
 
140
}
 
141
 
 
142
bool
 
143
MultiMeter::restoreSettings(QDomElement& element)
 
144
{
 
145
        lowerLimitActive = element.attribute("lowerLimitActive").toInt();
 
146
        lowerLimit = element.attribute("lowerLimit").toLong();
 
147
        upperLimitActive = element.attribute("upperLimitActive").toInt();
 
148
        upperLimit = element.attribute("upperLimit").toLong();
 
149
 
 
150
        normalDigitColor = restoreColor(element, "normalDigitColor",
 
151
                                                KSGRD::Style->firstForegroundColor());
 
152
        alarmDigitColor = restoreColor(element, "alarmDigitColor",
 
153
                                                KSGRD::Style->alarmColor());
 
154
        setBackgroundColor(restoreColor(element, "backgroundColor",
 
155
                                                KSGRD::Style->backgroundColor()));
 
156
 
 
157
        addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "integer" : element.attribute("sensorType")), "");
 
158
 
 
159
        SensorDisplay::restoreSettings(element);
 
160
 
 
161
        setModified(false);
 
162
 
 
163
        return (true);
 
164
}
 
165
 
 
166
bool
 
167
MultiMeter::saveSettings(QDomDocument& doc, QDomElement& element, bool save)
 
168
{
 
169
        element.setAttribute("hostName", sensors().at(0)->hostName());
 
170
        element.setAttribute("sensorName", sensors().at(0)->name());
 
171
        element.setAttribute("sensorType", sensors().at(0)->type());
 
172
        element.setAttribute("showUnit", showUnit());
 
173
        element.setAttribute("lowerLimitActive", (int) lowerLimitActive);
 
174
        element.setAttribute("lowerLimit", (int) lowerLimit);
 
175
        element.setAttribute("upperLimitActive", (int) upperLimitActive);
 
176
        element.setAttribute("upperLimit", (int) upperLimit);
 
177
 
 
178
        saveColor(element, "normalDigitColor", normalDigitColor);
 
179
        saveColor(element, "alarmDigitColor", alarmDigitColor);
 
180
        saveColor(element, "backgroundColor", lcd->backgroundColor());
 
181
 
 
182
        SensorDisplay::saveSettings(doc, element);
 
183
 
 
184
        if (save)
 
185
                setModified(false);
 
186
 
 
187
        return (true);
 
188
}
 
189
 
 
190
void
 
191
MultiMeter::configureSettings()
 
192
{
 
193
        mms = new MultiMeterSettings(this, "MultiMeterSettings");
 
194
        Q_CHECK_PTR(mms);
 
195
        mms->setTitle(title());
 
196
        mms->setShowUnit(showUnit());
 
197
        mms->setLowerLimitActive(lowerLimitActive);
 
198
        mms->setLowerLimit(lowerLimit);
 
199
        mms->setUpperLimitActive(upperLimitActive);
 
200
        mms->setUpperLimit(upperLimit);
 
201
        mms->setNormalDigitColor(normalDigitColor);
 
202
        mms->setAlarmDigitColor(alarmDigitColor);
 
203
        mms->setMeterBackgroundColor(lcd->backgroundColor());
 
204
 
 
205
        connect(mms, SIGNAL(applyClicked()), SLOT(applySettings()));
 
206
 
 
207
        if (mms->exec())
 
208
                applySettings();
 
209
 
 
210
        delete mms;
 
211
        mms = 0;
 
212
}
 
213
 
 
214
void
 
215
MultiMeter::applySettings()
 
216
{
 
217
        setShowUnit( mms->showUnit() );
 
218
        setTitle(mms->title());
 
219
        lowerLimitActive = mms->lowerLimitActive();
 
220
        lowerLimit = mms->lowerLimit();
 
221
        upperLimitActive = mms->upperLimitActive();
 
222
        upperLimit = mms->upperLimit();
 
223
 
 
224
        normalDigitColor = mms->normalDigitColor();
 
225
        alarmDigitColor = mms->alarmDigitColor();
 
226
        setBackgroundColor(mms->meterBackgroundColor());
 
227
 
 
228
        repaint();
 
229
        setModified(true);
 
230
}
 
231
 
 
232
void
 
233
MultiMeter::applyStyle()
 
234
{
 
235
        normalDigitColor = KSGRD::Style->firstForegroundColor();
 
236
        setBackgroundColor(KSGRD::Style->backgroundColor());
 
237
        repaint();
 
238
        setModified(true);
 
239
}
 
240
 
 
241
void
 
242
MultiMeter::setDigitColor(const QColor& col)
 
243
{
 
244
        QPalette p = lcd->palette();
 
245
        p.setColor(QColorGroup::Foreground, col);
 
246
        lcd->setPalette(p);
 
247
}
 
248
 
 
249
void
 
250
MultiMeter::setBackgroundColor(const QColor& col)
 
251
{
 
252
        lcd->setBackgroundColor(col);
 
253
 
 
254
        QPalette p = lcd->palette();
 
255
        p.setColor(QColorGroup::Light, col);
 
256
        p.setColor(QColorGroup::Dark, col);
 
257
        lcd->setPalette(p);
 
258
}