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( 2 );
122
if(mSharedSettings && !mSharedSettings->locked) {
123
action = pm.addAction( i18n( "&Remove Display" ) );
124
action->setData( 3 );
128
if(menuEmpty) return;
129
action = pm.exec( mapToGlobal(pos) );
131
switch ( action->data().toInt() ) {
133
KRun::run(*KService::serviceByDesktopName("ksysguard"), KUrl::List(), window());
139
if ( mDeleteNotifier ) {
140
DeleteEvent *event = new DeleteEvent( this );
141
kapp->postEvent( mDeleteNotifier, event );
149
bool SensorDisplay::eventFilter( QObject *object, QEvent *event )
151
if ( event->type() == QEvent::MouseButtonPress) {
152
QMouseEvent *e = static_cast<QMouseEvent *> (event);
153
if( e->button() == Qt::RightButton ) {
154
showContextMenu( e->pos() );
159
return QWidget::eventFilter( object, event );
161
void SensorDisplay::sendRequest( const QString &hostName,
162
const QString &command, int id )
164
if ( !SensorMgr->sendRequest( hostName, command, (SensorClient*)this, id ) ) {
165
sensorError( id, true );
169
void SensorDisplay::sensorError( int sensorId, bool err )
171
if ( sensorId >= (int)mSensors.count() || sensorId < 0 )
174
if ( err == mSensors.at( sensorId )->isOk() ) {
175
// this happens only when the sensorOk status needs to be changed.
176
mSensors.at( sensorId )->setIsOk( !err );
180
for ( uint i = 0; i < (uint)mSensors.count(); ++i )
181
if ( !mSensors.at( i )->isOk() ) {
189
void SensorDisplay::updateWhatsThis()
191
if(mSharedSettings && mSharedSettings->locked)
192
this->setWhatsThis( i18n(
193
"<qt><p>This is a sensor display. To customize a sensor display click "
194
"the right mouse button here "
195
"and select the <i>Properties</i> entry from the popup "
196
"menu. Select <i>Remove</i> to delete the display from the worksheet."
197
"</p>%1</qt>" , additionalWhatsThis() ) );
199
this->setWhatsThis( additionalWhatsThis());
202
void SensorDisplay::hosts( QStringList& list )
204
foreach( SensorProperties *s, mSensors)
205
if ( !list.contains( s->hostName() ) )
206
list.append( s->hostName() );
209
QColor SensorDisplay::restoreColor( QDomElement &element, const QString &attr,
210
const QColor& fallback )
213
int color = element.attribute( attr ).toUInt( &ok, 0 );
216
kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr) << " (Not a valid number)";
219
QColor c( (color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff), (color & 0xff000000) >> 24);
221
kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr);
225
if(c.alpha() == 0) c.setAlpha(255);
229
void SensorDisplay::saveColor( QDomElement &element, const QString &attr,
230
const QColor &color )
232
element.setAttribute( attr, "0x" + QString::number(color.rgba(),16) );
235
void SensorDisplay::saveColorAppend( QDomElement &element, const QString &attr,
236
const QColor &color )
238
element.setAttribute( attr, element.attribute(attr) + ",0x" + QString::number(color.rgba(),16) );
241
bool SensorDisplay::addSensor( const QString &hostName, const QString &name,
242
const QString &type, const QString &description )
244
registerSensor( new SensorProperties( hostName, name, type, description ) );
248
bool SensorDisplay::removeSensor( uint pos )
250
if((int) pos >= mSensors.count())
252
unregisterSensor( pos );
256
bool SensorDisplay::hasSettingsDialog() const
261
void SensorDisplay::configureSettings()
265
QString SensorDisplay::additionalWhatsThis()
270
void SensorDisplay::sensorLost( int reqId )
272
sensorError( reqId, true );
275
void SensorDisplay::setDeleteNotifier( QObject *object )
277
mDeleteNotifier = object;
280
bool SensorDisplay::restoreSettings( QDomElement &element )
282
mShowUnit = element.attribute( "showUnit", "0" ).toInt();
283
setUnit( element.attribute( "unit", QString() ) );
284
setTitle( element.attribute( "title", title() ) );
289
bool SensorDisplay::saveSettings( QDomDocument&, QDomElement &element )
291
element.setAttribute( "title", title() );
292
element.setAttribute( "unit", unit() );
293
element.setAttribute( "showUnit", mShowUnit );
298
QList<SensorProperties *> &SensorDisplay::sensors()
303
void SensorDisplay::rmbPressed()
305
emit showPopupMenu( this );
308
void SensorDisplay::applySettings()
312
void SensorDisplay::applyStyle()
316
void SensorDisplay::setSensorOk( bool ok )
320
delete mErrorIndicator;
323
if ( mErrorIndicator )
325
if ( !mPlotterWdg || mPlotterWdg->isVisible())
328
QPixmap errorIcon = KIconLoader::global()->loadIcon( "dialog-error", KIconLoader::Desktop,
329
KIconLoader::SizeSmall );
331
mErrorIndicator = new QWidget( mPlotterWdg );
332
QPalette palette = mErrorIndicator->palette();
333
palette.setBrush( mErrorIndicator->backgroundRole(), QBrush( errorIcon ) );
334
mErrorIndicator->setPalette( palette );
335
mErrorIndicator->resize( errorIcon.size() );
336
if ( !errorIcon.mask().isNull() )
337
mErrorIndicator->setMask( errorIcon.mask() );
339
mErrorIndicator->move( 0, 0 );
340
mErrorIndicator->show();
344
void SensorDisplay::changeEvent( QEvent * event ) {
345
if (event->type() == QEvent::LanguageChange) {
346
setTitle(mTitle); //retranslate
350
void SensorDisplay::setTitle( const QString &title )
353
mTranslatedTitle = i18n(title.toUtf8());
354
emit titleChanged(mTitle);
355
emit translatedTitleChanged(mTranslatedTitle);
358
QString SensorDisplay::translatedTitle() const
360
return mTranslatedTitle;
363
QString SensorDisplay::title() const
368
void SensorDisplay::setUnit( const QString &unit )
373
QString SensorDisplay::unit() const
378
void SensorDisplay::setShowUnit( bool value )
383
bool SensorDisplay::showUnit() const
388
void SensorDisplay::setPlotterWidget( QWidget *wdg )
393
SensorProperties::SensorProperties()
397
SensorProperties::SensorProperties( const QString &hostName, const QString &name,
398
const QString &type, const QString &description )
399
: mName( name ), mType( type ), mDescription( description )
401
setHostName(hostName);
405
SensorProperties::~SensorProperties()
409
void SensorProperties::setHostName( const QString &hostName )
411
mHostName = hostName;
412
mIsLocalhost = (mHostName.toLower() == "localhost" || mHostName.isEmpty());
415
bool SensorProperties::isLocalhost() const
420
QString SensorProperties::hostName() const
425
void SensorProperties::setName( const QString &name )
430
QString SensorProperties::name() const
435
void SensorProperties::setType( const QString &type )
440
QString SensorProperties::type() const
445
void SensorProperties::setDescription( const QString &description )
447
mDescription = description;
450
QString SensorProperties::description() const
455
void SensorProperties::setUnit( const QString &unit )
460
QString SensorProperties::unit() const
465
void SensorProperties::setIsOk( bool value )
470
bool SensorProperties::isOk() const
475
void SensorProperties::setRegExpName( const QString &name )
479
QString SensorProperties::regExpName() const
484
#include "SensorDisplay.moc"