File indexing completed on 2024-05-12 04:20:37
0001 /* 0002 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #ifndef KCHARTTEXTLABELCACHE_H 0010 #define KCHARTTEXTLABELCACHE_H 0011 0012 #include <QPixmap> 0013 #include <QFont> 0014 #include <QBrush> 0015 #include <QPen> 0016 0017 #include "KChartEnums.h" 0018 0019 /** 0020 * @brief base class for prerendered elements like labels, pixmaps, markers, etc. 0021 */ 0022 class PrerenderedElement { 0023 public: 0024 PrerenderedElement(); 0025 virtual ~PrerenderedElement() {} 0026 0027 /** Returns the rendered element. 0028 If any of the properties have change, the element will be 0029 regenerated. */ 0030 virtual const QPixmap& pixmap() const = 0; 0031 0032 /** Return the location of the reference point relatively to the 0033 pixmap's origin. */ 0034 virtual QPointF referencePointLocation( KChartEnums::PositionValue ) const = 0; 0035 0036 /** Set the position of the element. */ 0037 void setPosition( const QPointF& position ); 0038 /** Get the position of the element. */ 0039 const QPointF& position() const; 0040 0041 /** Set the reference point of the element. 0042 Every element has nine possible reference points (all compass 0043 directions, plus the center. 0044 */ 0045 void setReferencePoint( KChartEnums::PositionValue ); 0046 /** Get the reference point of the element. */ 0047 KChartEnums::PositionValue referencePoint() const; 0048 0049 protected: 0050 /** invalidate() needs to be called if any of the properties that 0051 determine the visual appearance of the prerendered element 0052 change. 0053 It can be called for a const object, as objects may need to 0054 force recalculation of the pixmap. 0055 */ 0056 virtual void invalidate() const = 0; 0057 0058 private: 0059 QPointF m_position; 0060 KChartEnums::PositionValue m_referencePoint; 0061 }; 0062 0063 /** 0064 @brief PrerenderedLabel is an internal KChart class that simplifies creation 0065 and caching of cached text labels. 0066 0067 It provides referenze points to anchor the text to other 0068 elements. Reference points use the positions defined in 0069 KChartEnums. 0070 0071 Usage: 0072 <pre> 0073 qreal angle = 90.0; 0074 CachedLabel label; 0075 label.paint( font, tr("Label"), angle ); 0076 </pre> 0077 */ 0078 0079 // FIXME this is merely a prototype 0080 // FIXME caching could be done by a second layer that can be used to, 0081 // e.g., query for a prerendered element by id or name, or by changing 0082 // the pixmap() method to do lazy evaluation. 0083 class PrerenderedLabel : public PrerenderedElement 0084 { 0085 public: 0086 PrerenderedLabel(); 0087 ~PrerenderedLabel() override; 0088 0089 0090 /** 0091 * Sets the label's font to \a font. 0092 */ 0093 void setFont( const QFont& font ); 0094 0095 /** 0096 * @return the label's font. 0097 */ 0098 const QFont& font() const; 0099 0100 0101 /** 0102 * Sets the label's text to \a text 0103 */ 0104 void setText( const QString& text ); 0105 0106 /** 0107 * @return the label's text 0108 */ 0109 const QString& text() const; 0110 0111 0112 /** 0113 * Sets the label's brush to \a brush 0114 */ 0115 void setBrush( const QBrush& brush ); 0116 0117 /** 0118 * @return the label's brush 0119 */ 0120 const QBrush& brush() const; 0121 0122 void setPen( const QPen& ); 0123 const QPen& pen() const; 0124 0125 0126 /** 0127 * Sets the angle of the label to \a angle degrees 0128 */ 0129 void setAngle( qreal angle ); 0130 0131 /** 0132 * @return the label's angle in degrees 0133 */ 0134 qreal angle() const; 0135 0136 // reimpl PrerenderedElement: 0137 const QPixmap& pixmap() const override; 0138 QPointF referencePointLocation( KChartEnums::PositionValue position ) const override; 0139 // overload: return location of referencePoint(): 0140 QPointF referencePointLocation() const; 0141 0142 protected: 0143 0144 /** 0145 * Invalidates the preredendered data, forces re-rendering. 0146 */ 0147 void invalidate() const override; 0148 0149 private: 0150 /** Create a label with the given text and the given rotation 0151 angle. Needs to be const, otherwise the pixmap() method cannot 0152 update when needed. */ 0153 void paint() const; 0154 0155 // store the settings (these are used for the painting): 0156 mutable bool m_dirty; 0157 QFont m_font; 0158 QString m_text; 0159 QBrush m_brush; 0160 QPen m_pen; 0161 qreal m_angle; 0162 0163 // these are valid once the label has been rendered: 0164 mutable QPixmap m_pixmap; 0165 mutable QPointF m_referenceBottomLeft; 0166 mutable QPointF m_textBaseLineVector; 0167 mutable QPointF m_textAscendVector; 0168 }; 0169 0170 #endif