KChart

KChartLayoutItems.h
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef KCHARTLAYOUTITEMS_H
10 #define KCHARTLAYOUTITEMS_H
11 
12 #include <QBrush>
13 #include <QFont>
14 #include <QFontMetricsF>
15 #include <QLayout>
16 #include <QLayoutItem>
17 #include <QPen>
18 
19 #include "KChartTextAttributes.h"
20 #include "KChartMarkerAttributes.h"
21 
22 QT_BEGIN_NAMESPACE
23 class QPainter;
24 class KTextDocument;
25 QT_END_NAMESPACE
26 
27 // TODO remove
28 QRectF rotatedRect( const QRectF& pt, qreal rotation );
29 
30 namespace KChart {
31  class AbstractDiagram;
32  class PaintContext;
33 
34  /**
35  * Base class for all layout items of KChart
36  * \internal
37  */
38  class KCHART_EXPORT AbstractLayoutItem : public QLayoutItem
39  {
40  public:
41  AbstractLayoutItem( Qt::Alignment itemAlignment = Qt::Alignment() ) :
42  QLayoutItem( itemAlignment ),
43  mParent( nullptr ),
44  mParentLayout( nullptr ) {}
45 
46  /**
47  * Default impl: just call paint.
48  *
49  * Derived classes like KChart::AbstractArea are providing
50  * additional action here.
51  */
52  virtual void paintAll( QPainter& painter );
53 
54  virtual void paint( QPainter* ) = 0;
55 
56 
57  /**
58  * Default impl: Paint the complete item using its layouted position and size.
59  */
60  virtual void paintCtx( PaintContext* context );
61 
62  /**
63  Inform the item about its widget: This enables the item,
64  to trigger that widget's update, whenever the size of the item's
65  contents has changed.
66 
67  Thus, you need to call setParentWidget on every item, that
68  has a non-fixed size.
69  */
70  virtual void setParentWidget( QWidget* widget );
71 
72  /**
73  Report changed size hint: ask the parent widget to recalculate the layout.
74  */
75  virtual void sizeHintChanged() const;
76 
77  void setParentLayout( QLayout* lay )
78  {
79  mParentLayout = lay;
80  }
81  QLayout* parentLayout()
82  {
83  return mParentLayout;
84  }
85  void removeFromParentLayout()
86  {
87  if ( mParentLayout ) {
88  if ( widget() )
89  mParentLayout->removeWidget( widget() );
90  else
91  mParentLayout->removeItem( this );
92  }
93  }
94  protected:
95  QWidget* mParent;
96  QLayout* mParentLayout;
97  };
98 
99  /**
100  * Layout item showing a text
101  *\internal
102  */
103  class KCHART_EXPORT TextLayoutItem : public AbstractLayoutItem
104  {
105  public:
106  TextLayoutItem();
107  TextLayoutItem( const QString& text,
108  const TextAttributes& attributes,
109  const QObject* autoReferenceArea,
110  KChartEnums::MeasureOrientation autoReferenceOrientation,
111  Qt::Alignment alignment = Qt::Alignment() );
112 
113  void setAutoReferenceArea( const QObject* area );
114  const QObject* autoReferenceArea() const;
115 
116  void setText(const QString & text);
117  QString text() const;
118 
119  void setTextAlignment( Qt::Alignment );
120  Qt::Alignment textAlignment() const;
121 
122  /**
123  \brief Use this to specify the text attributes to be used for this item.
124 
125  \sa textAttributes
126  */
127  void setTextAttributes( const TextAttributes& a );
128 
129  /**
130  Returns the text attributes to be used for this item.
131 
132  \sa setTextAttributes
133  */
134  TextAttributes textAttributes() const;
135 
136  /** pure virtual in QLayoutItem */
137  bool isEmpty() const override;
138  /** pure virtual in QLayoutItem */
139  Qt::Orientations expandingDirections() const override;
140  /** pure virtual in QLayoutItem */
141  QSize maximumSize() const override;
142  /** pure virtual in QLayoutItem */
143  QSize minimumSize() const override;
144  /** pure virtual in QLayoutItem */
145  QSize sizeHint() const override;
146  /** pure virtual in QLayoutItem */
147  void setGeometry( const QRect& r ) override;
148  /** pure virtual in QLayoutItem */
149  QRect geometry() const override;
150 
151  virtual int marginWidth() const;
152 
153  virtual QSize sizeHintUnrotated() const;
154 
155  virtual bool intersects( const TextLayoutItem& other, const QPointF& myPos, const QPointF& otherPos ) const;
156  virtual bool intersects( const TextLayoutItem& other, const QPoint& myPos, const QPoint& otherPos ) const;
157 
158  virtual qreal realFontSize() const;
159  virtual QFont realFont() const;
160 
161  void paint( QPainter* ) override;
162 
163  QPolygon boundingPolygon() const;
164  private:
165  bool maybeUpdateRealFont() const;
166  QSize unrotatedSizeHint( const QFont& fnt = QFont() ) const;
167  QSize unrotatedTextSize( QFont fnt = QFont() ) const;
168  QSize calcSizeHint( const QFont& font ) const;
169  int marginWidth( const QSize& textSize ) const;
170 
171  qreal fitFontSizeToGeometry() const;
172 
173  QRect mRect;
174  QString mText;
175  Qt::Alignment mTextAlignment;
176  TextAttributes mAttributes;
177  const QObject* mAutoReferenceArea;
178  KChartEnums::MeasureOrientation mAutoReferenceOrientation;
179  mutable QSize cachedSizeHint;
180  mutable QPolygon mCachedBoundingPolygon;
181  mutable qreal cachedFontSize;
182  mutable QFont cachedFont;
183  };
184 
185  class KCHART_EXPORT TextBubbleLayoutItem : public AbstractLayoutItem
186  {
187  public:
188  TextBubbleLayoutItem();
189  TextBubbleLayoutItem( const QString& text,
190  const TextAttributes& attributes,
191  const QObject* autoReferenceArea,
192  KChartEnums::MeasureOrientation autoReferenceOrientation,
193  Qt::Alignment alignment = Qt::Alignment() );
194 
195  ~TextBubbleLayoutItem() override;
196 
197  void setAutoReferenceArea( const QObject* area );
198  const QObject* autoReferenceArea() const;
199 
200  void setText(const QString & text);
201  QString text() const;
202 
203  void setTextAttributes( const TextAttributes& a );
204  TextAttributes textAttributes() const;
205 
206  /** pure virtual in QLayoutItem */
207  bool isEmpty() const override;
208  /** pure virtual in QLayoutItem */
209  Qt::Orientations expandingDirections() const override;
210  /** pure virtual in QLayoutItem */
211  QSize maximumSize() const override;
212  /** pure virtual in QLayoutItem */
213  QSize minimumSize() const override;
214  /** pure virtual in QLayoutItem */
215  QSize sizeHint() const override;
216  /** pure virtual in QLayoutItem */
217  void setGeometry( const QRect& r ) override;
218  /** pure virtual in QLayoutItem */
219  QRect geometry() const override;
220 
221  void paint( QPainter* painter ) override;
222 
223  protected:
224  int borderWidth() const;
225 
226  private:
227  TextLayoutItem* const m_text;
228  };
229 
230  /**
231  * Layout item showing a data point marker
232  * \internal
233  */
234  class KCHART_EXPORT MarkerLayoutItem : public AbstractLayoutItem
235  {
236  public:
238  const MarkerAttributes& marker,
239  const QBrush& brush,
240  const QPen& pen,
241  Qt::Alignment alignment = Qt::Alignment() );
242 
243  Qt::Orientations expandingDirections() const override;
244  QRect geometry() const override;
245  bool isEmpty() const override;
246  QSize maximumSize() const override;
247  QSize minimumSize() const override;
248  void setGeometry( const QRect& r ) override;
249  QSize sizeHint() const override;
250 
251  void paint( QPainter* ) override;
252 
253  static void paintIntoRect(
254  QPainter* painter,
255  const QRect& rect,
256  AbstractDiagram* diagram,
257  const MarkerAttributes& marker,
258  const QBrush& brush,
259  const QPen& pen );
260 
261  private:
262  AbstractDiagram* mDiagram;
263  QRect mRect;
264  MarkerAttributes mMarker;
265  QBrush mBrush;
266  QPen mPen;
267  };
268 
269  /**
270  * Layout item showing a coloured line
271  * \internal
272  */
273  class KCHART_EXPORT LineLayoutItem : public AbstractLayoutItem
274  {
275  public:
277  int length,
278  const QPen& pen,
279  Qt::Alignment mLegendLineSymbolAlignment,
280  Qt::Alignment alignment = Qt::Alignment() );
281 
282  Qt::Orientations expandingDirections() const override;
283  QRect geometry() const override;
284  bool isEmpty() const override;
285  QSize maximumSize() const override;
286  QSize minimumSize() const override;
287  void setGeometry( const QRect& r ) override;
288  QSize sizeHint() const override;
289 
290  void setLegendLineSymbolAlignment(Qt::Alignment legendLineSymbolAlignment);
291  virtual Qt::Alignment legendLineSymbolAlignment() const;
292 
293  void paint( QPainter* ) override;
294 
295  static void paintIntoRect(
296  QPainter* painter,
297  const QRect& rect,
298  const QPen& pen,
299  Qt::Alignment lineAlignment);
300 
301  private:
302  AbstractDiagram* mDiagram; //TODO: not used. remove it
303  int mLength;
304  QPen mPen;
305  QRect mRect;
306  Qt::Alignment mLegendLineSymbolAlignment;
307  };
308 
309  /**
310  * Layout item showing a coloured line and a data point marker
311  * \internal
312  */
313  class KCHART_EXPORT LineWithMarkerLayoutItem : public AbstractLayoutItem
314  {
315  public:
317  int lineLength,
318  const QPen& linePen,
319  int markerOffs,
320  const MarkerAttributes& marker,
321  const QBrush& markerBrush,
322  const QPen& markerPen,
323  Qt::Alignment alignment = Qt::Alignment() );
324 
325  Qt::Orientations expandingDirections() const override;
326  QRect geometry() const override;
327  bool isEmpty() const override;
328  QSize maximumSize() const override;
329  QSize minimumSize() const override;
330  void setGeometry( const QRect& r ) override;
331  QSize sizeHint() const override;
332 
333  void paint( QPainter* ) override;
334 
335  private:
336  AbstractDiagram* mDiagram;
337  QRect mRect;
338  int mLineLength;
339  QPen mLinePen;
340  int mMarkerOffs;
341  MarkerAttributes mMarker;
342  QBrush mMarkerBrush;
343  QPen mMarkerPen;
344  };
345 
346 
347  /**
348  * Layout item showing a horizontal line
349  * \internal
350  */
351  class KCHART_EXPORT HorizontalLineLayoutItem : public AbstractLayoutItem
352  {
353  public:
355 
356  Qt::Orientations expandingDirections() const override;
357  QRect geometry() const override;
358  bool isEmpty() const override;
359  QSize maximumSize() const override;
360  QSize minimumSize() const override;
361  void setGeometry( const QRect& r ) override;
362  QSize sizeHint() const override;
363 
364  void paint( QPainter* ) override;
365 
366  private:
367  QRect mRect;
368  };
369 
370  /**
371  * Layout item showing a vertical line
372  * \internal
373  */
374  class KCHART_EXPORT VerticalLineLayoutItem : public AbstractLayoutItem
375  {
376  public:
378 
379  Qt::Orientations expandingDirections() const override;
380  QRect geometry() const override;
381  bool isEmpty() const override;
382  QSize maximumSize() const override;
383  QSize minimumSize() const override;
384  void setGeometry( const QRect& r ) override;
385  QSize sizeHint() const override;
386 
387  void paint( QPainter* ) override;
388 
389  private:
390  QRect mRect;
391  };
392 
393  /**
394  * @brief An empty layout item
395  * \internal
396  *
397  * The AutoSpacerLayoutItem is automatically put into each corner cell of
398  * the planeLayout grid: one of its reference-layouts is a QVBoxLayout (for
399  * the top, or bottom axes resp.), the other one is a QHBoxLayout (for the
400  * left/right sided axes).
401  *
402  * The spacer reserves enough space so all of the AbstractAreas contained
403  * in the two reference-layouts can display not only their in-bounds
404  * content but also their overlapping content reaching out of their area.
405  *
406  * KChart's layouting is applying this schema:
407 \verbatim
408  +------------------+-------------------------+-----------------+
409  | +--------------+ | +---------------------+ | +-------------+ |
410  | | | | | QVBoxLayout for | | | | |
411  | | AUTO | | | the top axis/axes | | | AUTO | |
412  | | SPACER | | +---------------------+ | | SPACER | |
413  | | ITEM | | | | | | ITEM | |
414  | | | | | | | | | |
415  | +--------------+ | +---------------------+ | +-------------+ |
416  +------------------+-------------------------+-----------------+
417  | +--------+-----+ | +---------------------+ | +-------+-----+ |
418  | | | | | | | | | | | |
419  | | | | | | | | | | | |
420  | | QHBox- | | | | | | | Right | | |
421  | | Layout | | | | | | | | | |
422  | | | | | | | | | axes | | |
423  | | for | | | | | | | | | |
424  | | | | | | | | | layout| | |
425  | | the | | | | DIAGRAM(s) | | | | | |
426  | | | | | | | | | | | |
427  | | left | | | | | | | | | |
428  | | | | | | | | | | | |
429  | | axis | | | | | | | | | |
430  | | or | | | | | | | | | |
431  | | axes | | | | | | | | | |
432  | | | | | | | | | | | |
433  | +--------+-----+ | +---------------------+ | +-------+-----+ |
434  +------------------+-------------------------+-----------------+
435  | +--------------+ | +---------------------+ | +-------------+ |
436  | | | | | QVBoxLayout for | | | | |
437  | | AUTO | | | the bottom axes | | | AUTO | |
438  | | SPACER | | +---------------------+ | | SPACER | |
439  | | ITEM | | | | | | ITEM | |
440  | | | | | | | | | |
441  | +--------------+ | +---------------------+ | +-------------+ |
442  +------------------+-------------------------+-----------------+
443 \endverbatim
444  *
445  * A typical use case is an Abscissa axis with long labels:
446 \verbatim
447  2 -|
448  |
449  1 -|
450  |
451  0 -+------------------------------------
452  | | | | |
453  Monday Tuesday Wednesday Thursday Friday
454 \endverbatim
455  * The last letters of the word "Friday" would have been
456  * cut off in previous versions of KChart - that is
457  * if you did not call KChart::Chart::setGlobalLeading().
458  *
459  * Now the word will be shown completely because there
460  * is an auto-spacer-item taking care for the additional
461  * space needed in the lower/right corner.
462  */
463  class KCHART_EXPORT AutoSpacerLayoutItem : public AbstractLayoutItem
464  {
465  public:
467  bool layoutIsAtTopPosition, QHBoxLayout *rightLeftLayout,
468  bool layoutIsAtLeftPosition, QVBoxLayout *topBottomLayout );
469 
470  Qt::Orientations expandingDirections() const override;
471  QRect geometry() const override;
472  bool isEmpty() const override;
473  QSize maximumSize() const override;
474  QSize minimumSize() const override;
475  void setGeometry( const QRect& r ) override;
476  QSize sizeHint() const override;
477 
478  void paint( QPainter* ) override;
479 
480  private:
481  QRect mRect;
482  bool mLayoutIsAtTopPosition;
483  QHBoxLayout *mRightLeftLayout;
484  bool mLayoutIsAtLeftPosition;
485  QVBoxLayout *mTopBottomLayout;
486 
487  mutable QBrush mCommonBrush;
488  mutable QSize mCachedSize;
489  };
490 
491 }
492 
493 #endif /* KCHARTLAYOUTITEMS_H */
Base class for all layout items of KChart.
typedef Alignment
Class only listed here to document inheritance of some KChart classes.
Stores information about painting diagrams.
Layout item showing a data point marker.
KTextDocument is an internally used enhanced QTextDocument.
Definition: KTextDocument.h:30
Layout item showing a text.
void removeWidget(QWidget *widget)
Layout item showing a coloured line and a data point marker.
MeasureOrientation
Measure orientation mode: the way how the absolute value of a KChart::Measure is determined during KC...
Definition: KChartEnums.h:282
typedef Orientations
Class only listed here to document inheritance of some KChart classes.
Layout item showing a horizontal line.
A set of attributes controlling the appearance of data set markers.
AbstractDiagram defines the interface for diagram classes.
Layout item showing a coloured line.
A set of text attributes.
Layout item showing a vertical line.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:54:30 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.