qt-material-widgets/components/slider.h

95 lines
2.7 KiB
C
Raw Normal View History

2016-03-22 14:16:49 +00:00
#ifndef SLIDER_H
#define SLIDER_H
2016-04-03 17:08:26 +00:00
#include <QAbstractSlider>
2016-03-23 05:35:41 +00:00
#include <QPoint>
class QPropertyAnimation;
2016-03-23 05:35:41 +00:00
class Slider;
2016-03-22 14:16:49 +00:00
2016-03-22 15:05:02 +00:00
class Handle : public QWidget
{
Q_OBJECT
2016-05-01 09:10:24 +00:00
Q_PROPERTY(qreal knobSize WRITE setKnobSize READ knobSize)
2016-05-01 12:13:37 +00:00
Q_PROPERTY(qreal haloSize WRITE setHaloSize READ haloSize)
Q_PROPERTY(qreal phase WRITE setPhase READ phase)
2016-04-30 10:44:45 +00:00
2016-03-22 15:05:02 +00:00
public:
2016-03-23 05:35:41 +00:00
explicit Handle(Slider *slider);
2016-03-22 15:05:02 +00:00
~Handle();
2016-05-01 12:13:37 +00:00
inline QSize sizeHint() const Q_DECL_OVERRIDE { return QSize(30, 30); }
2016-03-25 20:37:59 +00:00
inline void setRelativePosition(const QPoint &pos) { setPosition(_offset + pos); }
2016-03-25 20:30:28 +00:00
inline void setPosition(const QPoint &pos) { _position = pos; refreshGeometry(); }
inline const QPoint &position() const { return _position; }
2016-03-25 20:37:59 +00:00
2016-03-25 20:30:28 +00:00
inline void setOffset(const QPoint &offset) { _offset = offset; update(); }
inline const QPoint &offset() const { return _offset; }
2016-03-25 20:37:59 +00:00
inline void setKnobSize (qreal size) { _knobSize = size; refreshGeometry(); }
2016-05-01 09:10:24 +00:00
inline qreal knobSize() const { return _knobSize; }
2016-04-30 10:44:45 +00:00
inline void setHaloSize (qreal size) { _haloSize = size; update(); }
2016-05-01 12:13:37 +00:00
inline qreal haloSize() const { return _haloSize; }
inline void setPhase (qreal phase) { _phase = phase; update(); }
inline qreal phase() const { return _phase; }
void refreshGeometry();
2016-03-23 05:35:41 +00:00
2016-03-22 15:05:02 +00:00
protected:
2016-03-26 10:37:26 +00:00
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
2016-03-23 05:35:41 +00:00
private:
2016-05-01 09:10:24 +00:00
Slider *const _slider;
QPoint _position;
QPoint _offset;
qreal _knobSize;
2016-05-01 12:13:37 +00:00
qreal _haloSize;
qreal _phase;
2016-03-22 15:05:02 +00:00
};
2016-04-03 17:08:26 +00:00
class Slider : public QAbstractSlider
2016-03-22 14:16:49 +00:00
{
Q_OBJECT
public:
explicit Slider(QWidget *parent = 0);
~Slider();
2016-03-22 15:05:02 +00:00
2016-05-01 17:07:21 +00:00
void setOrientation(Qt::Orientation orientation);
2016-03-23 05:35:41 +00:00
inline Qt::Orientation orientation() const { return _orientation; }
2016-03-22 15:05:02 +00:00
protected:
2016-03-26 10:37:26 +00:00
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
2016-05-01 09:10:24 +00:00
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
2016-03-26 10:37:26 +00:00
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
2016-05-01 09:10:24 +00:00
void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
2016-03-26 10:37:26 +00:00
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
2016-03-22 15:05:02 +00:00
2016-05-01 09:10:24 +00:00
bool overTrack(const QPoint &pos) const;
bool overHandle(const QPoint &pos) const;
2016-04-22 20:31:55 +00:00
void updateValue();
2016-05-01 09:10:24 +00:00
void updateHoverState(const QPoint &pos);
2016-03-25 20:30:28 +00:00
2016-05-01 12:13:37 +00:00
void beginHover();
void endHover();
private:
QPropertyAnimation *const _knobAnimation;
QPropertyAnimation *const _haloAnimation;
QPropertyAnimation *const _phaseAnimation;
2016-05-01 12:13:37 +00:00
Handle *const _handle;
bool _drag;
bool _hover;
Qt::Orientation _orientation;
2016-03-22 14:16:49 +00:00
};
#endif // SLIDER_H