Tulip  5.1.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(HandleMovementMode handleMovementMode READ handleMovementMode WRITE setHandleMovementMode)
43  Q_ENUMS(HandleMovementMode)
44 
45 public:
46  explicit RangeSlider(QWidget* parent = 0);
47  explicit RangeSlider(Qt::Orientation orientation, QWidget* parent = 0);
48  ~RangeSlider() {}
49 
50  enum HandleMovementMode {
51  FreeMovement,
52  NoCrossing,
53  NoOverlapping
54  };
55 
56  HandleMovementMode handleMovementMode() const;
57  void setHandleMovementMode(HandleMovementMode mode);
58 
59  int lowerValue() const;
60  int upperValue() const;
61 
62  int lowerPosition() const;
63  int upperPosition() const;
64 
65 public slots:
66  void setLowerValue(int lower);
67  void setUpperValue(int upper);
68  void setRange(int lower, int upper);
69 
70  void setLowerPosition(int lower);
71  void setUpperPosition(int upper);
72 
73 signals:
74  void rangeChanged(int lower, int upper);
75  void lowerValueChanged(int lower);
76  void upperValueChanged(int upper);
77 
78  void lowerPositionChanged(int lower);
79  void upperPositionChanged(int upper);
80 
81 protected:
82  virtual void keyPressEvent(QKeyEvent* event);
83  virtual void mousePressEvent(QMouseEvent* event);
84  virtual void mouseMoveEvent(QMouseEvent* event);
85  virtual void mouseReleaseEvent(QMouseEvent* event);
86  virtual void paintEvent(QPaintEvent* event);
87 
88 private:
89  enum RangeHandle {
90  NoHandle,
91  LowerHandle,
92  UpperHandle
93  };
94 
95  Q_PROPERTY(int lowerPosition READ lowerPosition WRITE setLowerPosition)
96  Q_PROPERTY(int upperPosition READ upperPosition WRITE setUpperPosition)
97 
98  void initStyleOption(QStyleOptionSlider* option, RangeHandle handle = UpperHandle) const;
99  int pick(const QPoint& pt) const {
100  return orientation() == Qt::Horizontal ? pt.x() : pt.y();
101  }
102  int pixelPosToRangeValue(int pos) const;
103  void handleMousePress(const QPoint& pos, QStyle::SubControl& control, int value, RangeHandle handle);
104  void drawHandle(QStylePainter* painter, RangeHandle handle) const;
105  void setupPainter(QPainter* painter, Qt::Orientation orientation, qreal x1, qreal y1, qreal x2, qreal y2) const;
106  void drawRange(QStylePainter* painter, const QRect& rect) const;
107  void triggerAction(QAbstractSlider::SliderAction action, bool main);
108  void swapControls();
109 
110  int lower;
111  int upper;
112  int lowerPos;
113  int upperPos;
114  int offset;
115  int position;
116  RangeHandle lastPressed;
117  RangeHandle mainControl;
118  QStyle::SubControl lowerPressed;
119  QStyle::SubControl upperPressed;
120  HandleMovementMode movement;
121  bool firstMovement;
122  bool blockTracking;
123 
124 public slots:
125  void updateRange(int min, int max);
126  void movePressedHandle();
127 };
128 
129 }
130 
131 #endif // RANGESLIDER_H