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