Skip to content

Commit bf50969

Browse files
author
ThePBone
committed
Animate slider value changes
1 parent 0176dfe commit bf50969

File tree

8 files changed

+332
-158
lines changed

8 files changed

+332
-158
lines changed

V4L_Frontend.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ SOURCES += \
5555
dialog/logdlg.cpp \
5656
dialog/palettedlg.cpp \
5757
dialog/presetdlg.cpp \
58+
dialog/qanimatedslider.cpp \
5859
dialog/qmessageoverlay.cpp \
5960
dialog/settingsdlg.cpp \
6061
dialog/statusfragment.cpp \
@@ -101,6 +102,7 @@ HEADERS += \
101102
dialog/logdlg.h \
102103
dialog/palettedlg.h \
103104
dialog/presetdlg.h \
105+
dialog/qanimatedslider.h \
104106
dialog/qmessageoverlay.h \
105107
dialog/settingsdlg.h \
106108
dialog/statusfragment.h \

dialog/liquidequalizerwidget.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ using namespace std;
2525
LiquidEqualizerWidget::LiquidEqualizerWidget(QWidget *parent)
2626
: QWidget(parent)
2727
{
28+
for (int i = 0; i < BANDS_NUM;i++) {
29+
anim[i] = new QVariantAnimation(this);
30+
}
2831
connect(this,&LiquidEqualizerWidget::redrawRequired,[this](){
2932
repaint();
3033
});
@@ -243,19 +246,20 @@ void LiquidEqualizerWidget::setBand(int i, float value, bool animate, bool manua
243246
mSelectedBand = i;
244247
mManual = manual;
245248
if(animate){
246-
QVariantAnimation* anim = new QVariantAnimation(this);
247-
anim->setDuration(mAnimationDuration);
248-
anim->setEasingCurve(QEasingCurve(QEasingCurve::Type::InOutCirc));
249-
anim->setStartValue(value);
250-
anim->setEndValue(mLevels[i]);
251-
anim->setDirection(QVariantAnimation::Backward);
252-
253-
connect(anim,QOverload<const QVariant &>::of(&QVariantAnimation::valueChanged),[this,i](const QVariant& v){
249+
anim[i]->stop();
250+
anim[i] = new QVariantAnimation(this);
251+
anim[i]->setDuration(500);
252+
anim[i]->setEasingCurve(QEasingCurve(QEasingCurve::Type::InOutCirc));
253+
anim[i]->setStartValue(mLevels[i]);
254+
anim[i]->setEndValue(value);
255+
anim[i]->setDirection(QVariantAnimation::Direction::Forward);
256+
257+
connect(anim[i],QOverload<const QVariant &>::of(&QVariantAnimation::valueChanged),[this,i](const QVariant& v){
254258
mLevels[i] = v.toFloat();
255259
repaint();
256260
});
257261

258-
anim->start();
262+
anim[i]->start();
259263

260264
}
261265
else{

dialog/liquidequalizerwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class LiquidEqualizerWidget : public QWidget
6969
void keyReleaseEvent(QKeyEvent *event) override;
7070
void mouseDoubleClickEvent(QMouseEvent *event) override;
7171
private:
72+
QVariantAnimation* anim[BANDS_NUM];
73+
7274
QPainter mGridLines;
7375
QPainter mControlBarText;
7476
QPainter mFrequencyResponseBg;

dialog/qanimatedslider.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "qanimatedslider.h"
2+
/*
3+
* MIT License
4+
5+
Copyright (c) 2020 Tim Schneeberger (ThePBone) <tim.schneeberger(at)outlook.de>
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
*
25+
*/
26+
27+
QAnimatedSlider::QAnimatedSlider(QWidget *parent)
28+
: QSlider(parent)
29+
{
30+
anim = new QPropertyAnimation(this,"value");
31+
connect(this,&QSlider::valueChanged,[this]{
32+
cValue = value();
33+
});
34+
}
35+
36+
QAnimatedSlider::~QAnimatedSlider()
37+
{
38+
}
39+
40+
void QAnimatedSlider::setValueA(int val, bool animate)
41+
{
42+
cValue = val;
43+
if(animate){
44+
anim->stop();
45+
anim->setDuration(mDuration);
46+
anim->setEasingCurve(mEasingCurve);
47+
anim->setStartValue(QSlider::value());
48+
anim->setEndValue(val);
49+
anim->start();
50+
}
51+
else
52+
QSlider::setValue(val);
53+
}
54+
55+
int QAnimatedSlider::valueA() const
56+
{
57+
return cValue;
58+
}
59+
60+
QEasingCurve QAnimatedSlider::easingCurve() const
61+
{
62+
return mEasingCurve;
63+
}
64+
65+
void QAnimatedSlider::setEasingCurve(const QEasingCurve &easingCurve)
66+
{
67+
mEasingCurve = easingCurve;
68+
}
69+
70+
int QAnimatedSlider::duration() const
71+
{
72+
return mDuration;
73+
}
74+
75+
void QAnimatedSlider::setDuration(int duration)
76+
{
77+
mDuration = duration;
78+
}
79+
80+

dialog/qanimatedslider.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* MIT License
3+
4+
Copyright (c) 2020 Tim Schneeberger (ThePBone) <tim.schneeberger(at)outlook.de>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*
24+
*/
25+
#ifndef QANIMATEDSLIDER_H
26+
#define QANIMATEDSLIDER_H
27+
28+
#include <QSlider>
29+
#include <QPropertyAnimation>
30+
31+
/*!
32+
\class QAnimatedSlider
33+
34+
\brief The QAnimatedSlider class provides an extended slider widget with animation capabilities.
35+
*/
36+
class QAnimatedSlider : public QSlider
37+
{
38+
Q_OBJECT
39+
40+
public:
41+
QAnimatedSlider(QWidget *parent = nullptr);
42+
~QAnimatedSlider();
43+
/*!
44+
\brief Updates the current stored value and animates this change if \c animate is \c true.
45+
*/
46+
void setValueA(int value, bool animate = true);
47+
/*!
48+
\brief Current value of the underlying slider widget
49+
*/
50+
int valueA() const;
51+
52+
/*!
53+
\brief Returns the currently set easing curve used for the animation
54+
*/
55+
QEasingCurve easingCurve() const;
56+
/*!
57+
\brief Set a custom easing curve used for the animation.\n
58+
By default, the \c QEasingCurve::InOutCirc curve is used.
59+
*/
60+
void setEasingCurve(const QEasingCurve &easingCurve);
61+
62+
/*!
63+
\brief Returns the currently set animation duration
64+
*/
65+
int duration() const;
66+
/*!
67+
\brief Set a custom animation duration.\n
68+
By default, this value is set to 300ms.
69+
*/
70+
void setDuration(int duration);
71+
72+
private:
73+
QVariantAnimation* anim;
74+
int cValue = 0;
75+
int mDuration = 300;
76+
QEasingCurve mEasingCurve = QEasingCurve(QEasingCurve::Type::InOutCirc);
77+
};
78+
#endif // QANIMATEDSLIDER_H

0 commit comments

Comments
 (0)