22
33import os
44import sys
5-
6- from PyQt4 import Qt
7- import qwt as Qwt
85import numpy as np
96
7+ from qwt .qt .QtGui import (QApplication , QColor , QBrush , QWidget , QVBoxLayout ,
8+ QLabel )
9+ from qwt .qt .QtCore import QRect , QTime
10+ from qwt .qt .QtCore import Qt
11+ from qwt import (QwtPlot , QwtPlotMarker , QwtScaleDraw , QwtLegend , QwtPlotCurve ,
12+ QwtPlotItem , QwtLegendData , QwtText )
13+
1014
1115class CpuStat :
1216 User = 0
@@ -175,7 +179,7 @@ def statistic(self):
175179 return 100.0 * userDelta / totalDelta , 100.0 * systemDelta / totalDelta
176180
177181 def upTime (self ):
178- result = Qt . QTime ()
182+ result = QTime ()
179183 for item in self .procValues :
180184 result = result .addSecs (item / 100 )
181185 return result
@@ -193,18 +197,18 @@ def __lookup(self):
193197 return result
194198
195199
196- class CpuPieMarker (Qwt . QwtPlotMarker ):
200+ class CpuPieMarker (QwtPlotMarker ):
197201 def __init__ (self , * args ):
198- Qwt . QwtPlotMarker .__init__ (self , * args )
202+ QwtPlotMarker .__init__ (self , * args )
199203 self .setZ (1000.0 )
200- self .setRenderHint (Qwt . QwtPlotItem .RenderAntialiased , True )
204+ self .setRenderHint (QwtPlotItem .RenderAntialiased , True )
201205
202206 def rtti (self ):
203- return Qwt . QwtPlotItem .Rtti_PlotUserItem
207+ return QwtPlotItem .Rtti_PlotUserItem
204208
205209 def draw (self , painter , xMap , yMap , rect ):
206210 margin = 5
207- pieRect = Qt . QRect ()
211+ pieRect = QRect ()
208212 pieRect .setX (rect .x () + margin )
209213 pieRect .setY (rect .y () + margin )
210214 pieRect .setHeight (yMap .transform (80.0 ))
@@ -216,49 +220,49 @@ def draw(self, painter, xMap, yMap, rect):
216220 if curve .dataSize ():
217221 value = int (5760 * curve .sample (0 ).y ()/ 100.0 )
218222 painter .save ()
219- painter .setBrush (Qt . QBrush (curve .pen ().color (),
220- Qt .Qt . SolidPattern ))
223+ painter .setBrush (QBrush (curve .pen ().color (),
224+ Qt .SolidPattern ))
221225 painter .drawPie (pieRect , - angle , - value )
222226 painter .restore ()
223227 angle += value
224228
225229
226- class TimeScaleDraw (Qwt . QwtScaleDraw ):
230+ class TimeScaleDraw (QwtScaleDraw ):
227231 def __init__ (self , baseTime , * args ):
228- Qwt . QwtScaleDraw .__init__ (self , * args )
232+ QwtScaleDraw .__init__ (self , * args )
229233 self .baseTime = baseTime
230234
231235 def label (self , value ):
232236 upTime = self .baseTime .addSecs (int (value ))
233- return Qwt . QwtText (upTime .toString ())
237+ return QwtText (upTime .toString ())
234238
235239
236- class Background (Qwt . QwtPlotItem ):
240+ class Background (QwtPlotItem ):
237241 def __init__ (self ):
238- Qwt . QwtPlotItem .__init__ (self )
242+ QwtPlotItem .__init__ (self )
239243 self .setZ (0.0 )
240244
241245 def rtti (self ):
242- return Qwt . QwtPlotItem .Rtti_PlotUserItem
246+ return QwtPlotItem .Rtti_PlotUserItem
243247
244248 def draw (self , painter , xMap , yMap , rect ):
245- c = Qt . QColor (Qt . Qt .white )
246- r = Qt . QRect (rect )
249+ c = QColor (Qt .white )
250+ r = QRect (rect )
247251
248252 for i in range (100 , 0 , - 10 ):
249253 r .setBottom (yMap .transform (i - 10 ))
250254 r .setTop (yMap .transform (i ))
251255 painter .fillRect (r , c )
252- c = c .dark (110 )
256+ c = c .darker (110 )
253257
254258
255- class CpuCurve (Qwt . QwtPlotCurve ):
259+ class CpuCurve (QwtPlotCurve ):
256260 def __init__ (self , * args ):
257- Qwt . QwtPlotCurve .__init__ (self , * args )
258- self .setRenderHint (Qwt . QwtPlotItem .RenderAntialiased )
261+ QwtPlotCurve .__init__ (self , * args )
262+ self .setRenderHint (QwtPlotItem .RenderAntialiased )
259263
260264 def setColor (self , color ):
261- c = Qt . QColor (color )
265+ c = QColor (color )
262266 c .setAlpha (150 )
263267
264268 self .setPen (c )
@@ -267,9 +271,9 @@ def setColor(self, color):
267271
268272HISTORY = 60
269273
270- class CpuPlot (Qwt . QwtPlot ):
274+ class CpuPlot (QwtPlot ):
271275 def __init__ (self , * args ):
272- Qwt . QwtPlot .__init__ (self , * args )
276+ QwtPlot .__init__ (self , * args )
273277
274278 self .curves = {}
275279 self .data = {}
@@ -280,20 +284,20 @@ def __init__(self, *args):
280284
281285 self .plotLayout ().setAlignCanvasToScales (True )
282286
283- legend = Qwt . QwtLegend ()
284- legend .setDefaultItemMode (Qwt . QwtLegendData .Checkable )
285- self .insertLegend (legend , Qwt . QwtPlot .RightLegend )
287+ legend = QwtLegend ()
288+ legend .setDefaultItemMode (QwtLegendData .Checkable )
289+ self .insertLegend (legend , QwtPlot .RightLegend )
286290
287- self .setAxisTitle (Qwt . QwtPlot .xBottom , "System Uptime [h:m:s]" )
291+ self .setAxisTitle (QwtPlot .xBottom , "System Uptime [h:m:s]" )
288292 self .setAxisScaleDraw (
289- Qwt . QwtPlot .xBottom , TimeScaleDraw (self .cpuStat .upTime ()))
290- self .setAxisScale (Qwt . QwtPlot .xBottom , 0 , HISTORY )
291- self .setAxisLabelRotation (Qwt . QwtPlot .xBottom , - 50.0 )
293+ QwtPlot .xBottom , TimeScaleDraw (self .cpuStat .upTime ()))
294+ self .setAxisScale (QwtPlot .xBottom , 0 , HISTORY )
295+ self .setAxisLabelRotation (QwtPlot .xBottom , - 50.0 )
292296 self .setAxisLabelAlignment (
293- Qwt . QwtPlot .xBottom , Qt .Qt . AlignLeft | Qt . Qt .AlignBottom )
297+ QwtPlot .xBottom , Qt .AlignLeft | Qt .AlignBottom )
294298
295- self .setAxisTitle (Qwt . QwtPlot .yLeft , "Cpu Usage [%]" )
296- self .setAxisScale (Qwt . QwtPlot .yLeft , 0 , 100 )
299+ self .setAxisTitle (QwtPlot .yLeft , "Cpu Usage [%]" )
300+ self .setAxisScale (QwtPlot .yLeft , 0 , 100 )
297301
298302 background = Background ()
299303 background .attach (self )
@@ -302,27 +306,27 @@ def __init__(self, *args):
302306 pie .attach (self )
303307
304308 curve = CpuCurve ('System' )
305- curve .setColor (Qt .Qt . red )
309+ curve .setColor (Qt .red )
306310 curve .attach (self )
307311 self .curves ['System' ] = curve
308312 self .data ['System' ] = np .zeros (HISTORY , np .float )
309313
310314 curve = CpuCurve ('User' )
311- curve .setColor (Qt .Qt . blue )
315+ curve .setColor (Qt .blue )
312316 curve .setZ (curve .z () - 1.0 )
313317 curve .attach (self )
314318 self .curves ['User' ] = curve
315319 self .data ['User' ] = np .zeros (HISTORY , np .float )
316320
317321 curve = CpuCurve ('Total' )
318- curve .setColor (Qt .Qt . black )
322+ curve .setColor (Qt .black )
319323 curve .setZ (curve .z () - 2.0 )
320324 curve .attach (self )
321325 self .curves ['Total' ] = curve
322326 self .data ['Total' ] = np .zeros (HISTORY , np .float )
323327
324328 curve = CpuCurve ('Idle' )
325- curve .setColor (Qt .Qt . darkCyan )
329+ curve .setColor (Qt .darkCyan )
326330 curve .setZ (curve .z () - 3.0 )
327331 curve .attach (self )
328332 self .curves ['Idle' ] = curve
@@ -335,7 +339,7 @@ def __init__(self, *args):
335339
336340 self .startTimer (1000 )
337341
338- self . connect ( legend , legend .SIG_CHECKED , self .showCurve )
342+ legend .SIG_CHECKED . connect ( self .showCurve )
339343 self .replot ()
340344
341345 def timerEvent (self , e ):
@@ -348,7 +352,7 @@ def timerEvent(self, e):
348352 self .timeData += 1.0
349353
350354 self .setAxisScale (
351- Qwt . QwtPlot .xBottom , self .timeData [- 1 ], self .timeData [0 ])
355+ QwtPlot .xBottom , self .timeData [- 1 ], self .timeData [0 ])
352356 for key in self .curves .keys ():
353357 self .curves [key ].setData (self .timeData , self .data [key ])
354358
@@ -364,15 +368,15 @@ def cpuPlotCurve(self, key):
364368
365369
366370def make ():
367- demo = Qt . QWidget ()
371+ demo = QWidget ()
368372 demo .setWindowTitle ('Cpu Plot' )
369373
370374 plot = CpuPlot (demo )
371375 plot .setTitle ("History" )
372376
373- label = Qt . QLabel ("Press the legend to en/disable a curve" , demo )
377+ label = QLabel ("Press the legend to en/disable a curve" , demo )
374378
375- layout = Qt . QVBoxLayout (demo )
379+ layout = QVBoxLayout (demo )
376380 layout .addWidget (plot )
377381 layout .addWidget (label )
378382
@@ -382,6 +386,6 @@ def make():
382386
383387
384388if __name__ == '__main__' :
385- app = Qt . QApplication (sys .argv )
389+ app = QApplication (sys .argv )
386390 demo = make ()
387391 sys .exit (app .exec_ ())
0 commit comments