summaryrefslogtreecommitdiffstats
path: root/ksysguard/gui/SensorDisplayLib/SensorDisplay.cpp
blob: 4dfff79572c16fb73d10ad05ad2e15725bad3faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
    KSysGuard, the KDE System Guard

    Copyright (c) 1999 - 2002 Chris Schlaeger <cs@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    KSysGuard is currently maintained by Chris Schlaeger <cs@kde.org>.
    Please do not commit any changes without consulting me first. Thanks!

*/

#include <tqcheckbox.h>
#include <tqdom.h>
#include <tqpopupmenu.h>
#include <tqspinbox.h>
#include <tqwhatsthis.h>
#include <tqbitmap.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <krun.h>
#include <kservice.h>

#include "SensorManager.h"
#include "TimerSettings.h"

#include "SensorDisplay.h"

using namespace KSGRD;

SensorDisplay::SensorDisplay( TQWidget *parent, const char *name,
                              const TQString &title, bool nf, bool isApplet)
  :	TQWidget( parent, name )
{
  mIsApplet = isApplet;
  mSensors.setAutoDelete( true );

  // default interval is 2 seconds.
  mUpdateInterval = 2;
  mUseGlobalUpdateInterval = true;
  mModified = false;
  mShowUnit = false;
  mTimerId = NONE;
  mFrame = 0;
  mErrorIndicator = 0;
  mPlotterWdg = 0;

  setTimerOn( true );
  TQWhatsThis::add( this, "dummy" );
  
  if(!nf) {
    mFrame = new TQGroupBox( 2, Qt::Vertical, "", this, "displayFrame");
    mFrame->setFlat(true);
    mFrame->setAlignment(Qt::AlignHCenter);
    mFrame->setInsideMargin(2);

    setTitle( title );
    /* All RMB clicks to the box frame will be handled by
    * SensorDisplay::eventFilter. */
    mFrame->installEventFilter( this );
  }


  setMinimumSize( 16, 16 );
  setModified( false );
  setSensorOk( false );

  /* Let's call updateWhatsThis() in case the derived class does not do
   * this. */
  updateWhatsThis();
}

SensorDisplay::~SensorDisplay()
{
  if ( SensorMgr != 0 )
    SensorMgr->disconnectClient( this );

  killTimer( mTimerId );
}

void SensorDisplay::registerSensor( SensorProperties *sp )
{
  /* Make sure that we have a connection established to the specified
   * host. When a work sheet has been saved while it had dangling
   * sensors, the connect info is not saved in the work sheet. In such
   * a case the user can re-enter the connect information and the
   * connection will be established. */
  if ( !SensorMgr->engageHost( sp->hostName() ) ) {
    TQString msg = i18n( "It is impossible to connect to \'%1\'." ).arg( sp->hostName() );
    KMessageBox::error( this, msg );
  }

  mSensors.append( sp );
}

void SensorDisplay::unregisterSensor( uint pos )
{
  mSensors.remove( pos );
}

void SensorDisplay::configureUpdateInterval()
{
  TimerSettings dlg( this );

	dlg.setUseGlobalUpdate( mUseGlobalUpdateInterval );
  dlg.setInterval( mUpdateInterval );

  if ( dlg.exec() ) {
    if ( dlg.useGlobalUpdate() ) {
      mUseGlobalUpdateInterval = true;

      SensorBoard* sb = dynamic_cast<SensorBoard*>( parentWidget() );
      if ( !sb ) {
        kdDebug(1215) << "dynamic cast lacks" << endl;
        setUpdateInterval( 2 );
      } else {
        setUpdateInterval( sb->updateInterval() );
      }
    } else {
      mUseGlobalUpdateInterval = false;
      setUpdateInterval( dlg.interval() );
    }

    setModified( true );
  }
}

void SensorDisplay::timerEvent( TQTimerEvent* )
{
  int i = 0;
  for ( SensorProperties *s = mSensors.first(); s; s = mSensors.next(), ++i )
    sendRequest( s->hostName(), s->name(), i );
}

