qt-material-widgets/components/slider.h

79 lines
2.2 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-04-30 10:44:45 +00:00
Q_PROPERTY(qreal scaleFactor WRITE setScaleFactor READ scaleFactor)
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-04-30 10:44:45 +00:00
inline void setScaleFactor (qreal factor ) { _scaleFactor = factor; refreshGeometry(); }
inline qreal scaleFactor() const { return _scaleFactor; }
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;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
2016-04-30 10:44:45 +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-03-23 05:35:41 +00:00
private:
2016-04-30 10:44:45 +00:00
Slider *const _slider;
QPropertyAnimation *const _animation;
QPoint _position;
QPoint _eventPos;
QPoint _offset;
qreal _scaleFactor;
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;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
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;
inline bool isOnTrack(int p, int x) const { return (p >= x-2 && p <= x+2); }
2016-04-22 20:31:55 +00:00
void updateValue();
2016-03-25 20:30:28 +00:00
bool _drag;
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