~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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 the GNU General Public
 
8
    License version 2 or at your option version 3 as published by
 
9
    the Free Software Foundation.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 
 
20
*/
 
21
 
 
22
#include <math.h>
 
23
#include <stdlib.h>
 
24
 
 
25
#include <QtXml/qdom.h>
 
26
#include <QtGui/QLCDNumber>
 
27
#include <QtGui/QHBoxLayout>
 
28
 
 
29
#include <kdebug.h>
 
30
 
 
31
#include <ksgrd/SensorManager.h>
 
32
#include "StyleEngine.h"
 
33
 
 
34
#include "MultiMeter.h"
 
35
#include "MultiMeterSettings.h"
 
36
 
 
37
MultiMeter::MultiMeter(QWidget* parent, const QString& title, SharedSettings *workSheetSettings)
 
38
  : KSGRD::SensorDisplay(parent, title, workSheetSettings)
 
39
{
 
40
  setShowUnit( true );
 
41
  mLowerLimit = mUpperLimit = 0.0;
 
42
  mLowerLimitActive = mUpperLimitActive = false;
 
43
 
 
44
  mIsFloat = false;
 
45
 
 
46
  mNormalDigitColor = KSGRD::Style->firstForegroundColor();
 
47
  mAlarmDigitColor = KSGRD::Style->alarmColor();
 
48
  QLayout *layout = new QHBoxLayout(this);
 
49
  mLcd = new QLCDNumber( this );
 
50
  layout->addWidget(mLcd);
 
51
 
 
52
  mLcd->setFrameStyle( QFrame::NoFrame );
 
53
  mLcd->setSegmentStyle( QLCDNumber::Filled );
 
54
  setDigitColor( KSGRD::Style->firstForegroundColor() );
 
55
  mLcd->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
 
56
 
 
57
  setBackgroundColor( KSGRD::Style->backgroundColor() );
 
58
 
 
59
  /* All RMB clicks to the mLcd widget will be handled by 
 
60
   * SensorDisplay::eventFilter. */
 
61
  mLcd->installEventFilter( this );
 
62
 
 
63
  setPlotterWidget( mLcd );
 
64
 
 
65
  setMinimumSize( 5, 5 );
 
66
}
 
67
 
 
68
bool MultiMeter::addSensor(const QString& hostName, const QString& sensorName,
 
69
          const QString& sensorType, const QString& title)
 
70
{
 
71
  if (sensorType != "integer" && sensorType != "float")
 
72
    return false;
 
73
 
 
74
  if(!sensors().isEmpty())
 
75
    return false;
 
76
 
 
77
  mIsFloat = (sensorType == "float");
 
78
  mLcd->setSmallDecimalPoint( mIsFloat );
 
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
  mLcd->setToolTip( QString("%1:%2").arg(hostName).arg(sensorName));
 
87
 
 
88
  return true;
 
89
}
 
90
 
 
91
void MultiMeter::answerReceived(int id, const QList<QByteArray>& answerlist)
 
92
{
 
93
  /* We received something, so the sensor is probably ok. */
 
94
  sensorError(id, false);
 
95
  QByteArray answer;
 
96
  if(!answerlist.isEmpty()) answer = answerlist[0];
 
97
 
 
98
  if (id == 100)
 
99
  {
 
100
    KSGRD::SensorIntegerInfo info(answer);
 
101
    setUnit(KSGRD::SensorMgr->translateUnit(info.unit()));
 
102
  }
 
103
  else
 
104
  {
 
105
    double val = answer.toDouble();
 
106
 
 
107
    int digits = 1;
 
108
    if (qAbs(val) >= 1) {
 
109
      digits = (int) log10(qAbs(val)) + 1;
 
110
    }
 
111
    if (mIsFloat) {
 
112
      //Show two digits after the decimal point
 
113
      digits += 3;
 
114
    }
 
115
    if (val < 0) {
 
116
      //Add a digit for the negative sign
 
117
      digits += 1;
 
118
    }
 
119
 
 
120
    mLcd->setNumDigits(qMin(15,digits));
 
121
 
 
122
    mLcd->display(val);
 
123
    if (mLowerLimitActive && val < mLowerLimit)
 
124
    {
 
125
      setDigitColor(mAlarmDigitColor);  
 
126
    }
 
127
    else if (mUpperLimitActive && val > mUpperLimit)
 
128
    {
 
129
      setDigitColor(mAlarmDigitColor);
 
130
    }
 
131
    else
 
132
      setDigitColor(mNormalDigitColor);
 
133
  }
 
134
}
 
135
 
 
136
bool MultiMeter::restoreSettings(QDomElement& element)
 
