power_bar_better.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from PyQt5 import QtWidgets, QtGui, QtCore
  2. from PyQt5.QtCore import Qt
  3. class _Bar(QtWidgets.QWidget):
  4. clickedValue = QtCore.pyqtSignal(int)
  5. def __init__(self, steps, *args, **kwargs):
  6. super().__init__(*args, **kwargs)
  7. self.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding,
  8. QtWidgets.QSizePolicy.MinimumExpanding)
  9. if isinstance(steps, list):
  10. # list of colors
  11. self.n_steps = len(steps)
  12. self.steps = steps
  13. elif isinstance(steps, int):
  14. self.n_steps = steps
  15. self.steps = ['red'] * steps
  16. else:
  17. raise TypeError('step must be a list or int')
  18. self._bar_solid_parent = 0.8
  19. self._background_color = QtGui.QColor('black')
  20. self._padding = 4.0
  21. def sizeHint(self):
  22. return QtCore.QSize(40, 120)
  23. def paintEvent(self, e):
  24. painter = QtGui.QPainter(self)
  25. brush = QtGui.QBrush()
  26. brush.setColor(self._background_color)
  27. brush.setStyle(Qt.SolidPattern)
  28. rect = QtCore.QRect(0, 0,
  29. painter.device().width(),
  30. painter.device().height())
  31. painter.fillRect(rect, brush)
  32. # Get current state.
  33. dial = self.parent()._dial
  34. vmin, vmax = dial.minimum(), dial.maximum()
  35. value = dial.value()
  36. pc = (value - vmin) / (vmax - vmin)
  37. n_steps_to_draw = int(pc * self.n_steps)
  38. # Define our canvas
  39. padding = 5
  40. d_height = painter.device().height() - (padding * 2)
  41. d_width = painter.device().width() - (padding * 2)
  42. # Draw the bars
  43. step_size = d_height / self.n_steps
  44. bar_height = int(step_size * self._bar_solid_parent)
  45. bar_spacer = int(step_size * self._bar_solid_parent / 2)
  46. for n in range(n_steps_to_draw):
  47. brush.setColor(QtGui.QColor(self.steps[n]))
  48. rect = QtCore.QRect(
  49. padding,
  50. int(padding + d_height - ((n + 1) * step_size) + bar_spacer),
  51. d_width, bar_height)
  52. painter.fillRect(rect, brush)
  53. def _trigger_refresh(self):
  54. self.update()
  55. def _calculate_clicked_value(self, e):
  56. parent = self.parent()
  57. vmin, vmax = parent.minimum(), parent.maximum()
  58. d_height = self.size().height() - (self._padding * 2)
  59. step_size = d_height / self.n_steps
  60. click_y = e.y() - self._padding - step_size / 2
  61. pc = (d_height - click_y) / d_height
  62. value = vmin + pc * (vmax - vmin)
  63. self.clickedValue.emit(value)
  64. def mouseMoveEvent(self, e: QtGui.QMouseEvent):
  65. self._calculate_clicked_value(e)
  66. def mouseReleaseEvent(self, e: QtGui.QMouseEvent):
  67. self._calculate_clicked_value(e)
  68. class PowerBar(QtWidgets.QWidget):
  69. """
  70. Custom Qt Widget to show a power bar and dial.
  71. Demonstrating compound and custom-drawn widget.
  72. Left-clicking the button shows the color-chooser, while
  73. right-clicking resets the color tho None (no color).
  74. """
  75. colorChanged = QtCore.pyqtSignal()
  76. def __init__(self, steps=5, *args, **kwargs):
  77. super(PowerBar, self).__init__(*args, **kwargs)
  78. layout = QtWidgets.QVBoxLayout()
  79. self._bar = _Bar(steps)
  80. layout.addWidget(self._bar)
  81. # Create the QDial widget and set up defaults.
  82. self._dial = QtWidgets.QDial()
  83. self._dial.setNotchesVisible(True)
  84. self._dial.setWrapping(False)
  85. self._dial.valueChanged.connect(self._bar._trigger_refresh)
  86. self._bar.clickedValue.connect(self._dial.setValue)
  87. layout.addWidget(self._dial)
  88. self.setLayout(layout)
  89. def __getattr__(self, name):
  90. if name in self.__dict__:
  91. return self[name]
  92. return getattr(self._dial, name)
  93. def setColor(self, color):
  94. self._bar.steps = [color] * self._bar.n_steps
  95. self._bar.update()
  96. def setColors(self, colors):
  97. self._bar.n_steps = len(colors)
  98. self._bar.steps = colors
  99. self._bar.update()
  100. def setBarPadding(self, i):
  101. self._bar._padding = int(i)
  102. self._bar.update()
  103. def setBarSolidPercent(self, f):
  104. self._bar._bar_solid_percent = float(f)
  105. self._bar.update()
  106. def setBackgroundColor(self, color):
  107. self._bar._background_color = QtGui.QColor(color)
  108. self._bar.update()
  109. if __name__ == '__main__':
  110. app = QtWidgets.QApplication([])
  111. volume = PowerBar()
  112. volume.show()
  113. app.exec_()