KD Chart API Documentation  3.1
KDChartRelativePosition.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 
13 #include "KDChartAbstractArea.h"
14 #include "KDChartEnums.h"
15 #include "KDChartMeasure.h"
16 #include "KDChartPosition.h"
17 
18 #include <QLayout>
19 #include <QWidget>
20 
21 #include <KDABLibFakes>
22 
23 using namespace KDChart;
24 
25 class RelativePosition::Private
26 {
27  friend class ::KDChart::RelativePosition;
28 
29 public:
30  Private();
31  ~Private();
32 
33 private:
34  QObject *area = nullptr;
35  PositionPoints points;
36  Position position;
37  Qt::Alignment alignment;
40  qreal rotation = 0;
41 };
42 
43 RelativePosition::Private::Private()
44  : alignment(Qt::AlignCenter)
45 {
46 }
47 
48 RelativePosition::Private::~Private()
49 {
50 }
51 
52 RelativePosition::RelativePosition()
53  : _d(new Private)
54 {
55 }
56 
58  : _d(new Private(*r._d))
59 {
60 }
61 
63 {
64  RelativePosition copy(other);
65  copy.swap(*this);
66  return *this;
67 }
68 
70 {
71  delete _d;
72 }
73 
74 #define d d_func()
75 
77 {
78  d->area = area;
79  if (area)
81 }
82 
84 {
85  return d->area;
86 }
87 
89 {
90  d->points = points;
91  if (!points.isNull())
92  setReferenceArea(nullptr);
93 }
95 {
96  return d->points;
97 }
98 
100 {
101  d->position = pos;
102 }
103 
105 {
106  d->position = Position::Unknown;
107 }
108 
110 {
111  return d->position;
112 }
113 
114 void RelativePosition::setAlignment(Qt::Alignment align)
115 {
116  d->alignment = align;
117 }
118 
119 Qt::Alignment RelativePosition::alignment() const
120 {
121  return d->alignment;
122 }
123 
125 {
126  d->horizontalPadding = pad;
127 }
128 
130 {
131  return d->horizontalPadding;
132 }
133 
135 {
136  d->verticalPadding = pad;
137 }
138 
140 {
141  return d->verticalPadding;
142 }
143 
145 {
146  d->rotation = rot;
147 }
148 
150 {
151  return d->rotation;
152 }
153 
154 const QPointF RelativePosition::referencePoint(qreal *polarDegrees) const
155 {
156  bool useRect = (d->area != nullptr);
157  QRect rect;
158  if (useRect) {
159  if (const auto *widget = qobject_cast<const QWidget *>(d->area)) {
160  const QLayout *layout = widget->layout();
161  rect = layout ? layout->geometry() : widget->geometry();
162  } else if (const auto *kdcArea = qobject_cast<const AbstractArea *>(d->area)) {
163  rect = kdcArea->geometry();
164  } else {
165  useRect = false;
166  }
167  }
168 
169  QPointF pt;
170  qreal angle = 0.0;
171  if (useRect) {
172  pt = PositionPoints(rect).point(d->position);
173  } else {
174  pt = d->points.point(d->position);
175  angle = d->points.degrees(d->position.value());
176  }
177 
178  if (polarDegrees) {
179  *polarDegrees = angle;
180  }
181  return pt;
182 }
183 
184 const QPointF RelativePosition::calculatedPoint(const QSizeF &autoSize) const
185 {
188 
189  qreal polarDegrees;
190  QPointF pt(referencePoint(&polarDegrees));
191  if (polarDegrees == 0.0) {
192  pt += QPointF(dx, dy);
193  } else {
194  const qreal rad = DEGTORAD(polarDegrees);
195  const qreal sinDeg = sin(rad);
196  const qreal cosDeg = cos(rad);
197  pt.setX(pt.x() + dx * cosDeg + dy * sinDeg);
198  pt.setY(pt.y() - dx * sinDeg + dy * cosDeg);
199  }
200  return pt;
201 }
202 
204 {
205  return d->area == r.referenceArea() && d->position == r.referencePosition() && d->alignment == r.alignment() && d->horizontalPadding == r.horizontalPadding() && d->verticalPadding == r.verticalPadding() && d->rotation == r.rotation();
206 }
207 
208 #undef d
209 
210 #if !defined(QT_NO_DEBUG_STREAM)
211 QDebug operator<<(QDebug dbg, const KDChart::RelativePosition &rp)
212 {
213  dbg << "KDChart::RelativePosition("
214  << "referencearea=" << rp.referenceArea()
215  << "referenceposition=" << rp.referencePosition()
216  << "alignment=" << rp.alignment()
217  << "horizontalpadding=" << rp.horizontalPadding()
218  << "verticalpadding=" << rp.verticalPadding()
219  << "rotation=" << rp.rotation()
220  << ")";
221  return dbg;
222 }
223 #endif /* QT_NO_DEBUG_STREAM */
Definition of global enums.
Declaring the class KDChart::Measure.
#define d
@ MeasureOrientationVertical
Definition: KDChartEnums.h:289
@ MeasureOrientationHorizontal
Definition: KDChartEnums.h:288
Measure is used to specify relative and absolute sizes in KDChart, e.g. font sizes.
qreal calculatedValue(const QObject *autoArea, KDChartEnums::MeasureOrientation autoOrientation) const
Stores the absolute target points of a Position.
QPointF point(Position position) const
Defines a position, using compass terminology.
static const Position & Unknown
Defines relative position information: reference area, position in this area (reference position),...
void setReferencePoints(const PositionPoints &points)
Set a set of points from which the anchor point will be selected.
void setReferenceArea(QObject *area)
Set the reference area to be used to find the anchor point.
void resetReferencePosition()
Resets the position of the anchor point to the built-in default.
const PositionPoints referencePoints() const
void setAlignment(Qt::Alignment flags)
void setReferencePosition(Position position)
Set the position of the anchor point.
bool operator==(const RelativePosition &) const
RelativePosition & operator=(const RelativePosition &other)
void setVerticalPadding(const Measure &padding)
const QPointF calculatedPoint(const QSizeF &autoSize) const
Calculate a point, according to the reference area/position and the padding.
const QPointF referencePoint(qreal *polarDegrees=nullptr) const
Return the reference point, according to the reference area/position, and ignoring padding.
void setHorizontalPadding(const Measure &padding)
QDebug operator<<(QDebug stream, const DataDimension &r)

© 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