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.

402 lines
12KB

  1. /* This file is part of the KDE project
  2. Copyright 2004 Laurent Montel <montel@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, 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 "kspread_genvalidationstyle.h"
  17. #include "kspread_cell.h"
  18. #include <KoXmlWriter.h>
  19. using namespace KSpread;
  20. GenValidationStyles::GenValidationStyles()
  21. {
  22. }
  23. GenValidationStyles::~GenValidationStyles()
  24. {
  25. }
  26. TQString GenValidationStyles::lookup( const GenValidationStyle& style )
  27. {
  28. StyleMap::iterator it = m_styles.find( style );
  29. if ( it == m_styles.end() ) {
  30. TQString styleName( "val" );
  31. styleName = makeUniqueName( styleName );
  32. m_names.insert( styleName, true );
  33. it = m_styles.insert( style, styleName );
  34. }
  35. return it.data();
  36. }
  37. TQString GenValidationStyles::makeUniqueName( const TQString& base ) const
  38. {
  39. int num = 1;
  40. TQString name;
  41. do {
  42. name = base;
  43. name += TQString::number( num++ );
  44. } while ( m_names.find( name ) != m_names.end() );
  45. return name;
  46. }
  47. void GenValidationStyles::writeStyle( KoXmlWriter& writer )
  48. {
  49. if ( m_styles.count()>0 )
  50. {
  51. writer.startElement( "table:content-validations" );
  52. StyleMap::Iterator it;
  53. for ( it = m_styles.begin(); it != m_styles.end(); ++it )
  54. {
  55. writer.startElement( "table:content-validation" );
  56. writer.addAttribute( "table:name", it.data() );
  57. writer.addAttribute( "table:allow-empty-cell", it.key().allowEmptyCell );
  58. writer.addAttribute( "table:condition", it.key().condition );
  59. writer.startElement( "table:help-message" );
  60. writer.addAttribute( "table:title", it.key().title );
  61. writer.addAttribute( "table:display", it.key().displayValidationInformation );
  62. TQStringList text = TQStringList::split( "\n", it.key().messageInfo );
  63. for ( TQStringList::Iterator it2 = text.begin(); it2 != text.end(); ++it2 ) {
  64. writer.startElement( "text:p" );
  65. writer.addTextNode( *it2 );
  66. writer.endElement();
  67. }
  68. writer.endElement();
  69. writer.startElement( "table:error-message" );
  70. writer.addAttribute( "table:message-type", it.key().messageType );
  71. writer.addAttribute("table:title", it.key().titleInfo);
  72. writer.addAttribute("table:display", it.key().displayMessage);
  73. text = TQStringList::split( "\n", it.key().message );
  74. for ( TQStringList::Iterator it3 = text.begin(); it3 != text.end(); ++it3 ) {
  75. writer.startElement( "text:p" );
  76. writer.addTextNode( *it3 );
  77. writer.endElement();
  78. }
  79. writer.endElement();
  80. writer.endElement();
  81. }
  82. writer.endElement();//close sheet:content-validation
  83. }
  84. }
  85. void GenValidationStyle::initVal( Validity *_val )
  86. {
  87. if ( _val )
  88. {
  89. allowEmptyCell = ( _val->allowEmptyCell ? "true" : "false" );
  90. condition = createValidationCondition( _val );
  91. title = _val->title;
  92. displayValidationInformation = ( _val->displayValidationInformation ? "true" : "false" );
  93. messageInfo = _val->messageInfo;
  94. switch( _val->m_action )
  95. {
  96. case Action::Warning:
  97. messageType = "warning";
  98. break;
  99. case Action::Information:
  100. messageType = "information";
  101. break;
  102. case Action::Stop:
  103. messageType = "stop";
  104. break;
  105. }
  106. titleInfo = _val->titleInfo;
  107. displayMessage = ( _val->displayMessage ? "true" : "false" );
  108. message = _val->message;
  109. }
  110. }
  111. TQString GenValidationStyle::createValidationCondition( Validity* _val )
  112. {
  113. TQString result;
  114. switch( _val->m_restriction )
  115. {
  116. case Restriction::None:
  117. //nothing
  118. break;
  119. case Restriction::Text:
  120. //doesn't exist into oo spec
  121. result = "cell-content-is-text()";
  122. break;
  123. case Restriction::Time:
  124. result = createTimeValidationCondition( _val );
  125. break;
  126. case Restriction::Date:
  127. result = createDateValidationCondition( _val );
  128. break;
  129. case Restriction::Integer:
  130. case Restriction::Number:
  131. result = createNumberValidationCondition( _val );
  132. break;
  133. case Restriction::TextLength:
  134. result = createTextValidationCondition( _val );
  135. break;
  136. case Restriction::List:
  137. result = createListValidationCondition( _val );
  138. break;
  139. }
  140. return result;
  141. }
  142. TQString GenValidationStyle::createListValidationCondition( Validity* _val )
  143. {
  144. TQString result = "oooc:cell-content-is-in-list(";
  145. result = _val->listValidity.join( ";" );
  146. result +=")";
  147. return result;
  148. }
  149. TQString GenValidationStyle::createNumberValidationCondition( Validity* _val )
  150. {
  151. TQString result;
  152. if ( _val->m_restriction == Restriction::Number )
  153. result = "oooc:cell-content-is-whole-number() and ";
  154. else if ( _val->m_restriction == Restriction::Integer )
  155. result = "oooc:cell-content-is-decimal-number() and ";
  156. switch( _val->m_cond )
  157. {
  158. case Conditional::None:
  159. //nothing
  160. break;
  161. case Conditional::Equal:
  162. result+="cell-content()";
  163. result+="=";
  164. result+=TQString::number( _val->valMin );
  165. break;
  166. case Conditional::Superior:
  167. result+="cell-content()";
  168. result+=">";
  169. result+=TQString::number( _val->valMin );
  170. break;
  171. case Conditional::Inferior:
  172. result+="cell-content()";
  173. result+="<";
  174. result+=TQString::number( _val->valMin );
  175. break;
  176. case Conditional::SuperiorEqual:
  177. result+="cell-content()";
  178. result+=">=";
  179. result+=TQString::number( _val->valMin );
  180. break;
  181. case Conditional::InferiorEqual:
  182. result+="cell-content()";
  183. result+="<=";
  184. result+=TQString::number( _val->valMin );
  185. break;
  186. case Conditional::Different:
  187. result+="cell-content()";
  188. result+="!=";
  189. result+=TQString::number( _val->valMin );
  190. break;
  191. case Conditional::Between:
  192. result+="cell-content-is-between(";
  193. result+=TQString::number( _val->valMin );
  194. result+=",";
  195. result+=TQString::number( _val->valMax );
  196. result+=")";
  197. break;
  198. case Conditional::DifferentTo:
  199. result+="cell-content-is-not-between(";
  200. result+=TQString::number( _val->valMin );
  201. result+=",";
  202. result+=TQString::number( _val->valMax );
  203. result+=")";
  204. break;
  205. }
  206. return result;
  207. }
  208. TQString GenValidationStyle::createTimeValidationCondition( Validity* _val )
  209. {
  210. TQString result( "oooc:cell-content-is-time() and " );
  211. switch( _val->m_cond )
  212. {
  213. case Conditional::None:
  214. //nothing
  215. break;
  216. case Conditional::Equal:
  217. result+="cell-content()";
  218. result+="=";
  219. result+=_val->timeMin.toString( );
  220. break;
  221. case Conditional::Superior:
  222. result+="cell-content()";
  223. result+=">";
  224. result+=_val->timeMin.toString( );
  225. break;
  226. case Conditional::Inferior:
  227. result+="cell-content()";
  228. result+="<";
  229. result+=_val->timeMin.toString( );
  230. break;
  231. case Conditional::SuperiorEqual:
  232. result+="cell-content()";
  233. result+=">=";
  234. result+=_val->timeMin.toString( );
  235. break;
  236. case Conditional::InferiorEqual:
  237. result+="cell-content()";
  238. result+="<=";
  239. result+=_val->timeMin.toString( );
  240. break;
  241. case Conditional::Different:
  242. result+="cell-content()";
  243. result+="!=";
  244. result+=_val->timeMin.toString( );
  245. break;
  246. case Conditional::Between:
  247. result+="cell-content-is-between(";
  248. result+=_val->timeMin.toString( );
  249. result+=",";
  250. result+=_val->timeMax.toString( );
  251. result+=")";
  252. break;
  253. case Conditional::DifferentTo:
  254. result+="cell-content-is-not-between(";
  255. result+=_val->timeMin.toString( );
  256. result+=",";
  257. result+=_val->timeMax.toString( );
  258. result+=")";
  259. break;
  260. }
  261. return result;
  262. }
  263. TQString GenValidationStyle::createDateValidationCondition( Validity* _val )
  264. {
  265. TQString result( "oooc:cell-content-is-date() and " );
  266. switch( _val->m_cond )
  267. {
  268. case Conditional::None:
  269. //nothing
  270. break;
  271. case Conditional::Equal:
  272. result+="cell-content()";
  273. result+="=";
  274. result+=_val->dateMin.toString();
  275. break;
  276. case Conditional::Superior:
  277. result+="cell-content()";
  278. result+=">";
  279. result+=_val->dateMin.toString();
  280. break;
  281. case Conditional::Inferior:
  282. result+="cell-content()";
  283. result+="<";
  284. result+=_val->dateMin.toString();
  285. break;
  286. case Conditional::SuperiorEqual:
  287. result+="cell-content()";
  288. result+=">=";
  289. result+=_val->dateMin.toString();
  290. break;
  291. case Conditional::InferiorEqual:
  292. result+="cell-content()";
  293. result+="<=";
  294. result+=_val->dateMin.toString();
  295. break;
  296. case Conditional::Different:
  297. result+="cell-content()";
  298. result+="!=";
  299. result+=_val->dateMin.toString();
  300. break;
  301. case Conditional::Between:
  302. result+="cell-content-is-between(";
  303. result+=_val->dateMin.toString();
  304. result+=",";
  305. result+=_val->dateMax.toString();
  306. result+=")";
  307. break;
  308. case Conditional::DifferentTo:
  309. result+="cell-content-is-not-between(";
  310. result+=_val->dateMin.toString();
  311. result+=",";
  312. result+=_val->dateMax.toString();
  313. result+=")";
  314. break;
  315. }
  316. return result;
  317. }
  318. TQString GenValidationStyle::createTextValidationCondition( Validity* _val )
  319. {
  320. TQString result;
  321. switch( _val->m_cond )
  322. {
  323. case Conditional::None:
  324. //nothing
  325. break;
  326. case Conditional::Equal:
  327. result+="oooc:cell-content-text-length()";
  328. result+="=";
  329. result+=TQString::number( _val->valMin );
  330. break;
  331. case Conditional::Superior:
  332. result+="oooc:cell-content-text-length()";
  333. result+=">";
  334. result+=TQString::number( _val->valMin );
  335. break;
  336. case Conditional::Inferior:
  337. result+="oooc:cell-content-text-length()";
  338. result+="<";
  339. result+=TQString::number( _val->valMin );
  340. break;
  341. case Conditional::SuperiorEqual:
  342. result+="oooc:cell-content-text-length()";
  343. result+=">=";
  344. result+=TQString::number( _val->valMin );
  345. break;
  346. case Conditional::InferiorEqual:
  347. result+="oooc:cell-content-text-length()";
  348. result+="<=";
  349. result+=TQString::number( _val->valMin );
  350. break;
  351. case Conditional::Different:
  352. result+="oooc:cell-content-text-length()";
  353. result+="!=";
  354. result+=TQString::number( _val->valMin );
  355. break;
  356. case Conditional::Between:
  357. result+="oooc:cell-content-text-length-is-between(";
  358. result+=TQString::number( _val->valMin );
  359. result+=",";
  360. result+=TQString::number( _val->valMax );
  361. result+=")";
  362. break;
  363. case Conditional::DifferentTo:
  364. result+="oooc:cell-content-text-length-is-not-between(";
  365. result+=TQString::number( _val->valMin );
  366. result+=",";
  367. result+=TQString::number( _val->valMax );
  368. result+=")";
  369. break;
  370. }
  371. return result;
  372. }