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
20using namespace KChart;
21
22class Q_DECL_HIDDEN RelativePosition::Private {
23 friend class ::KChart::RelativePosition;
24public:
25 Private();
26 ~Private();
27
28private:
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
39RelativePosition::Private::Private()
40 : area( nullptr ),
41 alignment( Qt::AlignCenter ),
42 rotation( 0 )
43{
44
45}
46
47RelativePosition::Private::~Private()
48{}
49
50
51
52RelativePosition::RelativePosition()
53 : _d( new Private )
54{
55
56}
57
58RelativePosition::RelativePosition( const RelativePosition& r )
59 : _d( new Private( *r._d ) )
60{
61
62}
63
64RelativePosition & RelativePosition::operator=( const RelativePosition & other ) {
65 RelativePosition copy( other );
66 copy.swap( *this );
67 return *this;
68}
69
70RelativePosition::~RelativePosition()
71{
72 delete _d;
73}
74
75#define d d_func()
76
78 d->area = area;
79 if ( area )
81}
82
83QObject * RelativePosition::referenceArea() const {
84 return d->area;
85}
86
88 d->points = points;
89 if ( !points.isNull() )
90 setReferenceArea( nullptr );
91}
92const PositionPoints RelativePosition::referencePoints() const{
93 return d->points;
94}
95
97 d->position = pos;
98}
99
101 d->position = Position::Unknown;
102}
103
104Position RelativePosition::referencePosition() const {
105 return d->position;
106}
107
109 d->alignment = align;
110}
111
112Qt::Alignment RelativePosition::alignment() const {
113 return d->alignment;
114}
115
117 d->horizontalPadding = pad;
118}
119
120Measure RelativePosition::horizontalPadding() const {
121 return d->horizontalPadding;
122}
123
125 d->verticalPadding = pad;
126}
127
128Measure RelativePosition::verticalPadding() const {
129 return d->verticalPadding;
130}
131
132void RelativePosition::setRotation( qreal rot ) {
133 d->rotation = rot;
134}
135
136qreal RelativePosition::rotation() const {
137 return d->rotation;
138}
139
140
141const 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
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
192bool 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)
206QDebug operator<<(QDebug dbg, const KChart::RelativePosition& rp)
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 */
Definition of global enums.
Declaring the class KChart::Measure.
An area in the chart with a background, a frame, etc.
Measure is used to specify relative and absolute sizes in KChart, e.g.
qreal calculatedValue(const QObject *autoArea, KChartEnums::MeasureOrientation autoOrientation) const
The reference area must either be derived from AbstractArea or from QWidget, so it can also be derive...
Stores the absolute target points of a Position.
Defines a position, using compass terminology.
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.
void setAlignment(Qt::Alignment flags)
Set the alignment of the content placed by this RelativePosition.
void setReferencePosition(Position position)
Set the position of the anchor point.
void setVerticalPadding(const Measure &padding)
Set the height of the vertical padding between the anchor point and the content placed by this Relati...
const QPointF calculatedPoint(const QSizeF &autoSize) const
Calculate a point, accordin 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)
Set the width of the horizontal padding between the anchor point and the content placed by this Relat...
QAction * copy(const QObject *recvr, const char *slot, QObject *parent)
virtual QRect geometry() const const override
virtual QLayout * layout() override
void setX(qreal x)
void setY(qreal y)
qreal x() const const
qreal y() const const
typedef Alignment
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:18:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.