KD Chart 2  [rev.2.6]
KDChartPosition.h
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 
23 #ifndef KDCHARTPOSITION_H
24 #define KDCHARTPOSITION_H
25 
26 #include <QDebug>
27 #include <Qt>
28 #include <QMetaType>
29 #include <QCoreApplication>
30 #include "KDChartGlobal.h"
31 #include "KDChartEnums.h"
32 
33 QT_BEGIN_NAMESPACE
34 class QStringList;
35 class QByteArray;
36 template <typename T> class QList;
37 QT_END_NAMESPACE
38 
39 namespace KDChart {
40 
75 class KDCHART_EXPORT Position
76 {
77  Q_DECLARE_TR_FUNCTIONS( Position )
78  Position( int value );
79 public:
80  Position();
81  Position( KDChartEnums::PositionValue value ); // intentionally non-explicit
82 
83  KDChartEnums::PositionValue value() const;
84 
85  const char *name() const;
86  QString printableName() const;
87 
88  bool isUnknown() const;
89 
90  bool isWestSide() const;
91  bool isNorthSide() const;
92  bool isEastSide() const;
93  bool isSouthSide() const;
94 
95  bool isCorner() const;
96  bool isPole() const;
97 
98  bool isFloating() const;
99 
100  static const Position& Unknown;
101  static const Position& Center;
102  static const Position& NorthWest;
103  static const Position& North;
104  static const Position& NorthEast;
105  static const Position& East;
106  static const Position& SouthEast;
107  static const Position& South;
108  static const Position& SouthWest;
109  static const Position& West;
110 
111  static const Position& Floating;
112 
113  // boolean flags: 1, 2, 4, 8, ...
114  enum Option {
115  IncludeCenter = 0x1,
116  IncludeFloating = 0x2 };
117  Q_DECLARE_FLAGS( Options, Option )
118 
119  // Unfortunately the following typecast from int to Options is needed
120  // as the | operator is not defined yet, this will be done by
121  // the makro Q_DECLARE_OPERATORS_FOR_FLAGS( KDChart::Position::Options )
122  // at the bottom of this file.
123  static QList<QByteArray> names( Options options = Options(IncludeCenter | IncludeFloating) );
124  static QStringList printableNames( Options options = Options(IncludeCenter | IncludeFloating) );
125 
126  static Position fromName(const char * name);
127  static Position fromName(const QByteArray & name);
128 
129  bool operator==( const Position& ) const;
130  bool operator==( int ) const;
131  bool operator!=( const Position& ) const;
132  bool operator!=( int ) const;
133 
134 private:
135  int m_value;
136 }; // End of class Position
137 
138 inline bool Position::operator!=( const Position & other ) const { return !operator==( other ); }
139 inline bool Position::operator!=( int other ) const { return !operator==( other ); }
140 
145 class KDCHART_EXPORT PositionPoints
146 {
147  public:
148  PositionPoints() {} // all points get initialized with the default automatically
149 
151  QPointF center,
152  QPointF northWest,
153  QPointF north,
154  QPointF northEast,
155  QPointF east,
156  QPointF southEast,
157  QPointF south,
158  QPointF southWest,
159  QPointF west )
160  : mPositionCenter( center )
161  , mPositionNorthWest( northWest )
162  , mPositionNorth( north )
163  , mPositionNorthEast( northEast )
164  , mPositionEast( east )
165  , mPositionSouthEast( southEast )
166  , mPositionSouth( south )
167  , mPositionSouthWest( southWest )
168  , mPositionWest( west )
169  {}
171  const QPointF& onePointForAllPositions )
172  : mPositionCenter( onePointForAllPositions )
173  , mPositionNorthWest( onePointForAllPositions )
174  , mPositionNorth( onePointForAllPositions )
175  , mPositionNorthEast( onePointForAllPositions )
176  , mPositionEast( onePointForAllPositions )
177  , mPositionSouthEast( onePointForAllPositions )
178  , mPositionSouth( onePointForAllPositions )
179  , mPositionSouthWest( onePointForAllPositions )
180  , mPositionWest( onePointForAllPositions )
181  {}
183  const QRectF& rect )
184  {
185  const QRectF r( rect.normalized() );
186  mPositionCenter = r.center();
187  mPositionNorthWest = r.topLeft();
188  mPositionNorth = QPointF(r.center().x(), r.top());
189  mPositionNorthEast = r.topRight();
190  mPositionEast = QPointF(r.right(), r.center().y());
191  mPositionSouthEast = r.bottomRight();
192  mPositionSouth = QPointF(r.center().x(), r.bottom());
193  mPositionSouthWest = r.bottomLeft();
194  mPositionWest = QPointF(r.left(), r.center().y());
195  }
197  QPointF northWest,
198  QPointF northEast,
199  QPointF southEast,
200  QPointF southWest )
201  : mPositionCenter( (northWest + southEast) / 2.0 )
202  , mPositionNorthWest( northWest )
203  , mPositionNorth( (northWest + northEast) / 2.0 )
204  , mPositionNorthEast( northEast )
205  , mPositionEast( (northEast + southEast) / 2.0 )
206  , mPositionSouthEast( southEast )
207  , mPositionSouth( (southWest + southEast) / 2.0 )
208  , mPositionSouthWest( southWest )
209  , mPositionWest( (northWest + southWest) / 2.0 )
210  {}
211 
212  void setDegrees( KDChartEnums::PositionValue pos, qreal degrees )
213  {
214  mapOfDegrees[pos] = degrees;
215  }
216 
217 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
218  const qreal degrees( KDChartEnums::PositionValue pos ) const
219 #else
220  qreal degrees( KDChartEnums::PositionValue pos ) const
221 #endif
222  {
223  if ( mapOfDegrees.contains(pos) )
224  return mapOfDegrees[pos];
225  return 0.0;
226  }
227 
228 #if QT_VERSION < 0x040400 || defined(Q_COMPILER_MANGLES_RETURN_TYPE)
229  const QPointF point( Position position ) const
230 #else
231  QPointF point( Position position ) const
232 #endif
233  {
234  //qDebug() << "point( " << position.name() << " )";
235  if ( position == Position::Center)
236  return mPositionCenter;
237  if ( position == Position::NorthWest)
238  return mPositionNorthWest;
239  if ( position == Position::North)
240  return mPositionNorth;
241  if ( position == Position::NorthEast)
242  return mPositionNorthEast;
243  if ( position == Position::East)
244  return mPositionEast;
245  if ( position == Position::SouthEast)
246  return mPositionSouthEast;
247  if ( position == Position::South)
248  return mPositionSouth;
249  if ( position == Position::SouthWest)
250  return mPositionSouthWest;
251  if ( position == Position::West)
252  return mPositionWest;
253  return mPositionUnknown;
254  }
255 
256  bool isNull() const
257  {
258  return
259  mPositionUnknown.isNull() &&
260  mPositionCenter.isNull() &&
261  mPositionNorthWest.isNull() &&
262  mPositionNorth.isNull() &&
263  mPositionNorthEast.isNull() &&
264  mPositionEast.isNull() &&
265  mPositionSouthEast.isNull() &&
266  mPositionSouth.isNull() &&
267  mPositionSouthWest.isNull() &&
268  mPositionWest.isNull();
269  }
270 
274  QPointF mPositionNorth;
276  QPointF mPositionEast;
278  QPointF mPositionSouth;
280  QPointF mPositionWest;
282 
283 }; // End of class PositionPoints
284 
285 
286 }
287 
288 
289 #if !defined(QT_NO_DEBUG_STREAM)
290 KDCHART_EXPORT QDebug operator<<(QDebug, const KDChart::Position& );
291 #endif /* QT_NO_DEBUG_STREAM */
292 
293 QT_BEGIN_NAMESPACE
294 Q_DECLARE_TYPEINFO( KDChart::Position, Q_MOVABLE_TYPE );
295 Q_DECLARE_OPERATORS_FOR_FLAGS( KDChart::Position::Options )
296 QT_END_NAMESPACE
297 
298 Q_DECLARE_METATYPE( KDChart::Position )
299 
300 #endif // KDCHARTPOSITION_H
static const Position & East
bool operator==(const Span &s1, const Span &s2)
bool operator!=(const Position &) const
PositionPoints(const QRectF &rect)
Definition of global enums.
PositionPoints(QPointF northWest, QPointF northEast, QPointF southEast, QPointF southWest)
static const Position & South
static const Position & Center
QT_BEGIN_NAMESPACE Q_DECLARE_TYPEINFO(KDChart::Position, Q_MOVABLE_TYPE)
QMap< KDChartEnums::PositionValue, qreal > mapOfDegrees
static const Position & Unknown
void setDegrees(KDChartEnums::PositionValue pos, qreal degrees)
static const Position & NorthWest
static const Position & West
const QPointF point(Position position) const
PositionValue
Numerical values of the static KDChart::Position instances, for using a Position::value() with a swit...
Definition: KDChartEnums.h:192
Stores the absolute target points of a Position.
static const Position & NorthEast
PositionPoints(QPointF center, QPointF northWest, QPointF north, QPointF northEast, QPointF east, QPointF southEast, QPointF south, QPointF southWest, QPointF west)
static const Position & North
Defines a position, using compass terminology.
QDebug operator<<(QDebug stream, const DataDimension &r)
static const Position & SouthEast
static const Position & Floating
PositionPoints(const QPointF &onePointForAllPositions)
const qreal degrees(KDChartEnums::PositionValue pos) const
static const Position & SouthWest

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