File: dials.py

package info (click to toggle)
pyqt-qwt 1.02.02-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 796 kB
  • sloc: python: 5,663; cpp: 273; makefile: 16; sh: 13
file content (400 lines) | stat: -rwxr-xr-x 15,999 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/python3

# python radio.py <qtversion (4 or 5)>
# Tested for python3 Qt5. Crashes if mouse is over plot canvas

import sys
from PyQt5 import Qwt
from PyQt5.QtCore import Qt,  QTimer,  QRectF, QPointF,  qRound # pyqtSignal, Qt,  QSize, QBasicTimer
from PyQt5.QtGui import QColor,  QPalette, QPainterPath,  QPen #,  QPixmap, QFont,  QIcon, QLinearGradient
from PyQt5.QtWidgets import (QWidget,  QApplication,  QTabWidget,  QGridLayout )
#from PyQt5.QtPrintSupport import QPrintDialog, QPrinter

M_PI=3.141

class CompassGrid( QWidget ): #QFrame( parent )
    def __init__(self, parent=None):
        QWidget.__init__(self,  parent)
        p = QPalette()
        p.setColor( self.backgroundRole(), Qt.gray )
        self.setPalette( p )
        self.setAutoFillBackground( True )
        layout = QGridLayout( self )
        layout.setSpacing( 5 )
        #layout.setMargin( 0 )
        for i in range(6):
            compass = self.createCompass( i )
            layout.addWidget( compass, i // 3, i % 3 )

        #for i in range(layout.columnCount()):
        #    layout.setColumnStretch( i, 1 )

    def createCompass( self, pos ):
        palette0 = QPalette()
        for c in range(QPalette.NColorRoles):
            colorRole = QPalette.ColorRole( c )
            palette0.setColor( colorRole, QColor() )

        #palette0.setColor( QPalette.Base, palette().color( QPalette.backgroundRole() ).light( 120 ) )
        palette0.setColor( QPalette.WindowText,
            palette0.color( QPalette.Base ) )

        compass = Qwt.QwtCompass( self )
        compass.setLineWidth( 4 )
        compass.setFrameShadow(Qwt.QwtCompass.Sunken)
        #    pos <= 2 ? QwtCompass.Sunken : QwtCompass.Raised )

        if pos == 0:
            #A compass with a rose and no needle. Scale and rose are rotating.
            compass.setMode( Qwt.QwtCompass.RotateScale )

            rose = Qwt.QwtSimpleCompassRose( 16, 2 )
            rose.setWidth( 0.15 )

            compass.setRose( rose )
        elif pos == 1:
            #A windrose, with a scale indicating the main directions only
            map = {}
            map[0.0] = "N" 
            map[90.0] = "E" 
            map[180.0] = "S" 
            map[270.0] = "W" 

            compass.setScaleDraw( Qwt.QwtCompassScaleDraw( map ) )

            rose = Qwt.QwtSimpleCompassRose( 4, 1 )
            compass.setRose( rose )

            compass.setNeedle( Qwt.QwtCompassWindArrow( Qwt.QwtCompassWindArrow.Style2 ) )
            compass.setValue( 60.0 )
        elif pos == 2:
            #A compass with a rotating needle in darkBlue. Shows
            #a ticks for each degree.

            palette0.setColor( QPalette.Base, Qt.darkBlue )
            palette0.setColor( QPalette.WindowText, QColor( Qt.darkBlue ))#.dark( 120 ) )
            palette0.setColor( QPalette.Text, Qt.white )

            scaleDraw = Qwt.QwtCompassScaleDraw()
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Ticks, True )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Labels, True )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Backbone, False )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MinorTick, 1 )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MediumTick, 1 )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MajorTick, 3 )

            compass.setScaleDraw( scaleDraw )

            compass.setScaleMaxMajor( 36 )
            compass.setScaleMaxMinor( 5 )

            compass.setNeedle( Qwt.QwtCompassMagnetNeedle( Qwt.QwtCompassMagnetNeedle.ThinStyle ) )
            compass.setValue( 220.0 )
        elif pos == 3:
            #A compass without a frame, showing numbers as tick labels.
            #The origin is at 220.0
            palette0.setColor( QPalette.Base, self.palette().color( self.backgroundRole() ) )
            palette0.setColor( QPalette.WindowText, Qt.blue )
            compass.setLineWidth( 0 )
            map = {}
            for  d in range(0,360,60):
                map[d] = "%.0f"%(d*1.0) 

            scaleDraw = Qwt.QwtCompassScaleDraw( map )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Ticks, True )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Labels, True )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Backbone, True )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MinorTick, 0 )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MediumTick, 0 )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MajorTick, 3 )
            compass.setScaleDraw( scaleDraw )
            compass.setScaleMaxMajor( 36 )
            compass.setScaleMaxMinor( 5 )
            compass.setNeedle( Qwt.QwtDialSimpleNeedle( Qwt.QwtDialSimpleNeedle.Ray, True, Qt.white ) )
            compass.setOrigin( 220.0 )
            compass.setValue( 20.0 )
        elif pos == 4:
            #A compass showing another needle
            scaleDraw = Qwt.QwtCompassScaleDraw()
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Ticks, True )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Labels, True )
            scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Backbone, False )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MinorTick, 0 )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MediumTick, 0 )
            scaleDraw.setTickLength( Qwt.QwtScaleDiv.MajorTick, 3 )
            compass.setScaleDraw( scaleDraw )
            compass.setNeedle( Qwt.QwtCompassMagnetNeedle( Qwt.QwtCompassMagnetNeedle.TriangleStyle, Qt.white, Qt.red ) )
            compass.setValue( 220.0 )
        elif pos == 5:
            #A compass with a yellow on black ray
            palette0.setColor( QPalette.WindowText, Qt.black )
            compass.setNeedle( Qwt.QwtDialSimpleNeedle( Qwt.QwtDialSimpleNeedle.Ray, False, Qt.yellow ) )
            compass.setValue( 315.0 )

        newPalette = compass.palette()
        for c in range(QPalette.NColorRoles):
            colorRole = QPalette.ColorRole( c )
            if ( palette0.color( colorRole ).isValid() ):
                newPalette.setColor( colorRole, palette0.color( colorRole ) )

        for i in range(QPalette.NColorGroups):
            colorGroup = QPalette.ColorGroup( i )
            light = newPalette.color( colorGroup, QPalette.Base )#.light( 170 )
            dark = newPalette.color( colorGroup, QPalette.Base )#.dark( 170 )
            #mid = compass.frameShadow() == QwtDial.Raised
            #    ? newPalette.color( colorGroup, QPalette.Base ).dark( 110 )
            #    : newPalette.color( colorGroup, QPalette.Base ).light( 110 )
            mid = newPalette.color( colorGroup, QPalette.Base )#.dark( 110 )
            newPalette.setColor( colorGroup, QPalette.Dark, dark )
            newPalette.setColor( colorGroup, QPalette.Mid, mid )
            newPalette.setColor( colorGroup, QPalette.Light, light )
        compass.setPalette( newPalette )
        return compass

