~ubuntu-branches/ubuntu/natty/kdebase-workspace/natty-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2011-04-03 16:54:55 UTC
  • mfrom: (1.1.55 upstream)
  • Revision ID: james.westby@ubuntu.com-20110403165455-8tnwxt82p21p15hh
Tags: 4:4.6.2a-0ubuntu1
* New upstream release
  - Update kde-sc-dev-latest version
  - Update kdebase-workspace-wallpapers.install,
    kde-window-manager.install and not-installed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    KSysGuard, the KDE System Guard
3
 
 
4
 
    Copyright (c) 1999 - 2002 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 <QCheckBox>
23
 
#include <QtXml/qdom.h>
24
 
#include <QMenu>
25
 
#include <QSpinBox>
26
 
 
27
 
#include <QBitmap>
28
 
#include <QPixmap>
29
 
#include <QEvent>
30
 
#include <QMouseEvent>
31
 
#include <QCustomEvent>
32
 
 
33
 
#include <kapplication.h>
34
 
#include <kiconloader.h>
35
 
#include <klocale.h>
36
 
#include <kdebug.h>
37
 
#include <kmessagebox.h>
38
 
#include <krun.h>
39
 
#include <kurl.h>
40
 
#include <kservice.h>
41
 
 
42
 
#include "ksgrd/SensorManager.h"
43
 
#include "SensorDisplay.h"
44
 
 
45
 
#define NONE -1
46
 
 
47
 
using namespace KSGRD;
48
 
 
49
 
SensorDisplay::DeleteEvent::DeleteEvent( SensorDisplay *display )
50
 
  : QEvent( QEvent::User ), mDisplay( display )
51
 
{
52
 
}
53
 
 
54
 
SensorDisplay* SensorDisplay::DeleteEvent::display() const
55
 
{
56
 
  return mDisplay;
57
 
}
58
 
 
59
 
SensorDisplay::SensorDisplay( QWidget *parent, const QString &title, SharedSettings *workSheetSettings)
60
 
  : QWidget( parent )
61
 
{
62
 
  mSharedSettings = workSheetSettings;
63
 
 
64
 
  mShowUnit = false;
65
 
  mTimerId = NONE;
66
 
  mErrorIndicator = 0;
67
 
  mPlotterWdg = 0;
68
 
 
69
 
  this->setWhatsThis( "dummy" );
70
 
 
71
 
  setMinimumSize( 16, 16 );
72
 
  setSensorOk( false );
73
 
  setTitle(title);
74
 
 
75
 
 
76
 
  /* Let's call updateWhatsThis() in case the derived class does not do
77
 
   * this. */
78
 
  updateWhatsThis();
79
 
}
80
 
 
81
 
SensorDisplay::~SensorDisplay()
82
 
{
83
 
  if ( SensorMgr != 0 )
84
 
    SensorMgr->disconnectClient( this );
85
 
 
86
 
  if ( mTimerId > 0 )
87
 
    killTimer( mTimerId );
88
 
  for(int i = mSensors.size()-1; i>=0; i--)
89
 
    unregisterSensor(i);
90
 
}
91
 
 
92
 
void SensorDisplay::registerSensor( SensorProperties *sp )
93
 
{
94
 
  mSensors.append( sp );
95
 
}
96
 
 
97
 
void SensorDisplay::unregisterSensor( uint pos )
98
 
{
99
 
  delete mSensors.takeAt( pos );
100
 
}
101
 
 
102
 
void SensorDisplay::timerTick()
103
 
{
104
 
  int i = 0;
105
 
 
106
 
  foreach( SensorProperties *s, mSensors) {
107
 
    sendRequest( s->hostName(), s->name(), i++ );
108
 
 }
109
 
}
110
 
 
111
 
void SensorDisplay::showContextMenu(const QPoint &pos)
112
 
{
113
 
    QMenu pm;
114
 
    QAction *action = 0;
115
 
    bool menuEmpty = true;
116
 
 
117
 
    if ( hasSettingsDialog() ) {
118
 
      action = pm.addAction( i18n( "&Properties" ) );
119
 
      action->setData( 2 );
120
 
      menuEmpty = false;
121
 
    }
122
 
    if(mSharedSettings && !mSharedSettings->locked) {  
123
 
      action = pm.addAction( i18n( "&Remove Display" ) );
124
 
      action->setData( 3 );
125
 
      menuEmpty = false;
126
 
    }
127
 
 
128
 
    if(menuEmpty) return;
129
 
    action = pm.exec( mapToGlobal(pos) );
130
 
    if ( action ) {
131
 
      switch ( action->data().toInt() ) {
132
 
        case 1:
133
 
          KRun::run(*KService::serviceByDesktopName("ksysguard"), KUrl::List(), window());
134
 
          break;
135
 
        case 2:
136
 
          configureSettings();
137
 
          break;
138
 
        case 3: {
139
 
            if ( mDeleteNotifier ) {
140
 
              DeleteEvent *event = new DeleteEvent( this );
141
 
              kapp->postEvent( mDeleteNotifier, event );
142
 
            }
143
 
          }
144
 
          break;
145
 
      }
146
 
    }
147
 
}
148
 
 
149
 
bool SensorDisplay::eventFilter( QObject *object, QEvent *event )
150
 
{
151
 
  if ( event->type() == QEvent::MouseButtonPress) {
152
 
    QMouseEvent *e = static_cast<QMouseEvent *> (event);
153
 
    if( e->button() == Qt::RightButton ) {
154
 
      showContextMenu( e->pos() );
155
 
      return true;
156
 
    }
157
 
  } 
158
 
 
159
 
  return QWidget::eventFilter( object, event );
160
 
}
161
 
void SensorDisplay::sendRequest( const QString &hostName,
162
 
                                 const QString &command, int id )
163
 
{
164
 
  if ( !SensorMgr->sendRequest( hostName, command, (SensorClient*)this, id ) ) {
165
 
    sensorError( id, true );
166
 
  }
167
 
}
168
 
 
169
 
void SensorDisplay::sensorError( int sensorId, bool err )
170
 
{
171
 
  if ( sensorId >= (int)mSensors.count() || sensorId < 0 )
172
 
    return;
173
 
 
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 );
177
 
  }
178
 
 
179
 
  bool ok = true;
180
 
  for ( uint i = 0; i < (uint)mSensors.count(); ++i )
181
 
    if ( !mSensors.at( i )->isOk() ) {
182
 
      ok = false;
183
 
      break;
184
 
    }
185
 
 
186
 
  setSensorOk( ok );
187
 
}
188
 
 
189
 
void SensorDisplay::updateWhatsThis()
190
 
{
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() ) );
198
 
  else
199
 
      this->setWhatsThis( additionalWhatsThis());
200
 
}
201
 
 
202
 
void SensorDisplay::hosts( QStringList& list )
203
 
{
204
 
  foreach( SensorProperties *s, mSensors)
205
 
    if ( !list.contains( s->hostName() ) )
206
 
      list.append( s->hostName() );
207
 
}
208
 
 
209
 
QColor SensorDisplay::restoreColor( QDomElement &element, const QString &attr,
210
 
                                    const QColor& fallback )
211
 
{
212
 
  bool ok;
213
 
  int color = element.attribute( attr ).toUInt( &ok, 0 );
214
 
  
215
 
  if ( !ok ) {
216
 
    kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr) << " (Not a valid number)";
217
 
    return fallback;
218
 
  }
219
 
  QColor c( (color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff), (color & 0xff000000) >> 24);
220
 
  if( !c.isValid()) {
221
 
    kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr);
222
 
    return fallback;
223
 
  }
224
 
 
225
 
  if(c.alpha() == 0) c.setAlpha(255);
226
 
  return c;
227
 
}
228
 
 
229
 
