root/DRIM_GUI/lime-perf/trunk/src/libsrc/monitor_gui.cpp

Revision 1129, 23.1 kB (checked in by dcwang, 2 years ago)

sync with our internal CVS server, fix some bugs when develop Asianux 3.0 SP1 and Red Flag Asianux 3.0 SP1 Tool CD

Line 
1 /*
2  * $Id: monitor_gui.cpp,v 2.2 2008/07/14 07:55:57 cvstaohe Exp $
3  *
4  * lime monitor main GUI
5  *
6  * FIXME:
7  *   o need more sensors, such as temperature, voltage, fan, network connection...
8  *   o text mode display
9  *   o .mon file parsing error handling
10  */
11
12 #include <stdlib.h>
13 #include <assert.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/utsname.h>
18 #include "lime-perf.h"
19
20 #include <qapplication.h>
21 #include <qpainter.h>
22 #include <qframe.h>
23 #include <qregexp.h>
24 #include <qpopupmenu.h>
25 #include <qlistview.h>
26 #include <qlayout.h>
27 #include <qlineedit.h>
28 #include <qvalidator.h>
29 #include <qlabel.h>
30 #include <qlistbox.h>
31 #include <qpushbutton.h>
32 #include <qcolordialog.h>
33 #include <math.h>
34 #include <qslider.h>
35 #include <qgroupbox.h>
36 #include <qdatetime.h>
37 #include <qmessagebox.h>
38 #include <qtranslator.h>
39 #include <qtextcodec.h>
40 #include <qfiledialog.h>
41 #include <qkeysequence.h>
42 #include <qtabwidget.h>
43 #include <qmenubar.h>
44 #include <qtoolbar.h>
45 #include <qwindowsstyle.h>
46 #include <qcursor.h>
47 #include <qtextbrowser.h>
48 #include <qsplitter.h>
49
50 #include "icon/properties.xpm"
51 //#include "icon/fileopen.xpm"
52 //#include "icon/filesave.xpm"
53 #include "icon/add.xpm"
54 #include "icon/del.xpm"
55 #include "icon/pause.xpm"
56 #include "icon/reset.xpm"
57 #include "icon/curve.xpm"
58 #include "icon/bar.xpm"
59 #include "icon/clear.xpm"
60 #include "icon/step.xpm"
61
62 #include "monitor_gui.h"
63 #include "alarm_gui.h"
64 #include "addsensordlg.h"
65 #include "monprop_gui.h"
66 #include "corefile/SensorManager.h"
67 #include "activesensor.h"
68
69 #define WAIT_CURSOR()                   \
70         QCursor old_c = cursor();       \
71         QCursor wait_c(Qt::WaitCursor); \
72         setCursor(wait_c);
73
74 #define NORM_CURSOR() setCursor(old_c);
75
76
77 Canvas::Canvas(QWidget* parent, const char *name, MonitorGUI *m)
78         : QFrame(parent, name), monitor(m)
79 {
80 //      setPaletteBackgroundColor(Qt::black);
81         setFrameShape( QFrame::StyledPanel );
82         setFrameShadow( QFrame::Sunken );
83 }
84
85 void
86 Canvas::paintEvent( QPaintEvent *e )
87 {
88         monitor->paintEvent(e);
89 }
90
91 void
92 Canvas::resizeEvent( QResizeEvent * )
93 {
94         monitor->paintEvent(NULL);
95 }
96
97 void
98 Canvas::mouseMoveEvent ( QMouseEvent * e )
99 {
100         qDebug("(%d,%d)", e->x(), e->y());
101 }
102
103 Marker::Marker( MonitorGUI *m, const char *name )
104         : QWidget( m->fra, name, WStaticContents ), monitor(m)
105 {}
106
107 void
108 Marker::paintEvent( QPaintEvent * )
109 {
110         draw_marker();
111 }
112
113 void
114 Marker::resizeEvent( QResizeEvent * )
115 {
116         draw_marker();
117         update();
118 }
119
120 static int
121 grid_number(int h)
122 {
123         int min_grid_h = 30;
124         int n;
125         int grid_h;
126
127         /* compute marker number */
128         if ((grid_h = h/10) > min_grid_h)
129                 n = 10;
130         else if ((grid_h = h/5) > min_grid_h)
131                 n = 5;
132         else
133         {
134                 grid_h = h/2;
135                 n = 2;
136         }
137         return n;
138 }
139
140 void
141 Marker::draw_marker()
142 {
143         int h = monitor->canvas->height();
144         int n = grid_number(h);;
145
146         QPainter p(this);
147         p.fillRect(0, 0, width(), height(),
148                    QBrush(backgroundColor()));
149         p.setPen( Qt::black );
150         for (int i = 0; i <= n; i++)
151         {
152                 int y = (i*h)/n;
153                 QString s = QString::number(i*100/n);
154                 p.drawText(0, h-y+20, s.rightJustify(3, ' '));
155         }       
156 }
157
158 MonitorGUI::MonitorGUI( SensorManager * sm,HostItem *hostItem, QWidget* parent, const char* name)
159         : MonitorBase(parent, name),
160           sensor_dlg(0), prop_dlg(0)
161 {
162         /* init sensor manager */
163         SetInit(sm,hostItem);
164         active_sensors.setAutoDelete(TRUE);
165        
166         /* set init sample rate: 1 sec */
167         sample_rate = 1;
168         startTimer(sample_rate*1000);
169         showmode = 0; /* curve mode */
170        
171         /* init sensor listview */
172         /* FIXME: need to spread the listview column nicely */
173
174         /* init frame */
175         step = 5; /* pixels */
176         cur_x = -step;
177
178         /* init splitter size */
179         {
180                 QValueList<int> vl;
181                 vl.append(400); /* init size of view window */
182                 splitter->setSizes(vl);
183         }
184        
185         /* init canvas */
186         QFrame* canvas_main_frame = new QFrame(fra);
187         canvas_main_frame->setFrameShape( QFrame::NoFrame );
188         QFrame* canvas_frame = new QFrame(canvas_main_frame);
189         canvas_frame->setFrameShape( QFrame::StyledPanel );
190         canvas_frame->setFrameShadow( QFrame::Sunken );
191         canvas = new Canvas( canvas_frame, "", this );
192         QVBoxLayout* canvas_layout = new QVBoxLayout(canvas_frame, 1, 0, "CanvasLayout");
193         canvas_layout->addWidget( canvas );
194         QVBoxLayout* canvas_main_layout = new QVBoxLayout(canvas_main_frame, 10, 0, "CanvasMainLayout");
195         canvas_main_layout->addWidget( canvas_frame );
196
197         /* init marker */
198         marker = new Marker( this );
199         marker->setMaximumSize( QSize( 25, 32767 ) );
200
201         /* glue canvas and marker together */
202         QHBoxLayout* layout = new QHBoxLayout(fra, 0, 0, "MarkerAndCanvasLayout");
203         layout->addWidget( marker );   
204         layout->addWidget( canvas_main_frame );
205        
206         /* init last, avg, max, min labels */
207         last_lbl->setMinimumSize( QSize( 20, 0 ) );
208         avg_lbl->setMinimumSize( QSize( 20, 0 ) );
209         min_lbl->setMinimumSize( QSize( 20, 0 ) );
210         max_lbl->setMinimumSize( QSize( 20, 0 ) );
211
212         sensors_lv->setSorting(2); /* sort by sensor name */
213         sensors_lv->setShowSortIndicator(TRUE);
214
215         /* init connections */
216         connect( sensors_lv, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ), this,
217                  SLOT( do_active_sensors_mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ) );
218
219         connect( ratio_sld, SIGNAL( valueChanged(int) ),
220                  this, SLOT( do_ratio_changed(int) ) );
221         connect( color_btn, SIGNAL( clicked() ),
222                  this, SLOT( do_color() ) );
223         connect( sensors_lv, SIGNAL( selectionChanged(QListViewItem*) ),
224                  this, SLOT( do_active_sensors_select(QListViewItem*) ) );
225        
226         /* init actions and menu items */
227
228         /* init sensor popup menu */
229 //      sensor_popup = new QPopupMenu(this);
230 //      addAction->addTo( sensor_popup );
231 //      delAction->addTo( sensor_popup );
232 //      sensor_popup->insertSeparator();
233 //      propertiesAction->addTo( sensor_popup );
234
235        layout11 = new QHBoxLayout( 0, 0, 6, "layout11");
236        layout22 = new QHBoxLayout( 0, 0, 6, "layout22");
237        layout44 = new QVBoxLayout( 0, 11, 6, "layout44");
238         /* init sensors */
239         show_active_sensors();
240
241 }
242
243
244 void MonitorGUI::SetInit(SensorManager * sm,HostItem * hostItem)
245 {
246         //init_actions();
247         this->sm=sm;
248         this->hostItem=hostItem;
249         do_reset();
250 }
251
252 void
253 MonitorGUI::do_color()
254 {
255         ActiveSensor* as = get_current_as();
256         if (!as)
257                 return;
258         QColor c = QColorDialog::getColor(as->color);
259         if (c.isValid())
260                 as->color = c;
261         show_active_sensors(true);
262 }
263
264 int
265 MonitorGUI::init_actions()
266 {
267         return 0;
268 }
269
270
271 void
272 MonitorGUI::do_properties()
273 {
274         if (!prop_dlg)
275                 prop_dlg = new MonPropGUI(this, this, "", TRUE);
276         prop_dlg->do_show();
277 }
278
279
280 int MonitorGUI::get_sample_rate()
281 {
282         return sample_rate;
283 }
284
285 int
286 MonitorGUI::set_sample_rate(int sr)
287 {
288         sample_rate = sr;
289         if (pause) {
290                 killTimers();
291                 if (sr > 0)
292                         startTimer(sample_rate*1000);
293         }
294         return 0;
295 }
296
297 void
298 MonitorGUI::do_add_sensor()
299 {
300         if (sensor_dlg)
301                 delete sensor_dlg;
302         sensor_dlg = new AddSensorDlg(this, sm, this);
303         sensor_dlg->exec();
304 }
305
306 void
307 MonitorGUI::do_del_sensor()
308 {
309         delete_current_sensor();
310 }
311
312 void
313 MonitorGUI::do_reset()
314 {
315         active_sensors.clear();
316         for(map<int,LastSensorPair*>::iterator it=last_sensors.begin();it!=last_sensors.end();it++)
317         {
318                 if((*it).second) delete (*it).second;
319         }
320         last_sensors.clear();
321         sensors_lv->clear();
322         cur_x = -step;
323         show_active_sensors(true); /* refresh the sensor list */
324 }
325
326 void MonitorGUI::refresh()
327 {
328         active_sensors.clear();
329         sensors_lv->clear();
330         if(ng)
331         {
332                 if(ng->currentHostItem)
333                 {
334                         //ng->currentHostItem->connectHost();
335                         ng->client=ng->currentHostItem->client;
336                         if(ng->sm) ng->sm->SetHostItem(ng->currentHostItem);
337                         else ng->sm= new SensorManager(ng->currentHostItem);
338                         int ret=0;
339                         try{
340                                 ng->sm->GetAllInstance();
341                         }CATCH_EXCEPTION(NULL,_("Initialize Failure"),_("Fetch the statisics Instances Failure!\n"),ret)
342                         while(ret==2)
343                         {
344                                 ret=0;
345                                 if(! ng->getConsole()->getHostListBar()->sltEditHost())
346                                 {
347                                         ng->alarm->SetInit(sm,NULL);
348                                         break;
349                                 }
350                                 ng->currentHostItem=ng->getConsole()->getHostListBar()->currentHostItem();
351                                 ng->client=ng->currentHostItem->client;
352                                 ng->sm->SetHostItem(ng->currentHostItem);
353                                 try{
354                                         ng->sm->GetAllInstance();
355                                 }CATCH_EXCEPTION(this,_("Initialize Failure"),_("Fetch the statisics Instances Failure!\n"),ret)
356                         }
357                         hostItem=ng->currentHostItem;
358                         ng->alarm->SetInit(ng->sm,ng->currentHostItem);
359
360                 }
361         }
362         cur_x = -step;
363         //add sensors
364         for(map<int,LastSensorPair*>::iterator it=last_sensors.begin();it!=last_sensors.end();it++)
365         {
366                 if((*it).second)
367                 {
368                         add_sensor((*it).second->instance,(*it).second->property);
369                 }
370         }
371 }
372
373
374 void
375 MonitorGUI::do_step()
376 {
377         timerEvent(NULL);
378 }
379
380 void
381 MonitorGUI::do_clear()
382 {
383         cur_x = -step;
384         for (ActiveSensor* as = active_sensors.first(); as; as = active_sensors.next() )
385                 as->clear_value();
386 //      sm->ClearSensorList();
387         update();
388 }
389
390 void MonitorGUI::do_pause(bool pause)
391 {       
392         this->pause=pause;
393         if (pause)
394         {
395                 /*
396                 qDebug("pause");
397                 stepAction->setEnabled(TRUE);
398                 pauseAction->setText( _( "Continue" ) );
399                 pauseAction->setMenuText( _( "&Continue" ) );
400                 pauseAction->setToolTip( _( "Continue" ) );
401                 pauseAction->setStatusTip( _( "Continue display" ) );
402 //              pauseAction->setAccel(QKeySequence(_("Ctrl+C")));
403 */
404                 killTimers();
405         }
406         else
407         {
408 /*
409                 qDebug("continue");
410                 stepAction->setEnabled(FALSE);
411                 pauseAction->setText( _( "Pause" ) );
412                 pauseAction->setMenuText( _( "&Pause" ) );
413                 pauseAction->setToolTip( _( "Pause" ) );
414                 pauseAction->setStatusTip( _( "Pause display" ) );
415 //              pauseAction->setAccel(QKeySequence(_("Ctrl+P")));
416 */
417                 if (sample_rate > 0)
418                         startTimer(sample_rate*1000);
419                 else
420                         killTimers();
421         }
422 }
423
424 void
425 MonitorGUI::do_ratio_changed(int n)
426 {
427         ActiveSensorItem* item;
428         item = (ActiveSensorItem*)sensors_lv->currentItem();
429         if (!item)
430                 return;
431         ActiveSensor* as = item->as;
432         if (!as)
433                 return;
434         double ratio = pow(10.0, (double)n);
435         as->ratio = ratio;
436         QString ratio_str = QString::number(as->ratio, 'f', 10);
437         ratio_str = ratio_str.replace( QRegExp("0+$"), "" );
438         ratio_str = ratio_str.replace( QRegExp("\\.$"), ".0" );
439         ratio_lbl->setText(ratio_str);
440         show_active_sensors(true);
441         update();
442 }
443
444 void
445 MonitorGUI::do_active_sensors_select(QListViewItem* item)
446 {
447         if (!item)
448                 return;
449         show_active_sensors(true);
450 }
451
452 void
453 MonitorGUI::do_active_sensors_mouseButtonPressed (int button, QListViewItem*,const QPoint & pos, int)
454 {
455         switch (button)
456         {
457         case 1: /* left button */
458                 show_current_sensor_value();
459                 update();
460                 break;
461         case 2: /* right button */
462 //              sensor_popup->popup(pos);
463                 update();
464                 break;
465         default:
466                 break;
467         }
468 }
469
470 void
471 MonitorGUI::sample(ActiveSensor* as, bool do_real_sample)
472 {               
473         int w = canvas->width();
474         unsigned int max_length = w/step + 2;
475
476         /* sample it */
477         as->sample(do_real_sample);
478
479         unsigned int size = as->samples.size();
480         if (size > max_length)
481                 for (unsigned int i = max_length; i < size; i++)
482                 {
483                         QValueList<double>::iterator it = as->samples.end();
484                         it--;
485                         as->samples.remove(it); /* remove the last one */
486                 }
487
488 #if 0
489         QValueList<int>::iterator it;   
490         for ( it = samples.begin(); it != samples.end(); ++it )
491                 cout << (*it) << ", ";
492         cout << endl;
493 #endif
494 }
495
496 void
497 MonitorGUI::timerEvent( QTimerEvent*)
498 {
499         // qDebug("TimerEvent");
500         int w = canvas->width();
501
502         if (active_sensors.count() == 0) /* no sensor */
503                 return;
504 ///////////////////////////////////////////
505 //      refresh the sensor instance
506        
507         sm->Sample();
508 //then refresh the sensor property
509        
510         QDict<bool> class_sampled;
511         class_sampled.setAutoDelete(TRUE);
512         for (ActiveSensor* as = active_sensors.first(); as; as = active_sensors.next() )
513         {
514                 bool* b = new bool;
515                 *b = false;
516                 class_sampled.insert(as->instance, b);
517         }
518         for (ActiveSensor* as = active_sensors.first(); as; as = active_sensors.next() )
519         {
520                 sample(as, !(*class_sampled[as->instance]));
521                 *class_sampled[as->instance] = true;
522         }
523
524         cur_x += step;
525         if (cur_x > w)
526                 cur_x = 0;
527         paintEvent(NULL);
528 }
529
530 void
531 MonitorGUI::draw_curve(ActiveSensor* as, QPainter& p)
532 {       
533         int x; 
534         int w = canvas->width();
535         int h = canvas->height();
536         double ratio = h/100.0*as->ratio;
537
538         /* sanity check */
539         if (!as)
540                 return;
541
542         QValueList<double>::iterator it = as->samples.begin();
543         QPen pen(as->color);
544         if (as->highlight)
545                 pen.setWidth(2);
546         p.setPen(pen);   
547
548         /* for the sample point, nothing needs to be done */
549         if (cur_x == 0 && as->samples.count() == 1)
550                 return;
551
552         /* left part */
553         for (x = cur_x; x > 0; x-=step)
554         {
555                 double y1 = *it;
556                 it++;
557                 if (it == as->samples.end())
558                         return;
559                 double y2 = *it;
560                 p.drawLine(x, (int)(y1*ratio), x-step, (int)(y2*ratio));
561         }
562
563         /* right part */
564         it++;
565         x = w - (w%step);
566         while(it != as->samples.end())
567         {
568                 double y1 = *it;
569                 it++;
570                 if (it == as->samples.end())
571                         return;
572                 double y2 = *it;
573                 p.drawLine(x, (int)(y1*ratio), x-step, (int)(y2*ratio));
574                 x-=step;
575         }
576 }
577
578 void MonitorGUI::set_viewport(QPainter& p)
579 {
580         int h = canvas->height();
581         p.translate(0, h);
582         p.scale(1.0, -1.0);
583 }
584
585 void MonitorGUI::draw_grid(QPainter& p)
586 {
587         int h = canvas->height();
588         int w = canvas->width();
589         int i; 
590         int n = grid_number(h);
591
592         p.fillRect(0, 0, w, h, QBrush(canvas->backgroundColor()));
593
594         /* horizontal grid */
595         p.setPen( Qt::black );
596         for (i = 0; i < n; i++) {
597                 int y = (i*h)/n;
598                 p.drawLine(0, y, w, y);
599         }
600         p.drawLine(0, 1, w, 1);
601         p.drawLine(0, h, w, h);
602
603
604 #if 0
605         /* vertical grid */
606         int s = (int)(10.0/(sample_rate/1.0)*step); /* 10 sec per mark */
607         for (i = s; i < w; i += s)
608                 p.drawLine(i, 0, i, h);
609 #endif
610 }
611
612
613 int
614 MonitorGUI::delete_current_sensor()
615 {
616         for(ActiveSensorItem* item=(ActiveSensorItem*)sensors_lv->firstChild(); item != NULL; )
617         {
618                 if(!item->isSelected())
619                 {
620                         item=(ActiveSensorItem*)item->itemBelow();
621                         continue;
622                 }
623                 ActiveSensor* as = item->as;
624                 for(map<int,LastSensorPair*>::iterator it=last_sensors.begin();it!=last_sensors.end();it++)
625                 {
626                         if((*it).second && (*it).second->instance==as->instance && (*it).second->property==as->property) 
627                         {
628                                 delete (*it).second;
629                                 last_sensors.erase(it);
630                         }
631                 }
632                 active_sensors.remove(as);
633                 if (active_sensors.count() == 0)
634                         cur_x = -step;
635                 ActiveSensorItem* tmp=item;
636                 item=(ActiveSensorItem*)item->itemBelow();
637                 sensors_lv->takeItem(tmp);
638         }
639        
640         sm->ClearSensorList();
641         for(ActiveSensor* as = active_sensors.first(); as; as = active_sensors.next())
642         {
643                 sm->AddSensorInstance(as->instance.latin1());
644         }
645        
646         show_active_sensors(true); /* refresh the sensor list */
647         return 0;
648 }
649
650 /*
651  * show last, agv, max, min of the selected sensor
652  */
653 void
654 MonitorGUI::show_current_sensor_value()
655 {
656         ActiveSensor* as;
657         for ( as = active_sensors.first(); as; as = active_sensors.next() )
658                 as->highlight = false;
659         ActiveSensorItem* item;
660        
661         item = (ActiveSensorItem*)sensors_lv->currentItem();
662         if (!item)
663         {
664                 last_lbl->setText("");
665                 avg_lbl->setText("");
666                 max_lbl->setText("");
667                 min_lbl->setText("");
668         }
669         else
670         {
671                 sensors_lv->setSelected(item, TRUE);
672                 as = item->as;
673                 as->highlight = true;
674                 last_lbl->setText(QString::number(as->last));
675                 avg_lbl->setText(QString::number(as->avg));
676                 if (as->max == INT_MIN)
677                         max_lbl->setText("-");
678                 else
679                         max_lbl->setText(QString::number(as->max));
680                 if (as->min == INT_MAX)
681                         min_lbl->setText("-");
682                 else
683                         min_lbl->setText(QString::number(as->min));
684
685                 //reset the posistion of components
686                 static int flag=-1;
687                 if(flag==-1)
688                 {
689
690                 }
691                 if(width()<500&&flag!=0)
692                 {
693                         Layout14->removeItem(Layout7_2);
694                         Layout14->removeItem(Layout13_2);
695                         Layout14->removeItem(Layout7_3_2);
696                         Layout14->removeItem(Layout7_4_2);
697
698                         Layout7_2->remove(TextLabel1_5);
699                         Layout7_2->remove(last_lbl);
700                         Layout7_2= new QHBoxLayout( 0, 0, 6, "layout7_2");
701                         Layout7_2->addWidget(TextLabel1_5);
702                         Layout7_2->addWidget(last_lbl);
703                         layout11->addLayout(Layout7_2);
704
705                         Layout13_2->remove(TextLabel1_2_2);
706                         Layout13_2->remove(avg_lbl);
707                         Layout13_2= new QHBoxLayout( 0, 0, 6, "layout13_2");
708                         Layout13_2->addWidget(TextLabel1_2_2);
709                         Layout13_2->addWidget(avg_lbl);
710                         layout11->addLayout(Layout13_2);
711
712                         Layout7_3_2->remove(TextLabel1_3_2);
713                         Layout7_3_2->remove(min_lbl);
714                         Layout7_3_2= new QHBoxLayout( 0, 0, 6, "layout7_3_2");
715                         Layout7_3_2->addWidget(TextLabel1_3_2);
716                         Layout7_3_2->addWidget(min_lbl);
717                         layout22->addLayout(Layout7_3_2);
718
719                         Layout7_4_2->remove(TextLabel1_4_2);
720                         Layout7_4_2->remove(max_lbl);
721                         Layout7_4_2= new QHBoxLayout( 0, 0, 6, "layout7_4_2");
722                         Layout7_4_2->addWidget(TextLabel1_4_2);
723                         Layout7_4_2->addWidget(max_lbl);
724                         layout22->addLayout(Layout7_4_2);
725
726                         layout44->addLayout(layout11);
727                         layout44->addLayout(layout22);
728                         Layout14->addLayout(layout44);
729                         flag=0;
730                 }
731                 else if(width()>=500&& flag==0)
732                 {
733                         layout11->removeItem(Layout7_2);
734                         layout11->removeItem(Layout13_2);
735                         layout44->removeItem(layout11);
736                         layout11= new QHBoxLayout(0, 0, 6, "layout11");
737                         layout22->removeItem(Layout7_3_2);
738                         layout22->removeItem(Layout7_4_2);
739                         layout44->removeItem(layout22);
740                         layout22= new QHBoxLayout(0, 0, 6, "layout22");
741
742                         Layout14->removeItem(layout44);
743                         layout44= new QVBoxLayout(0, 0, 6, "layout44");
744
745                         Layout7_2->remove(TextLabel1_5);
746                         Layout7_2->remove(last_lbl);
747                         Layout7_2= new QHBoxLayout( 0, 0, 6, "layout7_2");
748                         Layout7_2->addWidget(TextLabel1_5);
749                         Layout7_2->addWidget(last_lbl);
750                         Layout14->addLayout(Layout7_2);
751
752                         Layout13_2->remove(TextLabel1_2_2);
753                         Layout13_2->remove(avg_lbl);
754                         Layout13_2= new QHBoxLayout( 0, 0, 6, "layout13_2");
755                         Layout13_2->addWidget(TextLabel1_2_2);
756                         Layout13_2->addWidget(avg_lbl);
757                         Layout14->addLayout(Layout13_2);
758
759                         Layout7_3_2->remove(TextLabel1_3_2);
760                         Layout7_3_2->remove(min_lbl);
761                         Layout7_3_2= new QHBoxLayout( 0, 0, 6, "layout7_3_2");
762                         Layout7_3_2->addWidget(TextLabel1_3_2);
763                         Layout7_3_2->addWidget(min_lbl);
764                         Layout14->addLayout(Layout7_3_2);
765
766                         Layout7_4_2->remove(TextLabel1_4_2);
767                         Layout7_4_2->remove(max_lbl);
768                         Layout7_4_2= new QHBoxLayout( 0, 0, 6, "layout7_4_2");
769                         Layout7_4_2->addWidget(TextLabel1_4_2);
770                         Layout7_4_2->addWidget(max_lbl);
771                         Layout14->addLayout(Layout7_4_2);
772                         flag=1;
773                 }               
774
775         }       
776         update_display_prop(as);
777 }
778
779 void
780 MonitorGUI::do_showmode(bool showflag)
781 {
782         if(showflag) showmode = 0;
783         else  showmode = 1;
784         paintEvent(NULL);
785 }
786
787 void
788 MonitorGUI::draw_curve()
789 {       
790         int w = canvas->width();
791         int h = canvas->height();
792        
793         QPixmap pm( w, h );
794         QPainter p(&pm);
795         set_viewport(p);
796         draw_grid(p);
797         ActiveSensor* as;
798         ActiveSensor* highlighted_as = NULL;
799         for ( as = active_sensors.first(); as; as = active_sensors.next() )
800         {
801                 if (!as->highlight)
802                         draw_curve(as, p);
803                 else
804                         highlighted_as = as;   
805         }
806         /* draw the highlighted curve in the end */
807         if (highlighted_as)
808                 draw_curve(highlighted_as, p);
809
810         /* draw vertical mark */
811         p.setPen(QPen(Qt::red, 2));
812         p.drawLine(cur_x, 0, cur_x, h);
813
814         /* ok now show pixmap back */
815         bitBlt(canvas, 0, 0, &pm);
816 }
817
818 void
819 MonitorGUI::draw_bar()
820 {
821         int h = canvas->height();
822         int w = canvas->width();
823         int i;
824         int ww;
825         QPixmap pm( w, h );
826         QPainter p(&pm);
827         set_viewport(p);
828        
829         draw_grid(p);
830         if (active_sensors.count() == 0)
831                 goto End;
832         ww = w/active_sensors.count();
833         ActiveSensor* as;
834         for ( i = 0, as = active_sensors.first();
835               as; as = active_sensors.next(),i+=ww )
836         {
837                 double ratio = h/100.0*as->ratio;
838                 /* FIXME: samples may not have value yet */
839                 QValueList<double>::iterator it = as->samples.begin();
840                 int y = (int)*it;
841                 p.fillRect(i, 0, ww, (int)(y*ratio),
842                            QBrush(as->color));
843         }
844
845  End:
846         /* ok now show pixmap back */
847         bitBlt(canvas, 0, 0, &pm);
848 }
849
850 void MonitorGUI::paintEvent (QPaintEvent*)
851 {
852         switch (showmode)
853         {
854         case 0: /* curve */
855                 draw_curve();
856                 break;
857         case 1: /* bar mode */
858                 draw_bar();
859                 break;
860         default:
861                 qWarning("unknown show mode: %d\n", showmode);
862         }
863         /* show value */
864         show_current_sensor_value();
865 }
866
867 MonitorGUI::~MonitorGUI()
868 {
869         /* FIXME: need delete something here, right? */
870 }
871
872 void
873 MonitorGUI::show_active_sensors(bool update_only)
874 {
875         if (!update_only)
876         {
877                 sensors_lv->clear();
878                
879                 ActiveSensor* as;
880                 for ( as = active_sensors.first(); as; as = active_sensors.next() )
881                 {
882                         ActiveSensorItem *item;
883                         QString ratio_str = QString::number(as->ratio, 'f', 10);
884                         ratio_str = ratio_str.replace( QRegExp("0+$"), "" );
885                         ratio_str = ratio_str.replace( QRegExp("\\.$"), ".0" );
886                 //      QString pin_str = as->pin;
887                 //      pin_str.replace(QRegExp("%%"), "%");
888                         item = new ActiveSensorItem(
889                                 sensors_lv,
890                                 as,
891                                 "",                /* color */
892                                 ratio_str+"  ",    /* ratio */
893                         //      pin_str+"  ",      /* counter */
894                                 as->property+"  ", /* property */
895                                 as->instance+"  " /* sensor Object */
896                                 );         /* host */
897                         QPixmap pm(16,16);
898                         pm.fill(as->color);
899                         item->setPixmap(0, pm);
900                         sensors_lv->insertItem(item);
901                         if (as->highlight)
902                                 sensors_lv->setSelected(item, TRUE);
903                 }
904         }
905         else /* update only */
906         {
907                 QListViewItem* i;
908                 for (i = sensors_lv->firstChild();i;i=i->nextSibling())
909                 {
910                         ActiveSensorItem *item = (ActiveSensorItem *)i;
911                         ActiveSensor* as = item->as;
912                         QPixmap pm(16,16);
913                         pm.fill(as->color);
914                         item->setPixmap(0, pm);
915                         QString ratio_str = QString::number(as->ratio, 'f', 10);
916                         ratio_str = ratio_str.replace( QRegExp("0+$"), "" );
917                         ratio_str = ratio_str.replace( QRegExp("\\.$"), ".0" );
918                         item->setText(1, ratio_str);
919                 }
920         }
921         /* update display property group box */
922         update_display_prop(get_current_as());
923         update(); /* redraw */
924 }
925
926 /*
927  * for the purpose of sorting color, color is sorted by its name (#RRGGBB).
928  */
929
930
931 ActiveSensor*
932 MonitorGUI::get_current_as()
933 {
934         ActiveSensor* as = NULL;
935         ActiveSensorItem* item;
936         item = (ActiveSensorItem*)sensors_lv->currentItem();
937         if (item)
938                 as = item->as; 
939         return as;
940 }
941
942 int
943 MonitorGUI::update_display_prop(ActiveSensor* as)
944 {
945         if(!as)
946         {
947                 color_btn->setEnabled(FALSE);
948                 color_btn->setPaletteBackgroundColor(QColor("white"));
949                 display_prop_gb->setEnabled(FALSE);
950         //      delAction->setEnabled(FALSE);
951                 return 0;
952         }
953         display_prop_gb->setEnabled(TRUE);
954 //      delAction->setEnabled(TRUE);
955         QString ratio_str = QString::number(as->ratio, 'f', 10);
956         ratio_str = ratio_str.replace( QRegExp("0+$"), "" );
957         ratio_str = ratio_str.replace( QRegExp("\\.$"), ".0" );
958         ratio_lbl->setText(ratio_str);
959         ratio_sld->setValue((int)log10(as->ratio));
960         color_btn->setEnabled(TRUE);
961         color_btn->setPaletteBackgroundColor(as->color);
962         color_btn->setPaletteForegroundColor(QColor(255 - as->color.red(),
963                                                     255 - as->color.green(),
964                                                     255 - as->color.blue()));
965        
966         return 0;
967 }
968
969
970 ActiveSensor* MonitorGUI::add_sensor(const QString& instance,const QString& property)
971 {
972         ActiveSensor* as;
973        
974         /* check if the pin is already active */       
975         for ( as = active_sensors.first(); as; as = active_sensors.next() ) {
976                 if (as->property == property && as->instance == instance)
977                         return 0;
978         }
979        
980         sm->AddSensorInstance(instance.latin1());
981         /* make new active sensor object */
982         as = new ActiveSensor(sm, instance, property, alloc_color());
983         active_sensors.append(as);
984         /* refresh active sensor list */
985         show_active_sensors();
986         return as;
987 }
988
989 /* FIXME: DEFAULT_COLOR_NUM is not necessary */
990 QColor MonitorGUI::alloc_color()
991 {
992         static const int DEFAULT_COLOR_NUM = 8;
993         static QColor default_colors[] = {"red", "purple", "blue", "magenta",
994                                           "green", "brown", "pink", "orange"}; 
995         int hit[DEFAULT_COLOR_NUM];
996         int i;
997
998         /* usage counting */
999         for (i = 0; i < DEFAULT_COLOR_NUM; i++)
1000                 hit[i] = 0;
1001         ActiveSensor* as;
1002         for (i = 0; i < DEFAULT_COLOR_NUM; i++)
1003                 for ( as = active_sensors.first(); as; as = active_sensors.next() )
1004                         if (default_colors[i] == as->color)
1005                                 hit[i]++;
1006        
1007         /* if find one unused color, use it */
1008         for (i = 0; i < DEFAULT_COLOR_NUM; i++)
1009                 if (!hit[i])
1010                         return default_colors[i];
1011        
1012         /* if all colors are used, choose the less frequently used one */
1013         int minhit = hit[0];
1014         int chosen = 0;
1015         for (i = 1; i < DEFAULT_COLOR_NUM; i++)
1016                 if (minhit > hit[i])
1017                 {
1018                         minhit = hit[i];
1019                         chosen = i;
1020                 }
1021         return default_colors[chosen];
1022         // return default_colors[(int)(DEFAULT_COLOR_NUM*1.0*rand()/(RAND_MAX+1.0))];
1023 }
Note: See TracBrowser for help on using the browser.