slider.cpp 3.69 KB
Newer Older
1
2
3
/*
 *  slider.cpp  -  slider control with read-only option
 *  Program:  kalarm
4
 *  SPDX-FileCopyrightText: 2004-2021 David Jarvie <djarvie@kde.org>
5
 *
6
 *  SPDX-License-Identifier: GPL-2.0-or-later
7
8
 */

Laurent Montel's avatar
Laurent Montel committed
9
#include "slider.h"
10

11
#include <QLabel>
12
#include <QMouseEvent>
13
14


David Jarvie's avatar
David Jarvie committed
15
Slider::Slider(QWidget* parent)
16
    : QSlider(parent)
17
18
{ }

David Jarvie's avatar
David Jarvie committed
19
Slider::Slider(Qt::Orientation o, QWidget* parent)
20
    : QSlider(o, parent)
21
22
{ }

23
Slider::Slider(int minval, int maxval, int pageStep, Qt::Orientation o, QWidget* parent)
24
    : QSlider(o, parent)
25
{
26
27
    setRange(minval, maxval);
    setPageStep(pageStep);
28
}
29
30

/******************************************************************************
31
32
* Set the read-only status. If read-only, the slider can be moved by the
* application, but not by the user.
33
34
35
*/
void Slider::setReadOnly(bool ro)
{
36
    mReadOnly = ro;
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
* Set a label to contain the slider's value.
*/
void Slider::setValueLabel(QLabel* label, const QString& format, bool hideIfDisabled)
{
    if (label != mValueLabel)
        delete mValueLabel;    // delete any existing label
    mValueLabel = label;
    if (mValueLabel)
    {
        mValueLabel->setParent(this);
        mValueFormat = format;
        mValueLabelHide = hideIfDisabled;
        connect(this, &QAbstractSlider::valueChanged, this, &Slider::valueHasChanged);
        connect(mValueLabel, &QObject::destroyed, this, &Slider::valueLabelDestroyed);
    }
    else
        disconnect(this, &QAbstractSlider::valueChanged, this, &Slider::valueHasChanged);
}

/******************************************************************************
* Sets the visibility of the slider.
* This also sets the visibility of the value label.
*/
void Slider::setVisible(bool vis)
{
    QSlider::setVisible(vis);
    if (mValueLabel)
    {
        if (mValueLabelHide  &&  !isEnabled())
            vis = false;
        mValueLabel->setVisible(vis);
    }
}

/******************************************************************************
* Called when the slider's status has changed.
* If it is enabled or disabled, show or hide the value label if required.
*/
void Slider::changeEvent(QEvent* e)
{
    if (e->type() == QEvent::EnabledChange)
    {
        if (mValueLabel  &&  mValueLabelHide  &&  isVisible())
            mValueLabel->setVisible(isEnabled());
    }
}

/******************************************************************************
* Called when the slider's value has changed.
*/
void Slider::valueHasChanged(int value)
{
    if (mValueLabel)
        mValueLabel->setText(mValueFormat.arg(value));
}

/******************************************************************************
* Called when the value label is destroyed.
*/
void Slider::valueLabelDestroyed(QObject* obj)
{
    if (obj == mValueLabel)
        mValueLabel = nullptr;
}

/******************************************************************************
* Event handlers to intercept events if in read-only mode.
* Any events which could change the slider value are discarded.
108
109
110
*/
void Slider::mousePressEvent(QMouseEvent* e)
{
111
112
113
114
115
116
117
    if (mReadOnly)
    {
        // Swallow up the event if it's the left button
        if (e->button() == Qt::LeftButton)
            return;
    }
    QSlider::mousePressEvent(e);
118
119
120
121
}

void Slider::mouseReleaseEvent(QMouseEvent* e)
{
122
123
    if (!mReadOnly)
        QSlider::mouseReleaseEvent(e);
124
125
126
127
}

void Slider::mouseMoveEvent(QMouseEvent* e)
{
128
129
    if (!mReadOnly)
        QSlider::mouseMoveEvent(e);
130
131
132
133
}

void Slider::keyPressEvent(QKeyEvent* e)
{
134
135
    if (!mReadOnly  ||  e->key() == Qt::Key_Escape)
        QSlider::keyPressEvent(e);
136
137
138
139
}

void Slider::keyReleaseEvent(QKeyEvent* e)
{
140
141
    if (!mReadOnly)
        QSlider::keyReleaseEvent(e);
142
}
143

144
// vim: et sw=4: