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

« back to all changes in this revision

Viewing changes to ksysguard/gui/SensorDisplayLib/SensorDisplay.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 - 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( 0 );
 
120
      menuEmpty = false;
 
121
    }
 
122
    if(mSharedSettings && !mSharedSettings->locked) {
 
123
      action = pm.addAction( i18n( "&Remove Display" ) );
 
124
      action->setData( 1 );
 
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 0:
 
133
          configureSettings();
 
134
          break;
 
135
        case 1: {
 
136
            if ( mDeleteNotifier ) {
 
137
              DeleteEvent *event = new DeleteEvent( this );
 
138
              kapp->postEvent( mDeleteNotifier, event );
 
139
            }
 
140
          }
 
141
          break;
 
142
      }
 
143
    }
 
144
}
 
145
 
 
146
bool SensorDisplay::eventFilter( QObject *object, QEvent *event )
 
147
{
 
148
  if ( event->type() == QEvent::MouseButtonPress) {
 
149
    QMouseEvent *e = static_cast<QMouseEvent *> (event);
 
150
    if( e->button() == Qt::RightButton ) {
 
151
      showContextMenu( mapFromGlobal( e->globalPos() ) );
 
152
      return true;
 
153
    }
 
154
  } 
 
155
 
 
156
  return QWidget::eventFilter( object, event );
 
157
}
 
158
void SensorDisplay::sendRequest( const QString &hostName,
 
159
                                 const QString &command, int id )
 
160
{
 
161
  if ( !SensorMgr->sendRequest( hostName, command, (SensorClient*)this, id ) ) {
 
162
    sensorError( id, true );
 
163
  }
 
164
}
 
165
 
 
166
void SensorDisplay::sensorError( int sensorId, bool err )
 
167
{
 
168
  if ( sensorId >= (int)mSensors.count() || sensorId < 0 )
 
169
    return;
 
170
 
 
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 );
 
174
  }
 
175
 
 
176
  bool ok = true;
 
177
  for ( uint i = 0; i < (uint)mSensors.count(); ++i )
 
178
    if ( !mSensors.at( i )->isOk() ) {
 
179
      ok = false;
 
180
      break;
 
181
    }
 
182
 
 
183
  setSensorOk( ok );
 
184
}
 
185
 
 
186
void SensorDisplay::updateWhatsThis()
 
187
{
 
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() ) );
 
195
  else
 
196
      this->setWhatsThis( additionalWhatsThis());
 
197
}
 
198
 
 
199
void SensorDisplay::hosts( QStringList& list )
 
200
{
 
201
  foreach( SensorProperties *s, mSensors)
 
202
    if ( !list.contains( s->hostName() ) )
 
203
      list.append( s->hostName() );
 
204
}
 
205
 
 
206
QColor SensorDisplay::restoreColor( QDomElement &element, const QString &attr,
 
207
                                    const QColor& fallback )
 
208
{
 
209
  bool ok;
 
210
  int color = element.attribute( attr ).toUInt( &ok, 0 );
 
211
  
 
212
  if ( !ok ) {
 
213
    kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr) << " (Not a valid number)";
 
214
    return fallback;
 
215
  }
 
216
  QColor c( (color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff), (color & 0xff000000) >> 24);
 
217
  if( !c.isValid()) {
 
218
    kDebug(1215) << "Invalid color read in from worksheet for " << attr << " = " << element.attribute(attr);
 
219
    return fallback;
 
220
  }
 
221
 
 
222
  if(c.alpha() == 0) c.setAlpha(255);
 
223
  return c;
 
224
}
 
225
 
 
226
void SensorDisplay::saveColor( QDomElement &element, const QString &attr,
 
227
                               const QColor &color )
 
228
{
 
229
  element.setAttribute( attr, "0x" + QString::number(color.rgba(),16) );
 
230
}
 
231
 
 
232
void SensorDisplay::saveColorAppend( QDomElement &element, const QString &attr,
 
233
                               const QColor &color )
 
234
{
 
235
  element.setAttribute( attr, element.attribute(attr) + ",0x" + QString::number(color.rgba(),16) );
 
236
}
 
237
 
 
238
bool SensorDisplay::addSensor( const QString &hostName, const QString &name,
 
239
                               const QString &type, const QString &description )
 
240
{
 
241
  registerSensor( new SensorProperties( hostName, name, type, description ) );
 
242
  return true;
 
243
}
 
244
 
 
245
bool SensorDisplay::removeSensor( uint pos )
 
246
{
 
247
  if((int) pos >= mSensors.count())
 
248
    return false;
 
249
  unregisterSensor( pos );
 
250
  return true;
 
251
}
 
252
 
 
253
bool SensorDisplay::hasSettingsDialog() const
 
254
{
 
255
  return false;
 
256
}
 
257
 
 
258
void SensorDisplay::configureSettings()
 
259
{
 
260
}
 
261
 
 
262
QString SensorDisplay::additionalWhatsThis()
 
263
{
 
264
  return QString();
 
265
}
 
266
 
 
267
void SensorDisplay::sensorLost( int reqId )
 
268
{
 
269
  sensorError( reqId, true );
 
270
}
 
271
 
 
272
void SensorDisplay::setDeleteNotifier( QObject *object )
 
273
{
 
274
  mDeleteNotifier = object;
 
275
}
 
276
 
 
277
bool SensorDisplay::restoreSettings( QDomElement &element )
 
278
{
 
279
  mShowUnit = element.attribute( "showUnit", "0" ).toInt();
 
280
  setUnit( element.attribute( "unit", QString() ) );
 
281
  setTitle( element.attribute( "title", title() ) );
 
282
 
 
283
  return true;
 
284
}
 
