99SHOW = True # Show test in GUI-based test launcher
1010
1111import os
12- import sys
1312import numpy as np
1413
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
1916from qwt import (QwtPlot , QwtPlotMarker , QwtScaleDraw , QwtLegend , QwtPlotCurve ,
2017 QwtPlotItem , QwtLegendData , QwtText )
2118
@@ -176,20 +173,20 @@ def __init__(self):
176173
177174 def statistic (self ):
178175 values = self .__lookup ()
179- userDelta = 0.0
176+ userDelta = 0.
180177 for i in [CpuStat .User , CpuStat .Nice ]:
181178 userDelta += (values [i ] - self .procValues [i ])
182179 systemDelta = values [CpuStat .System ] - self .procValues [CpuStat .System ]
183- totalDelta = 0.0
180+ totalDelta = 0.
184181 for i in range (len (self .procValues )):
185182 totalDelta += (values [i ] - self .procValues [i ])
186183 self .procValues = values
187- return 100.0 * userDelta / totalDelta , 100.0 * systemDelta / totalDelta
184+ return 100. * userDelta / totalDelta , 100. * systemDelta / totalDelta
188185
189186 def upTime (self ):
190187 result = QTime ()
191188 for item in self .procValues :
192- result = result .addSecs (item / 100 )
189+ result = result .addSecs (int ( .01 * item ) )
193190 return result
194191
195192 def __lookup (self ):
@@ -208,7 +205,7 @@ def __lookup(self):
208205class CpuPieMarker (QwtPlotMarker ):
209206 def __init__ (self , * args ):
210207 QwtPlotMarker .__init__ (self , * args )
211- self .setZ (1000.0 )
208+ self .setZ (1000. )
212209 self .setRenderHint (QwtPlotItem .RenderAntialiased , True )
213210
214211 def rtti (self ):
@@ -219,18 +216,18 @@ def draw(self, painter, xMap, yMap, rect):
219216 pieRect = QRect ()
220217 pieRect .setX (rect .x () + margin )
221218 pieRect .setY (rect .y () + margin )
222- pieRect .setHeight (yMap .transform (80.0 ))
219+ pieRect .setHeight (int ( yMap .transform (80. ) ))
223220 pieRect .setWidth (pieRect .height ())
224221
225222 angle = 3 * 5760 / 4
226223 for key in ["User" , "System" , "Idle" ]:
227224 curve = self .plot ().cpuPlotCurve (key )
228225 if curve .dataSize ():
229- value = int (5760 * curve .sample (0 ).y ()/ 100.0 )
226+ value = int (5760 * curve .sample (0 ).y ()/ 100. )
230227 painter .save ()
231228 painter .setBrush (QBrush (curve .pen ().color (),
232229 Qt .SolidPattern ))
233- painter .drawPie (pieRect , - angle , - value )
230+ painter .drawPie (pieRect , int ( - angle ), int ( - value ) )
234231 painter .restore ()
235232 angle += value
236233
@@ -248,7 +245,7 @@ def label(self, value):
248245class Background (QwtPlotItem ):
249246 def __init__ (self ):
250247 QwtPlotItem .__init__ (self )
251- self .setZ (0.0 )
248+ self .setZ (0. )
252249
253250 def rtti (self ):
254251 return QwtPlotItem .Rtti_PlotUserItem
@@ -258,8 +255,8 @@ def draw(self, painter, xMap, yMap, rect):
258255 r = QRect (rect )
259256
260257 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 ) ))
263260 painter .fillRect (r , c )
264261 c = c .darker (110 )
265262
@@ -285,7 +282,7 @@ def __init__(self, *args):
285282
286283 self .curves = {}
287284 self .data = {}
288- self .timeData = 1.0 * np .arange (HISTORY - 1 , - 1 , - 1 )
285+ self .timeData = 1. * np .arange (HISTORY - 1 , - 1 , - 1 )
289286 self .cpuStat = CpuStat ()
290287
291288 self .setAutoReplot (False )
@@ -300,7 +297,7 @@ def __init__(self, *args):
300297 self .setAxisScaleDraw (
301298 QwtPlot .xBottom , TimeScaleDraw (self .cpuStat .upTime ()))
302299 self .setAxisScale (QwtPlot .xBottom , 0 , HISTORY )
303- self .setAxisLabelRotation (QwtPlot .xBottom , - 50.0 )
300+ self .setAxisLabelRotation (QwtPlot .xBottom , - 50. )
304301 self .setAxisLabelAlignment (
305302 QwtPlot .xBottom , Qt .AlignLeft | Qt .AlignBottom )
306303
@@ -321,21 +318,21 @@ def __init__(self, *args):
321318
322319 curve = CpuCurve ('User' )
323320 curve .setColor (Qt .blue )
324- curve .setZ (curve .z () - 1.0 )
321+ curve .setZ (curve .z () - 1. )
325322 curve .attach (self )
326323 self .curves ['User' ] = curve
327324 self .data ['User' ] = np .zeros (HISTORY , np .float )
328325
329326 curve = CpuCurve ('Total' )
330327 curve .setColor (Qt .black )
331- curve .setZ (curve .z () - 2.0 )
328+ curve .setZ (curve .z () - 2. )
332329 curve .attach (self )
333330 self .curves ['Total' ] = curve
334331 self .data ['Total' ] = np .zeros (HISTORY , np .float )
335332
336333 curve = CpuCurve ('Idle' )
337334 curve .setColor (Qt .darkCyan )
338- curve .setZ (curve .z () - 3.0 )
335+ curve .setZ (curve .z () - 3. )
339336 curve .attach (self )
340337 self .curves ['Idle' ] = curve
341338 self .data ['Idle' ] = np .zeros (HISTORY , np .float )
@@ -355,9 +352,9 @@ def timerEvent(self, e):
355352 data [1 :] = data [0 :- 1 ]
356353 self .data ["User" ][0 ], self .data ["System" ][0 ] = self .cpuStat .statistic ()
357354 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 ]
359356
360- self .timeData += 1.0
357+ self .timeData += 1.
361358
362359 self .setAxisScale (
363360 QwtPlot .xBottom , self .timeData [- 1 ], self .timeData [0 ])
@@ -375,25 +372,18 @@ def cpuPlotCurve(self, key):
375372 return self .curves [key ]
376373
377374
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+
395386
396387if __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