KChart

KChartAbstractAreaBase.cpp
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 #include "KChartAbstractAreaBase.h"
10 #include "KChartAbstractAreaBase_p.h"
11 
12 #include <KChartBackgroundAttributes.h>
13 #include <KChartFrameAttributes.h>
14 #include <KChartTextAttributes.h>
15 #include "KChartPainterSaver_p.h"
16 #include "KChartPrintingParameters.h"
17 #include "KChartMath_p.h"
18 
19 #include <QPainter>
20 #include <QPainterPath>
21 
22 
23 using namespace KChart;
24 
25 AbstractAreaBase::Private::Private() :
26  visible( true )
27 {
28  init();
29 }
30 
31 
32 AbstractAreaBase::Private::~Private() {}
33 
34 
35 void AbstractAreaBase::Private::init()
36 {
37 }
38 
39 
40 AbstractAreaBase::AbstractAreaBase() :
41  _d( new Private() )
42 {
43 }
44 
45 AbstractAreaBase::~AbstractAreaBase()
46 {
47  delete _d; _d = nullptr;
48 }
49 
50 
51 void AbstractAreaBase::init()
52 {
53 }
54 
55 
56 #define d d_func()
57 
58 bool AbstractAreaBase::compare( const AbstractAreaBase* other ) const
59 {
60  if ( other == this ) return true;
61  if ( !other ) {
62  return false;
63  }
64  return (frameAttributes() == other->frameAttributes()) &&
65  (backgroundAttributes() == other->backgroundAttributes());
66 }
67 
68 void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )
69 {
70  Q_UNUSED( position );
71  // PENDING(kalle) FIXME
72  qWarning( "Sorry, not implemented: void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )" );
73 }
74 
75 void AbstractAreaBase::setFrameAttributes( const FrameAttributes &a )
76 {
77  if ( d->frameAttributes == a )
78  return;
79 
80  d->frameAttributes = a;
82 }
83 
84 FrameAttributes AbstractAreaBase::frameAttributes() const
85 {
86  return d->frameAttributes;
87 }
88 
89 void AbstractAreaBase::setBackgroundAttributes( const BackgroundAttributes &a )
90 {
91  if ( d->backgroundAttributes == a )
92  return;
93 
94  d->backgroundAttributes = a;
96 }
97 
98 BackgroundAttributes AbstractAreaBase::backgroundAttributes() const
99 {
100  return d->backgroundAttributes;
101 }
102 
103 
104 /* static */
105 void AbstractAreaBase::paintBackgroundAttributes( QPainter& painter, const QRect& rect,
106  const KChart::BackgroundAttributes& attributes )
107 {
108  if ( !attributes.isVisible() ) return;
109 
110  /* first draw the brush (may contain a pixmap)*/
111  if ( Qt::NoBrush != attributes.brush().style() ) {
112  KChart::PainterSaver painterSaver( &painter );
113  painter.setPen( Qt::NoPen );
114  const QPointF newTopLeft( painter.deviceMatrix().map( rect.topLeft() ) );
115  painter.setBrushOrigin( newTopLeft );
116  painter.setBrush( attributes.brush() );
117  painter.drawRect( rect.adjusted( 0, 0, -1, -1 ) );
118  }
119  /* next draw the backPixmap over the brush */
120  if ( !attributes.pixmap().isNull() &&
121  attributes.pixmapMode() != BackgroundAttributes::BackgroundPixmapModeNone ) {
122  QPointF ol = rect.topLeft();
123  if ( BackgroundAttributes::BackgroundPixmapModeCentered == attributes.pixmapMode() )
124  {
125  ol.setX( rect.center().x() - attributes.pixmap().width() / 2 );
126  ol.setY( rect.center().y() - attributes.pixmap().height()/ 2 );
127  painter.drawPixmap( ol, attributes.pixmap() );
128  } else {
129  QMatrix m;
130  qreal zW = (qreal)rect.width() / (qreal)attributes.pixmap().width();
131  qreal zH = (qreal)rect.height() / (qreal)attributes.pixmap().height();
132  switch ( attributes.pixmapMode() ) {
133  case BackgroundAttributes::BackgroundPixmapModeScaled:
134  {
135  qreal z;
136  z = qMin( zW, zH );
137  m.scale( z, z );
138  }
139  break;
140  case BackgroundAttributes::BackgroundPixmapModeStretched:
141  m.scale( zW, zH );
142  break;
143  default:
144  ; // Cannot happen, previously checked
145  }
146  QPixmap pm = attributes.pixmap().transformed( m );
147  ol.setX( rect.center().x() - pm.width() / 2 );
148  ol.setY( rect.center().y() - pm.height()/ 2 );
149  painter.drawPixmap( ol, pm );
150  }
151  }
152 }
153 
154 /* static */
155 void AbstractAreaBase::paintFrameAttributes( QPainter& painter, const QRect& rect,
156  const KChart::FrameAttributes& attributes )
157 {
158 
159  if ( !attributes.isVisible() ) return;
160 
161  // Note: We set the brush to NoBrush explicitly here.
162  // Otherwise we might get a filled rectangle, so any
163  // previously drawn background would be overwritten by that area.
164 
165  const QPen oldPen( painter.pen() );
166  const QBrush oldBrush( painter.brush() );
167 
168  painter.setPen( PrintingParameters::scalePen( attributes.pen() ) );
169  painter.setBrush( Qt::NoBrush );
170  painter.drawRoundedRect( rect.adjusted( 0, 0, -1, -1 ), attributes.cornerRadius(), attributes.cornerRadius() );
171 
172  painter.setBrush( oldBrush );
173  painter.setPen( oldPen );
174 }
175 
176 void AbstractAreaBase::paintBackground( QPainter& painter, const QRect& rect )
177 {
178  Q_ASSERT_X ( d != nullptr, "AbstractAreaBase::paintBackground()",
179  "Private class was not initialized!" );
180 
181  PainterSaver painterSaver( &painter );
182 
183  const qreal radius = d->frameAttributes.cornerRadius();
184  QPainterPath path;
185  path.addRoundedRect( rect.adjusted( 0, 0, -1, -1 ), radius, radius );
186  painter.setClipPath(path);
187 
188  paintBackgroundAttributes( painter, rect, d->backgroundAttributes );
189 }
190 
191 
192 void AbstractAreaBase::paintFrame( QPainter& painter, const QRect& rect )
193 {
194  Q_ASSERT_X ( d != nullptr, "AbstractAreaBase::paintFrame()",
195  "Private class was not initialized!" );
196  paintFrameAttributes( painter, rect, d->frameAttributes );
197 }
198 
199 
200 void AbstractAreaBase::getFrameLeadings(int& left, int& top, int& right, int& bottom ) const
201 {
202  int padding = 0;
203  if ( d && d->frameAttributes.isVisible() ) {
204  padding = qMax( d->frameAttributes.padding(), 0 );
205  }
206  left = padding;
207  top = padding;
208  right = padding;
209  bottom = padding;
210 }
211 
213 {
214  int left;
215  int top;
216  int right;
217  int bottom;
218  getFrameLeadings( left, top, right, bottom );
219  return QRect ( QPoint( 0, 0 ), areaGeometry().size() ).adjusted( left, top, -right, -bottom );
220 }
221 
223 {
224  // this block left empty intentionally
225 }
Defines relative position information: reference area, position in this area (reference position)...
int width() const const
void setClipPath(const QPainterPath &path, Qt::ClipOperation operation)
Qt::BrushStyle style() const const
void getFrameLeadings(int &left, int &top, int &right, int &bottom) const
QRect areaGeometry() const override
const QMatrix & deviceMatrix() const const
void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
bool compare(const AbstractAreaBase *other) const
Returns true if both areas have the same settings.
int height() const const
void positionHasChanged() override
void setBrushOrigin(int x, int y)
int x() const const
int y() const const
void drawRect(const QRectF &rectangle)
void setPen(const QColor &color)
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPixmap transformed(const QMatrix &matrix, Qt::TransformationMode mode) const const
void setBrush(const QBrush &brush)
QPoint center() const const
Base class for AbstractArea and AbstractAreaWidget: An area in the chart with a background, a frame, etc.
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
bool isNull() const const
const QBrush & brush() const const
int height() const const
Set of attributes usable for background pixmaps.
QCA_EXPORT void init()
int width() const const
void setX(qreal x)
void setY(qreal y)
QPoint topLeft() const const
Global namespace.
void map(int x, int y, int *tx, int *ty) const const
const QPen & pen() const const
A set of attributes for frames around items.
QMatrix & scale(qreal sx, qreal sy)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 22:33:22 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.