Skip to content

Commit

Permalink
qtvcp -HalBar: add vertical and opposite appearence properties
Browse files Browse the repository at this point in the history
The drawing spaces are not perfect - but I'm out of steam for this for now.
  • Loading branch information
c-morley committed Jul 15, 2023
1 parent a04a5d0 commit ea934a1
Showing 1 changed file with 73 additions and 27 deletions.
100 changes: 73 additions & 27 deletions lib/python/qtvcp/widgets/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Bar(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.vertical = False
self.opposite = False
self._vertical = False
self._opposite = False
self._minimum = 0
self._maximum = 200
self._value = 100
Expand Down Expand Up @@ -53,7 +53,7 @@ def paintEvent(self, e):
d_height = painter.device().height() - (self._padding * 2)
d_width = painter.device().width() - (self._padding * 2)

if self.vertical:
if self._vertical:
# Draw the vertical bars.
step_size = d_height / self.n_steps
bar_height = step_size * self._bar_solid_percent
Expand All @@ -63,29 +63,49 @@ def paintEvent(self, e):
pc = (value - vmin) / (vmax - vmin)
n_steps_to_draw = int(pc * self.n_steps)
partial = (pc * self.n_steps) % 1
#print('pre',value,pc, n_steps_to_draw, partial)

n =-1 # default fall through value if n_steps_to_draw = 0
# draw complete step bars
for n in range(n_steps_to_draw):
brush.setColor(QColor(self._step_color_list[n]))
rect = QRectF(
self._padding,
self._padding + d_height - ((1 + n) * step_size) + bar_spacer,
d_width,
bar_height
)
if self._opposite:
rect = QRectF(
self._padding,
self._padding + (( + n) * step_size) + bar_spacer,
d_width,
bar_height
)
else:
rect = QRectF(
self._padding,
self._padding + d_height - ((1 + n) * step_size) + bar_spacer,
d_width,
bar_height
)
painter.fillRect(rect, brush)

# draw partial step bar for in-between values
if partial > 0:
n +=1
brush.setColor(QColor(self._step_color_list[n]))
rect = QRectF(
self._padding,
self._padding + d_height - ((1 + n) * step_size) + bar_spacer,
d_width,
bar_height * partial
)
if self._opposite:
rect = QRectF(
self._padding,
self._padding + (n * step_size) + bar_spacer,
d_width,
bar_height * partial
)
else:
height = (bar_height* partial)
# left,top,width,height
rect = QRectF(
self._padding,
self._padding + d_height - (n * step_size) + bar_spacer - height,
d_width,
height- bar_spacer
)

painter.fillRect(rect, brush)

else:
Expand All @@ -104,7 +124,7 @@ def paintEvent(self, e):
for n in range(n_steps_to_draw):
brush.setColor(QColor(self._step_color_list[n]))
# left, top, width and height floats
if self.opposite:
if self._opposite:
left = self._padding + d_width - ((1 + n) * step_size) + bar_spacer
else:
left = self._padding + bar_spacer + ((0 + n) * step_size)
Expand All @@ -120,14 +140,23 @@ def paintEvent(self, e):
if partial > 0:
n+=1
brush.setColor(QColor(self._step_color_list[n]))
if self.opposite:
left = self._padding + d_width - ((1 + n) * step_size) + bar_spacer
else:
left = self._padding + bar_spacer + ((0 + n) * step_size)
rect = QRectF(
# right to left
if self._opposite:
width = (bar_width* partial)
left = self._padding - (n * step_size) + d_width - width
rect = QRectF(
left ,
self._padding ,
bar_width*partial,
width - bar_spacer,
d_height
)
# left to right
else:
left = self._padding + bar_spacer + (n * step_size)
rect = QRectF(
left,
self._padding ,
bar_width * partial,
d_height
)
painter.fillRect(rect, brush)
Expand Down Expand Up @@ -167,7 +196,7 @@ def setValue(self, data):
self.update()

def sizeHint(self):
if self.vertical:
if self._vertical:
return QSize(40, 120)
else:
return QSize(40, 20)
Expand All @@ -185,8 +214,14 @@ def setRange(self, low, hi):
def setFormat(self, data):
pass

def getInvertedAppearance(self):
return self._opposite
def setInvertedAppearance(self, data):
self.opposite = bool(data)
self._opposite = bool(data)
self.update()
def resetInvertedAppearance(self, data):
self._opposite = False
self.update()

#########################################################################
# This is how designer can interact with our widget properties.
Expand Down Expand Up @@ -228,13 +263,24 @@ def setMin(self, data):
def resetMin(self):
self._minimum = 0

def getVertical(self):
return self._vertical
def setVertical(self, data):
self._vertical = data
self.update()
def resetVertical(self):
self._vertical = False
self.update()

stepColorList = pyqtProperty(
QVariant.typeToName(QVariant.StringList),
get_step_color_l, set_step_color_l, reset_step_color_l)

backgroundColor = pyqtProperty(QColor, getBackgroundColor, setBackgroundColor)
setMaximum = pyqtProperty(int, getMax, setMax, resetMax)
setMinimum = pyqtProperty(int, getMin, setMin, resetMin)
setVertical = pyqtProperty(bool, getVertical, setVertical, resetVertical)
setInverted = pyqtProperty(bool, getInvertedAppearance, setInvertedAppearance, resetInvertedAppearance)

class HALPinType:
NONE = 0
Expand Down Expand Up @@ -264,7 +310,7 @@ def _hal_init(self):

if self._pin_type == HALPinType.FLOAT:
self.hal_pin = self.HAL_GCOMP_.newpin(pname, hal.HAL_FLOAT, hal.HAL_IN)
self.hal_pin.value_changed.connect(lambda data: self.updateFloatDisplay(data))
self.hal_pin.value_changed.connect(lambda data: self.updateDisplay(data))
elif self._pin_type == HALPinType.S32:
self.hal_pin = self.HAL_GCOMP_.newpin(pname, hal.HAL_S32, hal.HAL_IN)
self.hal_pin.value_changed.connect(lambda data: self.updateDisplay(data))
Expand Down Expand Up @@ -292,8 +338,8 @@ def reset_pin_name(self):
if __name__ == '__main__':
app = QtWidgets.QApplication([])
bar = HalBar()
#bar.opposite=True
#bar.vertical = True
bar.setProperty('setInverted',True)
bar.setProperty('setVertical',True)
bar.setValue(51)
bar.show()
app.exec_()
Expand Down

0 comments on commit ea934a1

Please sign in to comment.