|
2 | 2 |
|
3 | 3 | from AnyQt.QtCore import Qt, QSize, QAbstractItemModel, Property |
4 | 4 | from AnyQt.QtWidgets import ( |
5 | | - QWidget, QSlider, QFormLayout, QComboBox, QStyle, |
6 | | - QHBoxLayout, QLineEdit, QLabel) |
| 5 | + QWidget, QSlider, QFormLayout, QComboBox, QStyle, QHBoxLayout, QLabel, |
| 6 | + QSizePolicy |
| 7 | +) |
7 | 8 | from AnyQt.QtCore import Signal |
8 | | -from AnyQt.QtGui import QFontMetrics, QDoubleValidator |
9 | 9 |
|
10 | 10 | from orangewidget.gui import Slider |
11 | 11 |
|
12 | 12 | from Orange.widgets.utils import itemmodels, colorpalettes |
| 13 | +from Orange.widgets.utils.spinbox import DoubleSpinBox, DBL_MIN, DBL_MAX |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class ColorGradientSelection(QWidget): |
@@ -49,19 +50,23 @@ def __init__(self, *args, thresholds=(0.0, 1.0), center=None, **kwargs): |
49 | 50 | self.gradient_cb.currentIndexChanged.connect(self.currentIndexChanged) |
50 | 51 |
|
51 | 52 | if center is not None: |
52 | | - def __on_center_changed(): |
53 | | - self.__center = float(self.center_edit.text() or "0") |
54 | | - self.centerChanged.emit(self.__center) |
| 53 | + def on_center_spin_value_changed(value): |
| 54 | + if self.__center != value: |
| 55 | + self.__center = value |
| 56 | + self.centerChanged.emit(self.__center) |
55 | 57 |
|
56 | 58 | self.center_box = QWidget() |
57 | 59 | center_layout = QHBoxLayout() |
58 | 60 | self.center_box.setLayout(center_layout) |
59 | | - width = QFontMetrics(self.font()).boundingRect("9999999").width() |
60 | | - self.center_edit = QLineEdit( |
61 | | - text=f"{self.__center}", |
62 | | - maximumWidth=width, placeholderText="0", alignment=Qt.AlignRight) |
63 | | - self.center_edit.setValidator(QDoubleValidator()) |
64 | | - self.center_edit.editingFinished.connect(__on_center_changed) |
| 61 | + self.center_edit = DoubleSpinBox( |
| 62 | + value=self.__center, |
| 63 | + minimum=DBL_MIN, maximum=DBL_MAX, minimumStep=0.01, |
| 64 | + minimumContentsLenght=8, |
| 65 | + stepType=DoubleSpinBox.AdaptiveDecimalStepType, |
| 66 | + keyboardTracking=False, |
| 67 | + sizePolicy=QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 68 | + ) |
| 69 | + self.center_edit.valueChanged.connect(on_center_spin_value_changed) |
65 | 70 | center_layout.setContentsMargins(0, 0, 0, 0) |
66 | 71 | center_layout.addStretch(1) |
67 | 72 | center_layout.addWidget(QLabel("Centered at")) |
@@ -140,14 +145,6 @@ def thresholdHigh(self) -> float: |
140 | 145 | def setThresholdHigh(self, high: float) -> None: |
141 | 146 | self.setThresholds(min(self.__threshold_low, high), high) |
142 | 147 |
|
143 | | - def center(self) -> float: |
144 | | - return self.__center |
145 | | - |
146 | | - def setCenter(self, center: float) -> None: |
147 | | - self.__center = center |
148 | | - self.center_edit.setText(f"{center}") |
149 | | - self.centerChanged.emit(center) |
150 | | - |
151 | 148 | thresholdHigh_ = Property( |
152 | 149 | float, thresholdLow, setThresholdLow, notify=thresholdsChanged) |
153 | 150 |
|
@@ -194,6 +191,17 @@ def __update_center_visibility(self): |
194 | 191 | isinstance(palette, colorpalettes.Palette) |
195 | 192 | and palette.flags & palette.Flags.Diverging != 0) |
196 | 193 |
|
| 194 | + def center(self) -> float: |
| 195 | + return self.__center |
| 196 | + |
| 197 | + def setCenter(self, center: float) -> None: |
| 198 | + if self.__center != center: |
| 199 | + self.__center = center |
| 200 | + self.center_edit.setValue(center) |
| 201 | + self.centerChanged.emit(center) |
| 202 | + |
| 203 | + center_ = Property(float, center, setCenter, notify=centerChanged) |
| 204 | + |
197 | 205 |
|
198 | 206 | def clip(a, amin, amax): |
199 | 207 | return min(max(a, amin), amax) |
0 commit comments