qt-material-widgets/components/slider.h

88 lines
2.5 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>
2016-04-30 10:44:45 +00:00
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-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();
inline QSize sizeHint() const Q_DECL_OVERRIDE { return QSize(20, 20); }
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
2016-05-01 09:10:24 +00:00
inline void setKnobSize (qreal size ) { _knobSize = size; refreshGeometry(); }
inline qreal knobSize() const { return _knobSize; }
2016-04-30 10:44:45 +00:00
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-05-01 09:10:24 +00:00
// void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
// void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
// void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
// void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
// void mouseMoveEvent(QMouseEvent *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 _eventPos;
QPoint _offset;
qreal _knobSize;
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-03-23 05:35:41 +00:00
inline void setOrientation(Qt::Orientation orientation) { _orientation = orientation; }
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
private:
2016-04-22 20:31:55 +00:00
friend class Handle;
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 09:10:24 +00:00
QPropertyAnimation *const _animation;
2016-03-25 20:30:28 +00:00
bool _drag;
2016-05-01 09:10:24 +00:00
bool _hover;
2016-03-23 05:35:41 +00:00
Handle *const _handle;
Qt::Orientation _orientation;
2016-03-22 14:16:49 +00:00
};
#endif // SLIDER_H