1 //=========================================================
2 //  MusE
3 //  Linux Music Editor
4 //  Copyright (C) 1999-2011 by Werner Schweer and others
5 //
6 //  compact_slider.h
7 //  (C) Copyright 2015-2016 Tim E. Real (terminator356 on sourceforge)
8 //
9 //  This program is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU General Public License
11 //  as published by the Free Software Foundation; version 2 of
12 //  the License, or (at your option) any later version.
13 //
14 //  This program is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 //  GNU General Public License for more details.
18 //
19 //  You should have received a copy of the GNU General Public License
20 //  along with this program; if not, write to the Free Software
21 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 //=========================================================
24 
25 #ifndef __COMPACT_SLIDER_H__
26 #define __COMPACT_SLIDER_H__
27 
28 //#include <QPixmap>
29 
30 #include "sclif.h"
31 #include "sliderbase.h"
32 #include "scldraw.h"
33 
34 class QPainterPath;
35 class QEvent;
36 
37 namespace MusEGui {
38 
39 class PopupDoubleSpinBox;
40 // //class LCDPainter;
41 
42 //---------------------------------------------------------
43 //   CompactSlider
44 //---------------------------------------------------------
45 
46 class CompactSlider : public SliderBase, public ScaleIf
47 {
48   Q_OBJECT
49 
50   public:
51       enum ScalePos { None, Left, Right, Top, Bottom, Embedded };
52 
53       // Can be OR'd together.
54       enum ActiveBorders { NoBorders = 0,
55                            LeftBorder = 1,
56                            RightBorder = 2,
57                            TopBorder = 4,
58                            BottomBorder = 8,
59                            AllBorders = LeftBorder | RightBorder | TopBorder | BottomBorder,
60                            AllBordersExceptTop = LeftBorder | RightBorder | BottomBorder,
61                            AllBordersExceptBottom = LeftBorder | RightBorder | TopBorder,
62                            AllBordersExceptLeft = RightBorder | TopBorder | BottomBorder,
63                            AllBordersExceptRight = LeftBorder | TopBorder | BottomBorder,
64                          };
65       typedef int ActiveBorders_t;
66 
67       // Can be OR'd together.
68       enum TextHighlightModes { //TextHighlightNone,
69                                //TextHighlightAlways,
70                                TextHighlightOn = 1,
71                                TextHighlightSplit = 2,
72                                TextHighlightShadow = 4,
73                                //TextHighlightSplitAndShadow,
74                                TextHighlightHover = 8,
75                                TextHighlightFocus = 16
76                                //TextHighlightHoverOrFocus
77                               };
78       typedef int TextHighlightMode_t;
79 
80       Q_PROPERTY( double lineStep READ lineStep WRITE setLineStep )
81       Q_PROPERTY( double pageStep READ pageStep WRITE setPageStep )
82       Q_PROPERTY( Qt::Orientation orientation READ orientation WRITE setOrientation )
83 
84       Q_PROPERTY( QSize margins READ margins WRITE setMargins )
85       Q_PROPERTY( int xMargin READ xMargin WRITE setXMargin )
86       Q_PROPERTY( int yMargin READ yMargin WRITE setYMargin )
87 
88       Q_PROPERTY( bool barSameColor READ barSameColor WRITE setBarSameColor )
89       Q_PROPERTY( int radius READ radius WRITE setRadius )
90       Q_PROPERTY( int thumbLength READ thumbLength WRITE setThumbLength )
91       Q_PROPERTY( QColor thumbColor READ thumbColor WRITE setThumbColor )
92 
93       Q_PROPERTY( QString labelText READ labelText WRITE setLabelText )
94       Q_PROPERTY( QString valPrefix READ valPrefix WRITE setValPrefix )
95       Q_PROPERTY( QString valSuffix READ valSuffix WRITE setValSuffix )
96       Q_PROPERTY( QString specialValueText READ specialValueText WRITE setSpecialValueText )
97       Q_PROPERTY( QString offText READ offText WRITE setOffText )
98       Q_PROPERTY( int valueDecimals READ valueDecimals WRITE setValueDecimals )
99 
100   private:
101     QRect d_sliderRect;
102     // Whether the mouse is currently over the thumb 'hit' space.
103     bool _mouseOverThumb;
104     // Whether the mouse is over the entire control.
105     bool _hovered;
106     // Cached pixmap values.
107     //QPixmap _onPixmap, _offPixmap;
108     //QPainterPath* _onPath;
109     //QPainterPath* _offPath;
110 
111     bool _detectThumb;
112     bool _autoHideThumb;
113     bool _hasOffMode;
114     int d_thumbLength;
115     int d_thumbHitLength;
116     int d_thumbHalf;
117     int d_thumbWidth;
118     int d_thumbWidthMargin;
119     int d_scaleDist;
120     int d_xMargin;
121     int d_yMargin;
122     int d_mMargin;
123     // Which of the coloured borders are active.
124     // This allows them to be stacked or placed side by side
125     //  without the border colours being too bright or annoying
126     //  since the joining edges are twice as thick.
127     ActiveBorders_t _activeBorders;
128 
129     QColor d_borderColor;
130     QColor d_barColor;
131     QColor d_slotColor;
132     QColor d_thumbColor;
133     bool _barSameColor;
134     int _radius;
135 
136     QString d_labelText;
137     QString d_valPrefix;
138     QString d_valSuffix;
139     QString d_specialValueText;
140     QString d_offText;
141     TextHighlightMode_t _textHighlightMode;
142     int _valueDecimals;
143     bool _off;
144     // Whether to display the value, along with the text.
145     bool _showValue;
146 
147     PopupDoubleSpinBox* _editor;
148     bool _editMode;
149     //LCDPainter* _LCDPainter;
150 
151     bool _entered; // Set on enter and leave.
152     bool d_resized;
153     bool d_autoResize;
154     double d_scaleStep;
155 
156     Qt::Orientation d_orient;
157     ScalePos d_scalePos;
158     int d_bgStyle;
159 
160     // Two slightly different versions of the same thing: valuePixel is the current value scaled to
161     //  a range up to and including the LAST pixel, while valueWidth is the current value scaled to
162     //  a range up to and including the last pixel PLUS ONE ie the 'pixel width'. Values start at zero.
163     int d_valuePixel;
164     int d_valuePixelWidth;
165 
166     //void updatePainterPaths();
167 
168   private slots:
169     void editorReturnPressed();
170     void editorEscapePressed();
171 
172   protected:
173     // At what point size to switch from aliased text (brighter, crisper but may look too jagged and unreadable with some fonts)
174     //  to non-aliased text (dimmer, fuzzier but looks better). Zero means always use anti-aliasing.
175     int _maxAliasedPointSize;
176 
177     //  Determine the value corresponding to a specified mouse location.
178     //  If borderless mouse is enabled p is a delta value not absolute, so can be negative.
179     double getValue(const QPoint &p);
180     //  Determine the value corresponding to a specified mouse movement.
181     double moveValue(const QPoint &deltaP, bool fineMode = false);
182     //  Determine scrolling mode and direction.
183     void getScrollMode( QPoint &p, const Qt::MouseButton &button, const Qt::KeyboardModifiers& modifiers, int &scrollMode, int &direction);
184 
185     // Update internal values.
186     void getPixelValues();
187     void getActiveArea();
188     void getMouseOverThumb(QPoint &p);
189 
190     void showEditor();
191 
192     virtual void resizeEvent(QResizeEvent*);
193     virtual void paintEvent (QPaintEvent*);
194     virtual void mouseMoveEvent(QMouseEvent*);
195     virtual void mouseDoubleClickEvent(QMouseEvent*);
196     virtual void keyPressEvent(QKeyEvent*);
197     virtual void leaveEvent(QEvent*);
198     virtual void enterEvent(QEvent*);
199     virtual bool event(QEvent*);
200 
201     void valueChange();
202     void rangeChange();
203     void scaleChange();
204     //void fontChange(const QFont &oldFont);
205 
206     //virtual void updatePixmaps();
207     virtual void processSliderPressed(int);
208     virtual void processSliderReleased(int);
209     // Show a handy tooltip value box.
210     virtual void showValueToolTip(QPoint);
211 
212   signals:
213     // Both value and off state changed combined into one signal.
214     // In typical automation use, this signal should be ignored in ScrDirect scroll mode.
215     // ScrDirect mode happens only once upon press with a modifier.
216     // In ScrDirect mode the slider sends both pressed AND changed signals
217     //  since the position jumps to the pressed location.
218     // Note the SliderBase::valueChanged signal is also available.
219     void valueStateChanged(double value, bool off, int id, int scrollMode);
220 
221   public:
222     CompactSlider(QWidget *parent = 0, const char *name = 0,
223           Qt::Orientation orient = Qt::Horizontal,
224           ScalePos scalePos = None,
225           const QString& labelText = QString(),
226           const QString& valPrefix = QString(),
227           const QString& valSuffix = QString(),
228           const QString& specialValueText = QString(),
229           const QColor& borderColor = QColor(),
230           const QColor& barColor = QColor(228,203,36),
231           const QColor& slotColor = QColor(),
232           const QColor& thumbColor = QColor());
233 
234     virtual ~CompactSlider();
235 
236     static QSize getMinimumSizeHint(const QFontMetrics& fm,
237                                     Qt::Orientation orient = Qt::Vertical,
238                                     ScalePos scalePos = None,
239                                     int xMargin = 0,
240                                     int yMargin = 0);
241 
242     // Which of the coloured borders are active.
243     // This allows them to be stacked or placed side by side
244     //  without the border colours being too bright or annoying
245     //  since the joining edges are twice as thick.
activeBorders()246     ActiveBorders_t activeBorders() const { return _activeBorders; }
247     void setActiveBorders(ActiveBorders_t borders);
248 
249 //    void setThumbLength(int l);
250     void setThumbWidth(int w);
251 
252     void setOrientation(Qt::Orientation o);
253     Qt::Orientation orientation() const;
254 
255     double lineStep() const;
256     double pageStep() const;
257 
258     void setLineStep(double);
259     void setPageStep(double);
260 
261 //    QColor borderColor() const { return d_borderColor; }
setBorderColor(const QColor & c)262     void setBorderColor(const QColor& c) { d_borderColor = c; update(); }
263 //    QColor barColor() const { return d_barColor; }
setBarColor(const QColor & c)264     void setBarColor(const QColor& c) { d_barColor = c; update(); }
265 //    QColor slotColor() const { return d_slotColor; }
setSlotColor(const QColor & c)266     void setSlotColor(const QColor& c) { d_slotColor = c; update(); }
thumbColor()267     QColor thumbColor() const { return d_thumbColor; }
setThumbColor(const QColor & c)268     void setThumbColor(const QColor& c) { d_thumbColor = c; update(); }
269 
barSameColor()270     bool barSameColor() const { return _barSameColor; }
setBarSameColor(const bool barSameColor)271     void setBarSameColor(const bool barSameColor) { _barSameColor = barSameColor; }
radius()272     int radius() const { return _radius; }
setRadius(const int radius)273     void setRadius(const int radius) { _radius = radius; }
thumbLength()274     int thumbLength() const { return d_thumbLength; }
setThumbLength(const int l)275     void setThumbLength(const int l) { d_thumbLength = l; }
276 
277     // Whether the user must click on the thumb or else anywhere in the control to move the value.
detectThumb()278     bool detectThumb() const { return _detectThumb; }
279     // Set whether the user must click on the thumb or else anywhere in the control to move the value.
setDetectThumb(bool v)280     void setDetectThumb(bool v) { _detectThumb = v; update(); }
281 
282     // At what point size to switch from aliased text to non-aliased text. Zero means always use anti-aliasing. Default 8.
maxAliasedPointSize()283     int maxAliasedPointSize() const { return _maxAliasedPointSize; }
284     // Sets at what point size to switch from aliased text (brighter, crisper but may look too jagged and unreadable with some fonts)
285     //  to non-aliased text (dimmer, fuzzier but looks better). Zero means always use anti-aliasing.
setMaxAliasedPointSize(int sz)286     void setMaxAliasedPointSize(int sz) { if(sz<0)sz=0;_maxAliasedPointSize = sz; update(); }
287 
labelText()288     QString labelText() const { return d_labelText; };
setLabelText(const QString & t)289     void setLabelText(const QString& t) { d_labelText = t; update(); }
valPrefix()290     QString valPrefix() const { return d_valPrefix; };
setValPrefix(const QString & t)291     void setValPrefix(const QString& t) { d_valPrefix = t; update(); }
valSuffix()292     QString valSuffix() const { return d_valSuffix; };
setValSuffix(const QString & t)293     void setValSuffix(const QString& t) { d_valSuffix = t; update(); }
specialValueText()294     QString specialValueText() const { return d_specialValueText; };
setSpecialValueText(const QString & t)295     void setSpecialValueText(const QString& t) { d_specialValueText = t; update(); }
offText()296     QString offText() const { return d_offText; };
setOffText(const QString & t)297     void setOffText(const QString& t) { d_offText = t; update(); }
textHighlightMode()298     TextHighlightMode_t textHighlightMode() const { return _textHighlightMode; }
setTextHighlightMode(TextHighlightMode_t mode)299     void setTextHighlightMode(TextHighlightMode_t mode) { _textHighlightMode = mode; update(); }
valueDecimals()300     int valueDecimals() const { return _valueDecimals; }
setValueDecimals(int d)301     void setValueDecimals(int d) { if(d < 0) return; _valueDecimals = d; update(); }
302 
303     QString toolTipValueText(bool inclLabel, bool inclVal) const;
304 
showValue()305     bool showValue() const { return _showValue; }
306     void setShowValue(bool show);
307 
hasOffMode()308     bool hasOffMode() const { return _hasOffMode; }
309     void setHasOffMode(bool v);
isOff()310     bool isOff() const { return _off; }
311     // Sets the off state and emits valueStateChanged signal if required.
312     void setOff(bool v);
313     // Both value and off state changed combined into one setter.
314     // By default it is assumed that setting a value naturally implies resetting the 'off' state to false.
315     // Emits valueChanged and valueStateChanged signals if required.
316     // Note setOff and SliderBase::setValue are also available.
317     void setValueState(double v, bool off = false, ConversionMode mode = ConvertDefault);
318 
margins()319     QSize margins() const { return QSize(d_xMargin, d_yMargin); }
xMargin()320     int xMargin() const { return d_xMargin; }
yMargin()321     int yMargin() const { return d_yMargin; }
322     void setMargins(QSize);
323     void setMargins(int x, int y);
324     void setXMargin(int x);
325     void setYMargin(int y);
326 
thumbWidthMargin()327     int thumbWidthMargin() const { return d_thumbWidthMargin; }
setThumbWidthMargin(int m)328     void setThumbWidthMargin(int m) { d_thumbWidthMargin = m; update(); }
thumbHitLength()329     int thumbHitLength() const { return d_thumbHitLength; }
setThumbHitLength(int l)330     void setThumbHitLength(int l) { d_thumbHitLength = l; }
autoHideThumb()331     bool autoHideThumb() const { return _autoHideThumb; }
setAutoHideThumb(bool v)332     void setAutoHideThumb(bool v) {_autoHideThumb = v; }
333 
334     virtual QSize sizeHint() const;
335 };
336 
337 } // namespace MusEGui
338 
339 #endif
340