Skip to content

Commit

Permalink
formatted altimeter with black
Browse files Browse the repository at this point in the history
  • Loading branch information
e100 committed Jun 15, 2024
1 parent 4d94a14 commit c2f4acd
Showing 1 changed file with 123 additions and 68 deletions.
191 changes: 123 additions & 68 deletions src/pyefis/instruments/altimeter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import pyavtools.fix as fix

from pyefis.instruments.NumericalDisplay import NumericalDisplay
import pyefis.hmi as hmi
import pyefis.hmi as hmi
from pyefis.instruments import helpers


class Altimeter(QWidget):
FULL_WIDTH = 300
def __init__(self, parent=None, bg_color=Qt.black, font_family="DejaVu Sans Condensed"):

def __init__(
self, parent=None, bg_color=Qt.black, font_family="DejaVu Sans Condensed"
):
super(Altimeter, self).__init__(parent)
self.setStyleSheet("border: 0px")
self.font_family = font_family
Expand Down Expand Up @@ -56,10 +60,10 @@ def paintEvent(self, event):
h = self.height()
dial = QPainter(self)
dial.setRenderHint(QPainter.Antialiasing)
radius = int(round(min(w,h)*.45))
radius = int(round(min(w, h) * 0.45))
diameter = radius * 2
center_x = w/2
center_y = h/2
center_x = w / 2
center_y = h / 2

# Draw the Black Background
dial.fillRect(0, 0, w, h, QColor(self.bg_color))
Expand All @@ -76,7 +80,9 @@ def paintEvent(self, event):

# Dial Setup
dial.setPen(dialPen)
dial.drawEllipse(QRectF(center_x-radius, center_y-radius, diameter, diameter))
dial.drawEllipse(
QRectF(center_x - radius, center_y - radius, diameter, diameter)
)

f = QFont(self.font_family)
fs = int(round(20 * w / self.FULL_WIDTH))
Expand All @@ -91,47 +97,60 @@ def paintEvent(self, event):
altimeter_numbers = 0
while count < 360:
if count % 36 == 0:
dial.drawLine(0, -(radius), 0, -(radius-15))
dial.drawLine(0, -(radius), 0, -(radius - 15))
x = fontMetrics.width(str(altimeter_numbers)) / 2
y = f.pixelSize()
dial.drawText(qRound(-x), qRound(-(radius-15-y)),
str(altimeter_numbers))
dial.drawText(
qRound(-x), qRound(-(radius - 15 - y)), str(altimeter_numbers)
)
altimeter_numbers += 1
else:
dial.drawLine(0, -(radius), 0, -(radius-10))
dial.drawLine(0, -(radius), 0, -(radius - 10))

dial.rotate(36)
count += 36
count = 0
while count < 360:
dial.drawLine(0, -(radius), 0, -(radius-10))
dial.drawLine(0, -(radius), 0, -(radius - 10))

dial.rotate(7.2)
count += 7.2

if self.item.fail:
warn_font = QFont(self.font_family, 30, QFont.Bold)
dial.resetTransform()
dial.setPen (QPen(QColor(Qt.red)))
dial.setBrush (QBrush(QColor(Qt.red)))
dial.setFont (warn_font)
dial.drawText (0,0,w,h, Qt.AlignCenter, "XXX")
dial.setPen(QPen(QColor(Qt.red)))
dial.setBrush(QBrush(QColor(Qt.red)))
dial.setFont(warn_font)
dial.drawText(0, 0, w, h, Qt.AlignCenter, "XXX")
return

dial.setBrush(dialBrush)
# Needle Movement
sm_dial = QPolygonF([QPointF(5, 0), QPointF(0, +5), QPointF(-5, 0),
QPointF(0, -(radius-15))])
lg_dial = QPolygonF([QPointF(10, -(radius/9)), QPointF(5, 0),
QPointF(0, +5), QPointF(-5, 0),
QPointF(-10, -(radius/9)),
QPointF(0, -int(round((radius*.6))))])
outside_dial = QPolygonF([QPointF( 7.5, -(radius)), QPointF( -7.5 , -(radius)),
QPointF(0, -(radius-10))])

sm_dial_angle = self._altimeter * .36 - 7.2
lg_dial_angle = self._altimeter / 10 * .36 - 7.2
outside_dial_angle = self._altimeter / 100 * .36 - 7.2
sm_dial = QPolygonF(
[QPointF(5, 0), QPointF(0, +5), QPointF(-5, 0), QPointF(0, -(radius - 15))]
)
lg_dial = QPolygonF(
[
QPointF(10, -(radius / 9)),
QPointF(5, 0),
QPointF(0, +5),
QPointF(-5, 0),
QPointF(-10, -(radius / 9)),
QPointF(0, -int(round((radius * 0.6)))),
]
)
outside_dial = QPolygonF(
[
QPointF(7.5, -(radius)),
QPointF(-7.5, -(radius)),
QPointF(0, -(radius - 10)),
]
)

