KD Chart 2  [rev.2.7]
KDChartAbstractAreaBase.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 "KDChartAbstractAreaBase_p.h"
26 #include <KDChartFrameAttributes.h>
27 #include <KDChartTextAttributes.h>
28 #include "KDChartPainterSaver_p.h"
30 #include <QPainter>
31 
32 #include <KDABLibFakes>
33 #include <QPainterPath>
34 
35 
36 using namespace KDChart;
37 
38 AbstractAreaBase::Private::Private() :
39  visible( true )
40 {
41  init();
42 }
43 
44 
45 AbstractAreaBase::Private::~Private() {}
46 
47 
48 void AbstractAreaBase::Private::init()
49 {
50 }
51 
52 
54  _d( new Private() )
55 {
56 }
57 
59 {
60  delete _d; _d = 0;
61 }
62 
63 
64 void AbstractAreaBase::init()
65 {
66 }
67 
68 
69 #define d d_func()
70 
71 bool AbstractAreaBase::compare( const AbstractAreaBase* other ) const
72 {
73  if ( other == this ) return true;
74  if ( !other ) {
75  return false;
76  }
77  return (frameAttributes() == other->frameAttributes()) &&
79 }
80 
82 {
83  Q_UNUSED( position );
84  // PENDING(kalle) FIXME
85  qWarning( "Sorry, not implemented: void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )" );
86 }
87 
89 {
90  if ( d->frameAttributes == a )
91  return;
92 
93  d->frameAttributes = a;
95 }
96 
98 {
99  return d->frameAttributes;
100 }
101 
103 {
104  if ( d->backgroundAttributes == a )
105  return;
106 
107  d->backgroundAttributes = a;
109 }
110 
112 {
113  return d->backgroundAttributes;
114 }
115 
116 
117 /* static */
118 void AbstractAreaBase::paintBackgroundAttributes( QPainter& painter, const QRect& rect,
119  const KDChart::BackgroundAttributes& attributes )
120 {
121  if ( !attributes.isVisible() ) return;
122 
123  /* first draw the brush (may contain a pixmap)*/
124  if ( Qt::NoBrush != attributes.brush().style() ) {
125  KDChart::PainterSaver painterSaver( &painter );
126  painter.setPen( Qt::NoPen );
127  const QPointF newTopLeft( painter.deviceMatrix().map( rect.topLeft() ) );
128  painter.setBrushOrigin( newTopLeft );
129  painter.setBrush( attributes.brush() );
130  painter.drawRect( rect.adjusted( 0, 0, -1, -1 ) );
131  }
132  /* next draw the backPixmap over the brush */
133  if ( !attributes.pixmap().isNull() &&
135  QPointF ol = rect.topLeft();
137  {
138  ol.setX( rect.center().x() - attributes.pixmap().width() / 2 );
139  ol.setY( rect.center().y() - attributes.pixmap().height()/ 2 );
140  painter.drawPixmap( ol, attributes.pixmap() );
141  } else {
142  QMatrix m;
143  qreal zW = (qreal)rect.width() / (qreal)attributes.pixmap().width();
144  qreal zH = (qreal)rect.height() / (qreal)attributes.pixmap().height();
145  switch ( attributes.pixmapMode() ) {
147  {
148  qreal z;
149  z = qMin( zW, zH );
150  m.scale( z, z );
151  }
152  break;
154  m.scale( zW, zH );
155  break;
156  default:
157  ; // Cannot happen, previously checked
158  }
159  QPixmap pm = attributes.pixmap().transformed( m );
160  ol.setX( rect.center().x() - pm.width() / 2 );
161  ol.setY( rect.center().y() - pm.height()/ 2 );
162  painter.drawPixmap( ol, pm );
163  }
164  }
165 }
166 
167 /* static */
168 void AbstractAreaBase::paintFrameAttributes( QPainter& painter, const QRect& rect,
169  const KDChart::FrameAttributes& attributes )
170 {
171 
172  if ( !attributes.isVisible() ) return;
173 
174  // Note: We set the brush to NoBrush explicitly here.
175  // Otherwise we might get a filled rectangle, so any
176  // previously drawn background would be overwritten by that area.
177 
178  const QPen oldPen( painter.pen() );
179  const QBrush oldBrush( painter.brush() );
180 
181  painter.setPen( PrintingParameters::scalePen( attributes.pen() ) );
182  painter.setBrush( Qt::NoBrush );
183  painter.drawRoundedRect( rect.adjusted( 0, 0, -1, -1 ), attributes.cornerRadius(), attributes.cornerRadius() );
184 
185  painter.setBrush( oldBrush );
186  painter.setPen( oldPen );
187 }
188 
189 void AbstractAreaBase::paintBackground( QPainter& painter, const QRect& rect )
190 {
191  Q_ASSERT_X ( d != 0, "AbstractAreaBase::paintBackground()",
192  "Private class was not initialized!" );
193 
194  PainterSaver painterSaver( &painter );
195 
196  const qreal radius = d->frameAttributes.cornerRadius();
197  QPainterPath path;
198  path.addRoundedRect( rect.adjusted( 0, 0, -1, -1 ), radius, radius );
199  painter.setClipPath(path);
200 
201  paintBackgroundAttributes( painter, rect, d->backgroundAttributes );
202 }
203 
204 
205 void AbstractAreaBase::paintFrame( QPainter& painter, const QRect& rect )
206 {
207  Q_ASSERT_X ( d != 0, "AbstractAreaBase::paintFrame()",
208  "Private class was not initialized!" );
209  paintFrameAttributes( painter, rect, d->frameAttributes );
210 }
211 
212 
213 void AbstractAreaBase::getFrameLeadings(int& left, int& top, int& right, int& bottom ) const
214 {
215  int padding = 0;
216  if ( d && d->frameAttributes.isVisible() ) {
217  padding = qMax( d->frameAttributes.padding(), 0 );
218  }
219  left = padding;
220  top = padding;
221  right = padding;
222  bottom = padding;
223 }
224 
226 {
227  int left;
228  int top;
229  int right;
230  int bottom;
231  getFrameLeadings( left, top, right, bottom );
232  return QRect ( QPoint( 0, 0 ), areaGeometry().size() ).adjusted( left, top, -right, -bottom );
233 }
234 
236 {
237  // this bloc left empty intentionally
238 }
static void paintFrameAttributes(QPainter &painter, const QRect &rectangle, const KDChart::FrameAttributes &attributes)
static void paintBackgroundAttributes(QPainter &painter, const QRect &rectangle, const KDChart::BackgroundAttributes &attributes)
BackgroundAttributes backgroundAttributes() const
virtual void paintFrame(QPainter &painter, const QRect &rectangle)
A set of attributes for frames around items.
Base class for AbstractArea and AbstractAreaWidget: An area in the chart with a background, a frame, etc.
void getFrameLeadings(int &left, int &top, int &right, int &bottom) const
virtual void paintBackground(QPainter &painter, const QRect &rectangle)
bool compare(const AbstractAreaBase *other) const
Returns true if both areas have the same settings.
virtual QRect areaGeometry() const =0
Defines relative position information: reference area, position in this area (reference position)...
static QPen scalePen(const QPen &pen)
void setBackgroundAttributes(const BackgroundAttributes &a)
FrameAttributes frameAttributes() const
BackgroundPixmapMode pixmapMode() const
void setFrameAttributes(const FrameAttributes &a)
#define d
Set of attributes usable for background pixmaps.
void alignToReferencePoint(const RelativePosition &position)

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/