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
20using namespace KChart;
21
22namespace {
23/**
24 * \internal
25 * Static strings, to be translated in printable()
26 */
27static 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 */
46static Position staticPositionUnknown = Position( KChartEnums::PositionUnknown );
47static Position staticPositionCenter = Position( KChartEnums::PositionCenter );
48static Position staticPositionNorthWest = Position( KChartEnums::PositionNorthWest );
49static Position staticPositionNorth = Position( KChartEnums::PositionNorth );
50static Position staticPositionNorthEast = Position( KChartEnums::PositionNorthEast );
51static Position staticPositionEast = Position( KChartEnums::PositionEast );
52static Position staticPositionSouthEast = Position( KChartEnums::PositionSouthEast );
53static Position staticPositionSouth = Position( KChartEnums::PositionSouth );
54static Position staticPositionSouthWest = Position( KChartEnums::PositionSouthWest );
55static Position staticPositionWest = Position( KChartEnums::PositionWest );
56static Position staticPositionFloating = Position( KChartEnums::PositionFloating );
57
58static const int maxPositionValue = 10;
59
60} // anon namespace
61
62const Position& Position::Unknown = staticPositionUnknown;
63const Position& Position::Center = staticPositionCenter;
64const Position& Position::NorthWest = staticPositionNorthWest;
65const Position& Position::North = staticPositionNorth;
66const Position& Position::NorthEast = staticPositionNorthEast;
67const Position& Position::East = staticPositionEast;
68const Position& Position::SouthEast = staticPositionSouthEast;
69const Position& Position::South = staticPositionSouth;
70const Position& Position::SouthWest = staticPositionSouthWest;
71const Position& Position::West = staticPositionWest;
72const Position& Position::Floating = staticPositionFloating;
73
74
75/**
76 * Default constructor. Creates a new Position, defaulting it to Position::Unknown.
77 */
78Position::Position()
79 : m_value( KChartEnums::PositionUnknown )
80{
81
82}
83
84Position::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
116bool Position::isUnknown() const
117{
118 return m_value == Position::Unknown.value();
119}
120
121bool Position::isWestSide() const
122{
123 return m_value == Position::SouthWest.value() ||
124 m_value == Position::West.value() ||
125 m_value == Position::NorthWest.value();
126}
127bool Position::isNorthSide() const
128{
129 return m_value == Position::NorthWest.value() ||
130 m_value == Position::North.value() ||
131 m_value == Position::NorthEast.value();
132}
133bool Position::isEastSide() const
134{
135 return m_value == Position::NorthEast.value() ||
136 m_value == Position::East.value() ||
137 m_value == Position::SouthEast.value();
138}
139bool 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
146bool 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}
153bool Position::isPole() const
154{
155 return m_value == Position::North.value() ||
156 m_value == Position::South.value();
157}
158
159bool 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 */
167const 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{
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
215Position 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
223Position Position::fromName( const QByteArray & name ) {
224 return fromName( name.data() );
225}
226
227bool Position::operator==( const Position& r ) const
228{
229 return ( value() == r.value() );
230}
231
232
233bool 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 */
Definition of global enums.
Project global class providing some enums needed both by KChartParams and by KChartCustomBox.
Definition KChartEnums.h:27
PositionValue
Numerical values of the static KChart::Position instances, for using a Position::value() with a switc...
Defines a position, using compass terminology.
QString printableName() const
Returns a translated string, corresponding to this Position.
KChartEnums::PositionValue value() const
Returns an integer value corresponding to this Position.
Position()
Default constructor.
static QList< QByteArray > names(Options options=Options(IncludeCenter|IncludeFloating))
Returns a list of all string, corresponding to the pre-defined positions.
static QStringList printableNames(Options options=Options(IncludeCenter|IncludeFloating))
Returns a list of all translated string, corresponding to the pre-defined positions.
const char * name() const
Returns a non-translated string in English language, corresponding to this Position.
Q_SCRIPTABLE Q_NOREPLY void start()
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.