1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include <KChartPosition.h>
21 
22 #include <KChartEnums.h>
23 #include "KChartMath_p.h"
24 
25 #include <QString>
26 #include <QStringList>
27 #include <QList>
28 #include <QByteArray>
29 
30 #include <cassert>
31 
32 using namespace KChart;
33 
34 namespace {
35 /**
36  * \internal
37  * Static strings, to be translated in printable()
38  */
39 static const char * staticPositionNames[] = {
40     QT_TRANSLATE_NOOP("Position","Unknown Position"),
41     QT_TRANSLATE_NOOP("Position","Center"),
42     QT_TRANSLATE_NOOP("Position","NorthWest"),
43     QT_TRANSLATE_NOOP("Position","North"),
44     QT_TRANSLATE_NOOP("Position","NorthEast"),
45     QT_TRANSLATE_NOOP("Position","East"),
46     QT_TRANSLATE_NOOP("Position","SouthEast"),
47     QT_TRANSLATE_NOOP("Position","South"),
48     QT_TRANSLATE_NOOP("Position","SouthWest"),
49     QT_TRANSLATE_NOOP("Position","West"),
50     QT_TRANSLATE_NOOP("Position","Floating")
51 };
52 
53 
54 /**
55  * \internal
56  * One value for unknown positions, and nine values for predefined positions.
57  */
58 static Position staticPositionUnknown   = Position( KChartEnums::PositionUnknown );
59 static Position staticPositionCenter    = Position( KChartEnums::PositionCenter );
60 static Position staticPositionNorthWest = Position( KChartEnums::PositionNorthWest );
61 static Position staticPositionNorth     = Position( KChartEnums::PositionNorth );
62 static Position staticPositionNorthEast = Position( KChartEnums::PositionNorthEast );
63 static Position staticPositionEast      = Position( KChartEnums::PositionEast );
64 static Position staticPositionSouthEast = Position( KChartEnums::PositionSouthEast );
65 static Position staticPositionSouth     = Position( KChartEnums::PositionSouth );
66 static Position staticPositionSouthWest = Position( KChartEnums::PositionSouthWest );
67 static Position staticPositionWest      = Position( KChartEnums::PositionWest );
68 static Position staticPositionFloating  = Position( KChartEnums::PositionFloating );
69 
70 static const int maxPositionValue = 10;
71 
72 } // anon namespace
73 
74 const Position& Position::Unknown   = staticPositionUnknown;
75 const Position& Position::Center    = staticPositionCenter;
76 const Position& Position::NorthWest = staticPositionNorthWest;
77 const Position& Position::North     = staticPositionNorth;
78 const Position& Position::NorthEast = staticPositionNorthEast;
79 const Position& Position::East      = staticPositionEast;
80 const Position& Position::SouthEast = staticPositionSouthEast;
81 const Position& Position::South     = staticPositionSouth;
82 const Position& Position::SouthWest = staticPositionSouthWest;
83 const Position& Position::West      = staticPositionWest;
84 const Position& Position::Floating  = staticPositionFloating;
85 
86 
87 /**
88  * Default constructor. Creates a new Position, defaulting it to Position::Unknown.
89  */
Position()90 Position::Position()
91     : m_value( KChartEnums::PositionUnknown )
92 {
93 
94 }
95 
Position(int value)96 Position::Position( int value )
97     : m_value( value )
98 {
99     assert( 0 <= value ); assert( value <= maxPositionValue );
100 }
101 
102 /**
103  * Constructor. Creates a new Position, defaulting it to the respective value.
104  *
105  * Valid values ranging from zero (unknown value) to 10.
106  * If invalid value is passed, a Position::Unknown is created.
107  *
108  * \note Normally there is no need to call this constructor, but you would
109  * rather use one of the nine pre-defined, static values, e.g. like this:
110  * \verbatim
111  * const KChart::Position myPosition = KChart::Position::NorthEast;
112  * \endverbatim
113  */
Position(KChartEnums::PositionValue value)114 Position::Position( KChartEnums::PositionValue value )
115     : m_value( value )
116 {
117 
118 }
119 
120 /**
121  * Returns an integer value corresponding to this Position.
122  */
value() const123 KChartEnums::PositionValue Position::value() const
124 {
125     return static_cast<KChartEnums::PositionValue>( m_value );
126 }
127 
isUnknown() const128 bool Position::isUnknown() const
129 {
130     return  m_value == Position::Unknown.value();
131 }
132 
isWestSide() const133 bool Position::isWestSide() const
134 {
135     return  m_value == Position::SouthWest.value() ||
136             m_value == Position::West.value() ||
137             m_value == Position::NorthWest.value();
138 }
isNorthSide() const139 bool Position::isNorthSide() const
140 {
141     return  m_value == Position::NorthWest.value() ||
142             m_value == Position::North.value() ||
143             m_value == Position::NorthEast.value();
144 }
isEastSide() const145 bool Position::isEastSide() const
146 {
147     return  m_value == Position::NorthEast.value() ||
148             m_value == Position::East.value() ||
149             m_value == Position::SouthEast.value();
150 }
isSouthSide() const151 bool Position::isSouthSide() const
152 {
153     return  m_value == Position::SouthWest.value() ||
154             m_value == Position::South.value() ||
155             m_value == Position::SouthEast.value();
156 }
157 
isCorner() const158 bool Position::isCorner() const
159 {
160     return  m_value == Position::NorthWest.value() ||
161             m_value == Position::NorthEast.value() ||
162             m_value == Position::SouthEast.value() ||
163             m_value == Position::SouthWest.value();
164 }
isPole() const165 bool Position::isPole() const
166 {
167     return  m_value == Position::North.value() ||
168         m_value == Position::South.value();
169 }
170 
isFloating() const171 bool Position::isFloating() const
172 {
173     return  m_value == Position::Floating.value();
174 }
175 
176 /**
177  * Returns a non-translated string in English language, corresponding to this Position.
178  */
name() const179 const char * Position::name() const
180 {
181     return staticPositionNames[m_value];
182 }
183 
184 /**
185  * Returns a translated string, corresponding to this Position.
186  */
printableName() const187 QString Position::printableName() const
188 {
189     return tr(staticPositionNames[m_value]);
190 }
191 
192 
193 /**
194  * \brief Returns a list of all string, corresponding to
195  * the pre-defined positions.
196  *
197  * \param options if set to \c ExcludeCenter, the returned list
198  * does not contain the Center position.
199  */
names(Options options)200 QList<QByteArray> Position::names( Options options )
201 {
202     QList<QByteArray> list;
203     const int start = ( options & IncludeCenter ) ? 1 : 2;
204     const int end   = ( options & IncludeFloating ) ? maxPositionValue : maxPositionValue-1;
205     for ( int i=start; i<=end; ++i)
206         list.append( staticPositionNames[i] );
207     return list;
208 }
209 
210 /**
211  * \brief Returns a list of all translated string, corresponding to
212  * the pre-defined positions.
213  *
214  * \param options if set to \c ExcludeCenter, the returned list
215  * does not contain the Center position.
216  */
printableNames(Options options)217 QStringList Position::printableNames( Options options )
218 {
219     QStringList list;
220     const int start = ( options & IncludeCenter ) ? 1 : 2;
221     const int end   = ( options & IncludeFloating ) ? maxPositionValue : maxPositionValue-1;
222     for ( int i=start; i<=end; ++i)
223         list.append( Position(i).printableName() );
224     return list;
225 }
226 
fromName(const char * name)227 Position Position::fromName(const char * name)
228 {
229     for ( int i=1; i<=maxPositionValue; ++i)
230         if ( !qstricmp( name, staticPositionNames[i] ) )
231             return Position(i);
232     return Position(0);
233 }
234 
fromName(const QByteArray & name)235 Position Position::fromName( const QByteArray & name ) {
236     return fromName( name.data() );
237 }
238 
operator ==(const Position & r) const239 bool Position::operator==( const Position& r ) const
240 {
241     return ( value() == r.value() );
242 }
243 
244 
operator ==(int value_) const245 bool Position::operator==( int value_ ) const
246 {
247     return ( value() == value_ );
248 }
249 
250 
251 #if !defined(QT_NO_DEBUG_STREAM)
operator <<(QDebug dbg,const KChart::Position & p)252 QDebug operator<<(QDebug dbg, const KChart::Position& p )
253 {
254     dbg << "KChart::Position("
255 	<< p.name() << ")";
256     return dbg;
257 }
258 #endif /* QT_NO_DEBUG_STREAM */
259