class SpeedoMeter( Qwt.QwtDial ): #QwtDial( parent ), 
    def __init__(self,parent = None):
        Qwt.QwtDial.__init__(self,parent)
        self.d_label = "km/h"
        scaleDraw = Qwt.QwtRoundScaleDraw()
        scaleDraw.setSpacing( 8 )
        scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Backbone, False )
        scaleDraw.setTickLength( Qwt.QwtScaleDiv.MinorTick, 0 )
        scaleDraw.setTickLength( Qwt.QwtScaleDiv.MediumTick, 4 )
        scaleDraw.setTickLength( Qwt.QwtScaleDiv.MajorTick, 8 )
        self. setScaleDraw( scaleDraw )

        self.setWrapping( False )
        self.setReadOnly( True )

        self.setOrigin( 135.0 )
        self.setScaleArc( 0.0, 270.0 )

        needle = Qwt.QwtDialSimpleNeedle(
            Qwt.QwtDialSimpleNeedle.Arrow, True, Qt.red,
            QColor( Qt.gray ))#.light( 130 ) )
        self.setNeedle( needle )

    def setLabel( self, label ):
        self.d_label = label
        self.update()

    def label(self):
        return self.d_label

    def drawScaleContents( self, painter, center, radius ):
        rect = QRectF( 0.0, 0.0, 2.0 * radius, 2.0 * radius - 10.0 )
        rect.moveCenter( center )
        color = self.palette().color( QPalette.Text )
        painter.setPen( color )
        flags = Qt.AlignBottom | Qt.AlignHCenter
        painter.drawText( rect, flags, self.d_label )