sm_dial_angle = self._altimeter * 0.36 - 7.2
lg_dial_angle = self._altimeter / 10 * 0.36 - 7.2
outside_dial_angle = self._altimeter / 100 * 0.36 - 7.2

dial.rotate(sm_dial_angle)
dial.drawPolygon(sm_dial)
Expand Down Expand Up @@ -166,11 +185,11 @@ def setUnitSwitching(self):
self.update()

def setUnits(self, args):
x = args.split(':')
x = args.split(":")
command = x[1].lower()
names = x[0].split(',')
if self.item.key in names or '*' in names or self.unitGroup in names:
#item = fix.db.get_item(self.dbkey)
names = x[0].split(",")
if self.item.key in names or "*" in names or self.unitGroup in names:
# item = fix.db.get_item(self.dbkey)
if command == "toggle":
if self.__currentUnits == 1:
self.unitsOverride = self.unitsOverride2
Expand All @@ -180,9 +199,8 @@ def setUnits(self, args):
self.unitsOverride = self.unitsOverride1
self.conversionFunction = self.conversionFunction1
self.__currentUnits = 1
#self.setAuxData(item.aux) # Trigger conversion for aux data
self.altimeter = self.item.value # Trigger the conversion for value

# self.setAuxData(item.aux) # Trigger conversion for aux data
self.altimeter = self.item.value # Trigger the conversion for value

def getAltimeter(self):
return self._altimeter
Expand All @@ -197,7 +215,15 @@ def setAltimeter(self, altimeter):


