KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

433 lines
13KB

  1. /* -*- Mode: C++ -*-
  2. KDChart - a multi-platform charting engine
  3. */
  4. /****************************************************************************
  5. ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB. All rights reserved.
  6. **
  7. ** This file is part of the KDChart library.
  8. **
  9. ** This file may be distributed and/or modified under the terms of the
  10. ** GNU General Public License version 2 as published by the Free Software
  11. ** Foundation and appearing in the file LICENSE.GPL included in the
  12. ** packaging of this file.
  13. **
  14. ** Licensees holding valid commercial KDChart licenses may use this file in
  15. ** accordance with the KDChart Commercial License Agreement provided with
  16. ** the Software.
  17. **
  18. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  19. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. **
  21. ** See http://www.klaralvdalens-datakonsult.se/?page=products for
  22. ** information about KDChart Commercial License Agreements.
  23. **
  24. ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
  25. ** licensing are not clear to you.
  26. **
  27. **********************************************************************/
  28. #ifndef __KDCHARTDATAINTERN_H__
  29. #define __KDCHARTDATAINTERN_H__
  30. #include <tqvariant.h>
  31. #include <tqstring.h>
  32. #include <tqdatetime.h>
  33. #include "KDChartGlobal.h"
  34. /**
  35. \file KDChartData.h
  36. \brief Provides a class to encapsulate one data value in a chart.
  37. */
  38. // Please leave all methods in this class inline!
  39. // It's necessary since it's part of the interface provided by KDChart
  40. class KDCHART_EXPORT KDChartData
  41. {
  42. public:
  43. // 0. default c'tor: initializing all values as undefined
  44. KDChartData() :
  45. _valueType( TQVariant::Invalid ),
  46. _valueType2( TQVariant::Invalid ),
  47. _propSetID( 0 )
  48. {}
  49. // 1. simple c'tors: used for 1-coordinate data
  50. KDChartData( double value ) :
  51. _valueType( TQVariant::Double ), dValue( value ),
  52. _valueType2( TQVariant::Invalid ),
  53. _propSetID( 0 )
  54. {}
  55. /* string values are only supported for legend texts or axis labels */
  56. KDChartData( const TQString& value ) :
  57. _valueType( TQVariant::String ), sValue( value ),
  58. _valueType2( TQVariant::Invalid ),
  59. _propSetID( 0 )
  60. {}
  61. /* date/time values for /ordinate/ axes are not implemented yet
  62. KDChartData( TQDateTime value ) :
  63. _valueType( TQVariant::DateTime ), dtValue( value ),
  64. _valueType2( TQVariant::Invalid ),
  65. _propSetID( 0 )
  66. {}*/
  67. // 2. complex c'tors: used for 2-coordinate data
  68. // 2.a) with additional Date/Time: normally used when Date on x-axis
  69. // e.g. for time related index numbers like water level measurements
  70. KDChartData( double yValue, TQDateTime xValue ) :
  71. _valueType( TQVariant::Double ), dValue( yValue ),
  72. _valueType2( TQVariant::DateTime ), dtValue2( xValue ),
  73. _propSetID( 0 )
  74. {}
  75. KDChartData( const TQVariant& _value1,
  76. const TQVariant& _value2 )
  77. {
  78. switch( _value1.type() ){
  79. case TQVariant::Int:
  80. case TQVariant::UInt:
  81. case TQVariant::Double:
  82. _valueType = TQVariant::Double;
  83. dValue = _value1.toDouble();
  84. break;
  85. case TQVariant::DateTime:
  86. _valueType = TQVariant::DateTime;
  87. dtValue = _value1.toDateTime();
  88. break;
  89. case TQVariant::String:
  90. _valueType = TQVariant::String;
  91. sValue = _value1.toString();
  92. break;
  93. default:
  94. _valueType = TQVariant::Invalid;
  95. }
  96. switch( _value2.type() ){
  97. case TQVariant::Int:
  98. case TQVariant::UInt:
  99. case TQVariant::Double:
  100. _valueType2 = TQVariant::Double;
  101. dValue2 = _value2.toDouble();
  102. break;
  103. case TQVariant::DateTime:
  104. _valueType2 = TQVariant::DateTime;
  105. dtValue2 = _value2.toDateTime();
  106. break;
  107. default:
  108. _valueType2 = TQVariant::Invalid;
  109. }
  110. _propSetID = 0;
  111. }
  112. /* date/time values for /ordinate/ axes are not implemented yet
  113. KDChartData( TQDateTime yValue, TQDateTime xValue ) :
  114. _valueType( DateTime ), dtValue( yValue ),
  115. _valueType2( DateTime ), dtValue2( xValue ),
  116. _propSetID( 0 )
  117. {}*/
  118. // 2.b) with additional Double: may be used for mathematical data...
  119. KDChartData( double yValue, double xValue ) :
  120. _valueType( TQVariant::Double ), dValue( yValue ),
  121. _valueType2( TQVariant::Double ), dValue2( xValue ),
  122. _propSetID( 0 )
  123. {}
  124. /* date/time values for /ordinate/ axes are not implemented yet
  125. KDChartData( TQDateTime yValue, double xValue ) :
  126. _valueType( DateTime ), dtValue( yValue ),
  127. _valueType2( Double ), dValue2( xValue ),
  128. _propSetID( 0 )
  129. {}*/
  130. /*
  131. /-**
  132. Copy constructor.
  133. \sa setData
  134. * /
  135. KDChartData( const KDChartData& other ) :
  136. {
  137. setData( other );
  138. }
  139. /-**
  140. Assignment operator.
  141. \sa setData
  142. * /
  143. KDChartData& operator=( const KDChartData& R )
  144. {
  145. setData( R );
  146. return *this;
  147. }
  148. */
  149. /**
  150. Compare operator.
  151. \sa isEqual
  152. */
  153. bool operator==( const KDChartData& it ) const
  154. {
  155. return isEqual(*this, it);
  156. }
  157. /**
  158. Compare operator.
  159. \sa isEqual
  160. */
  161. bool operator!=( const KDChartData& it ) const
  162. {
  163. return !isEqual(*this, it);
  164. }
  165. /**
  166. Compare method.
  167. \note The property set ID is <b>not</b> taken into account while comparing.
  168. Two KDChartData are considered equal if their data values are equal - the
  169. property set ID is ignored.
  170. */
  171. bool isEqual( const KDChartData& a, const KDChartData& b ) const
  172. {
  173. bool bRet = (a.hasValue( 1 ) == b.hasValue( 1 )) &&
  174. (a.hasValue( 2 ) == b.hasValue( 2 ));
  175. if ( bRet && a.hasValue( 1 ) ) {
  176. bRet = a.valueType( 1 ) == b.valueType( 1 );
  177. if ( bRet ) {
  178. switch ( a.valueType( 1 ) ) {
  179. case TQVariant::String:
  180. bRet = a.stringValue( 1 ) == b.stringValue( 1 );
  181. break;
  182. case TQVariant::Double:
  183. bRet = a.doubleValue( 1 ) == b.doubleValue( 1 );
  184. break;
  185. case TQVariant::DateTime:
  186. bRet = a.dateTimeValue( 1 ) == b.dateTimeValue( 1 );
  187. break;
  188. default:
  189. bRet = false;
  190. }
  191. }
  192. if ( bRet && a.hasValue( 2 ) ) {
  193. bRet = a.valueType( 2 ) == b.valueType( 2 );
  194. if ( bRet ) {
  195. switch ( a.valueType( 2 ) ) {
  196. // note: the 2nd value can not be a string
  197. // - must be a date or a number!
  198. case TQVariant::Double:
  199. bRet = a.doubleValue( 2 ) == b.doubleValue( 2 );
  200. break;
  201. case TQVariant::DateTime:
  202. bRet = a.dateTimeValue( 2 ) == b.dateTimeValue( 2 );
  203. break;
  204. default:
  205. bRet = false;
  206. }
  207. }
  208. }
  209. // Note: We do *not* compare the _propSetID here since it contains
  210. // no values but is used to handle some layout information...
  211. }
  212. return bRet;
  213. }
  214. /**
  215. Method setAll.
  216. \note The property set ID is <b>also</b> changed by the setAll method.
  217. \sa setData
  218. */
  219. void setAll( const KDChartData& R )
  220. {
  221. setData( R );
  222. setPropertySet( R.propertySet() );
  223. }
  224. /**
  225. Method setData.
  226. \note The property set ID is <b>not</b> changed by the setData method.
  227. If you want to set it please call \c KDChartData::setPropertySet() explicitely.
  228. \sa setAll
  229. */
  230. void setData( const KDChartData& R )
  231. {
  232. if( &R != this ){
  233. _valueType = R._valueType;
  234. _valueType2 = R._valueType2;
  235. switch ( valueType( 1 ) ) {
  236. case TQVariant::String:
  237. sValue = R.sValue;
  238. break;
  239. case TQVariant::Double:
  240. dValue = R.dValue;
  241. break;
  242. case TQVariant::DateTime:
  243. dtValue = R.dtValue;
  244. break;
  245. default:
  246. /* NOOP */;
  247. }
  248. switch ( valueType( 2 ) ) {
  249. // note: the 2nd value can not be a string
  250. // - must be a date or a number!
  251. case TQVariant::Double:
  252. dValue2 = R.dValue2;
  253. break;
  254. case TQVariant::DateTime:
  255. dtValue2 = R.dtValue2;
  256. break;
  257. default:
  258. /* NOOP */;
  259. }
  260. // Note: We do *not* copy the _propSetID here since it contains
  261. // no values but is used to handle some layout information...
  262. }
  263. }
  264. TQVariant::Type valueType( int valNo=1 ) const
  265. {
  266. return (1 == valNo)
  267. ? _valueType
  268. : _valueType2;
  269. }
  270. bool hasValue( int valNo=1 ) const
  271. {
  272. return (1 == valNo)
  273. ? (_valueType != TQVariant::Invalid)
  274. : ((_valueType2 == TQVariant::Double) || (_valueType2 == TQVariant::DateTime));
  275. }
  276. /* string values are only supported for legend texts or axis labels */
  277. bool isString( int valNo=1 ) const
  278. {
  279. return (1 == valNo)
  280. ? (_valueType == TQVariant::String)
  281. : false;
  282. }
  283. bool isDouble( int valNo=1 ) const
  284. {
  285. return (1 == valNo)
  286. ? (_valueType == TQVariant::Double)
  287. : (_valueType2 == TQVariant::Double);
  288. }
  289. bool isDateTime( int valNo=1 ) const
  290. {
  291. return (1 == valNo)
  292. ? (_valueType == TQVariant::DateTime)
  293. : (_valueType2 == TQVariant::DateTime);
  294. }
  295. void clearValue()
  296. {
  297. _valueType = TQVariant::Invalid;
  298. _valueType2 = TQVariant::Invalid;
  299. _propSetID = 0;
  300. }
  301. TQVariant value( int valNo=1 ) const
  302. {
  303. if( 1 == valNo )
  304. switch ( valueType( 1 ) ) {
  305. case TQVariant::String:
  306. return sValue;
  307. case TQVariant::Double:
  308. return dValue;
  309. case TQVariant::DateTime:
  310. return dtValue;
  311. default:
  312. return TQVariant();
  313. }
  314. else if( 2 == valNo )
  315. switch ( valueType( 2 ) ) {
  316. case TQVariant::Double:
  317. return dValue2;
  318. case TQVariant::DateTime:
  319. return dtValue2;
  320. default:
  321. return TQVariant();
  322. }
  323. else
  324. return TQVariant();
  325. }
  326. /* string values are only supported for legend texts or axis labels */
  327. TQString stringValue( int valNo=1 ) const
  328. {
  329. // note: the 2nd value can not be a string
  330. // - must be a date or a number!
  331. if ((1 == valNo) && isString( valNo ))
  332. return sValue;
  333. else
  334. return TQString();
  335. }
  336. double doubleValue( int valNo=1 ) const
  337. {
  338. return isDouble( valNo )
  339. ? ((1 == valNo) ? dValue : dValue2)
  340. : DBL_MIN;
  341. }
  342. TQDateTime dateTimeValue( int valNo=1 ) const
  343. {
  344. return isDateTime( valNo )
  345. ? ((1 == valNo) ? dtValue : dtValue2)
  346. : TQDateTime();
  347. }
  348. /**
  349. Assign a property set to a data cell.
  350. \param propSetID The ID of the property set to be assigned to this data cell.
  351. This ID can either be one of the built-in IDs documented
  352. at KDChartPropertySet::BuiltinDataPropertySetIDs or
  353. a special ID that was given back by a
  354. KDChartParams::registerProperties function call.
  355. \sa propertySet
  356. \sa KDCHART_PROPSET_NORMAL_DATA, KDCHART_PROPSET_TRANSPARENT_DATA
  357. */
  358. void setPropertySet( int propSetID = 0 )
  359. {
  360. _propSetID = propSetID;
  361. }
  362. /**
  363. Return the ID of the property set that is assigned to this data cell.
  364. Use KDChartParams::properties( int ID ) for accessing the respective property set information.
  365. \sa setPropertySet
  366. \sa KDCHART_PROPSET_NORMAL_DATA, KDCHART_PROPSET_TRANSPARENT_DATA
  367. */
  368. int propertySet() const
  369. {
  370. return _propSetID;
  371. }
  372. private:
  373. // OBLIGATORY 1st value: usually used for ordinate axis
  374. TQVariant::Type _valueType;
  375. TQDateTime dtValue;
  376. double dValue; // Sorry, d(t)Value and sValue cannot be a union,
  377. TQString sValue; // since TQString has a non-default constructor.
  378. // OPTIONAL 2nd value: if valid, normally used for abscissa axis
  379. // note: this 2nd value can not be a string - must be a date or a number!
  380. TQVariant::Type _valueType2;
  381. TQDateTime dtValue2;
  382. double dValue2;
  383. // ID number of the property set assigned to this cell
  384. int _propSetID;
  385. };
  386. #endif