KChart

KChartRelativePosition.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 "KChartRelativePosition.h"
10 
11 #include "KChartEnums.h"
12 #include "KChartMeasure.h"
13 #include "KChartPosition.h"
14 #include "KChartAbstractArea.h"
15 #include "KChartMath_p.h"
16 
17 #include <QWidget>
18 #include <QLayout>
19 
20 using namespace KChart;
21 
22 class Q_DECL_HIDDEN RelativePosition::Private {
23  friend class ::KChart::RelativePosition;
24 public:
25  Private();
26  ~Private();
27 
28 private:
29  QObject* area;
30  PositionPoints points;
31  Position position;
32  Qt::Alignment alignment;
33  Measure horizontalPadding;
34  Measure verticalPadding;
35  qreal rotation;
36 };
37 
38 
39 RelativePosition::Private::Private()
40  : area( nullptr ),
41  alignment( Qt::AlignCenter ),
42  rotation( 0 )
43 {
44 
45 }
46 
47 RelativePosition::Private::~Private()
48 {}
49 
50 
51 
52 RelativePosition::RelativePosition()
53  : _d( new Private )
54 {
55 
56 }
57 
58 RelativePosition::RelativePosition( const RelativePosition& r )
59  : _d( new Private( *r._d ) )
60 {
61 
62 }
63 
64 RelativePosition & RelativePosition::operator=( const RelativePosition & other ) {
65  RelativePosition copy( other );
66  copy.swap( *this );
67  return *this;
68 }
69 
70 RelativePosition::~RelativePosition()
71 {
72  delete _d;
73 }
74 
75 #define d d_func()
76 
78  d->area = area;
79  if ( area )
80  setReferencePoints( PositionPoints() );
81 }
82 
83 QObject * RelativePosition::referenceArea() const {
84  return d->area;
85 }
86 
88  d->points = points;
89  if ( !points.isNull() )
90  setReferenceArea( nullptr );
91 }
92 const PositionPoints RelativePosition::referencePoints() const{
93  return d->points;
94 }
95 
97  d->position = pos;
98 }
99 
101  d->position = Position::Unknown;
102 }
103 
104 Position RelativePosition::referencePosition() const {
105  return d->position;
106 }
107 
109  d->alignment = align;
110 }
111 
112 Qt::Alignment RelativePosition::alignment() const {
113  return d->alignment;
114 }
115 
117  d->horizontalPadding = pad;
118 }
119 
120 Measure RelativePosition::horizontalPadding() const {
121  return d->horizontalPadding;
122 }
123 
125  d->verticalPadding = pad;
126 }
127 
128 Measure RelativePosition::verticalPadding() const {
129  return d->verticalPadding;
130 }
131 
132 void RelativePosition::setRotation( qreal rot ) {
133  d->rotation = rot;
134 }
135 
136 qreal RelativePosition::rotation() const {
137  return d->rotation;
138 }
139 
140 
141 const QPointF RelativePosition::referencePoint( qreal* polarDegrees ) const
142 {
143  bool useRect = ( d->area != nullptr );
144  QRect rect;
145  if ( useRect ) {
146  if ( const QWidget* widget = qobject_cast< const QWidget* >( d->area ) ) {
147  const QLayout* layout = widget->layout();
148  rect = layout ? layout->geometry() : widget->geometry();
149  } else if ( const AbstractArea* kdcArea = qobject_cast< const AbstractArea* >( d->area ) ) {
150  rect = kdcArea->geometry();
151  } else {
152  useRect = false;
153  }
154  }
155 
156  QPointF pt;
157  qreal angle = 0.0;
158  if ( useRect ) {
159  pt = PositionPoints( rect ).point( d->position );
160  } else {
161  pt = d->points.point( d->position );
162  angle = d->points.degrees( d->position.value() );
163  }
164 
165  if ( polarDegrees ) {
166  *polarDegrees = angle;
167  }
168  return pt;
169 }
170 
171 
172 const QPointF RelativePosition::calculatedPoint( const QSizeF& autoSize ) const
173 {
174  const qreal dx = horizontalPadding().calculatedValue( autoSize, KChartEnums::MeasureOrientationHorizontal );
175  const qreal dy = verticalPadding().calculatedValue( autoSize, KChartEnums::MeasureOrientationVertical );
176 
177  qreal polarDegrees;
178  QPointF pt( referencePoint( &polarDegrees ) );
179  if ( polarDegrees == 0.0 ) {
180  pt += QPointF( dx, dy );
181  } else {
182  const qreal rad = DEGTORAD( polarDegrees);
183  const qreal sinDeg = sin(rad);
184  const qreal cosDeg = cos(rad);
185  pt.setX( pt.x() + dx * cosDeg + dy * sinDeg );
186  pt.setY( pt.y() - dx * sinDeg + dy * cosDeg );
187  }
188  return pt;
189 }
190 
191 
192 bool RelativePosition::operator==( const RelativePosition& r ) const
193 {
194  return d->area == r.referenceArea() &&
195  d->position == r.referencePosition() &&
196  d->alignment == r.alignment() &&
197  d->horizontalPadding == r.horizontalPadding() &&
198  d->verticalPadding == r.verticalPadding() &&
199  d->rotation == r.rotation() ;
200 }
201 
202 #undef d
203 
204 
205 #if !defined(QT_NO_DEBUG_STREAM)
207 {
208  dbg << "KChart::RelativePosition("
209  << "referencearea="<<rp.referenceArea()
210  << "referenceposition="<<rp.referencePosition()
211  << "alignment="<<rp.alignment()
212  << "horizontalpadding="<<rp.horizontalPadding()
213  << "verticalpadding="<<rp.verticalPadding()
214  << "rotation="<<rp.rotation()
215  << ")";
216  return dbg;
217 }
218 #endif /* QT_NO_DEBUG_STREAM */
Defines relative position information: reference area, position in this area (reference position)...
Class only listed here to document inheritance of some KChart classes.
virtual QLayout * layout() override
void setVerticalPadding(const Measure &padding)
Set the height of the vertical padding between the anchor point and the content placed by this Relati...
void setHorizontalPadding(const Measure &padding)
Set the width of the horizontal padding between the anchor point and the content placed by this Relat...
Defines a position, using compass terminology.
void setReferencePoints(const PositionPoints &points)
Set a set of points from which the anchor point will be selected.
typedef Alignment
Stores the absolute target points of a Position.
void setReferencePosition(Position position)
Set the position of the anchor point.
virtual QRect geometry() const const override
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Declaring the class KChart::Measure.
qreal x() const const
qreal y() const const
const QPointF referencePoint(qreal *polarDegrees=nullptr) const
Return the reference point, according to the reference area/position, and ignoring padding...
Class only listed here to document inheritance of some KChart classes.
An area in the chart with a background, a frame, etc.
const QPointF calculatedPoint(const QSizeF &autoSize) const
Calculate a point, accordin to the reference area/position and the padding.
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
Definition of global enums.
void setX(qreal x)
void setY(qreal y)
void setAlignment(Qt::Alignment flags)
Set the alignment of the content placed by this RelativePosition.
void setReferenceArea(QObject *area)
Set the reference area to be used to find the anchor point.
Global namespace.
Measure is used to specify relative and absolute sizes in KChart, e.g.
Definition: KChartMeasure.h:37
void resetReferencePosition()
Resets the position of the anchor point to the built-in default.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 27 2022 22:33:23 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.