KChart

KChartPosition.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 <KChartPosition.h>
10 
11 #include <KChartEnums.h>
12 #include "KChartMath_p.h"
13 
14 #include <QString>
15 #include <QList>
16 #include <QByteArray>
17 
18 #include <cassert>
19 
20 using namespace KChart;
21 
22 namespace {
23 /**
24  * \internal
25  * Static strings, to be translated in printable()
26  */
27 static const char * staticPositionNames[] = {
28  QT_TRANSLATE_NOOP("Position","Unknown Position"),
29  QT_TRANSLATE_NOOP("Position","Center"),
30  QT_TRANSLATE_NOOP("Position","NorthWest"),
31  QT_TRANSLATE_NOOP("Position","North"),
32  QT_TRANSLATE_NOOP("Position","NorthEast"),
33  QT_TRANSLATE_NOOP("Position","East"),
34  QT_TRANSLATE_NOOP("Position","SouthEast"),
35  QT_TRANSLATE_NOOP("Position","South"),
36  QT_TRANSLATE_NOOP("Position","SouthWest"),
37  QT_TRANSLATE_NOOP("Position","West"),
38  QT_TRANSLATE_NOOP("Position","Floating")
39 };
40 
41 
42 /**
43  * \internal
44  * One value for unknown positions, and nine values for predefined positions.
45  */
46 static Position staticPositionUnknown = Position( KChartEnums::PositionUnknown );
47 static Position staticPositionCenter = Position( KChartEnums::PositionCenter );
48 static Position staticPositionNorthWest = Position( KChartEnums::PositionNorthWest );
49 static Position staticPositionNorth = Position( KChartEnums::PositionNorth );
50 static Position staticPositionNorthEast = Position( KChartEnums::PositionNorthEast );
51 static Position staticPositionEast = Position( KChartEnums::PositionEast );
52 static Position staticPositionSouthEast = Position( KChartEnums::PositionSouthEast );
53 static Position staticPositionSouth = Position( KChartEnums::PositionSouth );
54 static Position staticPositionSouthWest = Position( KChartEnums::PositionSouthWest );
55 static Position staticPositionWest = Position( KChartEnums::PositionWest );
56 static Position staticPositionFloating = Position( KChartEnums::PositionFloating );
57 
58 static const int maxPositionValue = 10;
59 
60 } // anon namespace
61 
62 const Position& Position::Unknown = staticPositionUnknown;
63 const Position& Position::Center = staticPositionCenter;
64 const Position& Position::NorthWest = staticPositionNorthWest;
65 const Position& Position::North = staticPositionNorth;
66 const Position& Position::NorthEast = staticPositionNorthEast;
67 const Position& Position::East = staticPositionEast;
68 const Position& Position::SouthEast = staticPositionSouthEast;
69 const Position& Position::South = staticPositionSouth;
70 const Position& Position::SouthWest = staticPositionSouthWest;
71 const Position& Position::West = staticPositionWest;
72 const Position& Position::Floating = staticPositionFloating;
73 
74 
75 /**
76  * Default constructor. Creates a new Position, defaulting it to Position::Unknown.
77  */
79  : m_value( KChartEnums::PositionUnknown )
80 {
81 
82 }
83 
84 Position::Position( int value )
85  : m_value( value )
86 {
87  assert( 0 <= value ); assert( value <= maxPositionValue );
88 }
89 
90 /**
91  * Constructor. Creates a new Position, defaulting it to the respective value.
92  *
93  * Valid values ranging from zero (unknown value) to 10.
94  * If invalid value is passed, a Position::Unknown is created.
95  *
96  * \note Normally there is no need to call this constructor, but you would
97  * rather use one of the nine pre-defined, static values, e.g. like this:
98  * \verbatim
99  * const KChart::Position myPosition = KChart::Position::NorthEast;
100  * \endverbatim
101  */
103  : m_value( value )
104 {
105 
106 }
107 
108 /**
109  * Returns an integer value corresponding to this Position.
110  */
112 {
113  return static_cast<KChartEnums::PositionValue>( m_value );
114 }
115 
116 bool Position::isUnknown() const
117 {
118  return m_value == Position::Unknown.value();
119 }
120 
121 bool Position::isWestSide() const
122 {
123  return m_value == Position::SouthWest.value() ||
124  m_value == Position::West.value() ||
125  m_value == Position::NorthWest.value();
126 }
127 bool Position::isNorthSide() const
128 {
129  return m_value == Position::NorthWest.value() ||
130  m_value == Position::North.value() ||
131  m_value == Position::NorthEast.value();
132 }
133 bool Position::isEastSide() const
134 {
135  return m_value == Position::NorthEast.value() ||
136  m_value == Position::East.value() ||
137  m_value == Position::SouthEast.value();
138 }
139 bool Position::isSouthSide() const
140 {
141  return m_value == Position::SouthWest.value() ||
142  m_value == Position::South.value() ||
143  m_value == Position::SouthEast.value();
144 }
145 
146 bool Position::isCorner() const
147 {
148  return m_value == Position::NorthWest.value() ||
149  m_value == Position::NorthEast.value() ||
150  m_value == Position::SouthEast.value() ||
151  m_value == Position::SouthWest.value();
152 }
153 bool Position::isPole() const
154 {
155  return m_value == Position::North.value() ||
156  m_value == Position::South.value();
157 }
158 
159 bool Position::isFloating() const
160 {
161  return m_value == Position::Floating.value();
162 }
163 
164 /**
165  * Returns a non-translated string in English language, corresponding to this Position.
166  */
167 const char * Position::name() const
168 {
169  return staticPositionNames[m_value];
170 }
171 
172 /**
173  * Returns a translated string, corresponding to this Position.
174  */
176 {
177  return tr(staticPositionNames[m_value]);
178 }
179 
180 
181 /**
182  * \brief Returns a list of all string, corresponding to
183  * the pre-defined positions.
184  *
185  * \param options if set to \c ExcludeCenter, the returned list
186  * does not contain the Center position.
187  */
189 {
190  QList<QByteArray> list;
191  const int start = ( options & IncludeCenter ) ? 1 : 2;
192  const int end = ( options & IncludeFloating ) ? maxPositionValue : maxPositionValue-1;
193  for ( int i=start; i<=end; ++i)
194  list.append( staticPositionNames[i] );
195  return list;
196 }
197 
198 /**
199  * \brief Returns a list of all translated string, corresponding to
200  * the pre-defined positions.
201  *
202  * \param options if set to \c ExcludeCenter, the returned list
203  * does not contain the Center position.
204  */
206 {
207  QStringList list;
208  const int start = ( options & IncludeCenter ) ? 1 : 2;
209  const int end = ( options & IncludeFloating ) ? maxPositionValue : maxPositionValue-1;
210  for ( int i=start; i<=end; ++i)
211  list.append( Position(i).printableName() );
212  return list;
213 }
214 
215 Position Position::fromName(const char * name)
216 {
217  for ( int i=1; i<=maxPositionValue; ++i)
218  if ( !qstricmp( name, staticPositionNames[i] ) )
219  return Position(i);
220  return Position(0);
221 }
222 
223 Position Position::fromName( const QByteArray & name ) {
224  return fromName( name.data() );
225 }
226 
227 bool Position::operator==( const Position& r ) const
228 {
229  return ( value() == r.value() );
230 }
231 
232 
233 bool Position::operator==( int value_ ) const
234 {
235  return ( value() == value_ );
236 }
237 
238 
239 #if !defined(QT_NO_DEBUG_STREAM)
241 {
242  dbg << "KChart::Position("
243  << p.name() << ")";
244  return dbg;
245 }
246 #endif /* QT_NO_DEBUG_STREAM */
void append(const T &value)
Defines a position, using compass terminology.
Project global class providing some enums needed both by KChartParams and by KChartCustomBox.
Definition: KChartEnums.h:26
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Position()
Default constructor.
Q_SCRIPTABLE Q_NOREPLY void start()
static QStringList printableNames(Options options=Options(IncludeCenter|IncludeFloating))
Returns a list of all translated string, corresponding to the pre-defined positions.
static QList< QByteArray > names(Options options=Options(IncludeCenter|IncludeFloating))
Returns a list of all string, corresponding to the pre-defined positions.
Definition of global enums.
const char * name() const
Returns a non-translated string in English language, corresponding to this Position.
QString printableName() const
Returns a translated string, corresponding to this Position.
KChartEnums::PositionValue value() const
Returns an integer value corresponding to this Position.
PositionValue
Numerical values of the static KChart::Position instances, for using a Position::value() with a switc...
Definition: KChartEnums.h:180
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Jul 1 2022 05:09:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.