|
| 1 | +from typing import Any, Tuple |
| 2 | + |
| 3 | +from AnyQt.QtCore import Qt, QSize, QAbstractItemModel, Property |
| 4 | +from AnyQt.QtWidgets import ( |
| 5 | + QWidget, QSlider, QFormLayout, QComboBox, QStyle |
| 6 | +) |
| 7 | +from AnyQt.QtCore import Signal |
| 8 | + |
| 9 | +from Orange.widgets.utils import itemmodels |
| 10 | + |
| 11 | + |
| 12 | +class ColorGradientSelection(QWidget): |
| 13 | + activated = Signal(int) |
| 14 | + |
| 15 | + currentIndexChanged = Signal(int) |
| 16 | + thresholdsChanged = Signal(float, float) |
| 17 | + |
| 18 | + def __init__(self, *args, thresholds=(0.0, 1.0), **kwargs): |
| 19 | + super().__init__(*args, **kwargs) |
| 20 | + |
| 21 | + low = round(clip(thresholds[0], 0., 1.), 2) |
| 22 | + high = round(clip(thresholds[1], 0., 1.), 2) |
| 23 | + high = max(low, high) |
| 24 | + self.__threshold_low, self.__threshold_high = low, high |
| 25 | + form = QFormLayout( |
| 26 | + formAlignment=Qt.AlignLeft, |
| 27 | + labelAlignment=Qt.AlignLeft, |
| 28 | + fieldGrowthPolicy=QFormLayout.AllNonFixedFieldsGrow |
| 29 | + ) |
| 30 | + form.setContentsMargins(0, 0, 0, 0) |
| 31 | + self.gradient_cb = QComboBox( |
| 32 | + None, objectName="gradient-combo-box", |
| 33 | + ) |
| 34 | + self.gradient_cb.setAttribute(Qt.WA_LayoutUsesWidgetRect) |
| 35 | + icsize = self.style().pixelMetric( |
| 36 | + QStyle.PM_SmallIconSize, None, self.gradient_cb |
| 37 | + ) |
| 38 | + self.gradient_cb.setIconSize(QSize(64, icsize)) |
| 39 | + model = itemmodels.ContinuousPalettesModel() |
| 40 | + model.setParent(self) |
| 41 | + |
| 42 | + self.gradient_cb.setModel(model) |
| 43 | + self.gradient_cb.activated[int].connect(self.activated) |
| 44 | + self.gradient_cb.currentIndexChanged.connect(self.currentIndexChanged) |
| 45 | + |
| 46 | + slider_low = QSlider( |
| 47 | + objectName="threshold-low-slider", minimum=0, maximum=100, |
| 48 | + value=int(low * 100), orientation=Qt.Horizontal, |
| 49 | + tickPosition=QSlider.TicksBelow, pageStep=10, |
| 50 | + toolTip=self.tr("Low gradient threshold"), |
| 51 | + whatsThis=self.tr("Applying a low threshold will squeeze the " |
| 52 | + "gradient from the lower end") |
| 53 | + ) |
| 54 | + slider_high = QSlider( |
| 55 | + objectName="threshold-low-slider", minimum=0, maximum=100, |
| 56 | + value=int(high * 100), orientation=Qt.Horizontal, |
| 57 | + tickPosition=QSlider.TicksAbove, pageStep=10, |
| 58 | + toolTip=self.tr("High gradient threshold"), |
| 59 | + whatsThis=self.tr("Applying a high threshold will squeeze the " |
| 60 | + "gradient from the higher end") |
| 61 | + ) |
| 62 | + form.setWidget(0, QFormLayout.SpanningRole, self.gradient_cb) |
| 63 | + form.addRow(self.tr("Low:"), slider_low) |
| 64 | + form.addRow(self.tr("High:"), slider_high) |
| 65 | + self.slider_low = slider_low |
| 66 | + self.slider_high = slider_high |
| 67 | + self.slider_low.valueChanged.connect(self.__on_slider_low_moved) |
| 68 | + self.slider_high.valueChanged.connect(self.__on_slider_high_moved) |
| 69 | + self.setLayout(form) |
| 70 | + |
| 71 | + def setModel(self, model: QAbstractItemModel) -> None: |
| 72 | + self.gradient_cb.setModel(model) |
| 73 | + |
| 74 | + def model(self) -> QAbstractItemModel: |
| 75 | + return self.gradient_cb.model() |
| 76 | + |
| 77 | + def findData(self, data: Any, role: Qt.ItemDataRole) -> int: |
| 78 | + return self.gradient_cb.findData(data, role) |
| 79 | + |
| 80 | + def setCurrentIndex(self, index: int) -> None: |
| 81 | + self.gradient_cb.setCurrentIndex(index) |
| 82 | + |
| 83 | + def currentIndex(self) -> int: |
| 84 | + return self.gradient_cb.currentIndex() |
| 85 | + |
| 86 | + currentIndex_ = Property( |
| 87 | + int, currentIndex, setCurrentIndex, notify=currentIndexChanged) |
| 88 | + |
| 89 | + def currentData(self, role=Qt.UserRole) -> Any: |
| 90 | + return self.gradient_cb.currentData(role) |
| 91 | + |
| 92 | + def thresholds(self) -> Tuple[float, float]: |
| 93 | + return self.__threshold_low, self.__threshold_high |
| 94 | + |
| 95 | + thresholds_ = Property(object, thresholds, notify=thresholdsChanged) |
| 96 | + |
| 97 | + def thresholdLow(self) -> float: |
| 98 | + return self.__threshold_low |
| 99 | + |
| 100 | + def setThresholdLow(self, low: float) -> None: |
| 101 | + self.setThresholds(low, max(self.__threshold_high, low)) |
| 102 | + |
| 103 | + thresholdLow_ = Property( |
| 104 | + float, thresholdLow, setThresholdLow, notify=thresholdsChanged) |
| 105 | + |
| 106 | + def thresholdHigh(self) -> float: |
| 107 | + return self.__threshold_high |
| 108 | + |
| 109 | + def setThresholdHigh(self, high: float) -> None: |
| 110 | + self.setThresholds(min(self.__threshold_low, high), high) |
| 111 | + |
| 112 | + thresholdHigh_ = Property( |
| 113 | + float, thresholdLow, setThresholdLow, notify=thresholdsChanged) |
| 114 | + |
| 115 | + def __on_slider_low_moved(self, value: int) -> None: |
| 116 | + high = self.slider_high |
| 117 | + old = self.__threshold_low, self.__threshold_high |
| 118 | + self.__threshold_low = value / 100. |
| 119 | + if value >= high.value(): |
| 120 | + self.__threshold_high = value / 100. |
| 121 | + high.setSliderPosition(value) |
| 122 | + new = self.__threshold_low, self.__threshold_high |
| 123 | + if new != old: |
| 124 | + self.thresholdsChanged.emit(*new) |
| 125 | + |
| 126 | + def __on_slider_high_moved(self, value: int) -> None: |
| 127 | + low = self.slider_low |
| 128 | + old = self.__threshold_low, self.__threshold_high |
| 129 | + self.__threshold_high = value / 100. |
| 130 | + if low.value() >= value: |
| 131 | + self.__threshold_low = value / 100 |
| 132 | + low.setSliderPosition(value) |
| 133 | + new = self.__threshold_low, self.__threshold_high |
| 134 | + if new != old: |
| 135 | + self.thresholdsChanged.emit(*new) |
| 136 | + |
| 137 | + def setThresholds(self, low: float, high: float) -> None: |
| 138 | + low = round(clip(low, 0., 1.), 2) |
| 139 | + high = round(clip(high, 0., 1.), 2) |
| 140 | + if low > high: |
| 141 | + high = low |
| 142 | + if self.__threshold_low != low or self.__threshold_high != high: |
| 143 | + self.__threshold_high = high |
| 144 | + self.__threshold_low = low |
| 145 | + self.slider_low.setSliderPosition(low * 100) |
| 146 | + self.slider_high.setSliderPosition(high * 100) |
| 147 | + self.thresholdsChanged.emit(high, low) |
| 148 | + |
| 149 | + |
| 150 | +def clip(a, amin, amax): |
| 151 | + return min(max(a, amin), amax) |
0 commit comments