Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text support. #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion waitingspinnerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality,

void WaitingSpinnerWidget::initialize() {
_color = Qt::black;
_textColor = Qt::black;
_roundness = 100.0;
_minimumTrailOpacity = 3.14159265358979323846;
_trailFadePercentage = 80.0;
Expand Down Expand Up @@ -91,6 +92,7 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) {
painter.save();
painter.translate(_innerRadius + _lineLength,
_innerRadius + _lineLength);
painter.translate((width() - _imageSize.width()) / 2, 0);
qreal rotateAngle =
static_cast<qreal>(360 * i) / static_cast<qreal>(_numberOfLines);
painter.rotate(rotateAngle);
Expand All @@ -107,6 +109,12 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) {
_roundness, Qt::RelativeSize);
painter.restore();
}

if (!_text.isEmpty()) {
painter.setPen(QPen(_textColor));
painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()),
Qt::AlignBottom | Qt::AlignHCenter, _text);
}
}

void WaitingSpinnerWidget::start() {
Expand Down Expand Up @@ -159,10 +167,23 @@ void WaitingSpinnerWidget::setInnerRadius(int radius) {
updateSize();
}

void WaitingSpinnerWidget::setText(QString text) {
_text = text;
updateSize();
}

QColor WaitingSpinnerWidget::color() {
return _color;
}

QColor WaitingSpinnerWidget::textColor() {
return _textColor;
}

QString WaitingSpinnerWidget::text() {
return _text;
}

qreal WaitingSpinnerWidget::roundness() {
return _roundness;
}
Expand Down Expand Up @@ -207,6 +228,10 @@ void WaitingSpinnerWidget::setColor(QColor color) {
_color = color;
}

void WaitingSpinnerWidget::setTextColor(QColor color) {
_textColor = color;
}

void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) {
_revolutionsPerSecond = revolutionsPerSecond;
updateTimer();
Expand All @@ -230,7 +255,14 @@ void WaitingSpinnerWidget::rotate() {

void WaitingSpinnerWidget::updateSize() {
int size = (_innerRadius + _lineLength) * 2;
setFixedSize(size, size);
_imageSize = QSize(size, size);
if (_text.isEmpty()) {
setFixedSize(size, size);
} else {
QFontMetrics fm(font());
QSize textSize = QSize(fm.width(_text), fm.height());
setFixedSize(std::max(size, textSize.width()), size + size / 4 + textSize.height());
}
}

void WaitingSpinnerWidget::updateTimer() {
Expand Down
6 changes: 6 additions & 0 deletions waitingspinnerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public slots:

public:
void setColor(QColor color);
void setTextColor(QColor color);
void setRoundness(qreal roundness);
void setMinimumTrailOpacity(qreal minimumTrailOpacity);
void setTrailFadePercentage(qreal trail);
Expand All @@ -62,6 +63,8 @@ public slots:
void setText(QString text);

QColor color();
QColor textColor();
QString text();
qreal roundness();
qreal minimumTrailOpacity();
qreal trailFadePercentage();
Expand Down Expand Up @@ -101,6 +104,9 @@ private slots:
int _lineLength;
int _lineWidth;
int _innerRadius;
QString _text;
QSize _imageSize;
QColor _textColor;

private:
WaitingSpinnerWidget(const WaitingSpinnerWidget&);
Expand Down