|
|
@ -30,7 +30,7 @@ def iconFromString(st, size=128): |
|
|
|
px = QtGui.QPixmap(size,size) |
|
|
|
color = stringToColor(st) |
|
|
|
px.fill(color) |
|
|
|
return QtGui.QIcon(px) |
|
|
|
return QtGui.QIcon(px) |
|
|
|
|
|
|
|
def stringToColor(st): |
|
|
|
"""Convert a string to QColor. Same string, same color |
|
|
@ -44,7 +44,7 @@ def stringToColor(st): |
|
|
|
def invertColor(qcolor): |
|
|
|
r = 255 - qcolor.red() |
|
|
|
g = 255 - qcolor.green() |
|
|
|
b = 255 - qcolor.blue() |
|
|
|
b = 255 - qcolor.blue() |
|
|
|
return QtGui.QColor(r, g, b, qcolor.alpha()) |
|
|
|
|
|
|
|
def removeInstancesFromScene(qtGraphicsClass): |
|
|
@ -174,3 +174,166 @@ def setPaletteAndFont(qtApp): |
|
|
|
font.setPixelSize(12) |
|
|
|
qtApp.setFont(font) |
|
|
|
return fPalBlue |
|
|
|
|
|
|
|
class ToggleSwitch(QtWidgets.QAbstractButton): |
|
|
|
"""From https://stackoverflow.com/questions/14780517/toggle-switch-in-qt/38102598 |
|
|
|
|
|
|
|
Usage: |
|
|
|
|
|
|
|
# Thumb size < track size (Gitlab style) |
|
|
|
s1 = ToggleSwitch() |
|
|
|
s1.toggled.connect(lambda c: print('toggled', c)) |
|
|
|
s1.clicked.connect(lambda c: print('clicked', c)) |
|
|
|
s1.pressed.connect(lambda: print('pressed')) |
|
|
|
s1.released.connect(lambda: print('released')) |
|
|
|
|
|
|
|
s2 = ToggleSwitch() |
|
|
|
s2.setEnabled(False) |
|
|
|
|
|
|
|
# Thumb size > track size (Android style) |
|
|
|
s3 = ToggleSwitch(thumb_radius=11, track_radius=8) |
|
|
|
s4 = ToggleSwitch(thumb_radius=11, track_radius=8) |
|
|
|
s4.setEnabled(False) |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent=None, track_radius=10, thumb_radius=8): |
|
|
|
super().__init__(parent=parent) |
|
|
|
self.setCheckable(True) |
|
|
|
self.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) |
|
|
|
|
|
|
|
self._track_radius = track_radius |
|
|
|
self._thumb_radius = thumb_radius |
|
|
|
|
|
|
|
self._margin = max(0, self._thumb_radius - self._track_radius) |
|
|
|
self._base_offset = max(self._thumb_radius, self._track_radius) |
|
|
|
self._end_offset = { |
|
|
|
True: lambda: self.width() - self._base_offset, |
|
|
|
False: lambda: self._base_offset, |
|
|
|
} |
|
|
|
self._offset = self._base_offset |
|
|
|
|
|
|
|
palette = self.palette() |
|
|
|
if self._thumb_radius > self._track_radius: |
|
|
|
self._track_color = { |
|
|
|
True: palette.highlight(), |
|
|
|
False: palette.dark(), |
|
|
|
} |
|
|
|
self._thumb_color = { |
|
|
|
True: palette.highlight(), |
|
|
|
False: palette.light(), |
|
|
|
} |
|
|
|
self._text_color = { |
|
|
|
True: palette.highlightedText().color(), |
|
|
|
False: palette.dark().color(), |
|
|
|
} |
|
|
|
self._thumb_text = { |
|
|
|
True: '', |
|
|
|
False: '', |
|
|
|
} |
|
|
|
self._track_opacity = 0.5 |
|
|
|
else: |
|
|
|
self._thumb_color = { |
|
|
|
True: palette.highlightedText(), |
|
|
|
False: palette.light(), |
|
|
|
} |
|
|
|
self._track_color = { |
|
|
|
True: palette.highlight(), |
|
|
|
False: palette.dark(), |
|
|
|
} |
|
|
|
self._text_color = { |
|
|
|
True: palette.highlight().color(), |
|
|
|
False: palette.dark().color(), |
|
|
|
} |
|
|
|
self._thumb_text = { |
|
|
|
True: '✔', |
|
|
|
False: '✕', |
|
|
|
} |
|
|
|
self._track_opacity = 1 |
|
|
|
|
|
|
|
@QtCore.pyqtProperty(int) |
|
|
|
def offset(self): |
|
|
|
return self._offset |
|
|
|
|
|
|
|
@offset.setter |
|
|
|
def offset(self, value): |
|
|
|
self._offset = value |
|
|
|
self.update() |
|
|
|
|
|
|
|
def sizeHint(self): # pylint: disable=invalid-name |
|
|
|
return QtCore.QSize( |
|
|
|
4 * self._track_radius + 2 * self._margin, |
|
|
|
2 * self._track_radius + 2 * self._margin, |
|
|
|
) |
|
|
|
|
|
|
|
def setChecked(self, checked): |
|
|
|
super().setChecked(checked) |
|
|
|
self.offset = self._end_offset[checked]() |
|
|
|
|
|
|
|
def resizeEvent(self, event): |
|
|
|
super().resizeEvent(event) |
|
|
|
self.offset = self._end_offset[self.isChecked()]() |
|
|
|
|
|
|
|
def paintEvent(self, event): # pylint: disable=invalid-name, unused-argument |
|
|
|
p = QtGui.QPainter(self) |
|
|
|
p.setRenderHint(QtGui.QPainter.Antialiasing, True) |
|
|
|
p.setPen(QtCore.Qt.NoPen) |
|
|
|
track_opacity = self._track_opacity |
|
|
|
thumb_opacity = 1.0 |
|
|
|
text_opacity = 1.0 |
|
|
|
if self.isEnabled(): |
|
|
|
track_brush = self._track_color[self.isChecked()] |
|
|
|
thumb_brush = self._thumb_color[self.isChecked()] |
|
|
|
text_color = self._text_color[self.isChecked()] |
|
|
|
else: |
|
|
|
track_opacity *= 0.8 |
|
|
|
track_brush = self.palette().shadow() |
|
|
|
thumb_brush = self.palette().mid() |
|
|
|
text_color = self.palette().shadow().color() |
|
|
|
|
|
|
|
p.setBrush(track_brush) |
|
|
|
p.setOpacity(track_opacity) |
|
|
|
p.drawRoundedRect( |
|
|
|
self._margin, |
|
|
|
self._margin, |
|
|
|
self.width() - 2 * self._margin, |
|
|
|
self.height() - 2 * self._margin, |
|
|
|
self._track_radius, |
|
|
|
self._track_radius, |
|
|
|
) |
|
|
|
p.setBrush(thumb_brush) |
|
|
|
p.setOpacity(thumb_opacity) |
|
|
|
p.drawEllipse( |
|
|
|
self.offset - self._thumb_radius, |
|
|
|
self._base_offset - self._thumb_radius, |
|
|
|
2 * self._thumb_radius, |
|
|
|
2 * self._thumb_radius, |
|
|
|
) |
|
|
|
p.setPen(text_color) |
|
|
|
p.setOpacity(text_opacity) |
|
|
|
font = p.font() |
|
|
|
font.setPixelSize(1.5 * self._thumb_radius) |
|
|
|
p.setFont(font) |
|
|
|
p.drawText( |
|
|
|
QtCore.QRectF( |
|
|
|
self.offset - self._thumb_radius, |
|
|
|
self._base_offset - self._thumb_radius, |
|
|
|
2 * self._thumb_radius, |
|
|
|
2 * self._thumb_radius, |
|
|
|
), |
|
|
|
QtCore.Qt.AlignCenter, |
|
|
|
self._thumb_text[self.isChecked()], |
|
|
|
) |
|
|
|
|
|
|
|
def mouseReleaseEvent(self, event): # pylint: disable=invalid-name |
|
|
|
super().mouseReleaseEvent(event) |
|
|
|
if event.button() == QtCore.Qt.LeftButton: |
|
|
|
anim = QtCore.QPropertyAnimation(self, b'offset', self) |
|
|
|
anim.setDuration(120) |
|
|
|
anim.setStartValue(self.offset) |
|
|
|
anim.setEndValue(self._end_offset[self.isChecked()]()) |
|
|
|
anim.start() |
|
|
|
|
|
|
|
def enterEvent(self, event): # pylint: disable=invalid-name |
|
|
|
self.setCursor(QtCore.Qt.PointingHandCursor) |
|
|
|
super().enterEvent(event) |
|
|
|