From bef6b2fffd16de7d7ccbcb49eded53f9de6e550e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sat, 16 Apr 2022 00:04:32 +0200 Subject: [PATCH] [3rdparty] Introduce alignment flag for text --- 3rdparty/waitingspinnerwidget.cpp | 22 +++++++++++++++++++--- 3rdparty/waitingspinnerwidget.h | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/3rdparty/waitingspinnerwidget.cpp b/3rdparty/waitingspinnerwidget.cpp index cd5e1b07e..a55062b54 100644 --- a/3rdparty/waitingspinnerwidget.cpp +++ b/3rdparty/waitingspinnerwidget.cpp @@ -39,6 +39,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +static bool isAlignCenter(Qt::AlignmentFlag a) +{ + return a == Qt::AlignmentFlag::AlignVCenter; +} + WaitingSpinnerWidget::WaitingSpinnerWidget(QWidget *parent, bool centerOnParent, bool disableParentWhenSpinning) @@ -120,8 +125,13 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) { if (!_text.isEmpty()) { painter.setPen(QPen(_textColor)); - painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()), - Qt::AlignBottom | Qt::AlignHCenter, _text); + if (isAlignCenter(alignment())) { + painter.drawText(QRect(0, 0, width(), height()), + Qt::AlignVCenter | Qt::AlignHCenter, _text); + } else { + painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()), + Qt::AlignBottom | Qt::AlignHCenter, _text); + } } } @@ -180,6 +190,12 @@ void WaitingSpinnerWidget::setText(const QString& text) { updateSize(); } +void WaitingSpinnerWidget::setAlignment(Qt::AlignmentFlag align) +{ + _alignment = align; + updateSize(); +} + QColor WaitingSpinnerWidget::color() const { return _color; } @@ -264,7 +280,7 @@ void WaitingSpinnerWidget::rotate() { void WaitingSpinnerWidget::updateSize() { int size = (_innerRadius + _lineLength) * 2; _imageSize = QSize(size, size); - if (_text.isEmpty()) { + if (_text.isEmpty() || isAlignCenter(alignment())) { setFixedSize(size, size); } else { QFontMetrics fm(font()); diff --git a/3rdparty/waitingspinnerwidget.h b/3rdparty/waitingspinnerwidget.h index 7e540dd40..d5e8620d4 100644 --- a/3rdparty/waitingspinnerwidget.h +++ b/3rdparty/waitingspinnerwidget.h @@ -72,11 +72,27 @@ public: void setLineLength(int length); void setLineWidth(int width); void setInnerRadius(int radius); + + /** @brief Sets the text displayed in or below the spinner + * + * If the text is empty, no text is displayed. The text is displayed + * in or below the spinner depending on the value of alignment(). + * With AlignBottom, the text is displayed below the spinner, + * centered horizontally relative to the spinner; any other alignment + * will put the text in the middle of the spinner itself. + */ void setText(const QString& text); + /** @brief Sets the alignment of text for the spinner + * + * The only meaningful values are AlignBottom and AlignVCenter, + * for text below the spinner and text in the middle. + */ + void setAlignment(Qt::AlignmentFlag align); QColor color() const; QColor textColor() const; QString text() const; + Qt::AlignmentFlag alignment() const { return _alignment; } qreal roundness() const; qreal minimumTrailOpacity() const; qreal trailFadePercentage() const; @@ -120,6 +136,7 @@ private: int _lineLength; int _lineWidth; int _innerRadius; + Qt::AlignmentFlag _alignment = Qt::AlignmentFlag::AlignBottom; QString _text; QSize _imageSize; QColor _textColor;