137
{
 
138
  mLowerLimitActive = element.attribute("lowerLimitActive").toInt();
 
139
  mLowerLimit = element.attribute("lowerLimit").toDouble();
 
140
  mUpperLimitActive = element.attribute("upperLimitActive").toInt();
 
141
  mUpperLimit = element.attribute("upperLimit").toDouble();
 
142
 
 
143
  mNormalDigitColor = restoreColor(element, "normalDigitColor",
 
144
            KSGRD::Style->firstForegroundColor());
 
145
  mAlarmDigitColor = restoreColor(element, "mAlarmDigitColor",
 
146
            KSGRD::Style->alarmColor());
 
147
  setBackgroundColor(restoreColor(element, "backgroundColor",
 
148
            KSGRD::Style->backgroundColor()));
 
149
 
 
150
  addSensor(element.attribute("hostName"), element.attribute("sensorName"), (element.attribute("sensorType").isEmpty() ? "integer" : element.attribute("sensorType")), "");
 
151
 
 
152
  SensorDisplay::restoreSettings(element);
 
153
 
 
154
  return true;
 
155
}
 
156
 
 
157
bool MultiMeter::saveSettings(QDomDocument& doc, QDomElement& element)
 
158
{
 
159
  if(!sensors().isEmpty()) {
 
160
    element.setAttribute("hostName", sensors().at(0)->hostName());
 
161
    element.setAttribute("sensorName", sensors().at(0)->name());
 
162
    element.setAttribute("sensorType", sensors().at(0)->type());
 
163
  }
 
164
  element.setAttribute("showUnit", showUnit());
 
165
  element.setAttribute("lowerLimitActive", (int) mLowerLimitActive);
 
166
  element.setAttribute("lowerLimit", mLowerLimit);
 
167
  element.setAttribute("upperLimitActive", (int) mUpperLimitActive);
 
168
  element.setAttribute("upperLimit", mUpperLimit);
 
169
 
 
170
  saveColor(element, "normalDigitColor", mNormalDigitColor);
 
171
  saveColor(element, "mAlarmDigitColor", mAlarmDigitColor);
 
172
  saveColor(element, "backgroundColor", mBackgroundColor);
 
173
 
 
174
  SensorDisplay::saveSettings(doc, element);
 
175
 
 
176
  return true;
 
177
}
 
178
 
 
179
void MultiMeter::configureSettings()
 
180
{
 
181
  MultiMeterSettings dlg( this );
 
182
 
 
183
  dlg.setTitle(title());
 
184
  dlg.setShowUnit(showUnit());
 
185
  dlg.setLowerLimitActive(mLowerLimitActive);
 
186
  dlg.setLowerLimit(mLowerLimit);
 
187
  dlg.setUpperLimitActive(mUpperLimitActive);
 
188
  dlg.setUpperLimit(mUpperLimit);
 
189
  dlg.setNormalDigitColor(mNormalDigitColor);
 
190
  dlg.setAlarmDigitColor(mAlarmDigitColor);
 
191
  dlg.setMeterBackgroundColor(mBackgroundColor);
 
192
 
 
193
  if ( dlg.exec() ) {
 
194
    setShowUnit( dlg.showUnit() );
 
195
    setTitle( dlg.title() );
 
196
    mLowerLimitActive = dlg.lowerLimitActive();
 
197
    mLowerLimit = dlg.lowerLimit();
 
198
    mUpperLimitActive = dlg.upperLimitActive();
 
199
    mUpperLimit = dlg.upperLimit();
 
200
 
 
201
    mNormalDigitColor = dlg.normalDigitColor();
 
202
    mAlarmDigitColor = dlg.alarmDigitColor();
 
203
    setBackgroundColor( dlg.meterBackgroundColor() );
 
204
 
 
205
    repaint();
 
206
  }
 
207
}
 
208
 
 
209
void MultiMeter::applyStyle()
 
210
{
 
211
  mNormalDigitColor = KSGRD::Style->firstForegroundColor();
 
212
  setBackgroundColor( KSGRD::Style->backgroundColor() );
 
213
 
 
214
  repaint();
 
215
}
 
216
 
 
217
void MultiMeter::setDigitColor( const QColor& color )
 
218
{
 
219
  QPalette palette = mLcd->palette();
 
220
  palette.setColor( QPalette::WindowText, color );
 
221
  mLcd->setPalette( palette );
 
222
}
 
223
 
 
224
void MultiMeter::setBackgroundColor( const QColor& color )
 
225
{
 
226
  mBackgroundColor = color;
 
227
 
 
228
  QPalette pal = mLcd->palette();
 
229
  pal.setColor( mLcd->backgroundRole(), mBackgroundColor );
 
230
  mLcd->setPalette( pal );
 
231
}
 
232
 
 
233
#include "MultiMeter.moc"