Tulip  5.2.0
Large graphs analysis and drawing
RangeSlider.h
1 /**
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef RANGESLIDER_H
21 #define RANGESLIDER_H
22 
23 #include <QSlider>
24 #include <QStyle>
25 #include <QStylePainter>
26 
27 #include <tulip/tulipconf.h>
28 
29 namespace tlp {
30 
31 /*
32  RangeSlider is a slider with two handles. It is
33  handy for letting user to choose a range of integers between min/max.
34  This class is totally inspired by the QxtSpanSlider class
35  of the no longer maintainer Qxt code
36  see https://bitbucket.org/libqxt/libqxt/wiki/Home for more details
37 */
38 class TLP_QT_SCOPE RangeSlider : public QSlider {
39  Q_OBJECT
40  Q_PROPERTY(int lowerValue READ lowerValue WRITE setLowerValue)
41  Q_PROPERTY(int upperValue READ upperValue WRITE setUpperValue)
42  Q_PROPERTY(
43  HandleMovementMode handleMovementMode READ handleMovementMode WRITE setHandleMovementMode)
44  Q_ENUMS(HandleMovementMode)
45 
46 public:
47  explicit RangeSlider(QWidget *parent = nullptr);
48  explicit RangeSlider(Qt::Orientation orientation, QWidget *parent = nullptr);
49  ~RangeSlider() override {}
50 
51  enum HandleMovementMode { FreeMovement, NoCrossing, NoOverlapping };
52 
53  HandleMovementMode handleMovementMode() const;
54  void setHandleMovementMode(HandleMovementMode mode);
55 
56  int lowerValue() const;
57  int upperValue() const;
58 
59  int lowerPosition() const;
60  int upperPosition() const;
61 
62 public slots:
63  void setLowerValue(int lower);
64  void setUpperValue(int upper);
65  void setRange(int lower, int upper);
66 
67  void setLowerPosition(int lower);
68  void setUpperPosition(int upper);
69 
70 signals:
71  void rangeChanged(int lower, int upper);
72  void lowerValueChanged(int lower);
73  void upperValueChanged(int upper);
74 
75  void lowerPositionChanged(int lower);
76  void upperPositionChanged(int upper);
77 
78 protected:
79  void keyPressEvent(QKeyEvent *event) override;
80  void mousePressEvent(QMouseEvent *event) override;
81  void mouseMoveEvent(QMouseEvent *event) override;
82  void mouseReleaseEvent(QMouseEvent *event) override;
83  void paintEvent(QPaintEvent *event) override;
84 
85 private:
86  enum RangeHandle { NoHandle, LowerHandle, UpperHandle };
87 
88  Q_PROPERTY(int lowerPosition READ lowerPosition WRITE setLowerPosition)
89  Q_PROPERTY(int upperPosition READ upperPosition WRITE setUpperPosition)
90 
91  void initStyleOption(QStyleOptionSlider *option, RangeHandle handle = UpperHandle) const;
92  int pick(const QPoint &pt) const {
93  return orientation() == Qt::Horizontal ? pt.x() : pt.y();
94  }
95  int pixelPosToRangeValue(int pos) const;
96  void handleMousePress(const QPoint &pos, QStyle::SubControl &control, int value,
97  RangeHandle handle);
98  void drawHandle(QStylePainter *painter, RangeHandle handle) const;
99  void setupPainter(QPainter *painter, Qt::Orientation orientation, qreal x1, qreal y1, qreal x2,
100  qreal y2) const;
101  void drawRange(QStylePainter *painter, const QRect &rect) const;
102  void triggerAction(QAbstractSlider::SliderAction action, bool main);
103  void swapControls();
104 
105  int lower;
106  int upper;
107  int lowerPos;
108  int upperPos;
109  int offset;
110  int position;
111  RangeHandle lastPressed;
112  RangeHandle mainControl;
113  QStyle::SubControl lowerPressed;
114  QStyle::SubControl upperPressed;
115  HandleMovementMode movement;
116  bool firstMovement;
117  bool blockTracking;
118 
119 public slots:
120  void updateRange(int min, int max);
121  void movePressedHandle();
122 };
123 }
124 
125 #endif // RANGESLIDER_H