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

KDE's Doxygen guidelines are available online.