-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "colortransitionwidget.hpp" | ||
|
||
ColorTransitionWidget::ColorTransitionWidget(QWidget *parent) | ||
: QWidget(parent) | ||
, m_duration(250) | ||
, m_fps(60) | ||
, m_transitionTimer(this) | ||
{ | ||
setup(); | ||
|
||
connect(&m_transitionTimer, SIGNAL(timeout()), this, SLOT(onTimeout())); | ||
} | ||
|
||
ColorTransitionWidget::~ColorTransitionWidget() | ||
{ | ||
|
||
} | ||
|
||
void ColorTransitionWidget::stopTransition() | ||
{ | ||
m_transitionTimer.stop(); | ||
} | ||
|
||
void ColorTransitionWidget::setTransitionDuration(int duration) | ||
{ | ||
m_duration = duration; | ||
} | ||
|
||
int ColorTransitionWidget::transitionDuration() | ||
{ | ||
return m_duration; | ||
} | ||
|
||
void ColorTransitionWidget::setTransitionFps(int fps) | ||
{ | ||
m_fps = fps; | ||
} | ||
|
||
int ColorTransitionWidget::transitionFps() | ||
{ | ||
return m_fps; | ||
} | ||
|
||
void ColorTransitionWidget::setColor(const QColor &color) | ||
{ | ||
QPalette palette; | ||
palette.setColor(QPalette::Background, color); | ||
setPalette(palette); | ||
} | ||
|
||
QColor ColorTransitionWidget::color() const | ||
{ | ||
return palette().color(QPalette::Background); | ||
} | ||
|
||
void ColorTransitionWidget::startTransition(const QColor &color) | ||
{ | ||
m_currentStep = 0; | ||
m_totalSteps = static_cast<double>(m_fps * m_duration) / 1000.0; | ||
|
||
QColor current = this->color(); | ||
m_r = current.red(); | ||
m_g = current.green(); | ||
m_b = current.blue(); | ||
|
||
m_stepR = (static_cast<double>(color.red()) - m_r) / static_cast<double>(m_totalSteps); | ||
m_stepG = (static_cast<double>(color.green()) - m_g) / static_cast<double>(m_totalSteps); | ||
m_stepB = (static_cast<double>(color.blue()) - m_b) / static_cast<double>(m_totalSteps); | ||
|
||
m_transitionTimer.start(1.0 / static_cast<double>(m_fps) * 1000.0); | ||
emit transitionStarted(); | ||
} | ||
|
||
void ColorTransitionWidget::onTimeout() | ||
{ | ||
m_currentStep++; | ||
|
||
m_r += m_stepR; | ||
m_g += m_stepG; | ||
m_b += m_stepB; | ||
|
||
setColor(QColor(m_r, m_g, m_b)); | ||
|
||
if (m_currentStep == m_totalSteps) | ||
{ | ||
m_transitionTimer.stop(); | ||
emit transitionFinished(); | ||
} | ||
} | ||
|
||
void ColorTransitionWidget::setup() | ||
{ | ||
setAutoFillBackground(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef COLORTRANSITIONWIDGET_HPP | ||
#define COLORTRANSITIONWIDGET_HPP | ||
|
||
#include <QColor> | ||
#include <QPalette> | ||
#include <QTimer> | ||
#include <QWidget> | ||
|
||
class ColorTransitionWidget : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
ColorTransitionWidget(QWidget *parent = nullptr); | ||
~ColorTransitionWidget(); | ||
|
||
void setTransitionDuration(int duration); | ||
int transitionDuration(); | ||
|
||
void setTransitionFps(int fps); | ||
int transitionFps(); | ||
|
||
void setColor(const QColor &color); | ||
QColor color() const; | ||
|
||
void startTransition(const QColor &color); | ||
void stopTransition(); | ||
|
||
signals: | ||
void transitionStarted(); | ||
void transitionFinished(); | ||
|
||
private slots: | ||
void onTimeout(); | ||
|
||
private: | ||
void setup(); | ||
|
||
int m_duration; | ||
int m_fps; | ||
int m_totalSteps; | ||
int m_currentStep; | ||
|
||
double m_stepR; | ||
double m_stepG; | ||
double m_stepB; | ||
|
||
double m_r; | ||
double m_g; | ||
double m_b; | ||
|
||
QTimer m_transitionTimer; | ||
}; | ||
|
||
#endif // COLORTRANSITIONWIDGET_HPP |