class AttitudeIndicatorNeedle(Qwt.QwtDialNeedle):
    def __init__(self,  color=None):
        Qwt.QwtDialNeedle.__init__(self)
        palette = QPalette()
        palette.setColor( QPalette.Text, color )
        #self.setPalette( palette )

    def rawNeedle( self,  painter, length, colorGroup ):
        triangleSize = length * 0.1
        pos = length - 2.0

        path = QPainterPath()
        path.moveTo( pos, 0 )
        path.lineTo( pos - 2 * triangleSize, triangleSize )
        path.lineTo( pos - 2 * triangleSize, -triangleSize )
        path.closeSubpath()

        painter.setBrush( self.palette().brush( colorGroup, QPalette.Text ) )
        painter.drawPath( path )

        l = length - 2
        painter.setPen( QPen( self.palette().color( colorGroup, QPalette.Text ), 3 ) )
        painter.drawLine( QPointF( 0.0, -l ), QPointF( 0.0, l ) )


class AttitudeIndicator(Qwt.QwtDial):
    def __init__(self, parent = None):
        Qwt.QwtDial.__init__( self,  parent )
        self.d_gradient = 0.0
        scaleDraw = Qwt.QwtRoundScaleDraw()
        scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Backbone, False )
        scaleDraw.enableComponent( Qwt.QwtAbstractScaleDraw.Labels, False )
        self.setScaleDraw( scaleDraw )

        #self.setMode( Qwt.RotateScale )
        self.setWrapping( True )

        self.setOrigin( 270.0 )

        self.setScaleMaxMinor( 0 )
        self.setScaleStepSize( 30.0 )
        self.setScale( 0.0, 360.0 )

        color = self.palette().color( QPalette.Text )
        self.setNeedle( AttitudeIndicatorNeedle( color ) )

    def setGradient( self,  gradient ):
        if ( gradient < -1.0 ):
            gradient = -1.0
        elif ( gradient > 1.0 ):
            gradient = 1.0
        if ( self.d_gradient != gradient ):
            self.d_gradient = gradient
            self.update()

    def drawScale( self,  painter, center, radius ):
        offset = 4.0
        p0 = self.qwtPolar2Pos( center, offset, 1.5 * M_PI )
        w = self.innerRect().width()
        path = QPainterPath()
        path.moveTo( Qwt.qwtPolar2Pos( p0, w, 0.0 ) )
        path.lineTo( Qwt.qwtPolar2Pos( path.currentPosition(), 2 * w, M_PI ) )
        path.lineTo( Qwt.qwtPolar2Pos( path.currentPosition(), w, 0.5 * M_PI ) )
        path.lineTo( Qwt.qwtPolar2Pos( path.currentPosition(), w, 0.0 ) )
        painter.save()
        painter.setClipPath( path ) # swallow 180 - 360 degrees
        Qwt.QwtDial.drawScale( painter, center, radius )
        painter.restore()

    def drawScaleContents( self,  painter, double ):
        dir = 360 - qRound( self.origin() - self.value() ) # counter clockwise
        arc = 90 + qRound( self.gradient() * 90 )
        skyColor = QColor( 38, 151, 221 )
        painter.save()
        painter.setBrush( skyColor )
        painter.drawChord( self.scaleInnerRect(), ( dir - arc ) * 16, 2 * arc * 16 )
        painter.restore()

    def keyPressEvent( self,  event ):
        k = event.key() 
        if k == Qt.Key_Plus:
            self.setGradient( self.gradient() + 0.05 )
        elif k == Qt.Key_Minus:
            self.setGradient( self.gradient() - 0.05 )
        else:
            Qwt.QwtDial.keyPressEvent( event )

    def angle(self):
        return self.value()
        
    def gradient(self):
        return self.d_gradient

    def setAngle( self,  angle ):
        self.setValue( angle )

