9
9
SHOW = True # Show test in GUI-based test launcher
10
10
11
11
import os
12
- import sys
13
12
import numpy as np
14
13
15
- from qwt .qt .QtGui import (QApplication , QColor , QBrush , QWidget , QVBoxLayout ,
16
- QLabel )
17
- from qwt .qt .QtCore import QRect , QTime
18
- from qwt .qt .QtCore import Qt
14
+ from qwt .qt .QtGui import QColor , QBrush , QWidget , QVBoxLayout , QLabel
15
+ from qwt .qt .QtCore import QRect , QTime , Qt
19
16
from qwt import (QwtPlot , QwtPlotMarker , QwtScaleDraw , QwtLegend , QwtPlotCurve ,
20
17
QwtPlotItem , QwtLegendData , QwtText )
21
18
@@ -176,20 +173,20 @@ def __init__(self):
176
173
177
174
def statistic (self ):
178
175
values = self .__lookup ()
179
- userDelta = 0.0
176
+ userDelta = 0.
180
177
for i in [CpuStat .User , CpuStat .Nice ]:
181
178
userDelta += (values [i ] - self .procValues [i ])
182
179
systemDelta = values [CpuStat .System ] - self .procValues [CpuStat .System ]
183
- totalDelta = 0.0
180
+ totalDelta = 0.
184
181
for i in range (len (self .procValues )):
185
182
totalDelta += (values [i ] - self .procValues [i ])
186
183
self .procValues = values
187
- return 100.0 * userDelta / totalDelta , 100.0 * systemDelta / totalDelta
184
+ return 100. * userDelta / totalDelta , 100. * systemDelta / totalDelta
188
185
189
186
def upTime (self ):
190
187
result = QTime ()
191
188
for item in self .procValues :
192
- result = result .addSecs (item / 100 )
189
+ result = result .addSecs (int ( .01 * item ) )
193
190
return result
194
191
195
192
def __lookup (self ):
@@ -208,7 +205,7 @@ def __lookup(self):
208
205
class CpuPieMarker (QwtPlotMarker ):
209
206
def __init__ (self , * args ):
210
207
QwtPlotMarker .__init__ (self , * args )
211
- self .setZ (1000.0 )
208
+ self .setZ (1000. )
212
209
self .setRenderHint (QwtPlotItem .RenderAntialiased , True )
213
210
214
211
def rtti (self ):
@@ -219,18 +216,18 @@ def draw(self, painter, xMap, yMap, rect):
219
216
pieRect = QRect ()
220
217
pieRect .setX (rect .x () + margin )
221
218
pieRect .setY (rect .y () + margin )
222
- pieRect .setHeight (yMap .transform (80.0 ))
219
+ pieRect .setHeight (int ( yMap .transform (80. ) ))
223
220
pieRect .setWidth (pieRect .height ())
224
221
225
222
angle = 3 * 5760 / 4
226
223
for key in ["User" , "System" , "Idle" ]:
227
224
curve = self .plot ().cpuPlotCurve (key )
228
225
if curve .dataSize ():
229
- value = int (5760 * curve .sample (0 ).y ()/ 100.0 )
226
+ value = int (5760 * curve .sample (0 ).y ()/ 100. )
230
227
painter .save ()
231
228
painter .setBrush (QBrush (curve .pen ().color (),
232
229
Qt .SolidPattern ))
233
- painter .drawPie (pieRect , - angle , - value )
230
+ painter .drawPie (pieRect , int ( - angle ), int ( - value ) )
234
231
painter .restore ()
235
232
angle += value
236
233
@@ -248,7 +245,7 @@ def label(self, value):
248
245
class Background (QwtPlotItem ):
249
246
def __init__ (self ):
250
247
QwtPlotItem .__init__ (self )
251
- self .setZ (0.0 )
248
+ self .setZ (0. )
252
249
253
250
def rtti (self ):
254
251
return QwtPlotItem .Rtti_PlotUserItem
@@ -258,8 +255,8 @@ def draw(self, painter, xMap, yMap, rect):
258
255
r = QRect (rect )
259
256
260
257
for i in range (100 , 0 , - 10 ):
261
- r .setBottom (yMap .transform (i - 10 ))
262
- r .setTop (yMap .transform (i ))
258
+ r .setBottom (int ( yMap .transform (i - 10 ) ))
259
+ r .setTop (int ( yMap .transform (i ) ))
263
260
painter .fillRect (r , c )
264
261
c = c .darker (110 )
265
262
@@ -285,7 +282,7 @@ def __init__(self, *args):
285
282
286
283
self .curves = {}
287
284
self .data = {}
288
- self .timeData = 1.0 * np .arange (HISTORY - 1 , - 1 , - 1 )
285
+ self .timeData = 1. * np .arange (HISTORY - 1 , - 1 , - 1 )
289
286
self .cpuStat = CpuStat ()
290
287
291
288
self .setAutoReplot (False )
@@ -300,7 +297,7 @@ def __init__(self, *args):
300
297
self .setAxisScaleDraw (
301
298
QwtPlot .xBottom , TimeScaleDraw (self .cpuStat .upTime ()))
302
299
self .setAxisScale (QwtPlot .xBottom , 0 , HISTORY )
303
- self .setAxisLabelRotation (QwtPlot .xBottom , - 50.0 )
300
+ self .setAxisLabelRotation (QwtPlot .xBottom , - 50. )
304
301
self .setAxisLabelAlignment (
305
302
QwtPlot .xBottom , Qt .AlignLeft | Qt .AlignBottom )
306
303
@@ -321,21 +318,21 @@ def __init__(self, *args):
321
318
322
319
curve = CpuCurve ('User' )
323
320
curve .setColor (Qt .blue )
324
- curve .setZ (curve .z () - 1.0 )
321
+ curve .setZ (curve .z () - 1. )
325
322
curve .attach (self )
326
323
self .curves ['User' ] = curve
327
324
self .data ['User' ] = np .zeros (HISTORY , np .float )
328
325
329
326
curve = CpuCurve ('Total' )
330
327
curve .setColor (Qt .black )
331
- curve .setZ (curve .z () - 2.0 )
328
+ curve .setZ (curve .z () - 2. )
332
329
curve .attach (self )
333
330
self .curves ['Total' ] = curve
334
331
self .data ['Total' ] = np .zeros (HISTORY , np .float )
335
332
336
333
curve = CpuCurve ('Idle' )
337
334
curve .setColor (Qt .darkCyan )
338
- curve .setZ (curve .z () - 3.0 )
335
+ curve .setZ (curve .z () - 3. )
339
336
curve .attach (self )
340
337
self .curves ['Idle' ] = curve
341
338
self .data ['Idle' ] = np .zeros (HISTORY , np .float )
@@ -355,9 +352,9 @@ def timerEvent(self, e):
355
352
data [1 :] = data [0 :- 1 ]
356
353
self .data ["User" ][0 ], self .data ["System" ][0 ] = self .cpuStat .statistic ()
357
354
self .data ["Total" ][0 ] = self .data ["User" ][0 ] + self .data ["System" ][0 ]
358
- self .data ["Idle" ][0 ] = 100.0 - self .data ["Total" ][0 ]
355
+ self .data ["Idle" ][0 ] = 100. - self .data ["Total" ][0 ]
359
356
360
- self .timeData += 1.0
357
+ self .timeData += 1.
361
358
362
359
self .setAxisScale (
363
360
QwtPlot .xBottom , self .timeData [- 1 ], self .timeData [0 ])
@@ -375,25 +372,18 @@ def cpuPlotCurve(self, key):
375
372
return self .curves [key ]
376
373
377
374
378
- def make ():
379
- demo = QWidget ()
380
- demo .setWindowTitle ('Cpu Plot' )
381
-
382
- plot = CpuPlot (demo )
383
- plot .setTitle ("History" )
384
-
385
- label = QLabel ("Press the legend to en/disable a curve" , demo )
386
-
387
- layout = QVBoxLayout (demo )
388
- layout .addWidget (plot )
389
- layout .addWidget (label )
390
-
391
- demo .resize (600 , 400 )
392
- demo .show ()
393
- return demo
394
-
375
+ class CpuWidget (QWidget ):
376
+ def __init__ (self , parent = None ):
377
+ super (CpuWidget , self ).__init__ (parent )
378
+ layout = QVBoxLayout ()
379
+ self .setLayout (layout )
380
+ plot = CpuPlot ()
381
+ plot .setTitle ("History" )
382
+ layout .addWidget (plot )
383
+ label = QLabel ("Press the legend to en/disable a curve" )
384
+ layout .addWidget (label )
385
+
395
386
396
387
if __name__ == '__main__' :
397
- app = QApplication (sys .argv )
398
- demo = make ()
399
- sys .exit (app .exec_ ())
388
+ from qwt .tests import test_widget
389
+ app = test_widget (CpuWidget , (600 , 400 ))
0 commit comments