KD Chart API Documentation  3.1
KDChartAbstractAreaBase.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
12 #include "KDChartAbstractAreaBase_p.h"
13 #include "KDChartPainterSaver_p.h"
16 #include <KDChartFrameAttributes.h>
17 #include <KDChartTextAttributes.h>
18 #include <QPainter>
19 
20 #include <KDABLibFakes>
21 #include <QPainterPath>
22 
23 using namespace KDChart;
24 
25 AbstractAreaBase::Private::Private()
26 {
27  init();
28 }
29 
30 AbstractAreaBase::Private::~Private()
31 {
32 }
33 
34 void AbstractAreaBase::Private::init()
35 {
36 }
37 
39  : _d(new Private())
40 {
41 }
42 
44 {
45  delete _d;
46  _d = nullptr;
47 }
48 
49 void AbstractAreaBase::init()
50 {
51 }
52 
53 #define d d_func()
54 
56 {
57  if (other == this)
58  return true;
59  if (!other) {
60  return false;
61  }
62  return (frameAttributes() == other->frameAttributes()) && (backgroundAttributes() == other->backgroundAttributes());
63 }
64 
66 {
67  Q_UNUSED(position);
68  // PENDING(kalle) FIXME
69  qWarning("Sorry, not implemented: void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )");
70 }
71 
73 {
74  if (d->frameAttributes == a)
75  return;
76 
77  d->frameAttributes = a;
79 }
80 
82 {
83  return d->frameAttributes;
84 }
85 
87 {
88  if (d->backgroundAttributes == a)
89  return;
90 
91  d->backgroundAttributes = a;
93 }
94 
96 {
97  return d->backgroundAttributes;
98 }
99 
100 /* static */
101 void AbstractAreaBase::paintBackgroundAttributes(QPainter &painter, const QRect &rect,
102  const KDChart::BackgroundAttributes &attributes)
103 {
104  if (!attributes.isVisible())
105  return;
106 
107  /* first draw the brush (may contain a pixmap)*/
108  if (Qt::NoBrush != attributes.brush().style()) {
109  KDChart::PainterSaver painterSaver(&painter);
110  painter.setPen(Qt::NoPen);
111  const QPointF newTopLeft(painter.deviceTransform().map(rect.topLeft()));
112  painter.setBrushOrigin(newTopLeft);
113  painter.setBrush(attributes.brush());
114  painter.drawRect(rect.adjusted(0, 0, -1, -1));
115  }
116  /* next draw the backPixmap over the brush */
117  if (!attributes.pixmap().isNull() && attributes.pixmapMode() != BackgroundAttributes::BackgroundPixmapModeNone) {
118  QPointF ol = rect.topLeft();
120  ol.setX(rect.center().x() - attributes.pixmap().width() / 2);
121  ol.setY(rect.center().y() - attributes.pixmap().height() / 2);
122  painter.drawPixmap(ol, attributes.pixmap());
123  } else {
124  QTransform m;
125  qreal zW = ( qreal )rect.width() / ( qreal )attributes.pixmap().width();
126  qreal zH = ( qreal )rect.height() / ( qreal )attributes.pixmap().height();
127  switch (attributes.pixmapMode()) {
129  qreal z;
130  z = qMin(zW, zH);
131  m.scale(z, z);
132  } break;
134  m.scale(zW, zH);
135  break;
136  default:; // Cannot happen, previously checked
137  }
138  QPixmap pm = attributes.pixmap().transformed(m);
139  ol.setX(rect.center().x() - pm.width() / 2);
140  ol.setY(rect.center().y() - pm.height() / 2);
141  painter.drawPixmap(ol, pm);
142  }
143  }
144 }
145 
146 /* static */
147 void AbstractAreaBase::paintFrameAttributes(QPainter &painter, const QRect &rect,
148  const KDChart::FrameAttributes &attributes)
149 {
150  if (!attributes.isVisible())
151  return;
152 
153  // Note: We set the brush to NoBrush explicitly here.
154  // Otherwise we might get a filled rectangle, so any
155  // previously drawn background would be overwritten by that area.
156 
157  const QPen oldPen(painter.pen());
158  const QBrush oldBrush(painter.brush());
159 
160  painter.setPen(PrintingParameters::scalePen(attributes.pen()));
161  painter.setBrush(Qt::NoBrush);
162  painter.drawRoundedRect(rect.adjusted(0, 0, -1, -1), attributes.cornerRadius(), attributes.cornerRadius());
163 
164  painter.setBrush(oldBrush);
165  painter.setPen(oldPen);
166 }
167 
168 void AbstractAreaBase::paintBackground(QPainter &painter, const QRect &rect)
169 {
170  Q_ASSERT_X(d != nullptr, "AbstractAreaBase::paintBackground()",
171  "Private class was not initialized!");
172 
173  PainterSaver painterSaver(&painter);
174 
175  const qreal radius = d->frameAttributes.cornerRadius();
176  QPainterPath path;
177  path.addRoundedRect(rect.adjusted(0, 0, -1, -1), radius, radius);
178  painter.setClipPath(path);
179 
180  paintBackgroundAttributes(painter, rect, d->backgroundAttributes);
181 }
182 
183 void AbstractAreaBase::paintFrame(QPainter &painter, const QRect &rect)
184 {
185  Q_ASSERT_X(d != nullptr, "AbstractAreaBase::paintFrame()",
186  "Private class was not initialized!");
187  paintFrameAttributes(painter, rect, d->frameAttributes);
188 }
189 
190 void AbstractAreaBase::getFrameLeadings(int &left, int &top, int &right, int &bottom) const
191 {
192  int padding = 0;
193  if (d && d->frameAttributes.isVisible()) {
194  padding = qMax(d->frameAttributes.padding(), 0);
195  }
196  left = padding;
197  top = padding;
198  right = padding;
199  bottom = padding;
200 }
201 
203 {
204  int left;
205  int top;
206  int right;
207  int bottom;
208  getFrameLeadings(left, top, right, bottom);
209  return QRect(QPoint(0, 0), areaGeometry().size()).adjusted(left, top, -right, -bottom);
210 }
211 
213 {
214  // this block left empty intentionally
215 }
#define d
Base class for AbstractArea and AbstractAreaWidget: An area in the chart with a background,...
void setFrameAttributes(const FrameAttributes &a)
void getFrameLeadings(int &left, int &top, int &right, int &bottom) const
BackgroundAttributes backgroundAttributes() const
void alignToReferencePoint(const RelativePosition &position)
void setBackgroundAttributes(const BackgroundAttributes &a)
static void paintFrameAttributes(QPainter &painter, const QRect &rectangle, const KDChart::FrameAttributes &attributes)
virtual void paintFrame(QPainter &painter, const QRect &rectangle)
virtual void paintBackground(QPainter &painter, const QRect &rectangle)
bool compare(const AbstractAreaBase *other) const
static void paintBackgroundAttributes(QPainter &painter, const QRect &rectangle, const KDChart::BackgroundAttributes &attributes)
virtual QRect areaGeometry() const =0
FrameAttributes frameAttributes() const
BackgroundPixmapMode pixmapMode() const
A set of attributes for frames around items.
static QPen scalePen(const QPen &pen)
Defines relative position information: reference area, position in this area (reference position),...

© 2001 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/
Generated by doxygen 1.9.1