11#!/usr/bin/env python
22
3- # The Python version of qwt-*/examples/cpuplot
4-
5-
63import os
74import sys
85
96from PyQt4 import Qt
10- #import PyQt4.Qwt5 as Qwt
117import qwt as Qwt
12- from PyQt4 .Qwt5 .anynumpy import *
13-
8+ import numpy as np
149
15- #-- cpustat.cpp --#
1610
1711class CpuStat :
18-
1912 User = 0
2013 Nice = 1
2114 System = 2
@@ -169,8 +162,6 @@ class CpuStat:
169162 def __init__ (self ):
170163 self .procValues = self .__lookup ()
171164
172- # __init__()
173-
174165 def statistic (self ):
175166 values = self .__lookup ()
176167 userDelta = 0.0
@@ -183,16 +174,12 @@ def statistic(self):
183174 self .procValues = values
184175 return 100.0 * userDelta / totalDelta , 100.0 * systemDelta / totalDelta
185176
186- # statistics()
187-
188177 def upTime (self ):
189178 result = Qt .QTime ()
190179 for item in self .procValues :
191180 result = result .addSecs (item / 100 )
192181 return result
193182
194- # upTime()
195-
196183 def __lookup (self ):
197184 if os .path .exists ("/proc/stat" ):
198185 for line in open ("/proc/stat" ):
@@ -205,27 +192,16 @@ def __lookup(self):
205192 CpuStat .counter %= len (CpuStat .dummyValues )
206193 return result
207194
208- # __lookup
209-
210- # class CpuStat
211-
212-
213- #-- cpupiemarker.cpp --#
214195
215196class CpuPieMarker (Qwt .QwtPlotMarker ):
216-
217197 def __init__ (self , * args ):
218198 Qwt .QwtPlotMarker .__init__ (self , * args )
219199 self .setZ (1000.0 )
220200 self .setRenderHint (Qwt .QwtPlotItem .RenderAntialiased , True )
221201
222- # __init__()
223-
224202 def rtti (self ):
225203 return Qwt .QwtPlotItem .Rtti_PlotUserItem
226204
227- # rtti()
228-
229205 def draw (self , painter , xMap , yMap , rect ):
230206 margin = 5
231207 pieRect = Qt .QRect ()
@@ -238,51 +214,33 @@ def draw(self, painter, xMap, yMap, rect):
238214 for key in ["User" , "System" , "Idle" ]:
239215 curve = self .plot ().cpuPlotCurve (key )
240216 if curve .dataSize ():
241- value = int (5760 * curve .y ( 0 )/ 100.0 )
217+ value = int (5760 * curve .sample ( 0 ). y ( )/ 100.0 )
242218 painter .save ()
243219 painter .setBrush (Qt .QBrush (curve .pen ().color (),
244220 Qt .Qt .SolidPattern ))
245221 painter .drawPie (pieRect , - angle , - value )
246222 painter .restore ()
247223 angle += value
248224
249- # draw()
250-
251- # class CpuPieMarker
252-
253-
254- #-- cpuplot.cpp --#
255225
256226class TimeScaleDraw (Qwt .QwtScaleDraw ):
257-
258227 def __init__ (self , baseTime , * args ):
259228 Qwt .QwtScaleDraw .__init__ (self , * args )
260229 self .baseTime = baseTime
261230
262- # __init__()
263-
264231 def label (self , value ):
265232 upTime = self .baseTime .addSecs (int (value ))
266233 return Qwt .QwtText (upTime .toString ())
267234
268- # label()
269-
270- # class TimeScaleDraw
271-
272235
273236class Background (Qwt .QwtPlotItem ):
274-
275237 def __init__ (self ):
276238 Qwt .QwtPlotItem .__init__ (self )
277239 self .setZ (0.0 )
278240
279- # __init__()
280-
281241 def rtti (self ):
282242 return Qwt .QwtPlotItem .Rtti_PlotUserItem
283243
284- # rtti()
285-
286244 def draw (self , painter , xMap , yMap , rect ):
287245 c = Qt .QColor (Qt .Qt .white )
288246 r = Qt .QRect (rect )
@@ -293,49 +251,37 @@ def draw(self, painter, xMap, yMap, rect):
293251 painter .fillRect (r , c )
294252 c = c .dark (110 )
295253
296- # draw()
297-
298- # class Background
299-
300254
301255class CpuCurve (Qwt .QwtPlotCurve ):
302-
303256 def __init__ (self , * args ):
304257 Qwt .QwtPlotCurve .__init__ (self , * args )
305258 self .setRenderHint (Qwt .QwtPlotItem .RenderAntialiased )
306259
307- # __init__()
308-
309260 def setColor (self , color ):
310261 c = Qt .QColor (color )
311262 c .setAlpha (150 )
312263
313264 self .setPen (c )
314265 self .setBrush (c )
315266
316- # setColor()
317-
318- # class CpuCurve
319-
320267
321268HISTORY = 60
322269
323270class CpuPlot (Qwt .QwtPlot ):
324-
325271 def __init__ (self , * args ):
326272 Qwt .QwtPlot .__init__ (self , * args )
327273
328274 self .curves = {}
329275 self .data = {}
330- self .timeData = 1.0 * arange (HISTORY - 1 , - 1 , - 1 )
276+ self .timeData = 1.0 * np . arange (HISTORY - 1 , - 1 , - 1 )
331277 self .cpuStat = CpuStat ()
332278
333279 self .setAutoReplot (False )
334280
335281 self .plotLayout ().setAlignCanvasToScales (True )
336282
337283 legend = Qwt .QwtLegend ()
338- legend .setItemMode (Qwt .QwtLegend . CheckableItem )
284+ legend .setDefaultItemMode (Qwt .QwtLegendData . Checkable )
339285 self .insertLegend (legend , Qwt .QwtPlot .RightLegend )
340286
341287 self .setAxisTitle (Qwt .QwtPlot .xBottom , "System Uptime [h:m:s]" )
@@ -359,28 +305,28 @@ def __init__(self, *args):
359305 curve .setColor (Qt .Qt .red )
360306 curve .attach (self )
361307 self .curves ['System' ] = curve
362- self .data ['System' ] = zeros (HISTORY , Float )
308+ self .data ['System' ] = np . zeros (HISTORY , np . float )
363309
364310 curve = CpuCurve ('User' )
365311 curve .setColor (Qt .Qt .blue )
366312 curve .setZ (curve .z () - 1.0 )
367313 curve .attach (self )
368314 self .curves ['User' ] = curve
369- self .data ['User' ] = zeros (HISTORY , Float )
315+ self .data ['User' ] = np . zeros (HISTORY , np . float )
370316
371317 curve = CpuCurve ('Total' )
372318 curve .setColor (Qt .Qt .black )
373319 curve .setZ (curve .z () - 2.0 )
374320 curve .attach (self )
375321 self .curves ['Total' ] = curve
376- self .data ['Total' ] = zeros (HISTORY , Float )
322+ self .data ['Total' ] = np . zeros (HISTORY , np . float )
377323
378324 curve = CpuCurve ('Idle' )
379325 curve .setColor (Qt .Qt .darkCyan )
380326 curve .setZ (curve .z () - 3.0 )
381327 curve .attach (self )
382328 self .curves ['Idle' ] = curve
383- self .data ['Idle' ] = zeros (HISTORY , Float )
329+ self .data ['Idle' ] = np . zeros (HISTORY , np . float )
384330
385331 self .showCurve (self .curves ['System' ], True )
386332 self .showCurve (self .curves ['User' ], True )
@@ -389,13 +335,9 @@ def __init__(self, *args):
389335
390336 self .startTimer (1000 )
391337
392- self .connect (self ,
393- Qt .SIGNAL ('legendChecked(QwtPlotItem*, bool)' ),
394- self .showCurve )
338+ self .connect (legend , legend .SIG_CHECKED , self .showCurve )
395339 self .replot ()
396340
397- # __init__()
398-
399341 def timerEvent (self , e ):
400342 for data in self .data .values ():
401343 data [1 :] = data [0 :- 1 ]
@@ -412,32 +354,21 @@ def timerEvent(self, e):
412354
413355 self .replot ()
414356
415- # timerEvent()
416-
417- def showCurve (self , item , on ):
357+ def showCurve (self , item , on , index = None ):
418358 item .setVisible (on )
419- widget = self .legend ().find (item )
420- if isinstance (widget , Qwt .QwtLegendItem ):
421- widget .setChecked (on )
359+ self .legend ().legendWidget (item ).setChecked (on )
422360 self .replot ()
423361
424- # showCurve()
425-
426362 def cpuPlotCurve (self , key ):
427363 return self .curves [key ]
428364
429- # cpuPlotCurve()
430-
431- # class CpuPlot
432-
433365
434366def make ():
435367 demo = Qt .QWidget ()
436368 demo .setWindowTitle ('Cpu Plot' )
437369
438370 plot = CpuPlot (demo )
439371 plot .setTitle ("History" )
440- plot .setMargin (5 )
441372
442373 label = Qt .QLabel ("Press the legend to en/disable a curve" , demo )
443374
@@ -449,26 +380,8 @@ def make():
449380 demo .show ()
450381 return demo
451382
452- # make()
453-
454383
455- def main ( args ) :
456- app = Qt .QApplication (args )
384+ if __name__ == '__main__' :
385+ app = Qt .QApplication (sys . argv )
457386 demo = make ()
458387 sys .exit (app .exec_ ())
459-
460- # main()
461-
462-
463- # Admire!
464- if __name__ == '__main__' :
465- if 'settracemask' in sys .argv :
466- # for debugging, requires: python configure.py --trace ...
467- import sip
468- sip .settracemask (0x3f )
469-
470- main (sys .argv )
471-
472- # Local Variables: ***
473- # mode: python ***
474- # End: ***
0 commit comments