Skip to content

Commit

Permalink
feat: add DSpinner
Browse files Browse the repository at this point in the history
Change-Id: I4a52be4fdaa07a147b3f318e5f3b9953b67642da
  • Loading branch information
Iceyer committed Dec 14, 2017
1 parent 2709dc6 commit ad30eab
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 20 deletions.
76 changes: 59 additions & 17 deletions examples/dwidget-examples/collections/indicatortab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

#include "indicatortab.h"

#include "dspinner.h"
#include "dpicturesequenceview.h"
#include "dlinkbutton.h"

#include <QDebug>
#include <QString>
#include <QBoxLayout>

DWIDGET_USE_NAMESPACE

Expand All @@ -30,13 +32,28 @@ IndicatorTab::IndicatorTab(QWidget *parent) :
{
setStyleSheet("IndicatorTab{background-color: #252627;}");

//////////////////////////////////////////////////////////////--DLoadingIndicator
///
QLabel *loadingContainer = new QLabel(this);
QVBoxLayout * loadingContainerLayout = new QVBoxLayout(loadingContainer);
loadingContainer->setLayout(loadingContainerLayout);
loadingContainer->setFixedSize(200, 200);
auto layout = new QVBoxLayout(this);

auto *spinnerLayout = new QHBoxLayout();

auto addSpinner = [ = ](int size, QColor color) {
auto spinner = new DSpinner;
spinner->setFixedSize(size, size);
spinner->setBackgroundColor(color);
spinnerLayout->addWidget(spinner);
spinner->start();
};

addSpinner(16, Qt::transparent);
addSpinner(16, Qt::blue);
addSpinner(32, Qt::green);
addSpinner(32, Qt::yellow);
addSpinner(64, Qt::white);
addSpinner(64, Qt::black);

spinnerLayout->addStretch();

auto *loadingContainerLayout = new QHBoxLayout();
DLoadingIndicator *loadingIndicator = new DLoadingIndicator(this);
loadingIndicator->setImageSource(QPixmap(":/images/loading_indicator.png"));
loadingIndicator->setLoading(true);
Expand All @@ -56,62 +73,87 @@ IndicatorTab::IndicatorTab(QWidget *parent) :

loadingContainerLayout->addWidget(loadingIndicator);
loadingContainerLayout->addWidget(loadingIndicator2);
loadingContainerLayout->addStretch();

QStringList seq;
for (int i(1); i != 91; ++i)
for (int i(1); i != 91; ++i) {
seq.append(QString(":/images/Spinner/Spinner%1.png").arg(i, 2, 10, QChar('0')));
}

QStringList seq2;
for (int i(1); i != 91; ++i)
for (int i(1); i != 91; ++i) {
seq2.append(QString(":/images/eLoading/eLoading_%1.png").arg(i, 2, 10, QChar('0')));
}

DPictureSequenceView *seqView = new DPictureSequenceView(this);
seqView->setPictureSequence(":/images/Spinner/Spinner%1.png", QPair<int, int>(1, 91), 2);
seqView->move(200, 200);
seqView->setFixedSize(50, 50);
seqView->setSpeed(20);

DPictureSequenceView *seqView2 = new DPictureSequenceView(this);
seqView2->setPictureSequence(seq);
seqView2->move(200, 250);
seqView2->setFixedSize(50, 50);
seqView2->setSpeed(33);

DPictureSequenceView *seqView3 = new DPictureSequenceView(this);
seqView3->setPictureSequence(seq);
seqView3->move(200, 300);
seqView3->setFixedSize(50, 50);
seqView3->setSpeed(66);

DPictureSequenceView *seqView4 = new DPictureSequenceView(this);
seqView4->setPictureSequence(seq2);
seqView4->move(250, 200);
seqView4->setFixedSize(50, 50);
seqView4->setSpeed(20);

DPictureSequenceView *seqView5 = new DPictureSequenceView(this);
seqView5->setPictureSequence(seq2);
seqView5->move(250, 250);
seqView5->setFixedSize(50, 50);
seqView5->setSpeed(33);

DPictureSequenceView *seqView6 = new DPictureSequenceView(this);
seqView6->setPictureSequence(seq2);
seqView6->move(250, 300);
seqView6->setFixedSize(50, 50);
seqView6->setSpeed(66);

auto seqViewLayout = new QHBoxLayout;
seqViewLayout->addWidget(seqView);
seqViewLayout->addWidget(seqView2);
seqViewLayout->addWidget(seqView3);
seqViewLayout->addWidget(seqView4);
seqViewLayout->addWidget(seqView5);
seqViewLayout->addWidget(seqView6);
seqViewLayout->addStretch();


DPictureSequenceView *seqViewOnce = new DPictureSequenceView(this);
seqViewOnce->setPictureSequence(seq);
seqViewOnce->move(400, 300);
seqViewOnce->setFixedSize(50, 50);
seqViewOnce->setSpeed(33);
seqViewOnce->setSingleShot(true);

DLinkButton *restart = new DLinkButton("restart", this);
restart->move(450, 320);
DLinkButton *restart = new DLinkButton("Restart", this);

auto seqOnceViewLayout = new QHBoxLayout;
seqOnceViewLayout->addWidget(seqViewOnce);
seqOnceViewLayout->addWidget(restart);
seqOnceViewLayout->addStretch();

layout->addLayout(spinnerLayout);
layout->addLayout(loadingContainerLayout);
layout->addLayout(seqViewLayout);
layout->addLayout(seqOnceViewLayout);
layout->addStretch();

seqView->play();
seqView2->play();
seqView3->play();
seqView4->play();
seqView5->play();
seqView6->play();

connect(restart, &DLinkButton::clicked, [seqViewOnce] {
seqViewOnce->play();
});

setLayout(layout);
}
1 change: 1 addition & 0 deletions src/widgets/DSpinner
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dspinner.h"
119 changes: 119 additions & 0 deletions src/widgets/dspinner.cpp
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;
}
32 changes: 32 additions & 0 deletions src/widgets/dspinner.h
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
9 changes: 6 additions & 3 deletions src/widgets/widgets.pri
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ HEADERS += $$PWD/dslider.h\
$$PWD/dpushbutton.h \
$$PWD/dtickeffect.h \
$$PWD/dwaterprogress.h \
$$PWD/dsettingswidgetfactory.h
$$PWD/dsettingswidgetfactory.h \
$$PWD/dspinner.h

SOURCES += $$PWD/dslider.cpp \
$$PWD/dthememanager.cpp \
Expand Down Expand Up @@ -179,7 +180,8 @@ SOURCES += $$PWD/dslider.cpp \
$$PWD/dpushbutton.cpp \
$$PWD/dtickeffect.cpp \
$$PWD/dwaterprogress.cpp \
$$PWD/dsettingswidgetfactory.cpp
$$PWD/dsettingswidgetfactory.cpp \
$$PWD/dspinner.cpp

RESOURCES += \
$$PWD/themes/dui_theme_dark.qrc \
Expand Down Expand Up @@ -212,6 +214,7 @@ includes.files += \
$$PWD/DSearchEdit \
$$PWD/DPageIndicator \
$$PWD/DSettingsWidgetFactory \
$$PWD/DSettingsDialog
$$PWD/DSettingsDialog \
$$PWD/DSpinner


0 comments on commit ad30eab

Please sign in to comment.