KD Chart 2  [rev.2.6]
KDChartRelativePosition.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2019 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 
25 #include "KDChartEnums.h"
26 #include "KDChartMeasure.h"
27 #include "KDChartPosition.h"
28 #include "KDChartAbstractArea.h"
29 
30 #include <QWidget>
31 #include <QLayout>
32 
33 #include <KDABLibFakes>
34 
35 using namespace KDChart;
36 
37 class RelativePosition::Private {
38  friend class ::KDChart::RelativePosition;
39 public:
40  Private();
41  ~Private();
42 
43 private:
44  QObject* area;
45  PositionPoints points;
46  Position position;
47  Qt::Alignment alignment;
50  qreal rotation;
51 };
52 
53 
54 RelativePosition::Private::Private()
55  : area( 0 ),
56  alignment( Qt::AlignCenter ),
57  rotation( 0 )
58 {
59 
60 }
61 
62 RelativePosition::Private::~Private()
63 {}
64 
65 
66 
68  : _d( new Private )
69 {
70 
71 }
72 
74  : _d( new Private( *r._d ) )
75 {
76 
77 }
78 
80  RelativePosition copy( other );
81  copy.swap( *this );
82  return *this;
83 }
84 
86 {
87  delete _d;
88 }
89 
90 #define d d_func()
91 
93  d->area = area;
94  if ( area )
96 }
97 
99  return d->area;
100 }
101 
103  d->points = points;
104  if ( !points.isNull() )
105  setReferenceArea( 0 );
106 }
108  return d->points;
109 }
110 
112  d->position = pos;
113 }
114 
116  d->position = Position::Unknown;
117 }
118 
120  return d->position;
121 }
122 
123 void RelativePosition::setAlignment( Qt::Alignment align ) {
124  d->alignment = align;
125 }
126 
127 Qt::Alignment RelativePosition::alignment() const {
128  return d->alignment;
129 }
130 
132  d->horizontalPadding = pad;
133 }
134 
136  return d->horizontalPadding;
137 }
138 
140  d->verticalPadding = pad;
141 }
142 
144  return d->verticalPadding;
145 }
146 
147 void RelativePosition::setRotation( qreal rot ) {
148  d->rotation = rot;
149 }
150 
152  return d->rotation;
153 }
154 
155 
156 const QPointF RelativePosition::referencePoint( qreal* polarDegrees ) const
157 {
158  bool useRect = ( d->area != 0 );
159  QRect rect;
160  if ( useRect ) {
161  if ( const QWidget* widget = qobject_cast< const QWidget* >( d->area ) ) {
162  const QLayout* layout = widget->layout();
163  rect = layout ? layout->geometry() : widget->geometry();
164  } else if ( const AbstractArea* kdcArea = qobject_cast< const AbstractArea* >( d->area ) ) {
165  rect = kdcArea->geometry();
166  } else {
167  useRect = false;
168  }
169  }
170 
171  QPointF pt;
172  qreal angle = 0.0;
173  if ( useRect ) {
174  pt = PositionPoints( rect ).point( d->position );
175  } else {
176  pt = d->points.point( d->position );
177  angle = d->points.degrees( d->position.value() );
178  }
179 
180  if ( polarDegrees ) {
181  *polarDegrees = angle;
182  }
183  return pt;
184 }
185 
186 
187 const QPointF RelativePosition::calculatedPoint( const QSizeF& autoSize ) const
188 {
191 
192  qreal polarDegrees;
193  QPointF pt( referencePoint( &polarDegrees ) );
194  if ( polarDegrees == 0.0 ) {
195  pt += QPointF( dx, dy );
196  } else {
197  const qreal rad = DEGTORAD( polarDegrees);
198  const qreal sinDeg = sin(rad);
199  const qreal cosDeg = cos(rad);
200  pt.setX( pt.x() + dx * cosDeg + dy * sinDeg );
201  pt.setY( pt.y() - dx * sinDeg + dy * cosDeg );
202  }
203  return pt;
204 }
205 
206 
208 {
209  return d->area == r.referenceArea() &&
210  d->position == r.referencePosition() &&
211  d->alignment == r.alignment() &&
212  d->horizontalPadding == r.horizontalPadding() &&
213  d->verticalPadding == r.verticalPadding() &&
214  d->rotation == r.rotation() ;
215 }
216 
217 #undef d
218 
219 
220 #if !defined(QT_NO_DEBUG_STREAM)
221 QDebug operator<<(QDebug dbg, const KDChart::RelativePosition& rp)
222 {
223  dbg << "KDChart::RelativePosition("
224  << "referencearea="<<rp.referenceArea()
225  << "referenceposition="<<rp.referencePosition()
226  << "alignment="<<rp.alignment()
227  << "horizontalpadding="<<rp.horizontalPadding()
228  << "verticalpadding="<<rp.verticalPadding()
229  << "rotation="<<rp.rotation()
230  << ")";
231  return dbg;
232 }
233 #endif /* QT_NO_DEBUG_STREAM */
void setVerticalPadding(const Measure &padding)
Set the height of the vertical padding between the anchor point and the content placed by this Relati...
An area in the chart with a background, a frame, etc.
qreal calculatedValue(const QObject *autoArea, KDChartEnums::MeasureOrientation autoOrientation) const
The reference area must either be derived from AbstractArea or from QWidget, so it can also be derive...
Definition of global enums.
void setReferencePosition(Position position)
Set the position of the anchor point.
void setHorizontalPadding(const Measure &padding)
Set the width of the horizontal padding between the anchor point and the content placed by this Relat...
static const Position & Unknown
RelativePosition & operator=(const RelativePosition &other)
void setAlignment(Qt::Alignment flags)
Set the alignment of the content placed by this RelativePosition.
const QPointF referencePoint(qreal *polarDegrees=0) const
Return the reference point, according to the reference area/position, and ignoring padding...
Declaring the class KDChart::Measure.
const QPointF point(Position position) const
void resetReferencePosition()
Resets the position of the anchor point to the built-in default.
Stores the absolute target points of a Position.
const QPointF calculatedPoint(const QSizeF &autoSize) const
Calculate a point, accordin to the reference area/position and the padding.
const PositionPoints referencePoints() const
bool operator==(const RelativePosition &) const
Measure is used to specify relative and absolute sizes in KDChart, e.g.
#define d
Defines relative position information: reference area, position in this area (reference position)...
Class only listed here to document inheritance of some KDChart classes.
Defines a position, using compass terminology.
QDebug operator<<(QDebug stream, const DataDimension &r)
Class only listed here to document inheritance of some KDChart classes.
void setReferenceArea(QObject *area)
Set the reference area to be used to find the anchor point.
void setReferencePoints(const PositionPoints &points)
Set a set of points from which the anchor point will be selected.

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/