void SensorDisplay::resizeEvent( TQResizeEvent* )
{
  if(mFrame)
    mFrame->setGeometry( rect() );
}

bool SensorDisplay::eventFilter( TQObject *object, TQEvent *event )
{
  if ( event->type() == TQEvent::MouseButtonPress &&
     ( (TQMouseEvent*)event)->button() == Qt::RightButton ) {
    TQPopupMenu pm;
    if ( mIsApplet ) {
      pm.insertItem( i18n( "Launch &System Guard"), 1 );
      pm.insertSeparator();
    }
    if ( hasSettingsDialog() )
      pm.insertItem( i18n( "&Properties" ), 2 );
    pm.insertItem( i18n( "&Remove Display" ), 3 );
    pm.insertSeparator();
    pm.insertItem( i18n( "&Setup Update Interval..." ), 4 );
    if ( !timerOn() )
      pm.insertItem( i18n( "&Continue Update" ), 5 );
    else
      pm.insertItem( i18n( "P&ause Update" ), 6 );

    switch ( pm.exec( TQCursor::pos() ) ) {
      case 1:
        KRun::run(*KService::serviceByDesktopName("ksysguard"), KURL::List());
	break;
      case 2:
        configureSettings();
        break;
      case 3: {
          TQCustomEvent *e = new TQCustomEvent( TQEvent::User );
          e->setData( this );
          kapp->postEvent( parent(), e );
        }
        break;
      case 4:
        configureUpdateInterval();
        break;
      case 5:
        setTimerOn( true );
        setModified( true );
        break;
      case 6:
        setTimerOn( false );
        setModified( true );
        break;
    }

    return true;
  } else if ( event->type() == TQEvent::MouseButtonRelease &&
            ( ( TQMouseEvent*)event)->button() == Qt::LeftButton ) {
    setFocus();
  }

  return TQWidget::eventFilter( object, event );
}

void SensorDisplay::sendRequest( const TQString &hostName,
                                 const TQString &command, int id )
{
  if ( !SensorMgr->sendRequest( hostName, command, (SensorClient*)this, id ) )
    sensorError( id, true );
}

void SensorDisplay::sensorError( int sensorId, bool err )
{
  if ( sensorId >= (int)mSensors.count() || sensorId < 0 )
    return;

  if ( err == mSensors.at( sensorId )->isOk() ) {
    // this happens only when the sensorOk status needs to be changed.
		mSensors.at( sensorId )->setIsOk( !err );
	}

  bool ok = true;
  for ( uint i = 0; i < mSensors.count(); ++i )
    if ( !mSensors.at( i )->isOk() ) {
      ok = false;
      break;
    }

  setSensorOk( ok );
}

void SensorDisplay::updateWhatsThis()
{
  TQWhatsThis::add( this, i18n(
    "<qt><p>This is a sensor display. To customize a sensor display click "
    "and hold the right mouse button on either the frame or the "
    "display box and select the <i>Properties</i> entry from the popup "
    "menu. Select <i>Remove</i> to delete the display from the worksheet."
    "</p>%1</qt>" ).arg( additionalWhatsThis() ) );
}

void SensorDisplay::hosts( TQStringList& list )
{
  for ( SensorProperties *s = mSensors.first(); s; s = mSensors.next() )
    if ( !list.contains( s->hostName() ) )
      list.append( s->hostName() );
}

