From 978661491732833eb889fb7ed3e117481a189557 Mon Sep 17 00:00:00 2001 From: huxingyi Date: Wed, 7 Feb 2018 21:06:05 +0800 Subject: [PATCH] Add text support. Implement the setText function. The text will be displayed under the spinner image. --- 3rdparty/waitingspinnerwidget.cpp | 34 ++++++++++++++++++++++++++++++- 3rdparty/waitingspinnerwidget.h | 6 ++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/3rdparty/waitingspinnerwidget.cpp b/3rdparty/waitingspinnerwidget.cpp index 98931a6ad..a52cd17e5 100644 --- a/3rdparty/waitingspinnerwidget.cpp +++ b/3rdparty/waitingspinnerwidget.cpp @@ -65,6 +65,7 @@ WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality, void WaitingSpinnerWidget::initialize() { _color = Qt::black; + _textColor = Qt::white; _roundness = 100.0; _minimumTrailOpacity = 3.14159265358979323846; _trailFadePercentage = 80.0; @@ -98,6 +99,7 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) { painter.save(); painter.translate(_innerRadius + _lineLength, _innerRadius + _lineLength); + painter.translate((width() - _imageSize.width()) / 2, 0); qreal rotateAngle = static_cast(360 * i) / static_cast(_numberOfLines); painter.rotate(rotateAngle); @@ -114,6 +116,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() { @@ -166,10 +174,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; } @@ -214,6 +235,10 @@ void WaitingSpinnerWidget::setColor(QColor color) { _color = color; } +void WaitingSpinnerWidget::setTextColor(QColor color) { + _textColor = color; +} + void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) { _revolutionsPerSecond = revolutionsPerSecond; updateTimer(); @@ -237,7 +262,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() { diff --git a/3rdparty/waitingspinnerwidget.h b/3rdparty/waitingspinnerwidget.h index d171e9beb..5bf96488c 100644 --- a/3rdparty/waitingspinnerwidget.h +++ b/3rdparty/waitingspinnerwidget.h @@ -59,6 +59,7 @@ public slots: public: void setColor(QColor color); + void setTextColor(QColor color); void setRoundness(qreal roundness); void setMinimumTrailOpacity(qreal minimumTrailOpacity); void setTrailFadePercentage(qreal trail); @@ -70,6 +71,8 @@ public: void setText(QString text); QColor color(); + QColor textColor(); + QString text(); qreal roundness(); qreal minimumTrailOpacity(); qreal trailFadePercentage(); @@ -109,6 +112,9 @@ private: int _lineLength; int _lineWidth; int _innerRadius; + QString _text; + QSize _imageSize; + QColor _textColor; private: WaitingSpinnerWidget(const WaitingSpinnerWidget&);