FluentUI/src/FluWatermark.cpp

45 lines
1.8 KiB
C++
Raw Normal View History

2023-07-28 16:08:58 +08:00
#include "FluWatermark.h"
2024-04-09 20:53:52 +08:00
#include "FluTextStyle.h"
2024-04-11 14:51:43 +08:00
FluWatermark::FluWatermark(QQuickItem *parent) : QQuickPaintedItem(parent) {
_gap = QPoint(100, 100);
_offset = QPoint(_gap.x() / 2, _gap.y() / 2);
_rotate = 22;
_textColor = QColor(222, 222, 222, 222);
_textSize = 16;
2023-07-28 16:08:58 +08:00
setZ(9999);
2024-04-11 14:51:43 +08:00
connect(this, &FluWatermark::textColorChanged, this, [=] { update(); });
connect(this, &FluWatermark::gapChanged, this, [=] { update(); });
connect(this, &FluWatermark::offsetChanged, this, [=] { update(); });
connect(this, &FluWatermark::textChanged, this, [=] { update(); });
connect(this, &FluWatermark::rotateChanged, this, [=] { update(); });
connect(this, &FluWatermark::textSizeChanged, this, [=] { update(); });
2023-07-28 16:08:58 +08:00
}
2024-04-11 14:51:43 +08:00
void FluWatermark::paint(QPainter *painter) {
2023-07-28 16:08:58 +08:00
QFont font;
2024-04-09 20:53:52 +08:00
font.setFamily(FluTextStyle::getInstance()->family());
2023-07-28 16:08:58 +08:00
font.setPixelSize(_textSize);
painter->setFont(font);
painter->setPen(_textColor);
QFontMetricsF fontMetrics(font);
qreal fontWidth = fontMetrics.horizontalAdvance(_text);
qreal fontHeight = fontMetrics.height();
2024-04-11 14:51:43 +08:00
int stepX = qRound(fontWidth + _gap.x());
int stepY = qRound(fontHeight + _gap.y());
int rowCount = qRound(width() / stepX + 1);
int colCount = qRound(height() / stepY + 1);
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++) {
2023-07-28 16:08:58 +08:00
qreal centerX = stepX * r + _offset.x() + fontWidth / 2.0;
qreal centerY = stepY * c + _offset.y() + fontHeight / 2.0;
painter->save();
painter->translate(centerX, centerY);
painter->rotate(_rotate);
painter->drawText(QRectF(-fontWidth / 2.0, -fontHeight / 2.0, fontWidth, fontHeight), _text);
painter->restore();
}
}
}