void SensorDisplay::saveColor( QDomElement &element, const QString &attr,
230
 
                               const QColor &color )
231
 
{
232
 
  element.setAttribute( attr, "0x" + QString::number(color.rgba(),16) );
233
 
}
234
 
 
235
 
void SensorDisplay::saveColorAppend( QDomElement &element, const QString &attr,
236
 
                               const QColor &color )
237
 
{
238
 
  element.setAttribute( attr, element.attribute(attr) + ",0x" + QString::number(color.rgba(),16) );
239
 
}
240
 
 
241
 
bool SensorDisplay::addSensor( const QString &hostName, const QString &name,
242
 
                               const QString &type, const QString &description )
243
 
{
244
 
  registerSensor( new SensorProperties( hostName, name, type, description ) );
245
 
  return true;
246
 
}
247
 
 
248
 
bool SensorDisplay::removeSensor( uint pos )
249
 
{
250
 
  if((int) pos >= mSensors.count())
251
 
    return false;
252
 
  unregisterSensor( pos );
253
 
  return true;
254
 
}
255
 
 
256
 
bool SensorDisplay::hasSettingsDialog() const
257
 
{
258
 
  return false;
259
 
}
260
 
 
261
 
void SensorDisplay::configureSettings()
262
 
{
263
 
}
264
 
 
265
 
QString SensorDisplay::additionalWhatsThis()
266
 
{
267
 
  return QString();
268
 
}
269
 
 
270
 
void SensorDisplay::sensorLost( int reqId )
271
 
{
272
 
  sensorError( reqId, true );
273
 
}
274
 
 
275
 
void SensorDisplay::setDeleteNotifier( QObject *object )
276
 
{
277
 
  mDeleteNotifier = object;
278
 
}
279
 
 
280
 
bool SensorDisplay::restoreSettings( QDomElement &element )
281
 
{
282
 
  mShowUnit = element.attribute( "showUnit", "0" ).toInt();
283
 
  setUnit( element.attribute( "unit", QString() ) );
284
 
  setTitle( element.attribute( "title", title() ) );
285
 
 
286
 
  return true;
287
 
}
288
 
 
289
 
bool SensorDisplay::saveSettings( QDomDocument&, QDomElement &element )
290
 
{
291
 
  element.setAttribute( "title", title() );
292
 
  element.setAttribute( "unit", unit() );
293
 
  element.setAttribute( "showUnit", mShowUnit );
294
 
 
295
 
  return true;
296
 
}
297
 
 
298
 
QList<SensorProperties *> &SensorDisplay::sensors()
299
 
{
300
 
  return mSensors;
301
 
}
302
 
 
303
 
void SensorDisplay::rmbPressed()
304
 
{
305
 
  emit showPopupMenu( this );
306
 
}
307
 
 
308
 
void SensorDisplay::applySettings()
309
 
{
310
 
}
311
 
 
312
 
void SensorDisplay::applyStyle()
313
 
{
314
 
}
315
 
 
316
 
void SensorDisplay::setSensorOk( bool ok )
317
 
{
318
 
  if ( ok ) {
319
 
    if(mErrorIndicator)
320
 
      delete mErrorIndicator;
321
 
    mErrorIndicator = 0;
322
 
  } else {
323
 
    if ( mErrorIndicator )
324
 
      return;
325
 
    if ( !mPlotterWdg || mPlotterWdg->isVisible())
326
 
      return;
327
 
 
328
 
    QPixmap errorIcon = KIconLoader::global()->loadIcon( "dialog-error", KIconLoader::Desktop,
329
 
                                             KIconLoader::SizeSmall );
330
 
 
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() );
338
 
 
339
 
    mErrorIndicator->move( 0, 0 );
340
 
    mErrorIndicator->show();
341
 
  }
342
 
}
343
 
 
344
 
void SensorDisplay::changeEvent( QEvent * event ) {
345
 
  if (event->type() == QEvent::LanguageChange) {
346
 
    setTitle(mTitle);  //retranslate
347
 
  }
348
 
}
349
 
 
350
 
