summaryrefslogtreecommitdiff
path: root/silx/gui/widgets/RangeSlider.py
blob: 0b72e7165dd69dd5a0303f3f3290710d2ee023cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""This module provides a :class:`RangeSlider` widget.

.. image:: img/RangeSlider.png
   :align: center
"""
from __future__ import absolute_import, division

__authors__ = ["D. Naudet", "T. Vincent"]
__license__ = "MIT"
__date__ = "02/08/2018"


import numpy as numpy

from silx.gui import qt, icons, colors
from silx.gui.utils.image import convertArrayToQImage


class RangeSlider(qt.QWidget):
    """Range slider with 2 thumbs and an optional colored groove.

    The position of the slider thumbs can be retrieved either as values
    in the slider range or as a number of steps or pixels.

    :param QWidget parent: See QWidget
    """

    _SLIDER_WIDTH = 10
    """Width of the slider rectangle"""

    _PIXMAP_VOFFSET = 7
    """Vertical groove pixmap offset"""

    sigRangeChanged = qt.Signal(float, float)
    """Signal emitted when the value range has changed.

    It provides the new range (min, max).
    """

    sigValueChanged = qt.Signal(float, float)
    """Signal emitted when the value of the sliders has changed.

    It provides the slider values (first, second).
    """

    sigPositionCountChanged = qt.Signal(object)
    """This signal is emitted when the number of steps has changed.

    It provides the new position count.
    """

    sigPositionChanged = qt.Signal(int, int)
    """Signal emitted when the position of the sliders has changed.

    It provides the slider positions in steps or pixels (first, second).
    """

    def __init__(self, parent=None):
        self.__pixmap = None
        self.__positionCount = None
        self.__firstValue = 0.
        self.__secondValue = 1.
        self.__minValue = 0.
        self.__maxValue = 1.

        self.__focus = None
        self.__moving = None

        self.__icons = {
            'first': icons.getQIcon('previous'),
            'second': icons.getQIcon('next')
        }

        # call the super constructor AFTER defining all members that
        # are used in the "paint" method
        super(RangeSlider, self).__init__(parent)

        self.setFocusPolicy(qt.Qt.ClickFocus)

        self.setMinimumSize(qt.QSize(50, 20))
        self.setMaximumHeight(20)

        # Broadcast value changed signal
        self.sigValueChanged.connect(self.__emitPositionChanged)

    # Position <-> Value conversion

    def __positionToValue(self, position):
        """Returns value corresponding to position

        :param int position:
        :rtype: float
        """
        min_, max_ = self.getMinimum(), self.getMaximum()
        maxPos = self.__getCurrentPositionCount() - 1
        return min_ + (max_ - min_) * int(position) / maxPos

    def __valueToPosition(self, value):
        """Returns closest position corresponding to value

        :param float value:
        :rtype: int
        """
        min_, max_ = self.getMinimum(), self.getMaximum()
        maxPos = self.__getCurrentPositionCount() - 1
        return int(0.5 + maxPos * (float(value) - min_) / (max_ - min_))

    # Position (int) API

    def __getCurrentPositionCount(self):
        """Return current count (either position count or widget width

        :rtype: int
        """
        count = self.getPositionCount()
        if count is not None:
            return count
        else:
            return max(2, self.width() - self._SLIDER_WIDTH)

    def getPositionCount(self):
        """Returns the number of positions.

        :rtype: Union[int,None]"""
        return self.__positionCount

    def setPositionCount(self, count):
        """Set the number of positions.

        Slider values are eventually adjusted.

        :param Union[int,None] count:
            Either the number of possible positions or
            None to allow any values.
        :raise ValueError: If count <= 1
        """
        count = None if count is None else int(count)
        if count != self.getPositionCount():
            if count is not None and count <= 1:
                raise ValueError("Position count must be higher than 1")
            self.__positionCount = count
            emit = self.__setValues(*self.getValues())
            self.sigPositionCountChanged.emit(count)
            if emit:
                self.sigValueChanged.emit(*self.getValues())

    def getFirstPosition(self):
        """Returns first slider position

        :rtype: int
        """
        return self.__valueToPosition(self.getFirstValue())

    def setFirstPosition(self, position):
        """Set the position of the first slider

        The position is adjusted to valid values

        :param int position:
        """
        self.setFirstValue(self.__positionToValue(position))

    def getSecondPosition(self):
        """Returns second slider position

        :rtype: int
        """
        return self.__valueToPosition(self.getSecondValue())

    def setSecondPosition(self, position):
        """Set the position of the second slider

        The position is adjusted to valid values

        :param int position:
        """
        self.setSecondValue(self.__positionToValue(position))

    def getPositions(self):
        """Returns slider positions (first, second)

        :rtype: List[int]
        """
        return self.getFirstPosition(), self.getSecondPosition()

    def setPositions(self, first, second):
        """Set the position of both sliders at once

        First is clipped to the slider range: [0, max].
        Second is clipped to valid values: [first, max]

        :param int first:
        :param int second:
        """
        self.setValues(self.__positionToValue(first),
                       self.__positionToValue(second))

    # Value (float) API

    def __emitPositionChanged(self, *args, **kwargs):
        self.sigPositionChanged.emit(*self.getPositions())

    def __rangeChanged(self):
        """Handle change of value range"""
        emit = self.__setValues(*self.getValues())
        self.sigRangeChanged.emit(*self.getRange())
        if emit:
            self.sigValueChanged.emit(*self.getValues())

    def getMinimum(self):
        """Returns the minimum value of the slider range

        :rtype: float
        """
        return self.__minValue

    def setMinimum(self, minimum):
        """Set the minimum value of the slider range.

        It eventually adjusts maximum.
        Slider positions remains unchanged and slider values are modified.

        :param float minimum:
        """
        minimum = float(minimum)
        if minimum != self.getMinimum():
            if minimum > self.getMaximum():
                self.__maxValue = minimum
            self.__minValue = minimum
            self.__rangeChanged()

    def getMaximum(self):
        """Returns the maximum value of the slider range

        :rtype: float
        """
        return self.__maxValue

    def setMaximum(self, maximum):
        """Set the maximum value of the slider range

        It eventually adjusts minimum.
        Slider positions remains unchanged and slider values are modified.

        :param float maximum:
        """
        maximum = float(maximum)
        if maximum != self.getMaximum():
            if maximum < self.getMinimum():
                self.__minValue = maximum
            self.__maxValue = maximum
            self.__rangeChanged()

    def getRange(self):
        """Returns the range of values (min, max)

        :rtype: List[float]
        """
        return self.getMinimum(), self.getMaximum()

    def setRange(self, minimum, maximum):
        """Set the range of values.

        If maximum is lower than minimum, minimum is the only valid value.
        Slider positions remains unchanged and slider values are modified.

        :param float minimum:
        :param float maximum:
        """
        minimum, maximum = float(minimum), float(maximum)
        if minimum != self.getMinimum() or maximum != self.getMaximum():
            self.__minValue = minimum
            self.__maxValue = max(maximum, minimum)
            self.__rangeChanged()

    def getFirstValue(self):
        """Returns the value of the first slider

        :rtype: float
        """
        return self.__firstValue

    def __clipFirstValue(self, value, max_=None):
        """Clip first value to range and steps

        :param float value:
        :param float max_: Alternative maximum to use
        """
        if max_ is None:
            max_ = self.getSecondValue()
        value = min(max(self.getMinimum(), float(value)), max_)
        if self.getPositionCount() is not None:  # Clip to steps
            value = self.__positionToValue(self.__valueToPosition(value))
        return value

    def setFirstValue(self, value):
        """Set the value of the first slider

        Value is clipped to valid values.

        :param float value:
        """
        value = self.__clipFirstValue(value)
        if value != self.getFirstValue():
            self.__firstValue = value
            self.update()
            self.sigValueChanged.emit(*self.getValues())

    def getSecondValue(self):
        """Returns the value of the second slider

        :rtype: float
        """
        return self.__secondValue

    def __clipSecondValue(self, value):
        """Clip second value to range and steps

        :param float value:
        """
        value = min(max(self.getFirstValue(), float(value)), self.getMaximum())
        if self.getPositionCount() is not None:  # Clip to steps
            value = self.__positionToValue(self.__valueToPosition(value))
        return value

    def setSecondValue(self, value):
        """Set the value of the second slider

        Value is clipped to valid values.

        :param float value:
        """
        value = self.__clipSecondValue(value)
        if value != self.getSecondValue():
            self.__secondValue = value
            self.update()
            self.sigValueChanged.emit(*self.getValues())

    def getValues(self):
        """Returns value of both sliders at once

        :return: (first value, second value)
        :rtype: List[float]
        """
        return self.getFirstValue(), self.getSecondValue()

    def setValues(self, first, second):
        """Set values for both sliders at once

        First is clipped to the slider range: [minimum, maximum].
        Second is clipped to valid values: [first, maximum]

        :param float first:
        :param float second:
        """
        if self.__setValues(first, second):
            self.sigValueChanged.emit(*self.getValues())

    def __setValues(self, first, second):
        """Set values for both sliders at once

        First is clipped to the slider range: [minimum, maximum].
        Second is clipped to valid values: [first, maximum]

        :param float first:
        :param float second:
        :return: True if values has changed, False otherwise
        :rtype: bool
        """
        first = self.__clipFirstValue(first, self.getMaximum())
        second = self.__clipSecondValue(second)
        values = first, second

        if self.getValues() != values:
            self.__firstValue, self.__secondValue = values
            self.update()
            return True
        return False

    # Groove API

    def getGroovePixmap(self):
        """Returns the pixmap displayed in the slider groove if any.

        :rtype: Union[QPixmap,None]
        """
        return self.__pixmap

    def setGroovePixmap(self, pixmap):
        """Set the pixmap displayed in the slider groove.

        :param Union[QPixmap,None] pixmap: The QPixmap to use or None to unset.
        """
        assert pixmap is None or isinstance(pixmap, qt.QPixmap)
        self.__pixmap = pixmap
        self.update()

    def setGroovePixmapFromProfile(self, profile, colormap=None):
        """Set the pixmap displayed in the slider groove from histogram values.

        :param Union[numpy.ndarray,None] profile:
            1D array of values to display
        :param Union[Colormap,str] colormap:
            The colormap name or object to convert profile values to colors
        """
        if profile is None:
            self.setSliderPixmap(None)
            return

        profile = numpy.array(profile, copy=False)

        if profile.size == 0:
            self.setSliderPixmap(None)
            return

        if colormap is None:
            colormap = colors.Colormap()
        elif isinstance(colormap, str):
            colormap = colors.Colormap(name=colormap)
        assert isinstance(colormap, colors.Colormap)

        rgbImage = colormap.applyToData(profile.reshape(1, -1))[:, :, :3]
        qimage = convertArrayToQImage(rgbImage)
        qpixmap = qt.QPixmap.fromImage(qimage)
        self.setGroovePixmap(qpixmap)

    # Handle interaction

    def mousePressEvent(self, event):
        super(RangeSlider, self).mousePressEvent(event)

        if event.buttons() == qt.Qt.LeftButton:
            picked = None
            for name in ('first', 'second'):
                area = self.__sliderRect(name)
                if area.contains(event.pos()):
                    picked = name
                    break

            self.__moving = picked
            self.__focus = picked
            self.update()

    def mouseMoveEvent(self, event):
        super(RangeSlider, self).mouseMoveEvent(event)

        if self.__moving is not None:
            position = self.__xPixelToPosition(event.pos().x())
            if self.__moving == 'first':
                self.setFirstPosition(position)
            else:
                self.setSecondPosition(position)

    def mouseReleaseEvent(self, event):
        super(RangeSlider, self).mouseReleaseEvent(event)

        if event.button() == qt.Qt.LeftButton and self.__moving is not None:
            self.__moving = None
            self.update()

    def focusOutEvent(self, event):
        if self.__focus is not None:
            self.__focus = None
            self.update()
        super(RangeSlider, self).focusOutEvent(event)

    def keyPressEvent(self, event):
        key = event.key()
        if event.modifiers() == qt.Qt.NoModifier and self.__focus is not None:
            if key in (qt.Qt.Key_Left, qt.Qt.Key_Down):
                if self.__focus == 'first':
                    self.setFirstPosition(self.getFirstPosition() - 1)
                else:
                    self.setSecondPosition(self.getSecondPosition() - 1)
                return  # accept event
            elif key in (qt.Qt.Key_Right, qt.Qt.Key_Up):
                if self.__focus == 'first':
                    self.setFirstPosition(self.getFirstPosition() + 1)
                else:
                    self.setSecondPosition(self.getSecondPosition() + 1)
                return  # accept event

        super(RangeSlider, self).keyPressEvent(event)

    # Handle resize

    def resizeEvent(self, event):
        super(RangeSlider, self).resizeEvent(event)

        # If no step, signal position update when width change
        if (self.getPositionCount() is None and
                event.size().width() != event.oldSize().width()):
            self.sigPositionChanged.emit(*self.getPositions())

    # Handle repaint

    def __xPixelToPosition(self, x):
        """Convert position in pixel to slider position

        :param int x: X in pixel coordinates
        :rtype: int
        """
        sliderArea = self.__sliderAreaRect()
        maxPos = self.__getCurrentPositionCount() - 1
        position = maxPos * (x - sliderArea.left()) / (sliderArea.width() - 1)
        return int(position + 0.5)

    def __sliderRect(self, name):
        """Returns rectangle corresponding to slider in pixels

        :param str name: 'first' or 'second'
        :rtype: QRect
        :raise ValueError: If wrong name
        """
        assert name in ('first', 'second')
        if name == 'first':
            offset = - self._SLIDER_WIDTH
            position = self.getFirstPosition()
        elif name == 'second':
            offset = 0
            position = self.getSecondPosition()
        else:
            raise ValueError('Unknown name')

        sliderArea = self.__sliderAreaRect()

        maxPos = self.__getCurrentPositionCount() - 1
        xOffset = int((sliderArea.width() - 1) * position / maxPos)
        xPos = sliderArea.left() + xOffset + offset

        return qt.QRect(xPos,
                        sliderArea.top(),
                        self._SLIDER_WIDTH,
                        sliderArea.height())

    def __drawArea(self):
        return self.rect().adjusted(self._SLIDER_WIDTH, 0,
                                    -self._SLIDER_WIDTH, 0)

    def __sliderAreaRect(self):
        return self.__drawArea().adjusted(self._SLIDER_WIDTH / 2.,
                                          0,
                                          -self._SLIDER_WIDTH / 2.,
                                          0)

    def __pixMapRect(self):
        return self.__sliderAreaRect().adjusted(0,
                                                self._PIXMAP_VOFFSET,
                                                0,
                                                -self._PIXMAP_VOFFSET)

    def paintEvent(self, event):
        painter = qt.QPainter(self)

        style = qt.QApplication.style()

        area = self.__drawArea()
        pixmapRect = self.__pixMapRect()

        option = qt.QStyleOptionProgressBar()
        option.initFrom(self)
        option.rect = area
        option.state = ((self.isEnabled() and qt.QStyle.State_Enabled)
                        or qt.QStyle.State_None)
        style.drawControl(qt.QStyle.CE_ProgressBarGroove,
                          option,
                          painter,
                          self)

        painter.save()
        pen = painter.pen()
        pen.setWidth(1)
        pen.setColor(qt.Qt.black if self.isEnabled() else qt.Qt.gray)
        painter.setPen(pen)
        painter.drawRect(pixmapRect.adjusted(-1, -1, 1, 1))
        painter.restore()

        if self.isEnabled() and self.__pixmap is not None:
            painter.drawPixmap(area.adjusted(self._SLIDER_WIDTH / 2,
                                             self._PIXMAP_VOFFSET,
                                             -self._SLIDER_WIDTH / 2 + 1,
                                             -self._PIXMAP_VOFFSET + 1),
                               self.__pixmap,
                               self.__pixmap.rect())

        for name in ('first', 'second'):
            rect = self.__sliderRect(name)
            option = qt.QStyleOptionButton()
            option.initFrom(self)
            option.icon = self.__icons[name]
            option.iconSize = rect.size() * 0.7
            if option.state & qt.QStyle.State_MouseOver:
                option.state ^= qt.QStyle.State_MouseOver
            if self.__focus == name:
                option.state |= qt.QStyle.State_HasFocus
            elif option.state & qt.QStyle.State_HasFocus:
                option.state ^= qt.QStyle.State_HasFocus
            option.rect = rect
            style.drawControl(
                qt.QStyle.CE_PushButton, option, painter, self)

    def sizeHint(self):
        return qt.QSize(200, self.minimumHeight())