-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I4a52be4fdaa07a147b3f318e5f3b9953b67642da
- Loading branch information
Showing
5 changed files
with
217 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "dspinner.h" |
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,119 @@ | ||
#include "dspinner.h" | ||
|
||
#include <QDebug> | ||
#include <QtMath> | ||
#include <QPainter> | ||
#include <QTimer> | ||
|
||
class DSpinnerPrivate | ||
{ | ||
public: | ||
DSpinnerPrivate(DSpinner *parent) : q_ptr(parent) {} | ||
|
||
QList<QColor> createDefaultIndicatorColorList(QColor color); | ||
|
||
QTimer refreshTimer; | ||
|
||
double indicatorShadowOffset = 10; | ||
double currentDegree = 0.0; | ||
|
||
QList<QList<QColor>> indicatorColors; | ||
|
||
DSpinner *q_ptr; | ||
Q_DECLARE_PUBLIC(DSpinner) | ||
}; | ||
|
||
DSpinner::DSpinner(QWidget *parent) : | ||
QWidget(parent), d_ptr(new DSpinnerPrivate(this)) | ||
{ | ||
Q_D(DSpinner); | ||
|
||
d->indicatorColors << d->createDefaultIndicatorColorList(QColor(200, 46, 255)); | ||
d->indicatorColors << d->createDefaultIndicatorColorList(QColor(207, 225, 0)); | ||
d->indicatorColors << d->createDefaultIndicatorColorList(QColor(0, 255, 241)); | ||
|
||
d->refreshTimer.setInterval(30); | ||
|
||
connect(&d->refreshTimer, &QTimer::timeout, | ||
this, [ = ]() { | ||
d->currentDegree += 14; | ||
update(); | ||
}); | ||
} | ||
|
||
DSpinner::~DSpinner() | ||
{ | ||
|
||
} | ||
|
||
bool DSpinner::isPlaying() const | ||
{ | ||
Q_D(const DSpinner); | ||
return d->refreshTimer.isActive(); | ||
} | ||
|
||
void DSpinner::start() | ||
{ | ||
Q_D(DSpinner); | ||
d->refreshTimer.start(); | ||
} | ||
|
||
void DSpinner::stop() | ||
{ | ||
Q_D(DSpinner); | ||
d->refreshTimer.stop(); | ||
} | ||
|
||
void DSpinner::setBackgroundColor(QColor color) | ||
{ | ||
setAutoFillBackground(true); | ||
QPalette pal = palette(); | ||
pal.setColor(QPalette::Background, color); | ||
setPalette(pal); | ||
} | ||
|
||
void DSpinner::paintEvent(QPaintEvent *) | ||
{ | ||
Q_D(DSpinner); | ||
|
||
QPainter painter(this); | ||
painter.setRenderHints(QPainter::Antialiasing); | ||
|
||
auto degreeCurrent = d->currentDegree * 1.0; | ||
|
||
auto center = QRectF(rect()).center(); | ||
auto radius = qMin(rect().width(), rect().height()) / 2.0; | ||
auto indicatorRadius = radius / 2 / 2 * 1.1; | ||
auto indicatorDegreeDelta = 360 / d->indicatorColors.count(); | ||
|
||
for (int i = 0; i < d->indicatorColors.count(); ++i) { | ||
auto colors = d->indicatorColors.value(i); | ||
for (int j = 0; j < colors.count(); ++j) { | ||
degreeCurrent = d->currentDegree - j * d->indicatorShadowOffset + indicatorDegreeDelta * i; | ||
auto x = (radius - indicatorRadius) * qCos(qDegreesToRadians(degreeCurrent)); | ||
auto y = (radius - indicatorRadius) * qSin(qDegreesToRadians(degreeCurrent)); | ||
|
||
x = center.x() + x; | ||
y = center.y() + y; | ||
auto tl = QPointF(x - 1 * indicatorRadius, y - 1 * indicatorRadius); | ||
QRectF rf(tl.x(), tl.y(), indicatorRadius * 2, indicatorRadius * 2); | ||
|
||
QPainterPath path; | ||
path.addEllipse(rf); | ||
|
||
painter.fillPath(path, colors.value(j)); | ||
} | ||
} | ||
} | ||
|
||
QList<QColor> DSpinnerPrivate::createDefaultIndicatorColorList(QColor color) | ||
{ | ||
QList<QColor> colors; | ||
QList<int> opacitys; | ||
opacitys << 100 << 30 << 15 << 10 << 5 << 4 << 3 << 2 << 1; | ||
for (int i = 0; i < opacitys.count(); ++i) { | ||
color.setAlpha(255 * opacitys.value(i) / 100); | ||
colors << color; | ||
} | ||
return colors; | ||
} |
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,32 @@ | ||
#ifndef DSPINNER_H | ||
#define DSPINNER_H | ||
|
||
#include <QScopedPointer> | ||
#include <QWidget> | ||
|
||
#include "dtkwidget_global.h" | ||
|
||
class DSpinnerPrivate; | ||
class DSpinner : public QWidget | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit DSpinner(QWidget *parent = 0); | ||
~DSpinner(); | ||
|
||
bool isPlaying() const; | ||
|
||
public Q_SLOTS: | ||
void start(); | ||
void stop(); | ||
void setBackgroundColor(QColor color); | ||
|
||
protected: | ||
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; | ||
|
||
private: | ||
QScopedPointer<DSpinnerPrivate> d_ptr; | ||
Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSpinner) | ||
}; | ||
|
||
#endif |
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