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
|
from PyQt5 import QtWidgets
from PyQt5.QtGui import QColor, QBrush, QPainter
from PyQt5.QtCore import (Qt, pyqtSlot, pyqtProperty, QVariant, QRectF,
QSize)
try:
from PyQt5.QtCore import Q_ENUM
except:
# before Qt 5.10
from PyQt5.QtCore import Q_ENUMS as Q_ENUM
from qtvcp.widgets.widget_baseclass import _HalWidgetBase
import hal
# Set up logging
from qtvcp import logger
LOG = logger.getLogger(__name__)
# Force the log level for this module
#LOG.setLevel(logger.DEBUG) # One of DEBUG, INFO, WARNING, ERROR, CRITICAL
class Bar(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._vertical = False
self._opposite = False
self._minimum = 0
self._maximum = 200
self._value = 100
self.setSizePolicy(
QtWidgets.QSizePolicy.MinimumExpanding,
QtWidgets.QSizePolicy.MinimumExpanding
)
# set default number of colored steps
self.setSteps(None)
self._bar_solid_percent = 0.9
self._background_color = QColor('black')
self._padding = 4.0 # n-pixel gap around edge.
def paintEvent(self, e):
painter = QPainter(self)
brush = QBrush()
brush.setColor(self._background_color)
brush.setStyle(Qt.SolidPattern)
rect = QRectF(0, 0, painter.device().width(), painter.device().height())
painter.fillRect(rect, brush)
# Get current state.
vmin, vmax = self.minimum(), self.maximum()
value = self.value()
# Define our canvas.
d_height = painter.device().height() - (self._padding * 2)
d_width = painter.device().width() - (self._padding * 2)
if self._vertical:
# Draw the vertical bars.
step_size = d_height / self.n_steps
bar_height = step_size * self._bar_solid_percent
bar_spacer = step_size * (1 - self._bar_solid_percent) / 2
# Calculate the y-stop position, from the value in range.
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]))
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]))
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:
# Draw the horizontal bars.
step_size = d_width / self.n_steps
bar_width = step_size * self._bar_solid_percent
bar_spacer = step_size * (1 - self._bar_solid_percent) / 2
# Calculate the y-stop position, from the value in range.
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
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:
left = self._padding + d_width - ((1 + n) * step_size) + bar_spacer
else:
left = self._padding + bar_spacer + ((0 + n) * step_size)
rect = QRectF(
left ,
self._padding ,
bar_width,
d_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]))
# right to left
if self._opposite:
width = (bar_width* partial)
left = self._padding - (n * step_size) + d_width - width
rect = QRectF(
left ,
self._padding ,
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)
painter.end()
# list of color strings or an integer of steps
def setSteps(self, steps):
if steps is None:
steps = ['green', 'green', '#00b600', '#00b600', '#00d600',
'#00d600', 'yellow', 'yellow', 'red', 'red']
self.n_steps = len(steps)
self._step_color_list = steps
if isinstance(steps, list):
# list of colors.
self.n_steps = len(steps)
self._step_color_list = steps
elif isinstance(steps, int):
# int number of bars, defaults to red.
self.n_steps = steps
self._step_color_list = ['red'] * steps
else:
raise TypeError('steps must be a list or int')
def value(self):
return self._value
@pyqtSlot(float)
@pyqtSlot(int)
def setValue(self, data):
if data != self._value:
if data > self._maximum: data = self._maximum
if data < self._minimum : data = self._minimum
self._value = data
self.update()
def sizeHint(self):
if self._vertical:
return QSize(40, 120)
else:
return QSize(40, 20)
def minimum(self):
return self._minimum
def maximum(self):
return self._maximum
def setRange(self, low, hi):
self._minimum = low
self._maximum = hi
def setFormat(self, data):
pass
def getInvertedAppearance(self):
return self._opposite
def setInvertedAppearance(self, 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.
# designer will show the pyqtProperty properties in the editor
# it will use the get set and reset calls to do those actions
########################################################################
def set_step_color_l(self, data):
# convert stylesheet list of single string to list of multiple
if ',' in data[0]:
self.setSteps(data[0].split(','))
else:
self.setSteps(data)
def get_step_color_l(self):
return self._step_color_list
def reset_step_color_l(self):
self.setSteps( ['green', 'green', '#00b600', '#00b600',
'#00d600', '#00d600', 'yellow', 'yellow', 'red', 'red'])
def getBackgroundColor(self):
return self._background_color
@pyqtSlot(QColor)
def setBackgroundColor(self, value):
self._background_color = value
self.update()
def getMax(self):
return self._maximum
def setMax(self, data):
self._maximum = data
def resetMax(self):
self._maximum = 200
def getMin(self):
return self._minimum
def setMin(self, data):
self._minimum = 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
S32 = hal.HAL_S32
FLOAT = hal.HAL_FLOAT
class HalBar(Bar, _HalWidgetBase):
HALPinType = HALPinType
Q_ENUM(HALPinType)
# older version of pyqt5 need this as well as Q_ENUM
NONE = 0
S32 = hal.HAL_S32
FLOAT = hal.HAL_FLOAT
def __init__(self, *args, **kwargs):
super(). __init__( *args, **kwargs)
self._pin_type = HALPinType.S32
self._pin_name = ''
def _hal_init(self):
if self._pin_name == '':
pname = self.HAL_NAME_
else:
pname = self._pin_name
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.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))
def updateDisplay(self, data):
self.setValue(data)
def set_pin_type(self, value):
self._pin_type = value
def get_pin_type(self):
return self._pin_type
def reset_pin_type(self):
self._pin_type = HALPinType.S32
def set_pin_name(self, value):
self._pin_name = value
def get_pin_name(self):
return self._pin_name
def reset_pin_name(self):
self._pin_name = ''
pinType = pyqtProperty(HALPinType, get_pin_type, set_pin_type, reset_pin_type)
pinName = pyqtProperty(str, get_pin_name, set_pin_name, reset_pin_name)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
bar = HalBar()
bar.setProperty('setInverted',True)
bar.setProperty('setVertical',True)
bar.setValue(51)
bar.show()
app.exec_()
|