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

KDE's Doxygen guidelines are available online.