forked from mirror/nheko
Implement image uploads (#24)
parent
ed36bdb037
commit
edff71bc24
@ -1,120 +0,0 @@ |
|||||||
#pragma once |
|
||||||
|
|
||||||
#include <QObject> |
|
||||||
#include <QProgressBar> |
|
||||||
|
|
||||||
#include "Theme.h" |
|
||||||
|
|
||||||
class CircularProgressDelegate; |
|
||||||
|
|
||||||
class CircularProgress : public QProgressBar |
|
||||||
{ |
|
||||||
Q_OBJECT |
|
||||||
|
|
||||||
Q_PROPERTY(qreal lineWidth WRITE setLineWidth READ lineWidth) |
|
||||||
Q_PROPERTY(qreal size WRITE setSize READ size) |
|
||||||
Q_PROPERTY(QColor color WRITE setColor READ color) |
|
||||||
|
|
||||||
public: |
|
||||||
explicit CircularProgress(QWidget *parent = nullptr); |
|
||||||
~CircularProgress(); |
|
||||||
|
|
||||||
void setProgressType(ui::ProgressType type); |
|
||||||
void setLineWidth(qreal width); |
|
||||||
void setSize(int size); |
|
||||||
void setColor(const QColor &color); |
|
||||||
|
|
||||||
ui::ProgressType progressType() const; |
|
||||||
qreal lineWidth() const; |
|
||||||
int size() const; |
|
||||||
QColor color() const; |
|
||||||
|
|
||||||
QSize sizeHint() const override; |
|
||||||
|
|
||||||
protected: |
|
||||||
void paintEvent(QPaintEvent *event) override; |
|
||||||
|
|
||||||
private: |
|
||||||
CircularProgressDelegate *delegate_; |
|
||||||
|
|
||||||
ui::ProgressType progress_type_; |
|
||||||
|
|
||||||
QColor color_; |
|
||||||
|
|
||||||
// Circle width.
|
|
||||||
qreal width_; |
|
||||||
|
|
||||||
// Circle radius.
|
|
||||||
int size_; |
|
||||||
|
|
||||||
// Animation duration.
|
|
||||||
int duration_; |
|
||||||
}; |
|
||||||
|
|
||||||
class CircularProgressDelegate : public QObject |
|
||||||
{ |
|
||||||
Q_OBJECT |
|
||||||
|
|
||||||
Q_PROPERTY(qreal dashOffset WRITE setDashOffset READ dashOffset) |
|
||||||
Q_PROPERTY(qreal dashLength WRITE setDashLength READ dashLength) |
|
||||||
Q_PROPERTY(int angle WRITE setAngle READ angle) |
|
||||||
|
|
||||||
public: |
|
||||||
explicit CircularProgressDelegate(CircularProgress *parent); |
|
||||||
~CircularProgressDelegate(); |
|
||||||
|
|
||||||
inline void setDashOffset(qreal offset); |
|
||||||
inline void setDashLength(qreal length); |
|
||||||
inline void setAngle(int angle); |
|
||||||
|
|
||||||
inline qreal dashOffset() const; |
|
||||||
inline qreal dashLength() const; |
|
||||||
inline int angle() const; |
|
||||||
|
|
||||||
private: |
|
||||||
CircularProgress *const progress_; |
|
||||||
|
|
||||||
qreal dash_offset_; |
|
||||||
qreal dash_length_; |
|
||||||
|
|
||||||
int angle_; |
|
||||||
}; |
|
||||||
|
|
||||||
inline void |
|
||||||
CircularProgressDelegate::setDashOffset(qreal offset) |
|
||||||
{ |
|
||||||
dash_offset_ = offset; |
|
||||||
progress_->update(); |
|
||||||
} |
|
||||||
|
|
||||||
inline void |
|
||||||
CircularProgressDelegate::setDashLength(qreal length) |
|
||||||
{ |
|
||||||
dash_length_ = length; |
|
||||||
progress_->update(); |
|
||||||
} |
|
||||||
|
|
||||||
inline void |
|
||||||
CircularProgressDelegate::setAngle(int angle) |
|
||||||
{ |
|
||||||
angle_ = angle; |
|
||||||
progress_->update(); |
|
||||||
} |
|
||||||
|
|
||||||
inline qreal |
|
||||||
CircularProgressDelegate::dashOffset() const |
|
||||||
{ |
|
||||||
return dash_offset_; |
|
||||||
} |
|
||||||
|
|
||||||
inline qreal |
|
||||||
CircularProgressDelegate::dashLength() const |
|
||||||
{ |
|
||||||
return dash_length_; |
|
||||||
} |
|
||||||
|
|
||||||
inline int |
|
||||||
CircularProgressDelegate::angle() const |
|
||||||
{ |
|
||||||
return angle_; |
|
||||||
} |
|
@ -0,0 +1,49 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <QColor> |
||||||
|
#include <QPaintEvent> |
||||||
|
#include <QPainter> |
||||||
|
#include <QTimer> |
||||||
|
#include <QWidget> |
||||||
|
|
||||||
|
class LoadingIndicator : public QWidget |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
|
||||||
|
public: |
||||||
|
LoadingIndicator(QWidget *parent = 0); |
||||||
|
virtual ~LoadingIndicator(); |
||||||
|
|
||||||
|
void paintEvent(QPaintEvent *e); |
||||||
|
|
||||||
|
void start(); |
||||||
|
void stop(); |
||||||
|
|
||||||
|
QColor color() |
||||||
|
{ |
||||||
|
return color_; |
||||||
|
} |
||||||
|
void setColor(QColor color) |
||||||
|
{ |
||||||
|
color_ = color; |
||||||
|
} |
||||||
|
|
||||||
|
int interval() |
||||||
|
{ |
||||||
|
return interval_; |
||||||
|
} |
||||||
|
void setInterval(int interval) |
||||||
|
{ |
||||||
|
interval_ = interval; |
||||||
|
} |
||||||
|
|
||||||
|
private slots: |
||||||
|
void onTimeout(); |
||||||
|
|
||||||
|
private: |
||||||
|
int interval_; |
||||||
|
int angle_; |
||||||
|
|
||||||
|
QColor color_; |
||||||
|
QTimer *timer_; |
||||||
|
}; |
@ -1,201 +0,0 @@ |
|||||||
#include <QPainter> |
|
||||||
#include <QParallelAnimationGroup> |
|
||||||
#include <QPen> |
|
||||||
#include <QPropertyAnimation> |
|
||||||
|
|
||||||
#include "CircularProgress.h" |
|
||||||
#include "Theme.h" |
|
||||||
|
|
||||||
CircularProgress::CircularProgress(QWidget *parent) |
|
||||||
: QProgressBar{ parent } |
|
||||||
, progress_type_{ ui::ProgressType::IndeterminateProgress } |
|
||||||
, width_{ 6.25 } |
|
||||||
, size_{ 64 } |
|
||||||
, duration_{ 3050 } |
|
||||||
{ |
|
||||||
delegate_ = new CircularProgressDelegate(this); |
|
||||||
|
|
||||||
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); |
|
||||||
|
|
||||||
auto group = new QParallelAnimationGroup(this); |
|
||||||
group->setLoopCount(-1); |
|
||||||
|
|
||||||
auto length_animation = new QPropertyAnimation(this); |
|
||||||
length_animation->setPropertyName("dashLength"); |
|
||||||
length_animation->setTargetObject(delegate_); |
|
||||||
length_animation->setEasingCurve(QEasingCurve::InOutQuad); |
|
||||||
length_animation->setStartValue(0.1); |
|
||||||
length_animation->setKeyValueAt(0.15, 3); |
|
||||||
length_animation->setKeyValueAt(0.6, 20); |
|
||||||
length_animation->setKeyValueAt(0.7, 20); |
|
||||||
length_animation->setEndValue(20); |
|
||||||
length_animation->setDuration(duration_); |
|
||||||
|
|
||||||
auto offset_animation = new QPropertyAnimation(this); |
|
||||||
offset_animation->setPropertyName("dashOffset"); |
|
||||||
offset_animation->setTargetObject(delegate_); |
|
||||||
offset_animation->setEasingCurve(QEasingCurve::InOutSine); |
|
||||||
offset_animation->setStartValue(0); |
|
||||||
offset_animation->setKeyValueAt(0.15, 0); |
|
||||||
offset_animation->setKeyValueAt(0.6, -7); |
|
||||||
offset_animation->setKeyValueAt(0.7, -7); |
|
||||||
offset_animation->setEndValue(-25); |
|
||||||
offset_animation->setDuration(duration_); |
|
||||||
|
|
||||||
auto angle_animation = new QPropertyAnimation(this); |
|
||||||
angle_animation->setPropertyName("angle"); |
|
||||||
angle_animation->setTargetObject(delegate_); |
|
||||||
angle_animation->setStartValue(0); |
|
||||||
angle_animation->setEndValue(360); |
|
||||||
angle_animation->setDuration(duration_); |
|
||||||
|
|
||||||
group->addAnimation(length_animation); |
|
||||||
group->addAnimation(offset_animation); |
|
||||||
group->addAnimation(angle_animation); |
|
||||||
|
|
||||||
group->start(); |
|
||||||
} |
|
||||||
|
|
||||||
void |
|
||||||
CircularProgress::setProgressType(ui::ProgressType type) |
|
||||||
{ |
|
||||||
progress_type_ = type; |
|
||||||
update(); |
|
||||||
} |
|
||||||
|
|
||||||
void |
|
||||||
CircularProgress::setLineWidth(qreal width) |
|
||||||
{ |
|
||||||
width_ = width; |
|
||||||
update(); |
|
||||||
updateGeometry(); |
|
||||||
} |
|
||||||
|
|
||||||
void |
|
||||||
CircularProgress::setSize(int size) |
|
||||||
{ |
|
||||||
size_ = size; |
|
||||||
update(); |
|
||||||
updateGeometry(); |
|
||||||
} |
|
||||||
|
|
||||||
ui::ProgressType |
|
||||||
CircularProgress::progressType() const |
|
||||||
{ |
|
||||||
return progress_type_; |
|
||||||
} |
|
||||||
|
|
||||||
qreal |
|
||||||
CircularProgress::lineWidth() const |
|
||||||
{ |
|
||||||
return width_; |
|
||||||
} |
|
||||||
|
|
||||||
int |
|
||||||
CircularProgress::size() const |
|
||||||
{ |
|
||||||
return size_; |
|
||||||
} |
|
||||||
|
|
||||||
void |
|
||||||
CircularProgress::setColor(const QColor &color) |
|
||||||
{ |
|
||||||
color_ = color; |
|
||||||
} |
|
||||||
|
|
||||||
QColor |
|
||||||
CircularProgress::color() const |
|
||||||
{ |
|
||||||
if (!color_.isValid()) { |
|
||||||
return QColor("red"); |
|
||||||
} |
|
||||||
|
|
||||||
return color_; |
|
||||||
} |
|
||||||
|
|
||||||
QSize |
|
||||||
CircularProgress::sizeHint() const |
|
||||||
{ |
|
||||||
const qreal s = size_ + width_ + 8; |
|
||||||
return QSize(s, s); |
|
||||||
} |
|
||||||
|
|
||||||
void |
|
||||||
CircularProgress::paintEvent(QPaintEvent *event) |
|
||||||
{ |
|
||||||
Q_UNUSED(event); |
|
||||||
|
|
||||||
QPainter painter(this); |
|
||||||
painter.setRenderHint(QPainter::Antialiasing); |
|
||||||
|
|
||||||
/*
|
|
||||||
* If the progress bar is disabled draw an X instead |
|
||||||
*/ |
|
||||||
if (!isEnabled()) { |
|
||||||
QPen pen; |
|
||||||
pen.setCapStyle(Qt::RoundCap); |
|
||||||
pen.setWidthF(lineWidth()); |
|
||||||
pen.setColor("gray"); |
|
||||||
|
|
||||||
auto center = rect().center(); |
|
||||||
|
|
||||||
painter.setPen(pen); |
|
||||||
painter.drawLine(center - QPointF(20, 20), center + QPointF(20, 20)); |
|
||||||
painter.drawLine(center + QPointF(20, -20), center - QPointF(20, -20)); |
|
||||||
|
|
||||||
return; |
|
||||||
} |
|
||||||
|
|
||||||
if (progress_type_ == ui::ProgressType::IndeterminateProgress) { |
|
||||||
painter.translate(width() / 2, height() / 2); |
|
||||||
painter.rotate(delegate_->angle()); |
|
||||||
} |
|
||||||
|
|
||||||
QPen pen; |
|
||||||
pen.setCapStyle(Qt::RoundCap); |
|
||||||
pen.setWidthF(width_); |
|
||||||
pen.setColor(color()); |
|
||||||
|
|
||||||
if (ui::ProgressType::IndeterminateProgress == progress_type_) { |
|
||||||
QVector<qreal> pattern; |
|
||||||
pattern << delegate_->dashLength() * size_ / 50 << 30 * size_ / 50; |
|
||||||
|
|
||||||
pen.setDashOffset(delegate_->dashOffset() * size_ / 50); |
|
||||||
pen.setDashPattern(pattern); |
|
||||||
|
|
||||||
painter.setPen(pen); |
|
||||||
|
|
||||||
painter.drawEllipse(QPoint(0, 0), size_ / 2, size_ / 2); |
|
||||||
} else { |
|
||||||
painter.setPen(pen); |
|
||||||
|
|
||||||
const qreal x = (width() - size_) / 2; |
|
||||||
const qreal y = (height() - size_) / 2; |
|
||||||
|
|
||||||
const qreal a = 360 * (value() - minimum()) / (maximum() - minimum()); |
|
||||||
|
|
||||||
QPainterPath path; |
|
||||||
path.arcMoveTo(x, y, size_, size_, 0); |
|
||||||
path.arcTo(x, y, size_, size_, 0, a); |
|
||||||
|
|
||||||
painter.drawPath(path); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
CircularProgress::~CircularProgress() |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
CircularProgressDelegate::CircularProgressDelegate(CircularProgress *parent) |
|
||||||
: QObject(parent) |
|
||||||
, progress_(parent) |
|
||||||
, dash_offset_(0) |
|
||||||
, dash_length_(89) |
|
||||||
, angle_(0) |
|
||||||
{ |
|
||||||
Q_ASSERT(parent); |
|
||||||
} |
|
||||||
|
|
||||||
CircularProgressDelegate::~CircularProgressDelegate() |
|
||||||
{ |
|
||||||
} |
|
@ -0,0 +1,86 @@ |
|||||||
|
#include "LoadingIndicator.h" |
||||||
|
|
||||||
|
#include <QDebug> |
||||||
|
#include <QPoint> |
||||||
|
#include <QtGlobal> |
||||||
|
|
||||||
|
LoadingIndicator::LoadingIndicator(QWidget *parent) |
||||||
|
: QWidget(parent) |
||||||
|
, interval_(70) |
||||||
|
, angle_(0) |
||||||
|
, color_(Qt::black) |
||||||
|
{ |
||||||
|
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
||||||
|
setFocusPolicy(Qt::NoFocus); |
||||||
|
|
||||||
|
timer_ = new QTimer(); |
||||||
|
connect(timer_, SIGNAL(timeout()), this, SLOT(onTimeout())); |
||||||
|
} |
||||||
|
|
||||||
|
LoadingIndicator::~LoadingIndicator() |
||||||
|
{ |
||||||
|
stop(); |
||||||
|
|
||||||
|
delete timer_; |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
LoadingIndicator::paintEvent(QPaintEvent *e) |
||||||
|
{ |
||||||
|
Q_UNUSED(e) |
||||||
|
|
||||||
|
if (!timer_->isActive()) |
||||||
|
return; |
||||||
|
|
||||||
|
QPainter painter(this); |
||||||
|
painter.setRenderHint(QPainter::Antialiasing); |
||||||
|
|
||||||
|
int width = qMin(this->width(), this->height()); |
||||||
|
|
||||||
|
int outerRadius = (width - 4) * 0.5f; |
||||||
|
int innerRadius = outerRadius * 0.78f; |
||||||
|
|
||||||
|
int capsuleRadius = (outerRadius - innerRadius) / 2; |
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) { |
||||||
|
QColor color = color_; |
||||||
|
|
||||||
|
color.setAlphaF(1.0f - (i / 8.0f)); |
||||||
|
|
||||||
|
painter.setPen(Qt::NoPen); |
||||||
|
painter.setBrush(color); |
||||||
|
|
||||||
|
qreal radius = capsuleRadius * (1.0f - (i / 16.0f)); |
||||||
|
|
||||||
|
painter.save(); |
||||||
|
|
||||||
|
painter.translate(rect().center()); |
||||||
|
painter.rotate(angle_ - i * 45.0f); |
||||||
|
|
||||||
|
QPointF center = QPointF(-capsuleRadius, -innerRadius); |
||||||
|
painter.drawEllipse(center, radius * 2, radius * 2); |
||||||
|
|
||||||
|
painter.restore(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
LoadingIndicator::start() |
||||||
|
{ |
||||||
|
timer_->start(interval_); |
||||||
|
show(); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
LoadingIndicator::stop() |
||||||
|
{ |
||||||
|
timer_->stop(); |
||||||
|
hide(); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
LoadingIndicator::onTimeout() |
||||||
|
{ |
||||||
|
angle_ = (angle_ + 45) % 360; |
||||||
|
update(); |
||||||
|
} |
Loading…
Reference in new issue