9#include "KChartTextLabelCache.h"
16#include <QApplication>
21#define INC_HIT_COUNT { ++HitCount; }
22#define INC_MISS_COUNT { ++MissCount; }
23#define DUMP_CACHE_STATS \
24 if ( HitCount != 0 && MissCount != 0 ) { \
25 int total = HitCount + MissCount; \
26 qreal hitQuote = ( 1.0 * HitCount ) / total; \
27 qDebug() << "PrerenderedLabel dtor: hits/misses/total:" \
28 << HitCount << "/" << MissCount << "/" << total \
29 << "(" << 100 * hitQuote << "% hits)"; \
34#define DUMP_CACHE_STATS
37PrerenderedElement::PrerenderedElement()
38 : m_referencePoint(
KChartEnums::PositionNorthWest )
54 m_referencePoint = point;
59 return m_referencePoint;
62PrerenderedLabel::PrerenderedLabel()
65 , m_font( qApp->font() )
66 , m_brush(
Qt::black )
72PrerenderedLabel::~PrerenderedLabel()
137void PrerenderedLabel::paint()
const
142 const int Width = 1000;
143 const int Height = Width;
146 const QColor FullTransparent( 255, 255, 255, 0 );
149 qWarning() <<
"PrerenderedLabel::paint: using QImage for prerendered labels "
150 <<
"to work around XRender/Qt4 bug.";
156 static const QPointF Center ( 0.0, 0.0 );
163 painter.setPen( FullTransparent );
164 painter.setBrush( FullTransparent );
167 painter.drawRect( 0, 0, Width, Height );
168 painter.setCompositionMode( mode );
171 matrix.
translate( 0.5 * Width, 0.5 * Height );
173 painter.setWorldTransform( matrix );
175 painter.setPen( m_pen );
176 painter.setBrush( m_brush );
177 painter.setFont( m_font );
178 QRectF container( -0.5 * Width, -0.5 * Height, Width, 0.5 * Height );
180 m_text, &boundingRect );
183 m_textAscendVector = boundingRect.
topRight() - textBottomRight;
184 m_textBaseLineVector = textBottomRight - m_referenceBottomLeft;
187 boundingRect = matrix.
mapRect( boundingRect );
188 m_referenceBottomLeft = matrix.
map( m_referenceBottomLeft )
190 textBottomRight = matrix.
map( textBottomRight )
192 m_textAscendVector = matrix.
map( m_textAscendVector )
193 - matrix.
map( Center );
194 m_textBaseLineVector = matrix.
map( m_textBaseLineVector )
195 - matrix.
map( Center );
200 QPixmap temp(
static_cast<int>( boundingRect.
width() ),
201 static_cast<int>( boundingRect.
height() ) );
203 temp.fill( FullTransparent );
204 QPainter painter( &temp );
206 painter.drawImage( QPointF( 0.0, 0.0 ),
pixmap, boundingRect );
208 painter.drawPixmap( QPointF( 0.0, 0.0 ),
pixmap, boundingRect );
211#ifdef PRERENDEREDLABEL_DEBUG
212 painter.setPen( QPen(
Qt::red, 2 ) );
215 QList<KChartEnums::PositionValue> positions;
216 positions << KChartEnums::PositionCenter
217 << KChartEnums::PositionNorthWest
218 << KChartEnums::PositionNorth
219 << KChartEnums::PositionNorthEast
220 << KChartEnums::PositionEast
221 << KChartEnums::PositionSouthEast
222 << KChartEnums::PositionSouth
223 << KChartEnums::PositionSouthWest
224 << KChartEnums::PositionWest;
226 static const double Radius = 0.5;
227 static const double Diameter = 2 * Radius;
230 painter.drawEllipse( QRectF( point - QPointF( Radius, Radius ),
231 QSizeF( Diameter, Diameter ) ) );
254 case KChartEnums::PositionCenter:
255 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector + 0.5 * m_textAscendVector;
256 case KChartEnums::PositionNorthWest:
257 return m_referenceBottomLeft + m_textAscendVector;
258 case KChartEnums::PositionNorth:
259 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector + m_textAscendVector;
260 case KChartEnums::PositionNorthEast:
261 return m_referenceBottomLeft + m_textBaseLineVector + m_textAscendVector;
262 case KChartEnums::PositionEast:
263 return m_referenceBottomLeft + 0.5 * m_textAscendVector;
264 case KChartEnums::PositionSouthEast:
265 return m_referenceBottomLeft + m_textBaseLineVector;
266 case KChartEnums::PositionSouth:
267 return m_referenceBottomLeft + 0.5 * m_textBaseLineVector;
268 case KChartEnums::PositionSouthWest:
269 return m_referenceBottomLeft;
270 case KChartEnums::PositionWest:
271 return m_referenceBottomLeft + m_textBaseLineVector + 0.5 * m_textAscendVector;
273 case KChartEnums::PositionUnknown:
274 case KChartEnums::PositionFloating:
Project global class providing some enums needed both by KChartParams and by KChartCustomBox.
PositionValue
Numerical values of the static KChart::Position instances, for using a Position::value() with a switc...
base class for prerendered elements like labels, pixmaps, markers, etc.
void setPosition(const QPointF &position)
Set the position of the element.
void setReferencePoint(KChartEnums::PositionValue)
Set the reference point of the element.
KChartEnums::PositionValue referencePoint() const
Get the reference point of the element.
const QPointF & position() const
Get the position of the element.
void setAngle(qreal angle)
Sets the angle of the label to angle degrees.
const QString & text() const
void setFont(const QFont &font)
Sets the label's font to font.
const QBrush & brush() const
void invalidate() const override
Invalidates the preredendered data, forces re-rendering.
const QFont & font() const
void setBrush(const QBrush &brush)
Sets the label's brush to brush.
const QPixmap & pixmap() const override
Returns the rendered element.
QPointF referencePointLocation(KChartEnums::PositionValue position) const override
Return the location of the reference point relatively to the pixmap's origin.
void setText(const QString &text)
Sets the label's text to text.
QPointF bottomLeft() const const
QPointF bottomRight() const const
qreal height() const const
QPointF topLeft() const const
QPointF topRight() const const
qreal width() const const