slider.h 1.78 KB
#ifndef SLIDER_H
#define SLIDER_H

#include <QWidget>
#include <QPixmap>

class Slider : public QWidget
{
    Q_OBJECT

    bool isSliderDown_;
    int sliderPosition_;
    int value_;
    int minimum_;
    int maximum_;

    QPixmap groove;
    QPixmap sub;
    QPixmap handle;

    int minSliderX;
    int maxSliderX;

    QPoint grooveLeftTop;
    QPoint subLeftTop;

    bool subVisible_;
    bool focused;
    bool isSliderMoved_;

public:
    explicit Slider(QWidget *parent = 0);

    void setSubPixmap(const QPixmap &pixmap);
    void setSubPixmap(const QString &fileName);
    void setSubVisible(bool visible);

    void setRange(int min, int max);
    void setMinimum(int min);
    void setMaximum(int max);

    int value();
    int minimum();
    int maximum();

    bool isSliderDown();
    int sliderPosition();
    void setSliderPosition(int value);
    bool isSliderMoved();

    QList<int> ticks;
    QList<int> bigTicks;

    int tickInterval;
    int bigTickInterval;

protected:
    virtual void focusInEvent(QFocusEvent *event);
    virtual void focusOutEvent(QFocusEvent *event);
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void keyReleaseEvent(QKeyEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    virtual void paintEvent(QPaintEvent *event);
    virtual void resizeEvent(QResizeEvent *event);

private:
    int calcSliderPosition(int mouseX);
    int calcSubLength(int value);
    void updatePixmapPosition();

signals:
    void sliderMoved(int value);
    void valueChanged(int value);
    void sliderPressed();
    void sliderReleased();

public slots:
    void setValue(int value);
    void increase();
    void decrease();
};

#endif // SLIDER_H