15 #include <QApplication>
24 #define INC_HIT_COUNT \
28 #define INC_MISS_COUNT \
32 #define DUMP_CACHE_STATS \
33 if (HitCount != 0 && MissCount != 0) { \
34 int total = HitCount + MissCount; \
35 qreal hitQuote = (1.0 * HitCount) / total; \
36 qDebug() << "PrerenderedLabel dtor: hits/misses/total:" \
37 << HitCount << "/" << MissCount << "/" << total \
38 << "(" << 100 * hitQuote << "% hits)"; \
42 #define INC_MISS_COUNT
43 #define DUMP_CACHE_STATS
62 m_referencePoint = point;
67 return m_referencePoint;
72 , m_font(qApp->font())
170 void PrerenderedLabel::paint()
const
175 const int Width = 1000;
176 const int Height = Width;
179 const QColor FullTransparent(255, 255, 255, 0);
181 QImage
pixmap(Width, Height, QImage::Format_ARGB32_Premultiplied);
182 qWarning() <<
"PrerenderedLabel::paint: using QImage for prerendered labels "
183 <<
"to work around XRender/Qt4 bug.";
185 QPixmap
pixmap(Width, Height);
189 static const QPointF Center(0.0, 0.0);
190 QPointF textBottomRight;
191 QPainter painter(&
pixmap);
192 painter.setRenderHint(QPainter::TextAntialiasing,
true);
193 painter.setRenderHint(QPainter::Antialiasing,
true);
196 painter.setPen(FullTransparent);
197 painter.setBrush(FullTransparent);
198 QPainter::CompositionMode mode = painter.compositionMode();
199 painter.setCompositionMode(QPainter::CompositionMode_Clear);
200 painter.drawRect(0, 0, Width, Height);
201 painter.setCompositionMode(mode);
204 matrix.translate(0.5 * Width, 0.5 * Height);
205 matrix.rotate(m_angle);
206 painter.setWorldTransform(matrix);
208 painter.setPen(m_pen);
209 painter.setBrush(m_brush);
210 painter.setFont(m_font);
211 QRectF container(-0.5 * Width, -0.5 * Height, Width, 0.5 * Height);
212 painter.drawText(container, Qt::AlignHCenter | Qt::AlignBottom,
213 m_text, &boundingRect);
214 m_referenceBottomLeft = QPointF(boundingRect.bottomLeft().x(), 0.0);
215 textBottomRight = QPointF(boundingRect.bottomRight().x(), 0.0);
216 m_textAscendVector = boundingRect.topRight() - textBottomRight;
217 m_textBaseLineVector = textBottomRight - m_referenceBottomLeft;
220 boundingRect = matrix.mapRect(boundingRect);
221 m_referenceBottomLeft = matrix.map(m_referenceBottomLeft)
222 - boundingRect.topLeft();
223 textBottomRight = matrix.map(textBottomRight)
224 - boundingRect.topLeft();
225 m_textAscendVector = matrix.map(m_textAscendVector)
226 - matrix.map(Center);
227 m_textBaseLineVector = matrix.map(m_textBaseLineVector)
228 - matrix.map(Center);
233 QPixmap temp(
static_cast<int>(boundingRect.width()),
234 static_cast<int>(boundingRect.height()));
236 temp.fill(FullTransparent);
237 QPainter painter(&temp);
239 painter.drawImage(QPointF(0.0, 0.0),
pixmap, boundingRect);
241 painter.drawPixmap(QPointF(0.0, 0.0),
pixmap, boundingRect);
244 #ifdef PRERENDEREDLABEL_DEBUG
245 painter.setPen(QPen(Qt::red, 2));
246 painter.setBrush(Qt::red);
248 const QList<KDChartEnums::PositionValue> positions = {
260 static const double Radius = 0.5;
261 static const double Diameter = 2 * Radius;
264 painter.drawEllipse(QRectF(point - QPointF(Radius, Radius),
265 QSizeF(Diameter, Diameter)));
289 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector + 0.5 * m_textAscendVector;
291 return m_referenceBottomLeft + m_textAscendVector;
293 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector + m_textAscendVector;
295 return m_referenceBottomLeft + m_textBaseLineVector + m_textAscendVector;
297 return m_referenceBottomLeft + 0.5 * m_textAscendVector;
299 return m_referenceBottomLeft + m_textBaseLineVector;
301 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector;
303 return m_referenceBottomLeft;
305 return m_referenceBottomLeft + m_textBaseLineVector + 0.5 * m_textAscendVector;
base class for prerendered elements like labels, pixmaps, markers, etc.
KDChartEnums::PositionValue referencePoint() const
void setReferencePoint(KDChartEnums::PositionValue)
void setPosition(const QPointF &position)
const QPointF & position() const
void setAngle(qreal angle)
const QString & text() const
QPointF referencePointLocation() const
void setFont(const QFont &font)
const QBrush & brush() const
~PrerenderedLabel() override
void invalidate() const override
const QFont & font() const
void setBrush(const QBrush &brush)
const QPixmap & pixmap() const override
void setText(const QString &text)