KOffice – TDE office suite
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

438 строки
10 KiB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2003,2004 Ariya Hidayat <ariya@kde.org>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #ifndef KSPREAD_VALUE_H
  17. #define KSPREAD_VALUE_H
  18. #include <tqdatetime.h>
  19. #include <tqstring.h>
  20. #include <tqtextstream.h>
  21. #include <koffice_export.h>
  22. namespace KSpread
  23. {
  24. class ValueData;
  25. /**
  26. * Provides a wrapper for cell value.
  27. *
  28. * Each cell in a worksheet must hold a value, either as enterred by user
  29. * or as a result of formula evaluation. Default cell holds empty value.
  30. *
  31. * Value uses implicit data sharing to reduce memory usage.
  32. */
  33. class KSPREAD_EXPORT Value
  34. {
  35. public:
  36. typedef enum {
  37. Empty,
  38. Boolean,
  39. Integer,
  40. Float,
  41. String,
  42. Array,
  43. CellRange, // not used yet
  44. Error
  45. } Type;
  46. typedef enum {
  47. fmt_None,
  48. fmt_Boolean,
  49. fmt_Number,
  50. fmt_Percent,
  51. fmt_Money,
  52. fmt_DateTime,
  53. fmt_Date,
  54. fmt_Time,
  55. fmt_String
  56. } Format;
  57. /**
  58. * Creates an empty value, i.e holds nothing.
  59. */
  60. Value();
  61. /**
  62. * Creates a value of certain type.
  63. */
  64. Value( Type _type );
  65. /**
  66. * Destroys the value.
  67. */
  68. virtual ~Value();
  69. /**
  70. * Creates a copy from another value.
  71. */
  72. Value( const Value& _value );
  73. /**
  74. * Assigns from another value.
  75. *
  76. * Because the data is implicitly shared, such assignment is very fast and
  77. * doesn't consume additional memory.
  78. */
  79. Value& operator= ( const Value& _value );
  80. /**
  81. * Assigns from another value. Same as above.
  82. */
  83. Value& assign( const Value& _value );
  84. /**
  85. * Creates a boolean value.
  86. */
  87. Value( bool b );
  88. /**
  89. * Creates an integer value.
  90. */
  91. Value( long i );
  92. /**
  93. * Creates an integer value.
  94. */
  95. Value( int i );
  96. /**
  97. * Create a floating-point value.
  98. */
  99. Value( double f );
  100. /**
  101. * Create a string value.
  102. */
  103. Value( const TQString& s );
  104. Value (const char *s);
  105. /**
  106. * Create a floating-point value from date/time.
  107. *
  108. * Internally date/time is represented as serial-number, i.e number of
  109. * elapsed day since reference date. Day 61 is defined as March 1, 1900.
  110. */
  111. Value( const TQDateTime& dt );
  112. /**
  113. * Create a floating-point value from time.
  114. * See also note above.
  115. */
  116. Value( const TQTime& time );
  117. /**
  118. * Create a floating-point value from date.
  119. * See also note above.
  120. */
  121. Value( const TQDate& date );
  122. /**
  123. * Create an array of values.
  124. */
  125. Value( unsigned columns, unsigned rows );
  126. /**
  127. * Returns the type of the value.
  128. */
  129. Type type() const;
  130. /** Returns the format of the value (i.e. how should it be interpreted) */
  131. Format format() const;
  132. /**
  133. * Returns true if empty.
  134. */
  135. bool isEmpty() const { return type() == Empty; }
  136. /**
  137. * Returns true if the type of this value is Boolean.
  138. */
  139. bool isBoolean() const { return type() == Boolean; }
  140. /**
  141. * Returns true if the type of this value is integer.
  142. */
  143. bool isInteger() const { return type() == Integer; }
  144. /**
  145. * Returns true if the type of this value is floating-point.
  146. */
  147. bool isFloat() const { return type() == Float; }
  148. /**
  149. * Returns true if the type of this value is either
  150. * integer or floating-point.
  151. */
  152. bool isNumber() const { return (type() == Integer) || (type() == Float); }
  153. /**
  154. * Returns true if the type of this value is string.
  155. */
  156. bool isString() const { return type() == String; }
  157. /**
  158. * Returns true if the type of this value is array.
  159. */
  160. bool isArray() const { return type() == Array; }
  161. /**
  162. * Returns true if this value holds error information.
  163. */
  164. bool isError() const { return type() == Error; }
  165. /**
  166. * Sets this value to another value.
  167. */
  168. void setValue( const Value& v );
  169. /**
  170. * Sets this value to boolean value.
  171. */
  172. void setValue( bool b );
  173. /**
  174. * Sets this value to integer value.
  175. */
  176. void setValue( long i );
  177. /**
  178. * Sets this value to integer value.
  179. */
  180. void setValue( int i );
  181. /**
  182. * Sets this value to floating-point value.
  183. */
  184. void setValue( double f );
  185. /**
  186. * Sets this value to string value.
  187. */
  188. void setValue( const TQString& s );
  189. /**
  190. * Sets this value to hold error message.
  191. */
  192. void setError( const TQString& msg );
  193. /**
  194. * Sets this value to floating-point number representing the date/time.
  195. */
  196. void setValue( const TQDateTime& dt );
  197. /**
  198. * Sets this value to floating-point number representing the date.
  199. */
  200. void setValue( const TQTime& dt );
  201. /**
  202. * Sets this value to floating-point number representing the time.
  203. */
  204. void setValue( const TQDate& dt );
  205. /** Sets format information for this value. */
  206. void setFormat (Format fmt);
  207. /**
  208. * Returns the boolean value of this value.
  209. *
  210. * Call this function only if isBoolean() returns true.
  211. */
  212. bool asBoolean() const;
  213. /**
  214. * Returns the integer value of this value.
  215. *
  216. * Call this function only if isNumber() returns true.
  217. */
  218. long asInteger() const;
  219. /**
  220. * Returns the floating-point value of this value.
  221. *
  222. * Call this function only if isNumber() returns true.
  223. */
  224. double asFloat() const;
  225. /**
  226. * Returns the string value of this value.
  227. *
  228. * Call this function only if isString() returns true.
  229. */
  230. TQString asString() const;
  231. /**
  232. * Returns the date/time representation of this value.
  233. */
  234. TQDateTime asDateTime() const;
  235. /**
  236. * Returns the date representation of this value.
  237. */
  238. TQDate asDate() const;
  239. /**
  240. * Returns the time representation of this value.
  241. */
  242. TQTime asTime() const;
  243. /**
  244. * Return an element in the array value.
  245. */
  246. Value element( unsigned column, unsigned row ) const;
  247. /**
  248. * Sets an element in the array value. Do not use if isArray() is false.
  249. */
  250. void setElement( unsigned column, unsigned row, const Value& value );
  251. /**
  252. * If this value is an array, return the number of columns.
  253. * Returns 1 if isArray() returns false.
  254. */
  255. unsigned columns() const;
  256. /**
  257. * If this value is an array, return the number of rows.
  258. * Returns 1 if isArray() returns false.
  259. */
  260. unsigned rows() const;
  261. /**
  262. * Returns error message associated with this value.
  263. *
  264. * Call this function only if isError() returns true.
  265. */
  266. TQString errorMessage() const;
  267. /**
  268. * Detaches itself from shared value data, i.e make a private, deep copy
  269. * of the data. Usually this function is called automatically so you
  270. * don't have to care about it.
  271. */
  272. void detach();
  273. /**
  274. * Returns constant reference to empty value.
  275. */
  276. static const Value& empty();
  277. /**
  278. * Returns constant reference to #DIV/0! error.
  279. *
  280. * This is used to indicate that a formula divides by 0 (zero).
  281. */
  282. static const Value& errorDIV0();
  283. /**
  284. * Returns constant reference to #N/A error.
  285. *
  286. * This is to indicate that a value is not available to a function.
  287. */
  288. static const Value& errorNA();
  289. /**
  290. * Returns constant reference to #NAME? error.
  291. *
  292. * This is to indicate that certain text inside formula is not
  293. * recognized, possibly a misspelled name or name that
  294. * does not exist.
  295. */
  296. static const Value& errorNAME();
  297. /**
  298. * Returns constant reference to #NUM! error.
  299. *
  300. * This is to indicate a problem with a number in a formula.
  301. */
  302. static const Value& errorNUM();
  303. /**
  304. * Returns constant reference to #NULL! error.
  305. *
  306. * This is to indicate that two area do not intersect.
  307. */
  308. static const Value& errorNULL();
  309. /**
  310. * Returns constant reference to #REF! error.
  311. *
  312. * This is used to indicate an invalid cell reference.
  313. */
  314. static const Value& errorREF();
  315. /**
  316. * Returns constant reference to #VALUE! error.
  317. *
  318. * This is to indicate that wrong type of argument or operand
  319. * is used, usually within a function call, e.g SIN("some text").
  320. */
  321. static const Value& errorVALUE();
  322. /**
  323. * Returns true if it is OK to compare this value with v.
  324. * If this function returns false, then return value of compare is undefined.
  325. */
  326. bool allowComparison( const Value& v ) const;
  327. /**
  328. * Returns -1, 0, 1, depends whether this value is less than, equal to, or
  329. * greater than v.
  330. */
  331. int compare( const Value& v ) const;
  332. /**
  333. * Returns true if this value is equal to v.
  334. */
  335. bool equal( const Value& v ) const;
  336. /**
  337. * Returns true if this value is less than v.
  338. */
  339. bool less( const Value& v ) const;
  340. /**
  341. * Returns true if this value is greater than v.
  342. */
  343. bool greater( const Value& v ) const;
  344. // comparison operator - returns true only if strictly identical, unlike equal()/compare()
  345. bool operator==( const Value& v ) const;
  346. inline bool operator!=( const Value& other ) const { return !operator==( other ); }
  347. static int compare( double v1, double v2 );
  348. bool isZero() const;
  349. static bool isZero( double v );
  350. protected:
  351. ValueData* d; // can't never be 0
  352. };
  353. } // namespace KSpread
  354. TQTextStream& operator<<( TQTextStream& ts, KSpread::Value::Type type );
  355. TQTextStream& operator<<( TQTextStream& ts, KSpread::Value value );
  356. #endif // KSPREAD_VALUE_H