Add text support.

Implement the setText function. The text will be displayed under the spinner image.
This commit is contained in:
huxingyi 2018-02-07 21:06:05 +08:00 committed by Adriaan de Groot
parent e351b1dafa
commit 9786614917
2 changed files with 39 additions and 1 deletions

View File

@ -65,6 +65,7 @@ WaitingSpinnerWidget::WaitingSpinnerWidget(Qt::WindowModality modality,
void WaitingSpinnerWidget::initialize() { void WaitingSpinnerWidget::initialize() {
_color = Qt::black; _color = Qt::black;
_textColor = Qt::white;
_roundness = 100.0; _roundness = 100.0;
_minimumTrailOpacity = 3.14159265358979323846; _minimumTrailOpacity = 3.14159265358979323846;
_trailFadePercentage = 80.0; _trailFadePercentage = 80.0;
@ -98,6 +99,7 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) {
painter.save(); painter.save();
painter.translate(_innerRadius + _lineLength, painter.translate(_innerRadius + _lineLength,
_innerRadius + _lineLength); _innerRadius + _lineLength);
painter.translate((width() - _imageSize.width()) / 2, 0);
qreal rotateAngle = qreal rotateAngle =
static_cast<qreal>(360 * i) / static_cast<qreal>(_numberOfLines); static_cast<qreal>(360 * i) / static_cast<qreal>(_numberOfLines);
painter.rotate(rotateAngle); painter.rotate(rotateAngle);
@ -114,6 +116,12 @@ void WaitingSpinnerWidget::paintEvent(QPaintEvent *) {
_roundness, Qt::RelativeSize); _roundness, Qt::RelativeSize);
painter.restore(); 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() { void WaitingSpinnerWidget::start() {
@ -166,10 +174,23 @@ void WaitingSpinnerWidget::setInnerRadius(int radius) {
updateSize(); updateSize();
} }
void WaitingSpinnerWidget::setText(QString text) {
_text = text;
updateSize();
}
QColor WaitingSpinnerWidget::color() { QColor WaitingSpinnerWidget::color() {
return _color; return _color;
} }
QColor WaitingSpinnerWidget::textColor() {
return _textColor;
}
QString WaitingSpinnerWidget::text() {
return _text;
}
qreal WaitingSpinnerWidget::roundness() { qreal WaitingSpinnerWidget::roundness() {
return _roundness; return _roundness;
} }
@ -214,6 +235,10 @@ void WaitingSpinnerWidget::setColor(QColor color) {
_color = color; _color = color;
} }
void WaitingSpinnerWidget::setTextColor(QColor color) {
_textColor = color;
}
void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) { void WaitingSpinnerWidget::setRevolutionsPerSecond(qreal revolutionsPerSecond) {
_revolutionsPerSecond = revolutionsPerSecond; _revolutionsPerSecond = revolutionsPerSecond;
updateTimer(); updateTimer();
@ -237,7 +262,14 @@ void WaitingSpinnerWidget::rotate() {
void WaitingSpinnerWidget::updateSize() { void WaitingSpinnerWidget::updateSize() {
int size = (_innerRadius + _lineLength) * 2; int size = (_innerRadius + _lineLength) * 2;
_imageSize = QSize(size, size);
if (_text.isEmpty()) {
setFixedSize(size, size); 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() { void WaitingSpinnerWidget::updateTimer() {

View File

@ -59,6 +59,7 @@ public slots:
public: public:
void setColor(QColor color); void setColor(QColor color);
void setTextColor(QColor color);
void setRoundness(qreal roundness); void setRoundness(qreal roundness);
void setMinimumTrailOpacity(qreal minimumTrailOpacity); void setMinimumTrailOpacity(qreal minimumTrailOpacity);
void setTrailFadePercentage(qreal trail); void setTrailFadePercentage(qreal trail);
@ -70,6 +71,8 @@ public:
void setText(QString text); void setText(QString text);
QColor color(); QColor color();
QColor textColor();
QString text();
qreal roundness(); qreal roundness();
qreal minimumTrailOpacity(); qreal minimumTrailOpacity();
qreal trailFadePercentage(); qreal trailFadePercentage();
@ -109,6 +112,9 @@ private:
int _lineLength; int _lineLength;
int _lineWidth; int _lineWidth;
int _innerRadius; int _innerRadius;
QString _text;
QSize _imageSize;
QColor _textColor;
private: private:
WaitingSpinnerWidget(const WaitingSpinnerWidget&); WaitingSpinnerWidget(const WaitingSpinnerWidget&);