1 //========================================================= 2 // MusE 3 // Linux Music Editor 4 // 5 // compact_knob.h 6 // Copyright (C) 1999-2011 by Werner Schweer and others 7 // (C) Copyright 2011 Orcan Ogetbil (ogetbilo at sf.net) completely redesigned. 8 // (C) Copyright 2016 - 2017 Tim E. Real (terminator356 on sourceforge) 9 // 10 // This program is free software; you can redistribute it and/or 11 // modify it under the terms of the GNU General Public License 12 // as published by the Free Software Foundation; version 2 of 13 // the License, or (at your option) any later version. 14 // 15 // This program 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, write to the Free Software 22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 23 // 24 //========================================================= 25 26 #ifndef __COMPACT_KNOB_H__ 27 #define __COMPACT_KNOB_H__ 28 29 #include "sliderbase.h" 30 #include "sclif.h" 31 32 #include <QPoint> 33 #include <QRect> 34 #include <QSize> 35 #include <QColor> 36 #include <QFont> 37 38 class QFontMetrics; 39 class QResizeEvent; 40 class QPainter; 41 class QEvent; 42 class QPaintEvent; 43 class QMouseEvent; 44 class QKeyEvent; 45 46 namespace MusEGui { 47 48 class PopupDoubleSpinBox; 49 class ItemBackgroundPainter; 50 51 //--------------------------------------------------------- 52 // CompactKnob 53 //--------------------------------------------------------- 54 55 class CompactKnob : public SliderBase, public ScaleIf 56 { 57 Q_OBJECT 58 59 public: 60 enum KnobLabelPos { None, Left, Right, Top, Bottom }; 61 enum Symbol { Line, Dot }; 62 Q_ENUM(Symbol) 63 64 Q_PROPERTY( QSize margins READ margins WRITE setMargins ) 65 Q_PROPERTY( int xMargin READ xMargin WRITE setXMargin ) 66 Q_PROPERTY( int yMargin READ yMargin WRITE setYMargin ) 67 68 Q_PROPERTY( int knobWidth READ knobWidth WRITE setKnobWidth ) 69 Q_PROPERTY( double totalAngle READ totalAngle WRITE setTotalAngle ) 70 Q_PROPERTY( int borderWidth READ borderWidth WRITE setBorderWidth ) 71 72 Q_PROPERTY( QColor faceColor READ faceColor WRITE setFaceColor ) 73 Q_PROPERTY( QColor altFaceColor READ altFaceColor WRITE setAltFaceColor ) 74 Q_PROPERTY( QColor shinyColor READ shinyColor WRITE setShinyColor ) 75 Q_PROPERTY( QColor markerColor READ markerColor WRITE setMarkerColor ) 76 Q_PROPERTY( QColor activeColor READ activeColor WRITE setActiveColor ) 77 78 Q_PROPERTY( QString labelText READ labelText WRITE setLabelText ) 79 Q_PROPERTY( QString valPrefix READ valPrefix WRITE setValPrefix ) 80 Q_PROPERTY( QString valSuffix READ valSuffix WRITE setValSuffix ) 81 Q_PROPERTY( QString specialValueText READ specialValueText WRITE setSpecialValueText ) 82 Q_PROPERTY( QString offText READ offText WRITE setOffText ) 83 Q_PROPERTY( int valueDecimals READ valueDecimals WRITE setValueDecimals ) 84 85 Q_PROPERTY( bool style3d READ style3d WRITE setStyle3d ) 86 Q_PROPERTY( int radius READ radius WRITE setRadius ) 87 Q_PROPERTY( Symbol symbol READ symbol WRITE setSymbol ) 88 Q_PROPERTY( bool drawChord READ drawChord WRITE setDrawChord ) 89 90 private: 91 KnobLabelPos d_labelPos; 92 93 bool _hasOffMode; 94 95 QString d_labelText; 96 QString d_valPrefix; 97 QString d_valSuffix; 98 QString d_specialValueText; 99 QString d_offText; 100 int _valueDecimals; 101 bool _off; 102 // Whether to display the label. 103 bool _showLabel; 104 // Whether to display the value. 105 bool _showValue; 106 107 bool _style3d; 108 int _radius; 109 bool _drawChord; 110 111 PopupDoubleSpinBox* _editor; 112 bool _editMode; 113 114 ItemBackgroundPainter* _bkgPainter; 115 116 private slots: 117 void editorReturnPressed(); 118 void editorEscapePressed(); 119 120 protected: 121 bool hasScale; 122 // Whether the mouse is over the entire control. 123 bool _hovered; 124 // Whether the mouse is over the knob. 125 bool _knobHovered; 126 // Whether the mouse is over the label. 127 bool _labelHovered; 128 129 int d_xMargin; 130 int d_yMargin; 131 int d_borderWidth; 132 int d_shineWidth; 133 int d_scaleDist; 134 int d_maxScaleTicks; 135 int d_newVal; 136 int d_knobWidth; 137 int d_dotWidth; 138 139 Symbol d_symbol; 140 double d_angle; 141 double d_oldAngle; 142 double d_totalAngle; 143 double d_nTurns; 144 145 double l_const; 146 double l_slope; 147 148 QRect _labelRect; 149 QRect _knobRect; 150 bool _faceColSel; 151 QColor d_faceColor; 152 QColor d_altFaceColor; 153 QColor d_shinyColor; 154 QColor d_curFaceColor; 155 QColor d_markerColor; 156 QColor d_activeColor; 157 158 void recalcAngle(); 159 void valueChange(); 160 void rangeChange(); 161 void drawBackground(QPainter*); 162 void drawKnob(QPainter* p, const QRect &r); 163 void drawMarker(QPainter* p, double arc, const QColor &c); 164 void drawLabel(QPainter* p); 165 void showEditor(); 166 167 virtual void resizeEvent(QResizeEvent*); 168 virtual void paintEvent(QPaintEvent*); 169 virtual void mouseMoveEvent(QMouseEvent*); 170 virtual void mousePressEvent(QMouseEvent*); 171 virtual void mouseDoubleClickEvent(QMouseEvent*); 172 virtual void keyPressEvent(QKeyEvent*); 173 virtual void leaveEvent(QEvent*); 174 // virtual bool event(QEvent*); 175 176 double getValue(const QPoint &p); 177 // Determine the value corresponding to a specified mouse movement. 178 double moveValue(const QPoint& /*deltaP*/, bool /*fineMode*/ = false); 179 void getScrollMode( QPoint &p, const Qt::MouseButton &button, const Qt::KeyboardModifiers& modifiers, int &scrollMode, int &direction ); scaleChange()180 void scaleChange() { repaint(); } fontChange(const QFont &)181 void fontChange(const QFont &) { repaint(); } 182 183 virtual void processSliderPressed(int); 184 virtual void processSliderReleased(int); 185 // Show a handy tooltip value box. 186 virtual void showValueToolTip(QPoint); 187 188 signals: 189 // Both value and off state changed combined into one signal. 190 // In typical automation use, this signal should be ignored in ScrDirect scroll mode. 191 // ScrDirect mode happens only once upon press with a modifier. 192 // In ScrDirect mode the slider sends both pressed AND changed signals 193 // since the position jumps to the pressed location. 194 // Note the SliderBase::valueChanged signal is also available. 195 void valueStateChanged(double value, bool off, int id, int scrollMode); 196 197 public: 198 CompactKnob(QWidget* parent = 0, const char *name = 0, 199 KnobLabelPos labelPos = None, 200 const QString& labelText = QString(), 201 const QString& valPrefix = QString(), 202 const QString& valSuffix = QString(), 203 const QString& specialValueText = QString(), 204 const QColor& faceColor = QColor()); 205 206 static QSize getMinimumSizeHint(const QFontMetrics& fm, 207 //Qt::Orientation orient = Qt::Vertical, 208 KnobLabelPos labelPos = None, 209 bool showValue = true, 210 bool showLabel = true, 211 int xMargin = 0, 212 int yMargin = 0); 213 214 void setRange(double vmin, double vmax, double vstep = 0.0, 215 int pagesize = 1, DoubleRange::ConversionMode mode = ConvertDefault); 216 labelText()217 QString labelText() const { return d_labelText; }; setLabelText(const QString & t)218 void setLabelText(const QString& t) { d_labelText = t; update(); } valPrefix()219 QString valPrefix() const { return d_valPrefix; }; setValPrefix(const QString & t)220 void setValPrefix(const QString& t) { d_valPrefix = t; update(); } valSuffix()221 QString valSuffix() const { return d_valSuffix; }; setValSuffix(const QString & t)222 void setValSuffix(const QString& t) { d_valSuffix = t; update(); } specialValueText()223 QString specialValueText() const { return d_specialValueText; }; setSpecialValueText(const QString & t)224 void setSpecialValueText(const QString& t) { d_specialValueText = t; update(); } offText()225 QString offText() const { return d_offText; }; setOffText(const QString & t)226 void setOffText(const QString& t) { d_offText = t; update(); } valueDecimals()227 int valueDecimals() const { return _valueDecimals; } setValueDecimals(int d)228 void setValueDecimals(int d) { if(d < 0) return; _valueDecimals = d; update(); } 229 knobWidth()230 int knobWidth() const { return d_knobWidth; } 231 void setKnobWidth(int w); totalAngle()232 double totalAngle() const { return d_totalAngle; } 233 void setTotalAngle (double angle); borderWidth()234 int borderWidth() const { return d_borderWidth; } 235 void setBorderWidth(int bw); 236 237 void selectFaceColor(bool alt); selectedFaceColor()238 bool selectedFaceColor() const { return _faceColSel; } faceColor()239 QColor faceColor() const { return d_faceColor; } 240 void setFaceColor(const QColor& c); altFaceColor()241 QColor altFaceColor() const { return d_altFaceColor; } 242 void setAltFaceColor(const QColor& c); shinyColor()243 QColor shinyColor() const { return d_shinyColor; } 244 void setShinyColor(const QColor& c); markerColor()245 QColor markerColor() const { return d_markerColor; } 246 void setMarkerColor(const QColor& c); activeColor()247 QColor activeColor() const { return d_activeColor; } 248 void setActiveColor(const QColor& c); 249 250 QString toolTipValueText(bool inclLabel, bool inclVal) const; 251 showLabel()252 bool showLabel() const { return _showLabel; } 253 void setShowLabel(bool show); 254 showValue()255 bool showValue() const { return _showValue; } 256 void setShowValue(bool show); 257 hasOffMode()258 bool hasOffMode() const { return _hasOffMode; } 259 void setHasOffMode(bool v); isOff()260 bool isOff() const { return _off; } 261 // Sets the off state and emits valueStateChanged signal if required. 262 void setOff(bool v); 263 // Both value and off state changed combined into one setter. 264 // By default it is assumed that setting a value naturally implies resetting the 'off' state to false. 265 // Emits valueChanged and valueStateChanged signals if required. 266 // Note setOff and SliderBase::setValue are also available. 267 void setValueState(double v, bool off = false, ConversionMode mode = ConvertDefault); 268 margins()269 QSize margins() const { return QSize(d_xMargin, d_yMargin); } xMargin()270 int xMargin() const { return d_xMargin; } yMargin()271 int yMargin() const { return d_yMargin; } 272 void setMargins(QSize); 273 void setMargins(int x, int y); 274 void setXMargin(int x); 275 void setYMargin(int y); 276 style3d()277 bool style3d() const { return _style3d; } setStyle3d(const bool style3d)278 void setStyle3d(const bool style3d) { _style3d = style3d; } radius()279 int radius() const { return _radius; } setRadius(const int radius)280 void setRadius(const int radius) { _radius = radius; } symbol()281 Symbol symbol() const { return d_symbol; } setSymbol(const Symbol s)282 void setSymbol(const Symbol s) { d_symbol = s; } drawChord()283 bool drawChord() const { return _drawChord; } setDrawChord(const bool draw)284 void setDrawChord(const bool draw) { _drawChord = draw; } 285 286 virtual QSize sizeHint() const; 287 }; 288 289 290 } // namespace MusEGui 291 292 #endif 293