class Altimeter_Tape(QGraphicsView):
def __init__(self, parent=None, dbkey="ALT", maxalt=50000, fontsize=15,font_percent=None,font_family="DejaVu Sans Condensed"):
def __init__(
self,
parent=None,
dbkey="ALT",
maxalt=50000,
fontsize=15,
font_percent=None,
font_family="DejaVu Sans Condensed",
):
super(Altimeter_Tape, self).__init__(parent)
self.setStyleSheet("background: transparent")
self.font_family = font_family
Expand All @@ -221,7 +247,6 @@ def __init__(self, parent=None, dbkey="ALT", maxalt=50000, fontsize=15,font_perc
self.majorDiv = 200
self.minorDiv = 100


self.maxalt = maxalt
self._maxalt = maxalt
self.total_decimals = 5
Expand All @@ -234,33 +259,46 @@ def __init__(self, parent=None, dbkey="ALT", maxalt=50000, fontsize=15,font_perc
def resizeEvent(self, event):
if self.font_percent:
self.fontsize = qRound(self.width() * self.font_percent)
self.pph = self.fontsize / ( self.height() / 20)
self.pph = self.fontsize / (self.height() / 20)

w = self.width()
w_2 = w/2
w_2 = w / 2
h = self.height()
f = QFont(self.font_family)
if self.font_mask:
self.font_size = helpers.fit_to_mask(self.width()*.55,self.height()*0.05,self.font_mask,self.font_family)
self.font_size = helpers.fit_to_mask(
self.width() * 0.55,
self.height() * 0.05,
self.font_mask,
self.font_family,
)
f.setPointSizeF(self.font_size)
else:
f.setPixelSize(self.fontsize)
self.height_pixel = (self.maxalt*2*self.pph + h ) #+ abs(self.minalt*self.pph)
self.height_pixel = (
self.maxalt * 2 * self.pph + h
) # + abs(self.minalt*self.pph)

dialPen = QPen(QColor(Qt.white))
dialPen.setWidth(int(self.height() * 0.005))

self.scene = QGraphicsScene(0, 0, w, self.height_pixel)
x = self.scene.addRect(0, 0, w, self.height_pixel,
QPen(QColor(32, 32, 32)), QBrush(QColor(32, 32, 32)))
x = self.scene.addRect(
0,
0,
w,
self.height_pixel,
QPen(QColor(32, 32, 32)),
QBrush(QColor(32, 32, 32)),
)
x.setOpacity(self.backgroundOpacity)

for i in range(self.maxalt * 2, -1, -self.minorDiv):
y = self.y_offset(i)
if (i - self.maxalt) % self.majorDiv == 0:
l = self.scene.addLine(w_2 + 15, y, w, y, dialPen)
l.setOpacity(self.foregroundOpacity)
t = self.scene.addText(str(i-self.maxalt))
t = self.scene.addText(str(i - self.maxalt))
t.setFont(f)
self.scene.setFont(f)
t.setDefaultTextColor(QColor(Qt.white))
Expand All @@ -273,16 +311,24 @@ def resizeEvent(self, event):
l.setOpacity(self.foregroundOpacity)
self.setScene(self.scene)

self.numerical_display = NumericalDisplay(self, total_decimals=self.total_decimals, scroll_decimal=2)
nbh=w/1.20
self.numerical_display.resize (qRound(w/1.20), qRound(nbh/1.45))
self.numeric_box_pos = QPoint(0, qRound(h/2-(nbh/1.45)/2))
self.numerical_display = NumericalDisplay(
self, total_decimals=self.total_decimals, scroll_decimal=2
)
nbh = w / 1.20
self.numerical_display.resize(qRound(w / 1.20), qRound(nbh / 1.45))
self.numeric_box_pos = QPoint(0, qRound(h / 2 - (nbh / 1.45) / 2))
self.numerical_display.move(self.numeric_box_pos)
self.numeric_box_pos.setX(self.numeric_box_pos.x()+self.numerical_display.width())
self.numeric_box_pos.setY(qRound(self.numeric_box_pos.y()+(nbh/1.45)/2)+1)
self.numeric_box_pos.setX(
self.numeric_box_pos.x() + self.numerical_display.width()
)
self.numeric_box_pos.setY(
qRound(self.numeric_box_pos.y() + (nbh / 1.45) / 2) + 1
)
self.numerical_display.show()
self.numerical_display.value = self._altimeter
self.centerOn(self.scene.width() / 2, self.y_offset(self._altimeter+self.maxalt))
self.centerOn(
self.scene.width() / 2, self.y_offset(self._altimeter + self.maxalt)
)
self.setAltOld(self.item.old)
self.setAltBad(self.item.bad)
self.setAltFail(self.item.fail)
Expand All @@ -292,12 +338,15 @@ def resizeEvent(self, event):
self.item.failChanged[bool].connect(self.setAltFail)

def y_offset(self, alt):
return self.height_pixel - (alt*self.pph) - self.height()
return self.height_pixel - (alt * self.pph) - self.height()

def redraw(self):
if not self.isVisible(): return
if not self.isVisible():
return
self.resetTransform()
self.centerOn(self.scene.width() / 2, self.y_offset(self._altimeter+self.maxalt))
self.centerOn(
self.scene.width() / 2, self.y_offset(self._altimeter + self.maxalt)
)
self.numerical_display.value = self._altimeter

# Index Line that doesn't move to make it easy to read the altimeter.
Expand All @@ -312,25 +361,32 @@ def paintEvent(self, event):
p.translate(self.numeric_box_pos.x(), self.numeric_box_pos.y())
p.setPen(marks)
p.setBrush(QBrush(Qt.black))
triangle_size = w/8
p.drawConvexPolygon(QPolygonF([QPointF(0, -triangle_size),
QPointF(0, triangle_size),
QPointF(triangle_size, 0)]))
triangle_size = w / 8
p.drawConvexPolygon(
QPolygonF(
[
QPointF(0, -triangle_size),
QPointF(0, triangle_size),
QPointF(triangle_size, 0),
]
)
)

def setUnitSwitching(self):
"""When this function is called the unit switching features are used"""
self.__currentUnits = 1
self.unitsOverride = self.unitsOverride1
self.conversionFunction = self.conversionFunction1
hmi.actions.setInstUnits.connect(self.setUnits)
if self.isVisible(): self.update()
if self.isVisible():
self.update()

def setUnits(self, args):
x = args.split(':')
x = args.split(":")
command = x[1].lower()
names = x[0].split(',')
if self.item.key in names or '*' in names or self.unitGroup in names:
#item = fix.db.get_item(self.dbkey)
names = x[0].split(",")
if self.item.key in names or "*" in names or self.unitGroup in names:
# item = fix.db.get_item(self.dbkey)
if command == "toggle":
if self.__currentUnits == 1:
self.unitsOverride = self.unitsOverride2
Expand All @@ -340,8 +396,8 @@ def setUnits(self, args):
self.unitsOverride = self.unitsOverride1
self.conversionFunction = self.conversionFunction1
self.__currentUnits = 1
#self.setAuxData(item.aux) # Trigger conversion for aux data
self.altimeter = self.item.value # Trigger the conversion for value
# self.setAuxData(item.aux) # Trigger conversion for aux data
self.altimeter = self.item.value # Trigger the conversion for value

def getAltimeter(self):
return self._altimeter
Expand All @@ -354,13 +410,13 @@ def setAltimeter(self, altimeter):

altimeter = property(getAltimeter, setAltimeter)

def setAltOld(self,b):
def setAltOld(self, b):
self.numerical_display.old = b

def setAltBad(self,b):
def setAltBad(self, b):
self.numerical_display.bad = b

def setAltFail(self,b):
def setAltFail(self, b):
self.numerical_display.fail = b

# We don't want this responding to keystrokes
Expand All @@ -370,4 +426,3 @@ def keyPressEvent(self, event):
# Don't want it acting with the mouse scroll wheel either
def wheelEvent(self, event):
pass

0 comments on commit c2f4acd

Please sign in to comment.