KD Chart 2  [rev.2.6]
KDChartAbstractAreaBase.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2018 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 
34 
35 using namespace KDChart;
36 
37 AbstractAreaBase::Private::Private() :
38  visible( true )
39 {
40  init();
41 }
42 
43 
44 AbstractAreaBase::Private::~Private() {}
45 
46 
47 void AbstractAreaBase::Private::init()
48 {
49 }
50 
51 
53  _d( new Private() )
54 {
55 }
56 
58 {
59  delete _d; _d = 0;
60 }
61 
62 
63 void AbstractAreaBase::init()
64 {
65 }
66 
67 
68 #define d d_func()
69 
70 bool AbstractAreaBase::compare( const AbstractAreaBase* other ) const
71 {
72  if ( other == this ) return true;
73  if ( !other ) {
74  return false;
75  }
76  return (frameAttributes() == other->frameAttributes()) &&
78 }
79 
81 {
82  Q_UNUSED( position );
83  // PENDING(kalle) FIXME
84  qWarning( "Sorry, not implemented: void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )" );
85 }
86 
88 {
89  if ( d->frameAttributes == a )
90  return;
91 
92  d->frameAttributes = a;
94 }
95 
97 {
98  return d->frameAttributes;
99 }
100 
102 {
103  if ( d->backgroundAttributes == a )
104  return;
105 
106  d->backgroundAttributes = a;
108 }
109 
111 {
112  return d->backgroundAttributes;
113 }
114 
115 
116 /* static */
117 void AbstractAreaBase::paintBackgroundAttributes( QPainter& painter, const QRect& rect,
118  const KDChart::BackgroundAttributes& attributes )
119 {
120  if ( !attributes.isVisible() ) return;
121 
122  /* first draw the brush (may contain a pixmap)*/
123  if ( Qt::NoBrush != attributes.brush().style() ) {
124  KDChart::PainterSaver painterSaver( &painter );
125  painter.setPen( Qt::NoPen );
126  const QPointF newTopLeft( painter.deviceMatrix().map( rect.topLeft() ) );
127  painter.setBrushOrigin( newTopLeft );
128  painter.setBrush( attributes.brush() );
129  painter.drawRect( rect.adjusted( 0, 0, -1, -1 ) );
130  }
131  /* next draw the backPixmap over the brush */
132  if ( !attributes.pixmap().isNull() &&
134  QPointF ol = rect.topLeft();
136  {
137  ol.setX( rect.center().x() - attributes.pixmap().width() / 2 );
138  ol.setY( rect.center().y() - attributes.pixmap().height()/ 2 );
139  painter.drawPixmap( ol, attributes.pixmap() );
140  } else {
141  QMatrix m;
142  qreal zW = (qreal)rect.width() / (qreal)attributes.pixmap().width();
143  qreal zH = (qreal)rect.height() / (qreal)attributes.pixmap().height();
144  switch ( attributes.pixmapMode() ) {
146  {
147  qreal z;
148  z = qMin( zW, zH );
149  m.scale( z, z );
150  }
151  break;
153  m.scale( zW, zH );
154  break;
155  default:
156  ; // Cannot happen, previously checked
157  }
158  QPixmap pm = attributes.pixmap().transformed( m );
159  ol.setX( rect.center().x() - pm.width() / 2 );
160  ol.setY( rect.center().y() - pm.height()/ 2 );
161  painter.drawPixmap( ol, pm );
162  }
163  }
164 }
165 
166 /* static */
167 void AbstractAreaBase::paintFrameAttributes( QPainter& painter, const QRect& rect,
168  const KDChart::FrameAttributes& attributes )
169 {
170 
171  if ( !attributes.isVisible() ) return;
172 
173  // Note: We set the brush to NoBrush explicitly here.
174  // Otherwise we might get a filled rectangle, so any
175  // previously drawn background would be overwritten by that area.
176 
177  const QPen oldPen( painter.pen() );
178  const QBrush oldBrush( painter.brush() );
179 
180  painter.setPen( PrintingParameters::scalePen( attributes.pen() ) );
181  painter.setBrush( Qt::NoBrush );
182  painter.drawRoundedRect( rect.adjusted( 0, 0, -1, -1 ), attributes.cornerRadius(), attributes.cornerRadius() );
183 
184  painter.setBrush( oldBrush );
185  painter.setPen( oldPen );
186 }
187 
188 void AbstractAreaBase::paintBackground( QPainter& painter, const QRect& rect )
189 {
190  Q_ASSERT_X ( d != 0, "AbstractAreaBase::paintBackground()",
191  "Private class was not initialized!" );
192 
193  PainterSaver painterSaver( &painter );
194 
195  const qreal radius = d->frameAttributes.cornerRadius();
196  QPainterPath path;
197  path.addRoundedRect( rect.adjusted( 0, 0, -1, -1 ), radius, radius );
198  painter.setClipPath(path);
199 
200  paintBackgroundAttributes( painter, rect, d->backgroundAttributes );
201 }
202 
203 
204 void AbstractAreaBase::paintFrame( QPainter& painter, const QRect& rect )
205 {
206  Q_ASSERT_X ( d != 0, "AbstractAreaBase::paintFrame()",
207  "Private class was not initialized!" );
208  paintFrameAttributes( painter, rect, d->frameAttributes );
209 }
210 
211 
212 void AbstractAreaBase::getFrameLeadings(int& left, int& top, int& right, int& bottom ) const
213 {
214  int padding = 0;
215  if ( d && d->frameAttributes.isVisible() ) {
216  padding = qMax( d->frameAttributes.padding(), 0 );
217  }
218  left = padding;
219  top = padding;
220  right = padding;
221  bottom = padding;
222 }
223 
225 {
226  int left;
227  int top;
228  int right;
229  int bottom;
230  getFrameLeadings( left, top, right, bottom );
231  return QRect ( QPoint( 0, 0 ), areaGeometry().size() ).adjusted( left, top, -right, -bottom );
232 }
233 
235 {
236  // this bloc left empty intentionally
237 }
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)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/