void SensorDisplay::setTitle( const QString &title )
351
 
{
352
 
  mTitle = title;
353
 
  mTranslatedTitle = i18n(title.toUtf8());
354
 
  emit titleChanged(mTitle);
355
 
  emit translatedTitleChanged(mTranslatedTitle);
356
 
}
357
 
 
358
 
QString SensorDisplay::translatedTitle() const
359
 
{
360
 
  return mTranslatedTitle;
361
 
}
362
 
 
363
 
QString SensorDisplay::title() const
364
 
{
365
 
  return mTitle;
366
 
}
367
 
 
368
 
void SensorDisplay::setUnit( const QString &unit )
369
 
{
370
 
  mUnit = unit;
371
 
}
372
 
 
373
 
QString SensorDisplay::unit() const
374
 
{
375
 
  return mUnit;
376
 
}
377
 
 
378
 
void SensorDisplay::setShowUnit( bool value )
379
 
{
380
 
  mShowUnit = value;
381
 
}
382
 
 
383
 
bool SensorDisplay::showUnit() const
384
 
{
385
 
  return mShowUnit;
386
 
}
387
 
 
388
 
void SensorDisplay::setPlotterWidget( QWidget *wdg )
389
 
{
390
 
  mPlotterWdg = wdg;
391
 
}
392
 
 
393
 
SensorProperties::SensorProperties()
394
 
{
395
 
}
396
 
 
397
 
SensorProperties::SensorProperties( const QString &hostName, const QString &name,
398
 
                                    const QString &type, const QString &description )
399
 
  : mName( name ), mType( type ), mDescription( description )
400
 
{
401
 
  setHostName(hostName);
402
 
  mOk = false;
403
 
}
404
 
 
405
 
SensorProperties::~SensorProperties()
406
 
{
407
 
}
408
 
 
409
 
void SensorProperties::setHostName( const QString &hostName )
410
 
{
411
 
  mHostName = hostName;
412
 
  mIsLocalhost = (mHostName.toLower() == "localhost" || mHostName.isEmpty());
413
 
}
414
 
 
415
 
bool SensorProperties::isLocalhost() const
416
 
{
417
 
  return mIsLocalhost;
418
 
}
419
 
 
420
 
QString SensorProperties::hostName() const
421
 
{
422
 
  return mHostName;
423
 
}
424
 
 
425
 
void SensorProperties::setName( const QString &name )
426
 
{
427
 
  mName = name;
428
 
}
429
 
 
430
 
QString SensorProperties::name() const
431
 
{
432
 
  return mName;
433
 
}
434
 
 
435
 
void SensorProperties::setType( const QString &type )
436
 
{
437
 
  mType = type;
438
 
}
439
 
 
440
 
QString SensorProperties::type() const
441
 
{
442
 
  return mType;
443
 
}
444
 
 
445
 
void SensorProperties::setDescription( const QString &description )
446
 
{
447
 
  mDescription = description;
448
 
}
449
 
 
450
 
QString SensorProperties::description() const
451
 
{
452
 
  return mDescription;
453
 
}
454
 
 
455
 
void SensorProperties::setUnit( const QString &unit )
456
 
{
457
 
  mUnit = unit;
458
 
}
459
 
 
460
 
QString SensorProperties::unit() const
461
 
{
462
 
  return mUnit;
463
 
}
464
 
 
465
 
void SensorProperties::setIsOk( bool value )
466
 
{
467
 
  mOk = value;
468
 
}
469
 
 
470
 
bool SensorProperties::isOk() const
471
 
{
472
 
  return mOk;
473
 
}
474
 
 
475
 
void SensorProperties::setRegExpName( const QString &name )
476
 
{
477
 
  mRegExpName = name;
478
 
}
479
 
QString SensorProperties::regExpName() const
480
 
{
481
 
  return mRegExpName;
482
 
}
483
 
 
484
 
#include "SensorDisplay.moc"