summaryrefslogtreecommitdiffstats
path: root/src/cputimepanel.cpp
blob: 6b6612afe8475c09a23c68938ba9826fd1aa0384 (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
/***************************************************************************
                          rampanel.cpp  -  description
                             -------------------
    begin                : Fri Jan 11 2002
    copyright            : (C) 2002 by Miguel Novas
    email                : michaell@teleline.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "cputimepanel.h"

#include "procinfo.h"

CpuTimePanel::CpuTimePanel(TQWidget *parent, const char *name): Panel(parent,name)
{
 lcd1= new TQLCDString(this);
 lcd1->setGeometry(16,38,46,8);
 lcd1->setForeColor(red);
 lcd1->setAlign(TQLCDString::alignLeft);
 lcd1->setNumberDisplay(true);
 lcd1->installEventFilter(this);

 TQColor color2( 200, 0, 0);

 lcd2= new TQLCDString(this);
 lcd2->setGeometry(16,28,46,8);
 lcd2->setForeColor(color2);
 lcd2->setAlign(TQLCDString::alignLeft);
 lcd2->setNumberDisplay(true);
 lcd2->installEventFilter(this);

 TQColor color3( 128,32,0 );

 lcd3= new TQLCDString(this);
 lcd3->setGeometry(16,18,46,8);
 lcd3->setForeColor(color3);
 lcd3->setAlign(TQLCDString::alignLeft);
 lcd3->setNumberDisplay(true);
 lcd3->installEventFilter(this);

 lcd4= new TQLCDString(this);
 lcd4->setGeometry(16,8,46,8);
 lcd4->setForeColor(darkGreen);
 lcd4->setAlign(TQLCDString::alignLeft);
 lcd4->setNumberDisplay(true);
 lcd4->installEventFilter(this);

 barMeter= new TQBarMeter(this);
 barMeter->setGeometry(6,6,6,40);
 barMeter->setDirection(TQBarMeter::dirUp);
 barMeter->setSteps(20);
 barMeter->useValueMax(false);
 barMeter->setValueColor( 0, red );
 barMeter->setValueColor( 1, color2 );
 barMeter->setValueColor( 2, color3 );
 barMeter->setValueColor( 3, TQColor(0,220,0) );
 barMeter->setValueCount(4);
 barMeter->installEventFilter(this);

 getCpuTime(&old_user,&old_nice,&old_system,&old_idle);
 updateInfo();
}

CpuTimePanel::~CpuTimePanel(){
}

void CpuTimePanel::drawContents(TQPainter *p)
{
 int w= width();
 int h= height();
 int i2= (h * 4) / 5;
 int th= h-i2-h/11;
 TQLcd::draw(p, 2,i2+1,w-4,th, "CPU STATE",TQLcd::alignCenter,&getColorTitle());
}


void CpuTimePanel::updateInfo()
{
 int user,nice,system,idle;

 getCpuTime(&user,&nice,&system,&idle);

 int puser  = user   - old_user;
 int pnice  = nice   - old_nice;
 int psystem= system - old_system;
 int pidle  = idle   - old_idle;

 int ptotal=  puser+pnice+psystem+pidle;

 if (ptotal==0) ptotal=1;

 TQString str;
 str.sprintf ("USER%5.1f" , (float)puser  /ptotal * 100 );
 lcd1->display(str);
 str.sprintf ("NICE%5.1f" , (float)pnice  /ptotal * 100 );
 lcd2->display(str);
 str.sprintf ("SYS %5.1f" , (float)psystem/ptotal * 100 );
 lcd3->display(str);
 str.sprintf ("IDLE%5.1f" , (float)pidle  /ptotal * 100 );
 lcd4->display(str);

 barMeter->setValue(0,(double)puser);
 barMeter->setValue(1,(double)pnice);
 barMeter->setValue(2,(double)psystem);
 barMeter->setValue(3,(double)pidle);

 old_user  = user;
 old_nice  = nice;
 old_system= system;
 old_idle  = idle;
}

void CpuTimePanel::resizeEvent ( TQResizeEvent *e )
{
  int w= width();
  int h= height();

  int lw= (w*10)/14;
  int lh= h/8;
  int sh= (h*10)/64;
  int i= h/8;
  int i0= i-1;
  lcd4    ->setGeometry(w/4 ,i ,lw,lh);
  i+= sh;
  lcd3    ->setGeometry(w/4 ,i ,lw,lh);
  i+= sh;
  lcd2    ->setGeometry(w/4 ,i ,lw,lh);
  i+= sh;
  lcd1    ->setGeometry(w/4 ,i ,lw,lh);

  int bl= i+lh-i0;
  barMeter->setGeometry(w/10,i0,w/10,bl);
  barMeter->setSteps(bl/2);
}