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.

343 lines
8.4KB

  1. /* This file is part of the KDE project
  2. Copyright 2004 Tomas Mecir <mecirt@gmail.com>
  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, or (at your option) any later version.
  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. #include "valueconverter.h"
  17. #include "kspread_locale.h"
  18. #include "valueparser.h"
  19. using namespace KSpread;
  20. ValueConverter::ValueConverter (ValueParser* p) : parser( p )
  21. {
  22. }
  23. TDELocale* ValueConverter::locale()
  24. {
  25. return parser->locale();
  26. }
  27. Value ValueConverter::asBoolean (const Value &value) const
  28. {
  29. Value val;
  30. bool ok;
  31. switch (value.type()) {
  32. case Value::Empty:
  33. val.setValue (false);
  34. break;
  35. case Value::Boolean:
  36. val = value;
  37. break;
  38. case Value::Integer:
  39. val.setValue (value.asInteger() ? true : false);
  40. break;
  41. case Value::Float:
  42. val.setValue ((value.asFloat() == 0.0) ? false : true);
  43. break;
  44. case Value::String:
  45. val = parser->tryParseBool (value.asString(), &ok);
  46. if (!ok)
  47. val.setValue (false);
  48. break;
  49. case Value::Array:
  50. val = asBoolean (value.element (0, 0));
  51. break;
  52. case Value::CellRange:
  53. /* NOTHING */
  54. break;
  55. case Value::Error:
  56. val.setValue (false);
  57. break;
  58. };
  59. return val;
  60. }
  61. Value ValueConverter::asInteger (const Value &value) const
  62. {
  63. Value val;
  64. bool ok;
  65. switch (value.type()) {
  66. case Value::Empty:
  67. val.setValue (0);
  68. break;
  69. case Value::Boolean:
  70. val.setValue (value.asBoolean() ? 1 : 0);
  71. break;
  72. case Value::Integer:
  73. val = value;
  74. break;
  75. case Value::Float:
  76. val.setValue (value.asInteger());
  77. break;
  78. case Value::String:
  79. val.setValue ((int) parser->tryParseNumber
  80. (value.asString(), &ok).asFloat());
  81. if (!ok)
  82. val.setValue (0);
  83. break;
  84. case Value::Array:
  85. val = asInteger (value.element (0, 0));
  86. break;
  87. case Value::CellRange:
  88. /* NOTHING */
  89. break;
  90. case Value::Error:
  91. val.setValue (0);
  92. break;
  93. };
  94. return val;
  95. }
  96. Value ValueConverter::asFloat (const Value &value) const
  97. {
  98. Value val;
  99. bool ok;
  100. switch (value.type()) {
  101. case Value::Empty:
  102. val.setValue (0.0);
  103. break;
  104. case Value::Boolean:
  105. val.setValue (value.asBoolean() ? 1.0 : 0.0);
  106. break;
  107. case Value::Integer:
  108. val.setValue (value.asFloat ());
  109. break;
  110. case Value::Float:
  111. val = value;
  112. break;
  113. case Value::String:
  114. val = parser->tryParseNumber (value.asString(), &ok);
  115. if (!ok)
  116. val.setValue (0.0);
  117. break;
  118. case Value::Array:
  119. val = asFloat (value.element (0, 0));
  120. break;
  121. case Value::CellRange:
  122. /* NOTHING */
  123. break;
  124. case Value::Error:
  125. val.setValue (0.0);
  126. break;
  127. };
  128. return val;
  129. }
  130. Value ValueConverter::asNumeric (const Value &value) const
  131. {
  132. // as of now, we simply go to float; later on, further datatypes
  133. // may be possible as a result of this
  134. return asFloat (value);
  135. }
  136. Value ValueConverter::asString (const Value &value) const
  137. {
  138. // This is a simpler version of ValueFormatter... We cannot use that one,
  139. // as we sometimes want to generate the string differently ...
  140. Value val;
  141. TQString s;
  142. Value::Format fmt;
  143. TQChar decimal_point;
  144. int pos;
  145. switch (value.type()) {
  146. case Value::Empty:
  147. val = TQString();
  148. break;
  149. case Value::Boolean:
  150. val.setValue (value.asBoolean() ? parser->locale()->translate ("True") :
  151. parser->locale()->translate ("False"));
  152. break;
  153. case Value::Integer:
  154. {
  155. fmt = value.format();
  156. if (fmt == Value::fmt_Percent)
  157. val = TQString::number (value.asInteger() * 100) + " %";
  158. else if (fmt == Value::fmt_DateTime)
  159. val = parser->locale()->formatDateTime (value.asDateTime());
  160. else if (fmt == Value::fmt_Date)
  161. val = parser->locale()->formatDate (value.asDate());
  162. else if (fmt == Value::fmt_Time)
  163. val = parser->locale()->formatTime (value.asTime());
  164. else
  165. val = TQString::number (value.asInteger());
  166. }
  167. break;
  168. case Value::Float:
  169. fmt = value.format();
  170. if (fmt == Value::fmt_DateTime)
  171. val = parser->locale()->formatDateTime (value.asDateTime());
  172. else if (fmt == Value::fmt_Date)
  173. val = parser->locale()->formatDate (value.asDate(), true);
  174. else if (fmt == Value::fmt_Time)
  175. val = parser->locale()->formatTime (value.asTime());
  176. else
  177. {
  178. //convert the number, change decimal point from English to local
  179. s = TQString::number (value.asFloat(), 'g', 10);
  180. decimal_point = parser->locale()->decimalSymbol()[0];
  181. if (decimal_point && ((pos = s.find ('.')) != -1))
  182. s = s.replace (pos, 1, decimal_point);
  183. if (fmt == Value::fmt_Percent)
  184. s += " %";
  185. val.setValue (s);
  186. }
  187. break;
  188. case Value::String:
  189. val = value;
  190. break;
  191. case Value::Array:
  192. val = asString (value.element (0, 0));
  193. break;
  194. case Value::CellRange:
  195. /* NOTHING */
  196. break;
  197. case Value::Error:
  198. val.setValue (value.errorMessage ());
  199. break;
  200. };
  201. return val;
  202. }
  203. Value ValueConverter::asDateTime (const Value &value) const
  204. {
  205. Value val;
  206. bool ok;
  207. switch (value.type()) {
  208. case Value::Empty:
  209. val.setValue (TQDateTime::currentDateTime());
  210. break;
  211. case Value::Boolean:
  212. //ignore the bool value... any better idea? ;)
  213. val.setValue (TQDateTime::currentDateTime());
  214. break;
  215. case Value::Integer:
  216. val.setValue (value.asFloat());
  217. val.setFormat (Value::fmt_DateTime);
  218. break;
  219. case Value::Float:
  220. val.setValue (value.asFloat());
  221. val.setFormat (Value::fmt_DateTime);
  222. break;
  223. case Value::String:
  224. //no DateTime parser, so we parse as Date, hoping for the best ...
  225. val = parser->tryParseDate (value.asString(), &ok);
  226. if (!ok)
  227. val = Value::errorVALUE();
  228. val.setFormat (Value::fmt_DateTime);
  229. break;
  230. case Value::Array:
  231. val = asDateTime (value.element (0, 0));
  232. break;
  233. case Value::CellRange:
  234. /* NOTHING */
  235. break;
  236. case Value::Error:
  237. break;
  238. };
  239. return val;
  240. }
  241. Value ValueConverter::asDate (const Value &value) const
  242. {
  243. Value val;
  244. bool ok;
  245. switch (value.type()) {
  246. case Value::Empty:
  247. val.setValue (TQDate::currentDate());
  248. break;
  249. case Value::Boolean:
  250. //ignore the bool value... any better idea? ;)
  251. val.setValue (TQDate::currentDate());
  252. break;
  253. case Value::Integer:
  254. val.setValue (value.asFloat());
  255. val.setFormat (Value::fmt_Date);
  256. break;
  257. case Value::Float:
  258. val.setValue (value.asFloat());
  259. val.setFormat (Value::fmt_Date);
  260. break;
  261. case Value::String:
  262. val = parser->tryParseDate (value.asString(), &ok);
  263. if (!ok)
  264. val = Value::errorVALUE();
  265. break;
  266. case Value::Array:
  267. val = asDate (value.element (0, 0));
  268. break;
  269. case Value::CellRange:
  270. /* NOTHING */
  271. break;
  272. case Value::Error:
  273. break;
  274. };
  275. return val;
  276. }
  277. Value ValueConverter::asTime (const Value &value) const
  278. {
  279. Value val;
  280. bool ok;
  281. switch (value.type()) {
  282. case Value::Empty:
  283. val.setValue (TQTime::currentTime());
  284. break;
  285. case Value::Boolean:
  286. //ignore the bool value... any better idea? ;)
  287. val.setValue (TQTime::currentTime());
  288. break;
  289. case Value::Integer:
  290. val.setValue (value.asInteger());
  291. val.setFormat (Value::fmt_Time);
  292. break;
  293. case Value::Float:
  294. val.setValue (value.asFloat());
  295. val.setFormat (Value::fmt_Time);
  296. break;
  297. case Value::String:
  298. val = parser->tryParseTime (value.asString(), &ok);
  299. if (!ok)
  300. val = Value::errorVALUE();
  301. break;
  302. case Value::Array:
  303. val = asTime (value.element (0, 0));
  304. break;
  305. case Value::CellRange:
  306. /* NOTHING */
  307. break;
  308. case Value::Error:
  309. break;
  310. };
  311. return val;
  312. }