src/RangedSlider.h

changeset 92
fb0bb9a2a7e1
child 96
c36fef8bb088
equal deleted inserted replaced
91:409d9c7214be 92:fb0bb9a2a7e1
1 /*
2 * RangedSlider.h is part bmsapp.
3 *
4 * Original written for Brewtarget, and is Copyright the following
5 * authors 2009-2020
6 * - Matt Young <mfsy@yahoo.com>
7 * - Mik Firestone <mikfire@gmail.com>
8 * - Philip G. Lee <rocketman768@gmail.com>
9 *
10 * bmsapp is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * bmsapp is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef RANGEDSLIDER_H
24 #define RANGEDSLIDER_H
25
26 #include <QWidget>
27 #include <QSize>
28 #include <QString>
29 #include <QBrush>
30 #include <QPen>
31 class QPaintEvent;
32 class QMouseEvent;
33
34 /**
35 * @brief Widget to display a number with an optional range on a type of read-only slider.
36 * @author Philip G. Lee
37 */
38 class RangedSlider : public QWidget
39 {
40 Q_OBJECT
41
42 public:
43 RangedSlider(QWidget* parent=0);
44
45 Q_PROPERTY( double value READ value WRITE setValue )
46
47 double value() const { return _val; }
48
49 //! \brief Set the background brush for the widget.
50 void setBackgroundBrush( QBrush const& brush );
51 //! \brief Set the brush for the preffered range.
52 void setPreferredRangeBrush( QBrush const& brush );
53 //! \brief Set the pen for the preferred range
54 void setPreferredRangePen( QPen const& pen );
55 //! \brief Set the brush for the marker.
56 void setMarkerBrush( QBrush const& brush );
57 //! \brief Set the text displayed above the marker.
58 void setMarkerText( QString const& text );
59 //! \brief If true, the marker text will always be updated to the value given by \c setValue().
60 void setMarkerTextIsValue(bool val);
61
62 /*!
63 * \brief Set the tick mark intervals.
64 *
65 * If either parameter is <= 0, then the tick marks are not drawn.
66 *
67 * \param primaryInterval How often to draw big tick marks.
68 * \param secondaryTicks Number of secondary ticks per primary tick.
69 */
70 void setTickMarks( double primaryInterval, int secondaryTicks = 1 );
71
72 //! \brief Set the \c precision for displaying values.
73 void setPrecision(int precision);
74
75 //! \brief Reimplemented from QWidget.
76 virtual QSize sizeHint() const;
77
78 public slots:
79
80 //! \brief Set the \c value for the indicator.
81 void setValue(double value);
82
83 /*!
84 * \brief Set the range of values considered to be *best*
85 *
86 * \param range \c range.first and \c range.second are the min and max
87 * values for the preferred range resp.
88 */
89 void setPreferredRange(QPair<double,double> range);
90
91 /*!
92 * \brief Set the range of values that the widget displays
93 *
94 * \param range \c range.first and \c range.second are the min and max
95 * values for the preferred range resp.
96 */
97 void setRange(QPair<double,double> range);
98
99 //! \brief Convenience method for setting the widget range
100 void setRange( double min, double max );
101
102 //! \brief Convenience method for setting the preferred range
103 // Note that this is completely unrelated to "preferred size".
104 void setPreferredRange( double min, double max );
105
106 protected:
107 //! \brief Reimplemented from QWidget.
108 virtual void paintEvent(QPaintEvent* event);
109 //! \brief Reimplemented from QWidget for popup on mouseover.
110 virtual void mouseMoveEvent(QMouseEvent* event);
111 //! \brief Reimplemented from QWidget.
112 virtual void moveEvent(QMoveEvent *event);
113
114 private:
115 /**
116 * Sets minimum / maximum sizes and resize policy
117 */
118 void setSizes();
119 void recalculateHeightInPixels() const;
120
121 /**
122 * Minimum value the widget displays
123 */
124 double _min;
125 /**
126 * Maximum value the widget displays
127 */
128 double _max;
129
130 /**
131 * Minimum value for the "best" sub-range
132 */
133 double _prefMin;
134 /**
135 * Maximum value for the "best" sub-range
136 */
137 double _prefMax;
138 double _val;
139 QString _valText;
140 QString _markerText;
141 int _prec;
142 double _tickInterval;
143 int _secondaryTicks;
144 QString _tooltipText;
145 QBrush _bgBrush;
146 QBrush _prefRangeBrush;
147 QPen _prefRangePen;
148 QBrush _markerBrush;
149 bool _markerTextIsValue;
150
151 /**
152 * The font used for showing the value at the right-hand side of the slider
153 */
154 QFont const valueTextFont;
155
156 /**
157 * The font used for showing the indicator above the "needle" on the slider. Often this is just showing the same as
158 * the value - eg OG, FG, ABV - but sometimes it's something else - eg descriptive text such as "slightly hoppy" for
159 * the IBU/GU reading.
160 */
161 QFont const indicatorTextFont;
162
163 /**
164 * Since preferred and minimum dimensions are all based off a height we need to calculate based on the resolution of
165 * the current display (see more detailed comment in implementation of recalculateSizes()), it is useful to store
166 * that height here. However, its value is not really part of the current value/state of the object, hence mutable
167 * (ie OK to change in a const function).
168 */
169 mutable int heightInPixels;
170 };
171
172 #endif

mercurial