class CockpitGrid( QWidget ):
    def __init__(self, parent = None):
        QWidget.__init__(self, parent)
        self.setAutoFillBackground( True )
        #self.setPalette( self.colorTheme( QColor( Qt.darkGray )))#.dark( 150 ) ) )
        layout = QGridLayout( self )
        layout.setSpacing( 5 )
        #layout.setMargin( 0 )
        for i in range(2): # FIXME. Should be 3 but program crashes for the third one.
            dial = self.createDial( i )
            layout.addWidget( dial, 0, i )

        for i in range(layout.columnCount()):
            layout.setColumnStretch( i, 1 )

    def createDial( self, pos ):
        dial = None
        if pos == 0:
            self.d_clock = Qwt.QwtAnalogClock( )
            # disable minor ticks
            #d_clock.scaleDraw().setTickLength( QwtScaleDiv.MinorTick, 0 )
            knobColor = QColor( Qt.gray ) # .light( 130 )

            for i in range(Qwt.QwtAnalogClock.NHands):
                handColor = QColor( Qt.gray ) #.light( 150 )
                width = 8
                if i == Qwt.QwtAnalogClock.SecondHand:
                    handColor = Qt.gray
                    width = 5
                hand = Qwt.QwtDialSimpleNeedle( Qwt.QwtDialSimpleNeedle.Arrow, True, handColor, knobColor )
                hand.setWidth( width )
                self.d_clock.setHand( Qwt.QwtAnalogClock.Hand( i ), hand )
            timer = QTimer( self.d_clock )
            timer.timeout.connect(self.d_clock.setCurrentTime)
            timer.start( 1000 )
            dial = self.d_clock
        elif pos == 1:
            self.d_speedo = SpeedoMeter( self )
            self.d_speedo.setScaleStepSize( 20.0 )
            self.d_speedo.setScale( 0.0, 240.0 )
            self.d_speedo.scaleDraw().setPenWidth( 2 )
            timer = QTimer( self.d_speedo )
            timer.timeout.connect(self.changeSpeed )
            timer.start( 50 )
            dial = self.d_speedo
        elif pos == 2:
            self.d_ai = AttitudeIndicator( self )
            self.d_ai.scaleDraw().setPenWidth( 3 )
            gradientTimer = QTimer( self.d_ai )
            gradientTimer.timeout.connect(self.changeGradient )
            gradientTimer.start( 100 )
            angleTimer = QTimer( self.d_ai )
            angleTimer.timeout.connect( self.changeAngle )
            angleTimer.start( 100 )
            dial = self.d_ai

        if ( dial ):
            dial.setReadOnly( True )
            dial.setLineWidth( 4 )
            dial.setFrameShadow(Qwt.QwtDial.Sunken )
        return dial

    def colorTheme( self, base ):
        palette = QPalette
        palette.setColor( QPalette.Base, base )
        palette.setColor( QPalette.Window, base.dark( 150 ) )
        palette.setColor( QPalette.Mid, base.dark( 110 ) )
        palette.setColor( QPalette.Light, base.light( 170 ) )
        palette.setColor( QPalette.Dark, base.dark( 170 ) )
        palette.setColor( QPalette.Text, base.dark( 200 ).light( 800 ) )
        palette.setColor( QPalette.WindowText, base.dark( 200 ) )
        return palette

    def changeSpeed(self):
        offset = 0.8
        speed = self.d_speedo.value()
        if ( ( speed < 7.0 and offset < 0.0 ) or ( speed > 203.0 and offset > 0.0 ) ):
            offset = -offset
        counter = 0
        """switch( counter++ % 12 )
        {
            case 0:
            case 2:
            case 7:
            case 8:
                break
            default:
        }"""
        self.d_speedo.setValue( speed + offset )

    def changeAngle(self):
        offset = 0.05
        angle = self.d_ai.angle()
        if ( angle > 180.0 ):
            angle -= 360.0
        if ( ( angle < -5.0 and offset < 0.0 ) or ( angle > 5.0 and offset > 0.0 ) ):
            offset = -offset
        self.d_ai.setAngle( angle + offset )

    def changeGradient(self):
        offset = 0.005
        gradient = self.d_ai.gradient()
        if ( ( gradient < -0.05 and offset < 0.0 ) or ( gradient > 0.05 and offset > 0.0 ) ):
            offset = -offset
        self.d_ai.setGradient( gradient + offset )

a = QApplication(sys.argv)
tabWidget = QTabWidget()
tabWidget.addTab(CompassGrid(),"Compass")
tabWidget.addTab(CockpitGrid(),"Cockpit")
tabWidget.show()

sys.exit(a.exec_())