Skip to content

Commit 046bef0

Browse files
committed
Style slider to indicate it is non-interactive, fix tooltip on macOS (#6985)
1 parent 7be875f commit 046bef0

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

Orange/widgets/visualize/owscoringsheetviewer.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
QHBoxLayout,
1111
QWidget,
1212
QStyle,
13+
QProxyStyle,
1314
QToolTip,
1415
QStyleOptionSlider,
1516
)
@@ -90,6 +91,48 @@ def handle_item_changed(self, item):
9091
self.main_widget._update_slider_value()
9192

9293

94+
class NonDraggableSlider(QSlider):
95+
"""
96+
A custom slider that ignores mouse events.
97+
98+
It is used instead of disabling the default slider so that the
99+
tooltip will be shown when the mouse is over the slider thumb on macOS.
100+
"""
101+
102+
def mousePressEvent(self, event):
103+
event.ignore()
104+
105+
def mouseMoveEvent(self, event):
106+
event.ignore()
107+
108+
def mouseReleaseEvent(self, event):
109+
event.ignore()
110+
111+
112+
class CustomSliderStyle(QProxyStyle):
113+
"""
114+
A custom slider handle style.
115+
116+
It draws a 2px wide black rectangle to replace the default handle.
117+
This is done to suggest to the user that the slider is not interactive.
118+
"""
119+
120+
def drawComplexControl(self, cc, opt, painter, widget=None):
121+
if cc == QStyle.CC_Slider:
122+
opt2 = QStyleOptionSlider(opt)
123+
opt2.subControls &= ~QStyle.SC_SliderHandle
124+
super().drawComplexControl(cc, opt2, painter, widget)
125+
hr = self.subControlRect(cc, opt, QStyle.SC_SliderHandle, widget)
126+
painter.save()
127+
painter.setRenderHint(QPainter.Antialiasing)
128+
painter.fillRect(
129+
QRect(hr.center().x() - 1, hr.y(), 2, hr.height()), Qt.black
130+
)
131+
painter.restore()
132+
else:
133+
super().drawComplexControl(cc, opt, painter, widget)
134+
135+
93136
class RiskSlider(QWidget):
94137
def __init__(self, points, probabilities, parent=None):
95138
super().__init__(parent)
@@ -107,9 +150,8 @@ def __init__(self, points, probabilities, parent=None):
107150
# Setup the labels
108151
self.setup_labels()
109152

110-
# Create the slider
111-
self.slider = QSlider(Qt.Horizontal, self)
112-
self.slider.setEnabled(False)
153+
self.slider = NonDraggableSlider(Qt.Horizontal, self)
154+
self.slider.setStyle(CustomSliderStyle())
113155
self.layout.addWidget(self.slider)
114156

115157
self.points = points

0 commit comments

Comments
 (0)