TQColor SensorDisplay::restoreColor( TQDomElement &element, const TQString &attr,
                                    const TQColor& fallback )
{
  bool ok;
  uint c = element.attribute( attr ).toUInt( &ok );
  if ( !ok )
    return fallback;
  else
    return TQColor( (c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF );
}

void SensorDisplay::saveColor( TQDomElement &element, const TQString &attr,
                               const TQColor &color )
{
  int r, g, b;
  color.rgb( &r, &g, &b );
  element.setAttribute( attr, (r << 16) | (g << 8) | b );
}

bool SensorDisplay::addSensor( const TQString &hostName, const TQString &name,
                               const TQString &type, const TQString &description )
{
  registerSensor( new SensorProperties( hostName, name, type, description ) );
  return true;
}

bool SensorDisplay::removeSensor( uint pos )
{
  unregisterSensor( pos );
  return true;
}

void SensorDisplay::setUpdateInterval( uint interval )
{
  bool timerActive = timerOn();

  if ( timerActive )
    setTimerOn( false );

  mUpdateInterval = interval;

  if ( timerActive )
    setTimerOn( true );
}

bool SensorDisplay::hasSettingsDialog() const
{
  return false;
}

void SensorDisplay::configureSettings()
{
}

void SensorDisplay::setUseGlobalUpdateInterval( bool value )
{
  mUseGlobalUpdateInterval = value;
}

bool SensorDisplay::useGlobalUpdateInterval() const
{
  return mUseGlobalUpdateInterval;
}

TQString SensorDisplay::additionalWhatsThis()
{
  return TQString::null;
}

void SensorDisplay::sensorLost( int reqId )
{
  sensorError( reqId, true );
}

bool SensorDisplay::restoreSettings( TQDomElement &element )
{
  TQString str = element.attribute( "showUnit", "X" );
  if(!str.isEmpty() && str != "X") {
    mShowUnit = str.toInt();
  }
  str = element.attribute( "unit", TQString::null );
  if(!str.isEmpty())
    setUnit(str);
  str = element.attribute( "title", TQString::null );
  if(!str.isEmpty())
    setTitle(str);

  if ( element.attribute( "updateInterval" ) != TQString::null ) {
    mUseGlobalUpdateInterval = false;
    setUpdateInterval( element.attribute( "updateInterval", "2" ).toInt() );
  } else {
    mUseGlobalUpdateInterval = true;

    SensorBoard* sb = dynamic_cast<SensorBoard*>( parentWidget() );
    if ( !sb ) {
      kdDebug(1215) << "dynamic cast lacks" << endl;
      setUpdateInterval( 2 );
    } else
      setUpdateInterval( sb->updateInterval() );
  }

  if ( element.attribute( "pause", "0" ).toInt() == 0 )
    setTimerOn( true );
  else
    setTimerOn( false );

  return true;
}

bool SensorDisplay::saveSettings( TQDomDocument&, TQDomElement &element, bool )
{
  element.setAttribute( "title", title() );
  element.setAttribute( "unit", unit() );
  element.setAttribute( "showUnit", mShowUnit );

  if ( mUseGlobalUpdateInterval )
    element.setAttribute( "globalUpdate", "1" );
  else {
    element.setAttribute( "globalUpdate", "0" );
    element.setAttribute( "updateInterval", mUpdateInterval );
  }

  if ( !timerOn() )
    element.setAttribute( "pause", 1 );
  else
    element.setAttribute( "pause", 0 );

  return true;
}

void SensorDisplay::setTimerOn( bool on )
{
  if ( on ) {
    if ( mTimerId == NONE )
      mTimerId = startTimer( mUpdateInterval * 1000 );
  } else {
    if ( mTimerId != NONE ) {
      killTimer( mTimerId );
      mTimerId = NONE;
    }
  }
}

bool SensorDisplay::timerOn() const
{
  return ( mTimerId != NONE );
}

bool SensorDisplay::modified() const
{
  return mModified;
}

TQPtrList<SensorProperties> &SensorDisplay::sensors()
{
  return mSensors;
}

void SensorDisplay::rmbPressed()
{
  emit showPopupMenu( this );
}

void SensorDisplay::applySettings()
{
}

void SensorDisplay::applyStyle()
{
}

void SensorDisplay::setModified( bool value )
{
  if ( value != mModified ) {
    mModified = value;
    emit modified( mModified );
  }
}

void SensorDisplay::setSensorOk( bool ok )
{
  if ( ok ) {
    delete mErrorIndicator;
    mErrorIndicator = 0;
  } else {
    if ( mErrorIndicator )
      return;

    TQPixmap errorIcon = TDEGlobal::iconLoader()->loadIcon( "connect_creating", TDEIcon::Desktop,
                                                          TDEIcon::SizeSmall );
    if ( !mPlotterWdg )
      return;

    mErrorIndicator = new TQWidget( mPlotterWdg );
    mErrorIndicator->setErasePixmap( errorIcon );
    mErrorIndicator->resize( errorIcon.size() );
    if ( errorIcon.mask() )
      mErrorIndicator->setMask( *errorIcon.mask() );
    mErrorIndicator->move( 0, 0 );
    mErrorIndicator->show();
  }
}

void SensorDisplay::setTitle( const TQString &title )
{
  mTitle = title;

  if(!mFrame) {
    return; //fixme. create a frame and move widget inside it.
  }

  /* Changing the frame title may increase the width of the frame and
   * hence breaks the layout. To avoid this, we save the original size
   * and restore it after we have set the frame title. */
  TQSize s = mFrame->size();

  if ( mShowUnit && !mUnit.isEmpty() )
    mFrame->setTitle( mTitle + " [" + mUnit + "]" );
  else
    mFrame->setTitle( mTitle );
  mFrame->setGeometry( 0, 0, s.width(), s.height() );
}

TQString SensorDisplay::title() const
{
  return mTitle;
}

void SensorDisplay::setUnit( const TQString &unit )
{
  mUnit = unit;
}

TQString SensorDisplay::unit() const
{
  return mUnit;
}

void SensorDisplay::setShowUnit( bool value )
{
  mShowUnit = value;
}

bool SensorDisplay::showUnit() const
{
  return mShowUnit;
}

void SensorDisplay::setPlotterWidget( TQWidget *wdg )
{
  mPlotterWdg = wdg;

}

TQWidget *SensorDisplay::frame()
{
  return mFrame;
}

//void SensorDisplay::setNoFrame( bool /*value*/ )
//{
	//FIXME - delete or create the frame as needed
//}

bool SensorDisplay::noFrame() const
{
  return !mFrame;
}

void SensorDisplay::reorderSensors(const TQValueList<int> &orderOfSensors)
{
  TQPtrList<SensorProperties> newSensors;
  for ( uint i = 0; i < orderOfSensors.count(); ++i ) {
    newSensors.append( mSensors.at(orderOfSensors[i] ));
  }

  mSensors.setAutoDelete( false );
  mSensors = newSensors;
  mSensors.setAutoDelete( true );
}


SensorProperties::SensorProperties()
{
}

SensorProperties::SensorProperties( const TQString &hostName, const TQString &name,
                                    const TQString &type, const TQString &description )
  : mHostName( hostName ), mName( name ), mType( type ), mDescription( description )
{
  mOk = false;
}

SensorProperties::~SensorProperties()
{
}

void SensorProperties::setHostName( const TQString &hostName )
{
  mHostName = hostName;
}

TQString SensorProperties::hostName() const
{
  return mHostName;
}

void SensorProperties::setName( const TQString &name )
{
  mName = name;
}

TQString SensorProperties::name() const
{
  return mName;
}

void SensorProperties::setType( const TQString &type )
{
  mType = type;
}

TQString SensorProperties::type() const
{
  return mType;
}

void SensorProperties::setDescription( const TQString &description )
{
  mDescription = description;
}

TQString SensorProperties::description() const
{
  return mDescription;
}

void SensorProperties::setUnit( const TQString &unit )
{
  mUnit = unit;
}

TQString SensorProperties::unit() const
{
  return mUnit;
}

void SensorProperties::setIsOk( bool value )
{
  mOk = value;
}

bool SensorProperties::isOk() const
{
  return mOk;
}

#include "SensorDisplay.moc"