KD Chart 2  [rev.2.7]
KDChartLayoutItems.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #ifndef KDCHARTLAYOUTITEMS_H
24 #define KDCHARTLAYOUTITEMS_H
25 
26 #include <QBrush>
27 #include <QFont>
28 #include <QFontMetricsF>
29 #include <QLayout>
30 #include <QLayoutItem>
31 #include <QPen>
32 
33 #include "KDChartTextAttributes.h"
35 
36 QT_BEGIN_NAMESPACE
37 class QPainter;
38 QT_END_NAMESPACE
39 
40 // TODO remove
41 QRectF rotatedRect( const QRectF& pt, qreal rotation );
42 
43 namespace KDChart {
44  class AbstractDiagram;
45  class PaintContext;
46 
51  class KDCHART_EXPORT AbstractLayoutItem : public QLayoutItem
52  {
53  public:
54  AbstractLayoutItem( Qt::Alignment itemAlignment = 0 ) :
55  QLayoutItem( itemAlignment ),
56  mParent( 0 ),
57  mParentLayout( 0 ) {}
58 
65  virtual void paintAll( QPainter& painter );
66 
67  virtual void paint( QPainter* ) = 0;
68 
69  virtual void paintCtx( PaintContext* context );
70  virtual void setParentWidget( QWidget* widget );
71  virtual void sizeHintChanged() const;
72 
73  void setParentLayout( QLayout* lay )
74  {
75  mParentLayout = lay;
76  }
77  QLayout* parentLayout()
78  {
79  return mParentLayout;
80  }
82  {
83  if ( mParentLayout ) {
84  if ( widget() )
85  mParentLayout->removeWidget( widget() );
86  else
87  mParentLayout->removeItem( this );
88  }
89  }
90  protected:
92  QLayout* mParentLayout;
93  };
94 
99  class KDCHART_EXPORT TextLayoutItem : public AbstractLayoutItem
100  {
101  public:
102  TextLayoutItem();
103  TextLayoutItem( const QString& text,
104  const TextAttributes& attributes,
105  const QObject* autoReferenceArea,
106  KDChartEnums::MeasureOrientation autoReferenceOrientation,
107  Qt::Alignment alignment = 0 );
108 
109  void setAutoReferenceArea( const QObject* area );
110  const QObject* autoReferenceArea() const;
111 
112  void setText(const QString & text);
113  QString text() const;
114 
115  void setTextAlignment( Qt::Alignment );
116  Qt::Alignment textAlignment() const;
117 
118  void setTextAttributes( const TextAttributes& a );
119  TextAttributes textAttributes() const;
120 
122  bool isEmpty() const override;
124  Qt::Orientations expandingDirections() const override;
126  QSize maximumSize() const override;
128  QSize minimumSize() const override;
130  QSize sizeHint() const override;
132  void setGeometry( const QRect& r ) override;
134  QRect geometry() const override;
135 
136  virtual int marginWidth() const;
137 
138  virtual QSize sizeHintUnrotated() const;
139 
140  virtual bool intersects( const TextLayoutItem& other, const QPointF& myPos, const QPointF& otherPos ) const;
141  virtual bool intersects( const TextLayoutItem& other, const QPoint& myPos, const QPoint& otherPos ) const;
142 
143  virtual qreal realFontSize() const;
144  virtual QFont realFont() const;
145 
146  void paint( QPainter* ) override;
147 
148  QPolygon boundingPolygon() const;
149  private:
150  bool maybeUpdateRealFont() const;
151  QSize unrotatedSizeHint( const QFont& fnt = QFont() ) const;
152  QSize unrotatedTextSize( QFont fnt = QFont() ) const;
153  QSize calcSizeHint( const QFont& font ) const;
154  int marginWidth( const QSize& textSize ) const;
155 
156  qreal fitFontSizeToGeometry() const;
157 
158  QRect mRect;
159  QString mText;
160  Qt::Alignment mTextAlignment;
161  TextAttributes mAttributes;
162  const QObject* mAutoReferenceArea;
163  KDChartEnums::MeasureOrientation mAutoReferenceOrientation;
164  mutable QSize cachedSizeHint;
165  mutable QPolygon mCachedBoundingPolygon;
166  mutable qreal cachedFontSize;
167  mutable QFont cachedFont;
168  };
169 
170  class KDCHART_EXPORT TextBubbleLayoutItem : public AbstractLayoutItem
171  {
172  public:
174  TextBubbleLayoutItem( const QString& text,
175  const TextAttributes& attributes,
176  const QObject* autoReferenceArea,
177  KDChartEnums::MeasureOrientation autoReferenceOrientation,
178  Qt::Alignment alignment = 0 );
179 
180  ~TextBubbleLayoutItem() override;
181 
182  void setAutoReferenceArea( const QObject* area );
183  const QObject* autoReferenceArea() const;
184 
185  void setText(const QString & text);
186  QString text() const;
187 
188  void setTextAttributes( const TextAttributes& a );
189  TextAttributes textAttributes() const;
190 
192  bool isEmpty() const override;
194  Qt::Orientations expandingDirections() const override;
196  QSize maximumSize() const override;
198  QSize minimumSize() const override;
200  QSize sizeHint() const override;
202  void setGeometry( const QRect& r ) override;
204  QRect geometry() const override;
205 
206  void paint( QPainter* painter ) override;
207 
208  protected:
209  int borderWidth() const;
210 
211  private:
212  TextLayoutItem* const m_text;
213  };
214 
219  class KDCHART_EXPORT MarkerLayoutItem : public AbstractLayoutItem
220  {
221  public:
223  const MarkerAttributes& marker,
224  const QBrush& brush,
225  const QPen& pen,
226  Qt::Alignment alignment = 0 );
227 
228  Qt::Orientations expandingDirections() const override;
229  QRect geometry() const override;
230  bool isEmpty() const override;
231  QSize maximumSize() const override;
232  QSize minimumSize() const override;
233  void setGeometry( const QRect& r ) override;
234  QSize sizeHint() const override;
235 
236  void paint( QPainter* ) override;
237 
238  static void paintIntoRect(
239  QPainter* painter,
240  const QRect& rect,
241  AbstractDiagram* diagram,
242  const MarkerAttributes& marker,
243  const QBrush& brush,
244  const QPen& pen );
245 
246  private:
247  AbstractDiagram* mDiagram;
248  QRect mRect;
249  MarkerAttributes mMarker;
250  QBrush mBrush;
251  QPen mPen;
252  };
253 
258  class KDCHART_EXPORT LineLayoutItem : public AbstractLayoutItem
259  {
260  public:
262  int length,
263  const QPen& pen,
264  Qt::Alignment mLegendLineSymbolAlignment,
265  Qt::Alignment alignment = 0 );
266 
267  Qt::Orientations expandingDirections() const override;
268  QRect geometry() const override;
269  bool isEmpty() const override;
270  QSize maximumSize() const override;
271  QSize minimumSize() const override;
272  void setGeometry( const QRect& r ) override;
273  QSize sizeHint() const override;
274 
275  void setLegendLineSymbolAlignment(Qt::Alignment legendLineSymbolAlignment);
276  virtual Qt::Alignment legendLineSymbolAlignment() const;
277 
278  void paint( QPainter* ) override;
279 
280  static void paintIntoRect(
281  QPainter* painter,
282  const QRect& rect,
283  const QPen& pen,
284  Qt::Alignment lineAlignment);
285 
286  private:
287  AbstractDiagram* mDiagram; //TODO: not used. remove it
288  int mLength;
289  QPen mPen;
290  QRect mRect;
291  Qt::Alignment mLegendLineSymbolAlignment;
292  };
293 
298  class KDCHART_EXPORT LineWithMarkerLayoutItem : public AbstractLayoutItem
299  {
300  public:
302  int lineLength,
303  const QPen& linePen,
304  int markerOffs,
305  const MarkerAttributes& marker,
306  const QBrush& markerBrush,
307  const QPen& markerPen,
308  Qt::Alignment alignment = 0 );
309 
310  Qt::Orientations expandingDirections() const override;
311  QRect geometry() const override;
312  bool isEmpty() const override;
313  QSize maximumSize() const override;
314  QSize minimumSize() const override;
315  void setGeometry( const QRect& r ) override;
316  QSize sizeHint() const override;
317 
318  void paint( QPainter* ) override;
319 
320  private:
321  AbstractDiagram* mDiagram;
322  QRect mRect;
323  int mLineLength;
324  QPen mLinePen;
325  int mMarkerOffs;
326  MarkerAttributes mMarker;
327  QBrush mMarkerBrush;
328  QPen mMarkerPen;
329  };
330 
331 
336  class KDCHART_EXPORT HorizontalLineLayoutItem : public AbstractLayoutItem
337  {
338  public:
340 
341  Qt::Orientations expandingDirections() const override;
342  QRect geometry() const override;
343  bool isEmpty() const override;
344  QSize maximumSize() const override;
345  QSize minimumSize() const override;
346  void setGeometry( const QRect& r ) override;
347  QSize sizeHint() const override;
348 
349  void paint( QPainter* ) override;
350 
351  private:
352  QRect mRect;
353  };
354 
359  class KDCHART_EXPORT VerticalLineLayoutItem : public AbstractLayoutItem
360  {
361  public:
363 
364  Qt::Orientations expandingDirections() const override;
365  QRect geometry() const override;
366  bool isEmpty() const override;
367  QSize maximumSize() const override;
368  QSize minimumSize() const override;
369  void setGeometry( const QRect& r ) override;
370  QSize sizeHint() const override;
371 
372  void paint( QPainter* ) override;
373 
374  private:
375  QRect mRect;
376  };
377 
448  class KDCHART_EXPORT AutoSpacerLayoutItem : public AbstractLayoutItem
449  {
450  public:
452  bool layoutIsAtTopPosition, QHBoxLayout *rightLeftLayout,
453  bool layoutIsAtLeftPosition, QVBoxLayout *topBottomLayout );
454 
455  Qt::Orientations expandingDirections() const override;
456  QRect geometry() const override;
457  bool isEmpty() const override;
458  QSize maximumSize() const override;
459  QSize minimumSize() const override;
460  void setGeometry( const QRect& r ) override;
461  QSize sizeHint() const override;
462 
463  void paint( QPainter* ) override;
464 
465  private:
466  QRect mRect;
467  bool mLayoutIsAtTopPosition;
468  QHBoxLayout *mRightLeftLayout;
469  bool mLayoutIsAtLeftPosition;
470  QVBoxLayout *mTopBottomLayout;
471 
472  mutable QBrush mCommonBrush;
473  mutable QSize mCachedSize;
474  };
475 
476 }
477 
478 #endif /* KDCHARTLAYOUTITEMS_H */
MeasureOrientation
Measure orientation mode: the way how the absolute value of a KDChart::Measure is determined during K...
Definition: KDChartEnums.h:290
Layout item showing a coloured line and a data point marker.
Layout item showing a text.
AbstractDiagram defines the interface for diagram classes.
Layout item showing a data point marker.
QT_END_NAMESPACE QRectF rotatedRect(const QRectF &pt, qreal rotation)
Class only listed here to document inheritance of some KDChart classes.
Layout item showing a horizontal line.
Base class for all layout items of KD Chart.
AbstractLayoutItem(Qt::Alignment itemAlignment=0)
Layout item showing a vertial line.
Stores information about painting diagrams.
A set of attributes controlling the appearance of data set markers.
Layout item showing a coloured line.
Class only listed here to document inheritance of some KDChart classes.
void setParentLayout(QLayout *lay)
Class only listed here to document inheritance of some KDChart classes.
static qreal fitFontSizeToGeometry(const QString &text, const QFont &font, const QRectF &geometry, const TextAttributes &ta)
A set of text attributes.

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/