aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Ui/skinned/horizontalslider.cpp
blob: 9a253932b2220f3d333a792874c77df1bde72e63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
 *   Copyright (C) 2015 by Ilya Kotov                                      *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include <QPainter>
#include <QResizeEvent>
#include <math.h>
#include "skin.h"
#include "horizontalslider.h"
#include "pixmapwidget.h"

#define MIN_SLIDER_SIZE 18

HorizontalSlider::HorizontalSlider(QWidget *parent)
        : QWidget(parent)
{
    m_skin = Skin::instance();
    m_min = 0;
    m_max = 100;
    m_value = 0;
    connect(m_skin, SIGNAL(skinChanged()), this, SLOT(updateSkin()));
    updateSkin();
}

HorizontalSlider::~HorizontalSlider()
{}

void HorizontalSlider::paintEvent(QPaintEvent *)
{
    if(m_max <= m_min)
        return;

    bool rtl = (layoutDirection() == Qt::RightToLeft);
    int p = ceil((m_value - m_min)*(width() - sliderSize())/(m_max - m_min));
    if(rtl)
        p = width() - p - sliderSize();

    QPainter paint(this);

    paint.fillRect(0, 0, width(), height(), m_normal_bg);
    paint.setPen(m_normal);

    paint.drawRect(0, 0, width() - 1, height() - 1);
    paint.fillRect(p, 0, sliderSize(), height() - 1, m_normal);

    m_slider_pos = p;
}

void HorizontalSlider::mousePressEvent(QMouseEvent *e)
{
    m_press_pos = e->x();
    if (m_slider_pos < e->x() && e->x() < m_slider_pos + sliderSize())
    {
        m_press_pos = e->x() - m_slider_pos;
    }
    update();
}

void HorizontalSlider::mouseMoveEvent(QMouseEvent* e)
{
    int po = e->x() - m_press_pos;
    bool rtl = (layoutDirection() == Qt::RightToLeft);

    if (0 <= po && po <= width() - sliderSize())
    {
        if(rtl)
            po = width() - po - sliderSize();

        m_value = convert(po);
        update();
        if (m_value != m_old_value)
        {
            m_old_value = m_value;
            emit sliderMoved(m_value);
        }
    }
}

void HorizontalSlider::setPos(int v, int max)
{
    m_max = max;
    m_value = v;
    update();
}

void HorizontalSlider::updateSkin()
{
    m_normal.setNamedColor(m_skin->getPLValue("normal"));
    m_normal_bg.setNamedColor(m_skin->getPLValue("normalbg"));
    update();
}

int HorizontalSlider::convert(int p) const
{
    if(m_max > m_min)
        return ceil((m_max - m_min) * p / (width() - sliderSize())) + m_min;
    else
        return 0;
}

int HorizontalSlider::sliderSize() const
{
    if(m_max > m_min)
        return qMax(width() - abs(m_max - m_min), MIN_SLIDER_SIZE * m_skin->ratio());
    else
        return MIN_SLIDER_SIZE;
}