2
KSysGuard, the KDE System Guard
4
Copyright (c) 1999 - 2002 Chris Schlaeger <cs@kde.org>
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.
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.
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.
23
#include <QtXml/qdom.h>
30
#include <QMouseEvent>
31
#include <QCustomEvent>
33
#include <kapplication.h>
34
#include <kiconloader.h>
37
#include <kmessagebox.h>
42
#include "ksgrd/SensorManager.h"
43
#include "SensorDisplay.h"
47
using namespace KSGRD;
49
SensorDisplay::DeleteEvent::DeleteEvent( SensorDisplay *display )
50
: QEvent( QEvent::User ), mDisplay( display )
54
SensorDisplay* SensorDisplay::DeleteEvent::display() const
59
SensorDisplay::SensorDisplay( QWidget *parent, const QString &title, SharedSettings *workSheetSettings)
62
mSharedSettings = workSheetSettings;
69
this->setWhatsThis( "dummy" );
71
setMinimumSize( 16, 16 );
76
/* Let's call updateWhatsThis() in case the derived class does not do
81
SensorDisplay::~SensorDisplay()
84
SensorMgr->disconnectClient( this );
87
killTimer( mTimerId );
88
for(int i = mSensors.size()-1; i>=0; i--)
92
void SensorDisplay::registerSensor( SensorProperties *sp )
94
mSensors.append( sp );
97
void SensorDisplay::unregisterSensor( uint pos )
99
delete mSensors.takeAt( pos );
102
void SensorDisplay::timerTick()
106
foreach( SensorProperties *s, mSensors) {
107
sendRequest( s->hostName(), s->name(), i++ );
111
void SensorDisplay::showContextMenu(const QPoint &pos)
115
bool menuEmpty = true;
117
if ( hasSettingsDialog() ) {
118
action = pm.addAction( i18n( "&Properties" ) );
119
action->setData( 0 );
122
if(mSharedSettings && !mSharedSettings->locked) {
123
action = pm.addAction( i18n( "&Remove Display" ) );
124
action->setData( 1 );
128
if(menuEmpty) return;
129
action = pm.exec( mapToGlobal(pos) );
131
switch ( action->data().toInt() ) {
136
if ( mDeleteNotifier ) {
137
DeleteEvent *event = new DeleteEvent( this );
138
kapp->postEvent( mDeleteNotifier, event );
146
bool SensorDisplay::eventFilter( QObject *object, QEvent *event )
148
if ( event->type() == QEvent::MouseButtonPress) {
149
QMouseEvent *e = static_cast<QMouseEvent *> (event);
150
if( e->button() == Qt::RightButton ) {
151
showContextMenu( mapFromGlobal( e->globalPos() ) );
156
return QWidget::eventFilter( object, event );
158
void SensorDisplay::sendRequest( const QString &hostName,
159
const QString &command, int id )
161
if ( !SensorMgr->sendRequest( hostName, command, (SensorClient*)this, id ) ) {
162
sensorError( id, true );
166
void SensorDisplay::sensorError( int sensorId, bool err )
168
if ( sensorId >= (int)mSensors.count() || sensorId < 0 )
171
if ( err == mSensors.at( sensorId )->isOk() ) {
172
// this happens only when the sensorOk status needs to be changed.
173
mSensors.at( sensorId )->setIsOk( !err );
177
for ( uint i = 0; i < (uint)mSensors.count(); ++i )
178
if ( !mSensors.at( i )->isOk() ) {
186
void SensorDisplay::updateWhatsThis()
188
if(mSharedSettings && mSharedSettings->locked)
189
this->setWhatsThis( i18n(
190
"<qt><p>This is a sensor display. To customize a sensor display click "
191
"the right mouse button here "
192
"and select the <i>Properties</i> entry from the popup "
193
"menu. Select <i>Remove</i> to delete the display from the worksheet."
194
"</p>%1</qt>" , additionalWhatsThis() ) );
196
this->setWhatsThis( additionalWhatsThis());
199
void SensorDisplay::hosts( QStringList& list )
201
foreach( SensorProperties *s, mSensors)
202
if ( !list.contains( s->hostName() ) )
203
list.append( s->hostName() );
206
QColor SensorDisplay::restoreColor( QDomElement &element, const QString &attr,
207
const QColor& fallback )
210
int color = element.attribute( attr ).toUInt( &ok, 0 );
213
kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr) << " (Not a valid number)";
216
QColor c( (color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff), (color & 0xff000000) >> 24);
218
kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr);
222
if(c.alpha() == 0) c.setAlpha(255);
226
void SensorDisplay::saveColor( QDomElement &element, const QString &attr,
227
const QColor &color )
229
element.setAttribute( attr, "0x" + QString::number(color.rgba(),16) );
232
void SensorDisplay::saveColorAppend( QDomElement &element, const QString &attr,
233
const QColor &color )
235
element.setAttribute( attr, element.attribute(attr) + ",0x" + QString::number(color.rgba(),16) );
238
bool SensorDisplay::addSensor( const QString &hostName, const QString &name,
239
const QString &type, const QString &description )
241
registerSensor( new SensorProperties( hostName, name, type, description ) );
245
bool SensorDisplay::removeSensor( uint pos )
247
if((int) pos >= mSensors.count())
249
unregisterSensor( pos );
253
bool SensorDisplay::hasSettingsDialog() const
258
void SensorDisplay::configureSettings()
262
QString SensorDisplay::additionalWhatsThis()
267
void SensorDisplay::sensorLost( int reqId )
269
sensorError( reqId, true );
272
void SensorDisplay::setDeleteNotifier( QObject *object )
274
mDeleteNotifier = object;
277
bool SensorDisplay::restoreSettings( QDomElement &element )
279
mShowUnit = element.attribute( "showUnit", "0" ).toInt();
280
setUnit( element.attribute( "unit", QString() ) );
281
setTitle( element.attribute( "title", title() ) );
286
bool SensorDisplay::saveSettings( QDomDocument&, QDomElement &element )
288
element.setAttribute( "title", title() );
289
element.setAttribute( "unit", unit() );
290
element.setAttribute( "showUnit", mShowUnit );
295
QList<SensorProperties *> &SensorDisplay::sensors()
300
void SensorDisplay::rmbPressed()
302
emit showPopupMenu( this );
305
void SensorDisplay::applySettings()
309
void SensorDisplay::applyStyle()
313
void SensorDisplay::setSensorOk( bool ok )
316
delete mErrorIndicator;
319
if ( mErrorIndicator )
321
if ( !mPlotterWdg || mPlotterWdg->isVisible())
324
QPixmap errorIcon = KIconLoader::global()->loadIcon( "dialog-error", KIconLoader::Desktop,
325
KIconLoader::SizeSmall );
327
mErrorIndicator = new QWidget( mPlotterWdg );
328
QPalette palette = mErrorIndicator->palette();
329
palette.setBrush( mErrorIndicator->backgroundRole(), QBrush( errorIcon ) );
330
mErrorIndicator->setPalette( palette );
331
mErrorIndicator->resize( errorIcon.size() );
332
if ( !errorIcon.mask().isNull() )
333
mErrorIndicator->setMask( errorIcon.mask() );
335
mErrorIndicator->move( 0, 0 );
336
mErrorIndicator->show();
340
void SensorDisplay::changeEvent( QEvent * event ) {
341
if (event->type() == QEvent::LanguageChange) {
342
setTitle(mTitle); //retranslate
346
void SensorDisplay::setTitle( const QString &title )
349
mTranslatedTitle = i18n(title.toUtf8());
350
emit titleChanged(mTitle);
351
emit translatedTitleChanged(mTranslatedTitle);
354
QString SensorDisplay::translatedTitle() const
356
return mTranslatedTitle;
359
QString SensorDisplay::title() const
364
void SensorDisplay::setUnit( const QString &unit )
369
QString SensorDisplay::unit() const
374
void SensorDisplay::setShowUnit( bool value )
379
bool SensorDisplay::showUnit() const
384
void SensorDisplay::setPlotterWidget( QWidget *wdg )
389
SensorProperties::SensorProperties()
393
SensorProperties::SensorProperties( const QString &hostName, const QString &name,
394
const QString &type, const QString &description )
395
: mName( name ), mType( type ), mDescription( description )
397
setHostName(hostName);
401
SensorProperties::~SensorProperties()
405
void SensorProperties::setHostName( const QString &hostName )
407
mHostName = hostName;
408
mIsLocalhost = (mHostName.toLower() == "localhost" || mHostName.isEmpty());
411
bool SensorProperties::isLocalhost() const
416
QString SensorProperties::hostName() const
421
void SensorProperties::setName( const QString &name )
426
QString SensorProperties::name() const
431
void SensorProperties::setType( const QString &type )
436
QString SensorProperties::type() const
441
void SensorProperties::setDescription( const QString &description )
443
mDescription = description;
446
QString SensorProperties::description() const
451
void SensorProperties::setUnit( const QString &unit )
456
QString SensorProperties::unit() const
461
void SensorProperties::setIsOk( bool value )
466
bool SensorProperties::isOk() const
471
void SensorProperties::setRegExpName( const QString &name )
475
QString SensorProperties::regExpName() const
480
#include "SensorDisplay.moc"