285
 
 
286
bool SensorDisplay::saveSettings( QDomDocument&, QDomElement &element )
 
287
{
 
288
  element.setAttribute( "title", title() );
 
289
  element.setAttribute( "unit", unit() );
 
290
  element.setAttribute( "showUnit", mShowUnit );
 
291
 
 
292
  return true;
 
293
}
 
294
 
 
295
QList<SensorProperties *> &SensorDisplay::sensors()
 
296
{
 
297
  return mSensors;
 
298
}
 
299
 
 
300
void SensorDisplay::rmbPressed()
 
301
{
 
302
  emit showPopupMenu( this );
 
303
}
 
304
 
 
305
void SensorDisplay::applySettings()
 
306
{
 
307
}
 
308
 
 
309
void SensorDisplay::applyStyle()
 
310
{
 
311
}
 
312
 
 
313
void SensorDisplay::setSensorOk( bool ok )
 
314
{
 
315
  if ( ok ) {
 
316
    delete mErrorIndicator;
 
317
    mErrorIndicator = 0;
 
318
  } else {
 
319
    if ( mErrorIndicator )
 
320
      return;
 
321
    if ( !mPlotterWdg || mPlotterWdg->isVisible())
 
322
      return;
 
323
 
 
324
    QPixmap errorIcon = KIconLoader::global()->loadIcon( "dialog-error", KIconLoader::Desktop,
 
325
                                             KIconLoader::SizeSmall );
 
326
 
 
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() );
 
334
 
 
335
    mErrorIndicator->move( 0, 0 );
 
336
    mErrorIndicator->show();
 
337
  }
 
338
}
 
339
 
 
340
void SensorDisplay::changeEvent( QEvent * event ) {
 
341
  if (event->type() == QEvent::LanguageChange) {
 
342
    setTitle(mTitle);  //retranslate
 
343
  }
 
344
}
 
345
 
 
346
void SensorDisplay::setTitle( const QString &title )
 
347
{
 
348
  mTitle = title;
 
349
  mTranslatedTitle = i18n(title.toUtf8());
 
350
  emit titleChanged(mTitle);
 
351
  emit translatedTitleChanged(mTranslatedTitle);
 
352
}
 
353
 
 
354
QString SensorDisplay::translatedTitle() const
 
355
{
 
356
  return mTranslatedTitle;
 
357
}
 
358
 
 
359
QString SensorDisplay::title() const
 
360
{
 
361
  return mTitle;
 
362
}
 
363
 
 
364
void SensorDisplay::setUnit( const QString &unit )
 
365
{
 
366
  mUnit = unit;
 
367
}
 
368
 
 
369
QString SensorDisplay::unit() const
 
370
{
 
371
  return mUnit;
 
372
}
 
373
 
 
374
void SensorDisplay::setShowUnit( bool value )
 
375
{
 
376
  mShowUnit = value;
 
377
}
 
378
 
 
379
bool SensorDisplay::showUnit() const
 
380
{
 
381
  return mShowUnit;
 
382
}
 
383
 
 
384
void SensorDisplay::setPlotterWidget( QWidget *wdg )
 
385
{
 
386
  mPlotterWdg = wdg;
 
387
}
 
388
 
 
389
SensorProperties::SensorProperties()
 
390
{
 
391
}
 
392
 
 
393
SensorProperties::SensorProperties( const QString &hostName, const QString &name,
 
394
                                    const QString &type, const QString &description )
 
395
  : mName( name ), mType( type ), mDescription( description )
 
396
{
 
397
  setHostName(hostName);
 
398
  mOk = false;
 
399
}
 
400
 
 
401
SensorProperties::~SensorProperties()
 
402
{
 
403
}
 
404
 
 
405
void SensorProperties::setHostName( const QString &hostName )
 
406
{
 
407
  mHostName = hostName;
 
408
  mIsLocalhost = (mHostName.toLower() == "localhost" || mHostName.isEmpty());
 
409
}
 
410
 
 
411
bool SensorProperties::isLocalhost() const
 
412
{
 
413
  return mIsLocalhost;
 
414
}
 
415
 
 
416
QString SensorProperties::hostName() const
 
417
{
 
418
  return mHostName;
 
419
}
 
420
 
 
421
void SensorProperties::setName( const QString &name )
 
422
{
 
423
  mName = name;
 
424
}
 
425
 
 
426
QString SensorProperties::name() const
 
427
{
 
428
  return mName;
 
429
}
 
430
 
 
431
void SensorProperties::setType( const QString &type )
 
432
{
 
433
  mType = type;
 
434
}
 
435
 
 
436
QString SensorProperties::type() const
 
437
{
 
438
  return mType;
 
439
}
 
440
 
 
441
void SensorProperties::setDescription( const QString &description )
 
442
{
 
443
  mDescription = description;
 
444
}
 
445
 
 
446
QString SensorProperties::description() const
 
447
{
 
448
  return mDescription;
 
449
}
 
450
 
 
451
void SensorProperties::setUnit( const QString &unit )
 
452
{
 
453
  mUnit = unit;
 
454
}
 
455
 
 
456
QString SensorProperties::unit() const
 
457
{
 
458
  return mUnit;
 
459
}
 
460
 
 
461
void SensorProperties::setIsOk( bool value )
 
462
{
 
463
  mOk = value;
 
464
}
 
465
 
 
466
bool SensorProperties::isOk() const
 
467
{
 
468
  return mOk;
 
469
}
 
470
 
 
471
void SensorProperties::setRegExpName( const QString &name )
 
472
{
 
473
  mRegExpName = name;
 
474
}
 
475
QString SensorProperties::regExpName() const
 
476
{
 
477
  return mRegExpName;
 
478
}
 
479
 
 
480
#include "SensorDisplay.moc"