[3rdparty] Introduce alignment flag for text

This commit is contained in:
Adriaan de Groot 2022-04-16 00:04:32 +02:00
parent fac8662387
commit bef6b2fffd
2 changed files with 36 additions and 3 deletions

View File

@ -39,6 +39,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
static bool isAlignCenter(Qt::AlignmentFlag a)
{
return a == Qt::AlignmentFlag::AlignVCenter;
}
WaitingSpinnerWidget::WaitingSpinnerWidget(QWidget *parent, WaitingSpinnerWidget::WaitingSpinnerWidget(QWidget *parent,
bool centerOnParent, bool centerOnParent,
bool disableParentWhenSpinning) bool disableParentWhenSpinning)
@ -120,8 +125,13 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) {
if (!_text.isEmpty()) { if (!_text.isEmpty()) {
painter.setPen(QPen(_textColor)); painter.setPen(QPen(_textColor));
painter.drawText(QRect(0, _imageSize.height(), width(), height() - _imageSize.height()), if (isAlignCenter(alignment())) {
Qt::AlignBottom | Qt::AlignHCenter, _text); 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(); updateSize();
} }
void WaitingSpinnerWidget::setAlignment(Qt::AlignmentFlag align)
{
_alignment = align;
updateSize();
}
QColor WaitingSpinnerWidget::color() const { QColor WaitingSpinnerWidget::color() const {
return _color; return _color;
} }
@ -264,7 +280,7 @@ void WaitingSpinnerWidget::rotate() {
void WaitingSpinnerWidget::updateSize() { void WaitingSpinnerWidget::updateSize() {
int size = (_innerRadius + _lineLength) * 2; int size = (_innerRadius + _lineLength) * 2;
_imageSize = QSize(size, size); _imageSize = QSize(size, size);
if (_text.isEmpty()) { if (_text.isEmpty() || isAlignCenter(alignment())) {
setFixedSize(size, size); setFixedSize(size, size);
} else { } else {
QFontMetrics fm(font()); QFontMetrics fm(font());

View File

@ -72,11 +72,27 @@ public:
void setLineLength(int length); void setLineLength(int length);
void setLineWidth(int width); void setLineWidth(int width);
void setInnerRadius(int radius); 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); 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 color() const;
QColor textColor() const; QColor textColor() const;
QString text() const; QString text() const;
Qt::AlignmentFlag alignment() const { return _alignment; }
qreal roundness() const; qreal roundness() const;
qreal minimumTrailOpacity() const; qreal minimumTrailOpacity() const;
qreal trailFadePercentage() const; qreal trailFadePercentage() const;
@ -120,6 +136,7 @@ private:
int _lineLength; int _lineLength;
int _lineWidth; int _lineWidth;
int _innerRadius; int _innerRadius;
Qt::AlignmentFlag _alignment = Qt::AlignmentFlag::AlignBottom;
QString _text; QString _text;
QSize _imageSize; QSize _imageSize;
QColor _textColor; QColor _textColor;