KD Chart 2  [rev.2.7]
KDChartAbstractAreaWidget.cpp
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 
24 #include "KDChartAbstractAreaWidget_p.h"
25 
26 #include <KDABLibFakes>
27 
28 
29 using namespace KDChart;
30 
31 
32 AbstractAreaWidget::Private::Private()
33 {
34  // this block left empty intentionally
35 }
36 
37 AbstractAreaWidget::Private::~Private()
38 {
39  // this block left empty intentionally
40 }
41 
42 
43 void AbstractAreaWidget::Private::resizeLayout(
44  AbstractAreaWidget* widget, const QSize& size )
45 {
46  if ( size == currentLayoutSize ) return;
47 
48  currentLayoutSize = size;
49 
50  // Now we call adjust the size, for the inner parts of the widget.
51  int left;
52  int top;
53  int right;
54  int bottom;
55  widget->getFrameLeadings( left, top, right, bottom );
56  const QSize innerSize( size.width() - left - right,
57  size.height() - top - bottom );
58  // With this adjusted size we call the real resizeLayout method,
59  // which normally will call resizeLayout( size ) in the derived class
60  // - which in turn is the place to resize the layout of that class.
61  widget->resizeLayout( innerSize );
62 }
63 
64 
66  : QWidget( parent )
67  , AbstractAreaBase( new Private() )
68 {
69  init();
70 }
71 
73 {
74  // this block left empty intentionally
75 }
76 
77 void AbstractAreaWidget::init()
78 {
79  // this block left empty intentionally
80 }
81 
83 {
84  // this block left empty intentionally
85 }
86 
87 #define d d_func()
88 
89 void AbstractAreaWidget::resizeLayout( const QSize& size )
90 {
91  Q_UNUSED( size );
92  // this block left empty intentionally
93 }
94 
95 void AbstractAreaWidget::paintEvent( QPaintEvent* event )
96 {
97  Q_UNUSED( event );
98  QPainter painter( this );
99  if ( size() != d->currentLayoutSize ) {
100  d->resizeLayout( this, size() );
101  }
102  paintAll( painter );
103 }
104 
105 void AbstractAreaWidget::paintIntoRect( QPainter& painter, const QRect& rect )
106 {
107  if ( rect.isEmpty() ) return;
108 
109  d->resizeLayout( this, rect.size() );
110 
111  const QPoint translation( rect.topLeft() );
112  painter.translate( translation );
113  paintAll( painter );
114  painter.translate( -translation.x(), -translation.y() );
115 
116 /*
117  // guide for subclassing
118 
119  // set up the contents of the widget so we get a useful geometry
120  needSizeHint();
121 
122  const QRect oldGeometry( layout()->geometry() );
123  const QRect newGeo( QPoint(0,0), rect.size() );
124  const bool mustChangeGeo = layout() && oldGeometry != newGeo;
125  if ( mustChangeGeo )
126  layout()->setGeometry( newGeo );
127  painter.translate( rect.left(), rect.top() );
128  paintAll( painter );
129  painter.translate( -rect.left(), -rect.top() );
130  if ( mustChangeGeo )
131  layout()->setGeometry( oldGeometry );
132 */
133 }
134 
136 {
137  //bloc left empty intentionally
138 }
139 
140 void AbstractAreaWidget::paintAll( QPainter& painter )
141 {
142  paintBackground( painter, QRect(QPoint(0, 0), size() ) );
143  paintFrame( painter, QRect(QPoint(0, 0), size() ) );
144 
145 /*
146  // guide for subclassing
147 
148  // we do not call setContentsMargins() now,
149  // but we call resizeLayout() whenever the size or the frame has changed
150 
151  // adjust the widget's content margins,
152  // to be sure all content gets calculated
153  // to fit into the inner rectangle
154  const QRect oldGeometry( areaGeometry() );
155  const QRect inner( innerRect() );
156  //qDebug() << "areaGeometry():" << oldGeometry
157  // << " contentsRect():" << contentsRect() << " inner:" << inner;
158  if ( contentsRect() != inner ) {
159  //qDebug() << "old contentsRect():" << contentsRect() << " new innerRect:" << inner;
160  setContentsMargins(
161  inner.left(),
162  inner.top(),
163  oldGeometry.width() - inner.width() - 1,
164  oldGeometry.height() - inner.height() - 1 );
165  //forceRebuild();
166  }
167 */
168  int left;
169  int top;
170  int right;
171  int bottom;
172  getFrameLeadings( left, top, right, bottom );
173  const QPoint translation( left, top );
174  painter.translate( translation );
175  paint( &painter );
176  painter.translate( -translation.x(), -translation.y() );
177 }
178 
180 {
181  return geometry();
182 }
183 
185 {
186  emit positionChanged( this );
187 }
virtual void paintIntoRect(QPainter &painter, const QRect &rect)
Draws the background and frame, then calls paint().
virtual void needSizeHint()
Call this to trigger an conditional re-building of the widget&#39;s internals.
virtual void paintFrame(QPainter &painter, const QRect &rectangle)
void positionChanged(AbstractAreaWidget *)
Base class for AbstractArea and AbstractAreaWidget: An area in the chart with a background, a frame, etc.
virtual void forceRebuild()
Call this to trigger an unconditional re-building of the widget&#39;s internals.
void getFrameLeadings(int &left, int &top, int &right, int &bottom) const
virtual void paintBackground(QPainter &painter, const QRect &rectangle)
An area in the chart with a background, a frame, etc.
void paintAll(QPainter &painter)
Call paintAll, if you want the background and the frame to be drawn before the normal paint() is invo...
virtual void resizeLayout(const QSize &)
virtual void paint(QPainter *painter)=0
Overwrite this to paint the inner contents of your widget.
void paintEvent(QPaintEvent *event) override
Draws the background and frame, then calls paint().
Class only listed here to document inheritance of some KDChart classes.

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/