KOffice – TDE office suite
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

gnumericimport.cc 88KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. /* This file is part of the KDE project
  2. Copyright (C) 1999 David Faure <faure@kde.org>
  3. Copyright (C) 2005 Laurent Montel <montel@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. /* GNUmeric import filter by Phillip Ezolt 6-2-2001 */
  18. /* phillipezolt@hotmail.com */
  19. /* additions: Norbert Andres nandres@web.de */
  20. #include <tqdict.h>
  21. #include <tqfile.h>
  22. #include <tqstringlist.h>
  23. #include <gnumericimport.h>
  24. #include <tdemessagebox.h>
  25. #include <kfilterdev.h>
  26. #include <kdebug.h>
  27. #include <kgenericfactory.h>
  28. #include <KoFilterChain.h>
  29. #include <KoGlobal.h>
  30. // hehe >:->
  31. #include <kspread_doc.h>
  32. #include <kspread_map.h>
  33. #include <kspread_sheet.h>
  34. #include <kspread_sheetprint.h>
  35. #include <kspread_cell.h>
  36. #include <kspread_util.h>
  37. #include <KoDocumentInfo.h>
  38. #include <math.h>
  39. #define SECS_PER_DAY 86400
  40. #define HALF_SEC (0.5 / SECS_PER_DAY)
  41. using namespace KSpread;
  42. // copied from gnumeric: src/format.c:
  43. static const int g_dateSerial_19000228 = 59;
  44. /* One less that the Julian day number of 19000101. */
  45. static int g_dateOrigin = 0;
  46. // copied from gnumeric: src/formats.c:
  47. static char const * cell_format_date [] = {
  48. "m/d/yy", /* 0 Cell::date_format5*/
  49. "m/d/yyyy", /* 1 Cell::date_format6*/
  50. "d-mmm-yy", /* 2 Cell::date_format1 18-Feb-99 */
  51. "d-mmm-yyyy", /* 3 Cell::date_format2 18-Feb-1999 */
  52. "d-mmm", /* 4 Cell::date_format3 18-Feb */
  53. "d-mm", /* 5 Cell::date_format4 18-05 */
  54. "mmm/d", /* 6 Cell::date_format11*/
  55. "mm/d", /* 7 Cell::date_format12*/
  56. "mm/dd/yy", /* 8 Cell::date_format19*/
  57. "mm/dd/yyyy", /* 9 Cell::date_format18*/
  58. "mmm/dd/yy", /* 10 Cell::date_format20*/
  59. "mmm/dd/yyyy", /* 11 Cell::date_format21*/
  60. "mmm/ddd/yy", /* 12 */
  61. "mmm/ddd/yyyy", /* 13 */
  62. "mm/ddd/yy", /* 14 */
  63. "mm/ddd/yyyy", /* 15 */
  64. "mmm-yy", /* 16 Cell::date_format7*/
  65. "mmm-yyyy", /* 17 Cell::date_format22*/
  66. "mmmm-yy", /* 18 Cell::date_format8*/
  67. "mmmm-yyyy", /* 19 Cell::date_format9*/
  68. "m/d/yy h:mm", /* 20 */
  69. "m/d/yyyy h:mm", /* 21 */
  70. "yyyy/mm/d", /* 22 Cell::date_format25*/
  71. "yyyy/mmm/d", /* 23 Cell::date_format14*/
  72. "yyyy/mm/dd", /* 24 Cell::date_format25*/
  73. "yyyy/mmm/dd", /* 25 Cell::date_format26*/
  74. "yyyy-mm-d", /* 26 Cell::date_format16*/
  75. "yyyy-mmm-d", /* 27 Cell::date_format15*/
  76. "yyyy-mm-dd", /* 28 Cell::date_format16*/
  77. "yyyy-mmm-dd", /* 29 Cell::date_format15*/
  78. "yy", /* 30 Cell::date_format24*/
  79. "yyyy", /* 31 Cell::date_format23*/
  80. NULL
  81. };
  82. // copied from gnumeric: src/formats.c:
  83. static char const * cell_format_time [] = {
  84. "h:mm AM/PM", // Cell::Time_format1 9 : 01 AM
  85. "h:mm:ss AM/PM", // Cell::Time_format2 9:01:05 AM
  86. "h:mm", // Cell::Time_format4 9:01
  87. "h:mm:ss", // Cell::Time_format5 9:01:12
  88. "m/d/yy h:mm",
  89. "mm:ss", // Cell::Time_format6 01:12
  90. "mm:ss.0", // Cell::Time_format6 01:12
  91. "[h]:mm:ss",
  92. "[h]:mm",
  93. "[mm]:ss",
  94. "[ss]",
  95. NULL
  96. };
  97. namespace gnumeric_import_LNS
  98. {
  99. TQStringList list1;
  100. TQStringList list2;
  101. }
  102. using namespace gnumeric_import_LNS;
  103. void GNUMERICFilter::dateInit()
  104. {
  105. // idea copied form gnumeric src/format.c:
  106. /* Day 1 means 1st of January of 1900 */
  107. g_dateOrigin = GnumericDate::greg2jul( 1900, 1, 1 ) - 1;
  108. }
  109. uint GNUMERICFilter::GnumericDate::greg2jul( int y, int m, int d )
  110. {
  111. return TQDate::gregorianToJulian( y, m, d );
  112. }
  113. void GNUMERICFilter::GnumericDate::jul2greg( double num, int & y, int & m, int & d )
  114. {
  115. int i = (int) floor( num + HALF_SEC );
  116. if (i > g_dateSerial_19000228)
  117. --i;
  118. else if (i == g_dateSerial_19000228 + 1)
  119. kdWarning(30521) << "Request for date 02/29/1900." << endl;
  120. kdDebug(30521) << "***** Num: " << num << ", i: " << i << endl;
  121. TQDate::julianToGregorian( i + g_dateOrigin, y, m, d );
  122. kdDebug(30521) << "y: " << y << ", m: " << m << ", d: " << d << endl;
  123. }
  124. TQTime GNUMERICFilter::GnumericDate::getTime( double num )
  125. {
  126. // idea copied from gnumeric: src/datetime.c
  127. num += HALF_SEC;
  128. int secs = tqRound( (num - floor(num)) * SECS_PER_DAY );
  129. kdDebug(30521) << "***** Num: " << num << ", secs " << secs << endl;
  130. const int h = secs / 3600;
  131. secs -= h * 3600;
  132. const int m = secs / 60;
  133. secs -= h * 60;
  134. kdDebug(30521) << "****** h: " << h << ", m: " << m << ", secs: " << secs << endl;
  135. const TQTime time( h, m, ( secs < 0 || secs > 59 ? 0 : secs ) );
  136. return time;
  137. }
  138. typedef KGenericFactory<GNUMERICFilter, KoFilter> GNUMERICFilterFactory;
  139. K_EXPORT_COMPONENT_FACTORY( libgnumericimport, GNUMERICFilterFactory( "kofficefilters" ) )
  140. GNUMERICFilter::GNUMERICFilter( KoFilter *, const char *, const TQStringList & )
  141. : KoFilter()
  142. {
  143. }
  144. /* This converts GNUmeric's color string "0:0:0" to a TQColor. */
  145. void convert_string_to_qcolor(TQString color_string, TQColor * color)
  146. {
  147. int red, green, blue, first_col_pos, second_col_pos;
  148. bool number_ok;
  149. first_col_pos = color_string.find(":", 0);
  150. second_col_pos = color_string.find(":", first_col_pos + 1);
  151. /* Fore="0:0:FF00" */
  152. /* If GNUmeric kicks out some invalid colors, we could crash. */
  153. /* GNUmeric gives us two bytes of color data per element. */
  154. /* We only care about the top byte. */
  155. red = color_string.mid( 0, first_col_pos).toInt( &number_ok, 16 ) >> 8;
  156. green = color_string.mid(first_col_pos + 1,
  157. (second_col_pos-first_col_pos) - 1).toInt( &number_ok, 16) >> 8;
  158. blue = color_string.mid(second_col_pos + 1,
  159. (color_string.length() - first_col_pos) - 1).toInt( &number_ok, 16) >> 8;
  160. color->setRgb(red, green, blue);
  161. }
  162. void areaNames( Doc * ksdoc, const TQString &_name, TQString _zone )
  163. {
  164. //Sheet2!$A$2:$D$8
  165. TQString tableName;
  166. int pos = _zone.find( '!' );
  167. if ( pos != -1 )
  168. {
  169. tableName = _zone.left( pos );
  170. _zone = _zone.right( _zone.length()-pos-1 );
  171. pos = _zone.find( ':' );
  172. TQRect rect;
  173. if ( pos != -1 )
  174. {
  175. TQString left = _zone.mid( 1, pos-1 );
  176. TQString right = _zone.mid( pos+2, _zone.length()-pos-2 );
  177. int pos = left.find( '$' );
  178. rect.setLeft( util_decodeColumnLabelText(left.left(pos ) ) );
  179. rect.setTop( left.right( left.length()-pos-1 ).toInt() );
  180. pos = right.find( '$' );
  181. rect.setRight( util_decodeColumnLabelText(right.left(pos ) ) );
  182. rect.setBottom( right.right( right.length()-pos-1 ).toInt() );
  183. }
  184. else
  185. {
  186. TQString left = _zone;
  187. int pos = left.find( '$' );
  188. int leftPos = util_decodeColumnLabelText(left.left(pos ) );
  189. rect.setLeft( leftPos );
  190. rect.setRight( leftPos );
  191. int top = left.right( left.length()-pos-1 ).toInt();
  192. rect.setTop( top );
  193. rect.setBottom( top );
  194. }
  195. ksdoc->addAreaName( rect, _name ,tableName);
  196. }
  197. }
  198. void set_document_area_names( Doc * ksdoc, TQDomElement * docElem )
  199. {
  200. TQDomNode areaNamesElement = docElem->namedItem( "gmr:Names" );
  201. if ( areaNamesElement.isNull() )
  202. return;
  203. TQDomNode areaNameItem = areaNamesElement.namedItem( "gmr:Name" );
  204. while ( !areaNameItem.isNull() )
  205. {
  206. TQDomNode gmr_name = areaNameItem.namedItem("gmr:name");
  207. TQDomNode gmr_value = areaNameItem.namedItem("gmr:value");
  208. TQString name = gmr_name.toElement().text();
  209. areaNames( ksdoc, name, gmr_value.toElement().text() );
  210. areaNameItem = areaNameItem.nextSibling();
  211. }
  212. }
  213. void set_document_attributes( Doc * ksdoc, TQDomElement * docElem)
  214. {
  215. ksdoc->loadConfigFromFile();
  216. TQDomNode attributes = docElem->namedItem("gmr:Attributes");
  217. if ( attributes.isNull() )
  218. return;
  219. TQDomNode attributeItem = attributes.namedItem("gmr:Attribute");
  220. while( !attributeItem.isNull() )
  221. {
  222. TQDomNode gmr_name = attributeItem.namedItem("gmr:name");
  223. TQDomNode gmr_value = attributeItem.namedItem("gmr:value");
  224. if (gmr_name.toElement().text() == "WorkbookView::show_horizontal_scrollbar")
  225. {
  226. ksdoc->setShowHorizontalScrollBar( gmr_value.toElement().text().lower()=="true"? true : false );
  227. }
  228. else if ( gmr_name.toElement().text() == "WorkbookView::show_vertical_scrollbar")
  229. {
  230. ksdoc->setShowVerticalScrollBar( gmr_value.toElement().text().lower()=="true"? true : false );
  231. }
  232. else if ( gmr_name.toElement().text() == "WorkbookView::show_notebook_tabs")
  233. {
  234. ksdoc->setShowTabBar(gmr_value.toElement().text().lower()=="true"? true : false );
  235. }
  236. else if ( gmr_name.toElement().text() == "WorkbookView::do_auto_completion")
  237. {
  238. ksdoc->setCompletionMode( TDEGlobalSettings::CompletionAuto);
  239. }
  240. else if ( gmr_name.toElement().text() == "WorkbookView::is_protected")
  241. {
  242. //TODO protect document ???
  243. //ksdoc->map()->isProtected()
  244. }
  245. attributeItem = attributeItem.nextSibling();
  246. }
  247. }
  248. /* This sets the documentation information from the information stored in
  249. the GNUmeric file. Particularly in the "gmr:Summary" subcategory.
  250. */
  251. void set_document_info(KoDocument * document, TQDomElement * docElem)
  252. {
  253. /* Summary Handling START */
  254. TQDomNode summary = docElem->namedItem("gmr:Summary");
  255. TQDomNode gmr_item = summary.namedItem("gmr:Item");
  256. while( !gmr_item.isNull() )
  257. {
  258. TQDomNode gmr_name = gmr_item.namedItem("gmr:name");
  259. TQDomNode gmr_value = gmr_item.namedItem("gmr:val-string");
  260. KoDocumentInfo * DocumentInfo = document->documentInfo();
  261. KoDocumentInfoAbout * aboutPage = static_cast<KoDocumentInfoAbout *>(DocumentInfo->page( "about" ));
  262. KoDocumentInfoAuthor * authorPage = static_cast<KoDocumentInfoAuthor*>(DocumentInfo->page( "author" ));
  263. if (gmr_name.toElement().text() == "title")
  264. {
  265. aboutPage->setTitle(gmr_value.toElement().text());
  266. }
  267. else if (gmr_name.toElement().text() == "keywords")
  268. {
  269. aboutPage->setKeywords( gmr_value.toElement().text());
  270. }
  271. else if (gmr_name.toElement().text() == "comments")
  272. {
  273. aboutPage->setAbstract(gmr_value.toElement().text());
  274. }
  275. else if (gmr_name.toElement().text() == "category")
  276. {
  277. /* Not supported by KSpread */
  278. }
  279. else if (gmr_name.toElement().text() == "manager")
  280. {
  281. /* Not supported by KSpread */
  282. }
  283. else if (gmr_name.toElement().text() == "application")
  284. {
  285. /* Not supported by KSpread */
  286. }
  287. else if (gmr_name.toElement().text() == "author")
  288. {
  289. authorPage->setFullName(gmr_value.toElement().text());
  290. }
  291. else if (gmr_name.toElement().text() == "company")
  292. {
  293. authorPage->setCompany(gmr_value.toElement().text());
  294. }
  295. gmr_item = gmr_item.nextSibling();
  296. }
  297. /* Summany Handling STOP */
  298. }
  299. void setColInfo(TQDomNode * sheet, Sheet * table)
  300. {
  301. TQDomNode columns = sheet->namedItem("gmr:Cols");
  302. TQDomNode columninfo = columns.namedItem("gmr:ColInfo");
  303. TQDomElement def = columns.toElement();
  304. if ( def.hasAttribute( "DefaultSizePts" ) )
  305. {
  306. bool ok = false;
  307. double d = def.attribute( "DefaultSizePts" ).toDouble( &ok );
  308. if ( ok )
  309. {
  310. Format::setGlobalColWidth( d );
  311. table->setDefaultWidth( d );
  312. }
  313. }
  314. while( !columninfo.isNull() )
  315. {
  316. TQDomElement e = columninfo.toElement(); // try to convert the node to an element.
  317. int column_number;
  318. column_number = e.attribute("No").toInt()+1;
  319. ColumnFormat *cl = new ColumnFormat(table, column_number);
  320. if (e.hasAttribute("Hidden"))
  321. {
  322. if (e.attribute("Hidden")=="1")
  323. {
  324. cl->setHide(true);
  325. }
  326. }
  327. if (e.hasAttribute("Unit"))
  328. {
  329. // xmm = (x_points) * (1 inch / 72 points) * (25.4 mm/ 1 inch)
  330. cl->setDblWidth(e.attribute("Unit").toDouble());
  331. //cl->setWidth(e.attribute("Unit").toInt());
  332. }
  333. table->insertColumnFormat(cl);
  334. columninfo = columninfo.nextSibling();
  335. }
  336. }
  337. void setRowInfo(TQDomNode *sheet, Sheet *table)
  338. {
  339. TQDomNode rows = sheet->namedItem("gmr:Rows");
  340. TQDomNode rowinfo = rows.namedItem("gmr:RowInfo");
  341. double d;
  342. bool ok = false;
  343. TQDomElement def = rows.toElement();
  344. if ( def.hasAttribute( "DefaultSizePts" ) )
  345. {
  346. d = def.attribute( "DefaultSizePts" ).toDouble( &ok );
  347. if ( ok )
  348. {
  349. Format::setGlobalRowHeight( d );
  350. table->setDefaultHeight( d );
  351. }
  352. }
  353. while( !rowinfo.isNull() )
  354. {
  355. TQDomElement e = rowinfo.toElement(); // try to convert the node to an element.
  356. int row_number;
  357. row_number = e.attribute("No").toInt() + 1;
  358. RowFormat *rl = new RowFormat(table, row_number);
  359. if (e.hasAttribute("Hidden"))
  360. {
  361. if (e.attribute("Hidden") == "1")
  362. {
  363. rl->setHide(true);
  364. }
  365. }
  366. if (e.hasAttribute("Unit"))
  367. {
  368. double dbl = e.attribute( "Unit" ).toDouble( &ok );
  369. if ( ok )
  370. rl->setDblHeight( dbl );
  371. }
  372. table->insertRowFormat(rl);
  373. rowinfo = rowinfo.nextSibling();
  374. }
  375. }
  376. void setSelectionInfo( TQDomNode * sheet, Sheet * /* table */ )
  377. {
  378. TQDomNode selections = sheet->namedItem("gmr:Selections");
  379. TQDomNode selection = selections.namedItem("gmr:Selection");
  380. /* Kspread does not support mutiple selections.. */
  381. /* This code will set the selection to the last one GNUmeric's multiple
  382. selections. */
  383. while( !selection.isNull() )
  384. {
  385. TQDomElement e = selection.toElement(); // try to convert the node to an element.
  386. TQRect kspread_selection;
  387. kspread_selection.setLeft((e.attribute("startCol").toInt() + 1));
  388. kspread_selection.setTop((e.attribute("startRow").toInt() + 1));
  389. kspread_selection.setRight((e.attribute("endCol").toInt() + 1));
  390. kspread_selection.setBottom((e.attribute("endRow").toInt() + 1));
  391. /* can't set it in the table -- must set it to a view */
  392. // table->setSelection(kspread_selection);
  393. selection = selection.nextSibling();
  394. }
  395. }
  396. void setObjectInfo(TQDomNode * sheet, Sheet * table)
  397. {
  398. TQDomNode gmr_objects = sheet->namedItem("gmr:Objects");
  399. TQDomNode gmr_cellcomment = gmr_objects.namedItem("gmr:CellComment");
  400. while( !gmr_cellcomment.isNull() )
  401. {
  402. TQDomElement e = gmr_cellcomment.toElement(); // try to convert the node to an element.
  403. if (e.hasAttribute("Text"))
  404. {
  405. if (e.hasAttribute("ObjectBound"))
  406. {
  407. Point point(e.attribute("ObjectBound"));
  408. Cell * cell = table->nonDefaultCell( point.pos().x(), point.pos().y() );
  409. cell->format()->setComment(e.attribute("Text"));
  410. }
  411. }
  412. gmr_cellcomment = gmr_cellcomment.nextSibling();
  413. }
  414. }
  415. void convertToPen( TQPen & pen, int style )
  416. {
  417. switch( style )
  418. {
  419. case 0:
  420. break;
  421. case 1:
  422. pen.setStyle( Qt::SolidLine );
  423. pen.setWidth( 1 );
  424. break;
  425. case 2:
  426. pen.setStyle( Qt::SolidLine );
  427. pen.setWidth( 2 );
  428. break;
  429. case 3:
  430. pen.setStyle( Qt::DashLine );
  431. pen.setWidth( 1 );
  432. break;
  433. case 4:
  434. // width should be 1 but otherwise it would be the same as 7
  435. pen.setStyle( Qt::DotLine );
  436. pen.setWidth( 2 );
  437. break;
  438. case 5:
  439. pen.setStyle( Qt::SolidLine );
  440. pen.setWidth( 3 );
  441. break;
  442. case 6:
  443. // TODO should be double
  444. pen.setStyle( Qt::SolidLine );
  445. pen.setWidth( 1 );
  446. break;
  447. case 7:
  448. // very thin dots => no match in KSpread
  449. pen.setStyle( Qt::DotLine );
  450. pen.setWidth( 1 );
  451. break;
  452. case 8:
  453. pen.setStyle( Qt::DashLine );
  454. pen.setWidth( 2 );
  455. break;
  456. case 9:
  457. pen.setStyle( Qt::DashDotLine );
  458. pen.setWidth( 1 );
  459. break;
  460. case 10:
  461. pen.setStyle( Qt::DashDotLine );
  462. pen.setWidth( 2 );
  463. break;
  464. case 11:
  465. pen.setStyle( Qt::DashDotDotLine );
  466. pen.setWidth( 1 );
  467. break;
  468. case 12:
  469. pen.setStyle( Qt::DashDotDotLine );
  470. pen.setWidth( 2 );
  471. break;
  472. case 13:
  473. // TODO: long dash, short dash, long dash,...
  474. pen.setStyle( Qt::DashDotLine );
  475. pen.setWidth( 3 );
  476. break;
  477. default:
  478. pen.setStyle( Qt::SolidLine );
  479. pen.setWidth( 1 );
  480. }
  481. }
  482. void GNUMERICFilter::ParseBorder( TQDomElement & gmr_styleborder, Cell * kspread_cell )
  483. {
  484. TQDomNode gmr_diagonal = gmr_styleborder.namedItem("gmr:Diagonal");
  485. TQDomNode gmr_rev_diagonal = gmr_styleborder.namedItem("gmr:Rev-Diagonal");
  486. TQDomNode gmr_top = gmr_styleborder.namedItem("gmr:Top");
  487. TQDomNode gmr_bottom = gmr_styleborder.namedItem("gmr:Bottom");
  488. TQDomNode gmr_left = gmr_styleborder.namedItem("gmr:Left");
  489. TQDomNode gmr_right = gmr_styleborder.namedItem("gmr:Right");
  490. // NoPen - no line at all. For example,
  491. // TQPainter::drawRect() fills but does not
  492. // draw any explicit boundary
  493. // line. SolidLine - a simple line. DashLine
  494. // - dashes, separated by a few
  495. // pixels. DotLine - dots, separated by a
  496. // few pixels. DashDotLine - alternately
  497. // dots and dashes. DashDotDotLine - one dash, two dots, one dash, two dots...
  498. if ( !gmr_left.isNull() )
  499. {
  500. TQDomElement e = gmr_left.toElement(); // try to convert the node to an element.
  501. importBorder( e, Left, kspread_cell);
  502. }
  503. if ( !gmr_right.isNull() )
  504. {
  505. TQDomElement e = gmr_right.toElement(); // try to convert the node to an element.
  506. importBorder( e, Right, kspread_cell);
  507. }
  508. if ( !gmr_top.isNull() )
  509. {
  510. TQDomElement e = gmr_top.toElement(); // try to convert the node to an element.
  511. importBorder( e, Top, kspread_cell);
  512. }
  513. if ( !gmr_bottom.isNull() )
  514. {
  515. TQDomElement e = gmr_bottom.toElement(); // try to convert the node to an element.
  516. importBorder( e, Bottom, kspread_cell);
  517. }
  518. if ( !gmr_diagonal.isNull() )
  519. {
  520. TQDomElement e = gmr_diagonal.toElement(); // try to convert the node to an element.
  521. importBorder( e, Diagonal, kspread_cell);
  522. }
  523. if ( !gmr_rev_diagonal.isNull() )
  524. {
  525. TQDomElement e = gmr_rev_diagonal.toElement(); // try to convert the node to an element.
  526. importBorder( e, Revdiagonal, kspread_cell);
  527. }
  528. // TQDomElement gmr_styleborder_element = gmr_styleborder.toElement();
  529. }
  530. void GNUMERICFilter::importBorder( TQDomElement border, borderStyle _style, Cell *cell)
  531. {
  532. if ( !border.isNull() )
  533. {
  534. TQDomElement e = border.toElement(); // try to convert the node to an element.
  535. if ( e.hasAttribute( "Style" ) )
  536. {
  537. int style = e.attribute( "Style" ).toInt();
  538. TQPen pen;
  539. convertToPen( pen, style );
  540. if ( style > 0 )
  541. {
  542. switch( _style )
  543. {
  544. case Left:
  545. cell->setLeftBorderPen( pen );
  546. break;
  547. case Right:
  548. cell->setRightBorderPen( pen );
  549. break;
  550. case Top:
  551. cell->setTopBorderPen( pen );
  552. break;
  553. case Bottom:
  554. cell->setBottomBorderPen( pen );
  555. break;
  556. case Diagonal:
  557. cell->format()->setFallDiagonalPen( pen ); // check if this is really Fall border
  558. break;
  559. case Revdiagonal:
  560. cell->format()->setGoUpDiagonalPen( pen ); // check if this is really GoUp
  561. break;
  562. }
  563. }
  564. if ( e.hasAttribute( "Color" ) )
  565. {
  566. TQColor color;
  567. TQString colorString = e.attribute( "Color" );
  568. convert_string_to_qcolor( colorString, &color );
  569. {
  570. switch( _style )
  571. {
  572. case Left:
  573. cell->format()->setLeftBorderColor( color );
  574. break;
  575. case Right:
  576. cell->format()->setRightBorderColor( color );
  577. break;
  578. case Top:
  579. cell->format()->setTopBorderColor( color );
  580. break;
  581. case Bottom:
  582. cell->format()->setBottomBorderColor( color );
  583. break;
  584. case Diagonal:
  585. cell->format()->setFallDiagonalColor( color );
  586. break;
  587. case Revdiagonal:
  588. cell->format()->setGoUpDiagonalPen( color );
  589. break;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. bool GNUMERICFilter::setType( Cell * kspread_cell,
  597. TQString const & formatString,
  598. TQString & cell_content )
  599. {
  600. int i = 0;
  601. for ( i = 0; cell_format_date[i] ; ++i )
  602. {
  603. kdDebug(30521) << "Cell_format: " << cell_format_date[i] << ", FormatString: " << formatString << endl;
  604. if ( ( formatString == "d/m/yy" ) || ( formatString == cell_format_date[i] ) )
  605. {
  606. kdDebug(30521) << " FormatString: Date: " << formatString << ", CellContent: " << cell_content << endl;
  607. TQDate date;
  608. if ( !kspread_cell->isDate() )
  609. {
  610. // convert cell_content to date
  611. int y, m, d;
  612. bool ok = true;
  613. int val = cell_content.toInt( &ok );
  614. kdDebug(30521) << "!!! FormatString: Date: " << formatString << ", CellContent: " << cell_content
  615. << ", Double: " << val << endl;
  616. if ( !ok )
  617. return false;
  618. GnumericDate::jul2greg( val, y, m, d );
  619. kdDebug(30521) << " num: " << val << ", y: " << y << ", m: " << m << ", d: " << d << endl;
  620. date.setYMD( y, m, d );
  621. }
  622. else
  623. date = kspread_cell->value().asDate();
  624. FormatType type;
  625. switch( i )
  626. {
  627. case 0: type = date_format5; break;
  628. case 1: type = date_format6; break;
  629. case 2: type = date_format1; break;
  630. case 3: type = date_format2; break;
  631. case 4: type = date_format3; break;
  632. case 5: type = date_format4; break;
  633. case 6: type = date_format11; break;
  634. case 7: type = date_format12; break;
  635. case 8: type = date_format19; break;
  636. case 9: type = date_format18; break;
  637. case 10: type = date_format20; break;
  638. case 11: type = date_format21; break;
  639. case 16: type = date_format7; break;
  640. case 17: type = date_format22; break;
  641. case 18: type = date_format8; break;
  642. case 19: type = date_format9; break;
  643. case 22: type = date_format25; break;
  644. case 23: type = date_format14; break;
  645. case 24: type = date_format25; break;
  646. case 25: type = date_format26; break;
  647. case 26: type = date_format16; break;
  648. case 27: type = date_format15; break;
  649. case 28: type = date_format16; break;
  650. case 29: type = date_format15; break;
  651. case 30: type = date_format24; break;
  652. case 31: type = date_format23; break;
  653. default:
  654. type = ShortDate_format;
  655. break;
  656. /* 12, 13, 14, 15, 20, 21 */
  657. }
  658. kdDebug(30521) << "i: " << i << ", Type: " << type << ", Date: " << TQString(date.toString()) << endl;
  659. kspread_cell->setValue( date );
  660. kspread_cell->format()->setFormatType( type );
  661. return true;
  662. }
  663. }
  664. for ( i = 0; cell_format_time[i] ; ++i )
  665. {
  666. if (formatString == cell_format_time[i])
  667. {
  668. TQTime time;
  669. if ( !kspread_cell->isTime() )
  670. {
  671. bool ok = true;
  672. double content = cell_content.toDouble( &ok );
  673. kdDebug(30521) << " FormatString: Time: " << formatString << ", CellContent: " << cell_content
  674. << ", Double: " << content << endl;
  675. if ( !ok )
  676. return false;
  677. time = GnumericDate::getTime( content );
  678. }
  679. else
  680. time = kspread_cell->value().asTime();
  681. FormatType type;
  682. switch( i )
  683. {
  684. case 0: type = Time_format1; break;
  685. case 1: type = Time_format2; break;
  686. case 2: type = Time_format4; break;
  687. case 3: type = Time_format5; break;
  688. case 5: type = Time_format6; break;
  689. case 6: type = Time_format6; break;
  690. default:
  691. type = Time_format1; break;
  692. }
  693. kdDebug(30521) << "i: " << i << ", Type: " << type << endl;
  694. kspread_cell->setValue( time );
  695. kspread_cell->format()->setFormatType( type );
  696. return true;
  697. }
  698. }
  699. return false; // no date or time
  700. }
  701. TQString GNUMERICFilter::convertVars( TQString const & str, Sheet * table ) const
  702. {
  703. TQString result( str );
  704. uint count = list1.count();
  705. if ( count == 0 )
  706. {
  707. list1 << "&[TAB]" << "&[DATE]" << "&[PAGE]"
  708. << "&[PAGES]"<<"&[TIME]" << "&[FILE]";
  709. list2 << "<sheet>" << "<date>" << "<page>"
  710. << "<pages>" << "<time>" << "<file>";
  711. count = list1.count();
  712. }
  713. for ( uint i = 0; i < count; ++i )
  714. {
  715. int n = result.find( list1[i] );
  716. if ( n != -1 )
  717. {
  718. kdDebug(30521) << "Found var: " << list1[i] << endl;
  719. if ( i == 0 )
  720. result = result.replace( list1[i], table->tableName() );
  721. else
  722. result = result.replace( list1[i], list2[i] );
  723. }
  724. }
  725. return result;
  726. }
  727. double GNUMERICFilter::parseAttribute( const TQDomElement &_element )
  728. {
  729. TQString unit = _element.attribute( "PrefUnit" );
  730. bool ok;
  731. double value = _element.attribute("Points").toFloat( &ok );
  732. if ( !ok )
  733. value = 2.0;
  734. if ( unit == "mm" )
  735. return value;
  736. else if ( unit == "cm" )
  737. return ( value/10.0 );
  738. else if ( unit == "in" )
  739. return MM_TO_INCH( value );
  740. else if ( unit == "Pt" || unit == "Px" || unit == "points" )
  741. return MM_TO_POINT( value );
  742. else
  743. return value;
  744. }
  745. void GNUMERICFilter::ParsePrintInfo( TQDomNode const & printInfo, Sheet * table )
  746. {
  747. kdDebug(30521) << "Parsing print info " << endl;
  748. float fleft = 2.0;
  749. float fright = 2.0;
  750. float ftop = 2.0;
  751. float fbottom = 2.0;
  752. TQString paperSize("A4");
  753. TQString orientation("Portrait");
  754. TQString footLeft, footMiddle, footRight;
  755. TQString headLeft, headMiddle, headRight; // no we are zombies :-)
  756. TQDomNode margins( printInfo.namedItem("gmr:Margins") );
  757. if ( !margins.isNull() )
  758. {
  759. TQDomElement top( margins.namedItem( "gmr:top" ).toElement() );
  760. if ( !top.isNull() )
  761. ftop = parseAttribute( top );
  762. TQDomElement bottom( margins.namedItem( "gmr:bottom" ).toElement() );
  763. if ( !bottom.isNull() )
  764. fbottom= parseAttribute( bottom );
  765. TQDomElement left( margins.namedItem( "gmr:left" ).toElement() );
  766. if ( !left.isNull() )
  767. fleft = parseAttribute( left );
  768. TQDomElement right( margins.namedItem( "gmr:right" ).toElement() );
  769. if ( !right.isNull() )
  770. fright = parseAttribute( right );
  771. }
  772. TQDomElement foot( printInfo.namedItem("gmr:Footer").toElement() );
  773. if ( !foot.isNull() )
  774. {
  775. kdDebug(30521) << "Parsing footer: " << foot.attribute("Left") << ", " << foot.attribute("Middle") << ", "
  776. << foot.attribute("Right") << ", " <<endl;
  777. if ( foot.hasAttribute("Left") )
  778. footLeft = convertVars( foot.attribute("Left"), table );
  779. if ( foot.hasAttribute("Middle") )
  780. footMiddle = convertVars( foot.attribute("Middle"), table );
  781. if ( foot.hasAttribute("Right") )
  782. footRight = convertVars( foot.attribute("Right"), table );
  783. }
  784. TQDomElement head( printInfo.namedItem("gmr:Header").toElement() );
  785. if ( !head.isNull() )
  786. {
  787. kdDebug(30521) << "Parsing header: " << head.attribute("Left") << ", " << head.attribute("Middle") << ", " << head.attribute("Right") << ", "<< endl;
  788. if ( head.hasAttribute("Left") )
  789. headLeft = convertVars( head.attribute("Left"), table );
  790. if ( head.hasAttribute("Middle") )
  791. headMiddle = convertVars( head.attribute("Middle"), table );
  792. if ( head.hasAttribute("Right") )
  793. headRight = convertVars( head.attribute("Right"), table );
  794. }
  795. TQDomElement repeateColumn( printInfo.namedItem("gmr:repeat_top").toElement() );
  796. if ( !repeateColumn.isNull() )
  797. {
  798. TQString repeate = repeateColumn.attribute( "value" );
  799. if ( !repeate.isEmpty() )
  800. {
  801. Range range(repeate);
  802. //kdDebug()<<" repeate :"<<repeate<<"range. ::start row : "<<range.startRow ()<<" start col :"<<range.startCol ()<<" end row :"<<range.endRow ()<<" end col :"<<range.endCol ()<<endl;
  803. table->print()->setPrintRepeatRows( tqMakePair( range.startRow (),range.endRow ()) );
  804. }
  805. }
  806. TQDomElement repeateRow( printInfo.namedItem("gmr:repeat_left").toElement() );
  807. if ( !repeateRow.isNull() )
  808. {
  809. TQString repeate = repeateRow.attribute( "value" );
  810. if ( !repeate.isEmpty() )
  811. {
  812. //fix row too high
  813. repeate = repeate.replace( "65536", "32500" );
  814. Range range(repeate);
  815. //kdDebug()<<" repeate :"<<repeate<<"range. ::start row : "<<range.startRow ()<<" start col :"<<range.startCol ()<<" end row :"<<range.endRow ()<<" end col :"<<range.endCol ()<<endl;
  816. table->print()->setPrintRepeatColumns( tqMakePair( range.startCol (),range.endCol ()) );
  817. }
  818. }
  819. TQDomElement orient( printInfo.namedItem("gmr:orientation").toElement() );
  820. if ( !orient.isNull() )
  821. orientation = orient.text();
  822. TQDomElement size( printInfo.namedItem("gmr:paper").toElement() );
  823. if ( !size.isNull() )
  824. paperSize = size.text();
  825. table->print()->setPaperLayout( fleft, ftop, fright, fbottom,
  826. paperSize, orientation );
  827. table->print()->setHeadFootLine( headLeft, headMiddle, headRight,
  828. footLeft, footMiddle, footRight );
  829. }
  830. void GNUMERICFilter::ParseFormat(TQString const & formatString, Cell * kspread_cell)
  831. {
  832. int l = formatString.length();
  833. int lastPos = 0;
  834. if (formatString[l - 1] == '%')
  835. kspread_cell->format()->setFormatType(Percentage_format);
  836. else if (formatString[0] == '$')
  837. {
  838. kspread_cell->format()->setFormatType(Money_format);
  839. kspread_cell->format()->setCurrency( 1, "$" );
  840. lastPos = 1;
  841. }
  842. else if (formatString[0] == '')
  843. {
  844. kspread_cell->format()->setFormatType(Money_format);
  845. kspread_cell->format()->setCurrency( 1, "" );
  846. lastPos = 1;
  847. }
  848. else if (formatString[0] == '')
  849. {
  850. kspread_cell->format()->setFormatType(Money_format);
  851. kspread_cell->format()->setCurrency( 1, "" );
  852. lastPos = 1;
  853. }
  854. else if (formatString[0] == '')
  855. {
  856. kspread_cell->format()->setFormatType(Money_format);
  857. kspread_cell->format()->setCurrency( 1, "" );
  858. lastPos = 1;
  859. }
  860. else if (l > 1)
  861. {
  862. if ((formatString[0] == '[') && (formatString[1] == '$'))
  863. {
  864. int n = formatString.find(']');
  865. if (n != -1)
  866. {
  867. TQString currency = formatString.mid(2, n - 2);
  868. kspread_cell->format()->setFormatType(Money_format);
  869. kspread_cell->format()->setCurrency( 1, currency );
  870. }
  871. lastPos = ++n;
  872. }
  873. else if (formatString.find("E+0") != -1)
  874. {
  875. kspread_cell->format()->setFormatType(Scientific_format);
  876. }
  877. else
  878. {
  879. // do pattern matching with gnumeric formats
  880. TQString content(kspread_cell->value().asString());
  881. if ( setType(kspread_cell, formatString, content) )
  882. return;
  883. if (formatString.find("?/?") != -1)
  884. {
  885. // TODO: fixme!
  886. kspread_cell->format()->setFormatType( fraction_three_digits );
  887. return;
  888. }
  889. // so it's nothing we want to understand:-)
  890. return;
  891. }
  892. }
  893. while (formatString[lastPos] == ' ')
  894. ++lastPos;
  895. // GetPrecision and decimal point, format of negative items...
  896. // thousands separator
  897. if (formatString[lastPos] == '#')
  898. {
  899. bool sep = true;
  900. if (formatString[lastPos + 1] == ',')
  901. lastPos += 2;
  902. else
  903. sep = false;
  904. // since KSpread 1.3
  905. // kspread_cell->setThousandsSeparator( sep );
  906. }
  907. while (formatString[lastPos] == ' ')
  908. ++lastPos;
  909. int n = formatString.find( '.', lastPos );
  910. if ( n != -1)
  911. {
  912. lastPos = n + 1;
  913. int precision = lastPos;
  914. while (formatString[precision] == '0')
  915. ++precision;
  916. int tmp = lastPos;
  917. lastPos = precision;
  918. precision -= tmp;
  919. kspread_cell->format()->setPrecision( precision );
  920. }
  921. bool red = false;
  922. if (formatString.find("[RED]", lastPos) != -1)
  923. {
  924. red = true;
  925. kspread_cell->format()->setFloatColor( Format::NegRed );
  926. }
  927. if ( formatString.find('(', lastPos) != -1 )
  928. {
  929. if ( red )
  930. kspread_cell->format()->setFloatColor( Format::NegRedBrackets );
  931. else
  932. kspread_cell->format()->setFloatColor( Format::NegBrackets );
  933. }
  934. }
  935. void GNUMERICFilter::convertFormula( TQString & formula ) const
  936. {
  937. int n = formula.find( '=', 1 );
  938. // TODO: check if we do not screw something up here...
  939. if ( n != -1 )
  940. formula = formula.replace( n, 1, "==" );
  941. bool inQuote1 = false;
  942. bool inQuote2 = false;
  943. int l = formula.length();
  944. for ( int i = 0; i < l; ++i )
  945. {
  946. if ( formula[i] == '\'' )
  947. inQuote1 = !inQuote1;
  948. else if ( formula[i] == '"' )
  949. inQuote2 = !inQuote2;
  950. else if ( formula[i] == ',' && !inQuote1 && !inQuote2 )
  951. formula = formula.replace( i, 1, ";" );
  952. }
  953. }
  954. void GNUMERICFilter::setStyleInfo(TQDomNode * sheet, Sheet * table)
  955. {
  956. kdDebug(30521) << "SetStyleInfo entered " << endl;
  957. int row, column;
  958. TQDomNode styles = sheet->namedItem( "gmr:Styles" );
  959. if ( !styles.isNull() )
  960. {
  961. // Get a style region within that sheet.
  962. TQDomNode style_region = styles.namedItem( "gmr:StyleRegion" );
  963. while ( !style_region.isNull() )
  964. {
  965. TQDomElement e = style_region.toElement(); // try to convert the node to an element.
  966. TQDomNode style = style_region.namedItem( "gmr:Style" );
  967. TQDomNode font = style.namedItem( "gmr:Font" );
  968. TQDomNode validation = style.namedItem( "gmr:Validation" );
  969. TQDomNode gmr_styleborder = style.namedItem( "gmr:StyleBorder" );
  970. TQDomNode hyperlink = style.namedItem( "gmr:HyperLink" );
  971. int startCol = e.attribute( "startCol" ).toInt() + 1;
  972. int endCol = e.attribute( "endCol" ).toInt() + 1;
  973. int startRow = e.attribute( "startRow" ).toInt() + 1;
  974. int endRow = e.attribute( "endRow" ).toInt() + 1;
  975. kdDebug(30521) << "------Style: " << startCol << ", "
  976. << startRow << " - " << endCol << ", " << endRow << endl;
  977. if ( endCol - startCol > 200 || endRow - startRow > 200 )
  978. {
  979. style_region = style_region.nextSibling();
  980. continue;
  981. }
  982. for ( column = startCol; column <= endCol; ++column )
  983. {
  984. for ( row = startRow; row <= endRow; ++row )
  985. {
  986. kdDebug(30521) << "Cell: " << column << ", " << row << endl;
  987. Cell * kspread_cell = table->cellAt( column, row, false );
  988. // don't create new cells -> don't apply format on empty cells, if bigger region
  989. if ( ( kspread_cell->isDefault() || kspread_cell->isEmpty() )
  990. && ( ( endCol - startCol > 2 ) || ( endRow - startRow > 2 ) ) )
  991. {
  992. kdDebug(30521) << "CELL EMPTY OR RANGE TOO BIG " << endl;
  993. continue;
  994. }
  995. TQDomElement style_element = style.toElement(); // try to convert the node to an element.
  996. kdDebug(30521) << "Style valid for kspread" << endl;
  997. kspread_cell = table->nonDefaultCell( column, row, false );
  998. if (style_element.hasAttribute("Fore"))
  999. {
  1000. TQString color_string = style_element.attribute("Fore");
  1001. TQColor color;
  1002. convert_string_to_qcolor(color_string, &color);
  1003. kspread_cell->format()->setTextColor(color);
  1004. }
  1005. if (style_element.hasAttribute("Back"))
  1006. {
  1007. TQString color_string = style_element.attribute("Back");
  1008. TQColor color;
  1009. convert_string_to_qcolor(color_string, &color);
  1010. kspread_cell->format()->setBgColor(color);
  1011. }
  1012. if (style_element.hasAttribute("PatternColor"))
  1013. {
  1014. TQString color_string = style_element.attribute("PatternColor");
  1015. TQColor color;
  1016. convert_string_to_qcolor(color_string, &color);
  1017. kspread_cell->format()->setBackGroundBrushColor( color );
  1018. }
  1019. if (style_element.hasAttribute("Shade"))
  1020. {
  1021. /* Pattern's taken from: gnumeric's pattern.c */
  1022. /* if "TODO" added: doesn't match exactly the gnumeric one */
  1023. TQString shade = style_element.attribute("Shade");
  1024. if (shade == "0")
  1025. {
  1026. // nothing to do
  1027. }
  1028. else if (shade == "1")
  1029. {
  1030. /* 1 Solid */
  1031. //kspread_cell->format()->setBackGroundBrushStyle(Qt::SolidPattern);
  1032. //This is as empty
  1033. /* What should this be? */
  1034. }
  1035. else if (shade == "2")
  1036. {
  1037. /* 2 75% */
  1038. kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense2Pattern);
  1039. }
  1040. else if (shade == "3")
  1041. {
  1042. /* 3 50% */
  1043. kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense4Pattern);
  1044. }
  1045. else if (shade == "4")
  1046. {
  1047. kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense5Pattern);
  1048. /* This should be 25%... All qt has is 37% */
  1049. /* 4 25% */
  1050. }
  1051. else if (shade == "5")
  1052. {
  1053. kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense6Pattern);
  1054. /* 5 12.5% */
  1055. }
  1056. else if (shade == "6")
  1057. {
  1058. kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense7Pattern);
  1059. /* 6 6.25% */
  1060. }
  1061. else if (shade == "7")
  1062. {
  1063. kspread_cell->format()->setBackGroundBrushStyle(Qt::HorPattern);
  1064. /* 7Qt::Horizontal Stripe */
  1065. }
  1066. else if (shade == "8")
  1067. {
  1068. kspread_cell->format()->setBackGroundBrushStyle(Qt::VerPattern);
  1069. /* 8Qt::Vertical Stripe */
  1070. }
  1071. else if (shade == "9")
  1072. {
  1073. kspread_cell->format()->setBackGroundBrushStyle(Qt::BDiagPattern);
  1074. /* 9 Reverse Diagonal Stripe */
  1075. }
  1076. else if (shade == "10")
  1077. {
  1078. /* 10 Diagonal Stripe */
  1079. kspread_cell->format()->setBackGroundBrushStyle(Qt::FDiagPattern);
  1080. }
  1081. else if (shade == "11")
  1082. {
  1083. /* 11 Diagonal Crosshatch */
  1084. kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
  1085. }
  1086. else if (shade == "12")
  1087. {
  1088. /* 12 Thick Diagonal Crosshatch TODO!*/
  1089. kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
  1090. }
  1091. else if (shade == "13")
  1092. {
  1093. /* 13 ThinQt::Horizontal Stripe TODO: wrong: this is thick!*/
  1094. kspread_cell->format()->setBackGroundBrushStyle(Qt::HorPattern);
  1095. }
  1096. else if (shade == "14")
  1097. {
  1098. kspread_cell->format()->setBackGroundBrushStyle(Qt::VerPattern);
  1099. }
  1100. else if (shade == "15")
  1101. {
  1102. kspread_cell->format()->setBackGroundBrushStyle(Qt::FDiagPattern);
  1103. }
  1104. else if (shade == "16")
  1105. {
  1106. /* 16 Thick Reverse Stripe TODO:*/
  1107. kspread_cell->format()->setBackGroundBrushStyle(Qt::BDiagPattern);
  1108. }
  1109. else if (shade == "17")
  1110. {
  1111. kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
  1112. }
  1113. else if (shade == "18")
  1114. {
  1115. kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
  1116. }
  1117. else if (shade == "19")
  1118. {
  1119. /* 19 Applix small circle */
  1120. }
  1121. else if (shade == "20")
  1122. {
  1123. /* 20 Applix semicircle */
  1124. }
  1125. else if (shade == "21")
  1126. {
  1127. /* 21 Applix small thatch */
  1128. }
  1129. else if (shade == "22")
  1130. {
  1131. /* 22 Applix round thatch */
  1132. }
  1133. else if (shade == "23")
  1134. {
  1135. /* 23 Applix Brick */
  1136. }
  1137. else if (shade == "24")
  1138. {
  1139. /* 24 100% */
  1140. kspread_cell->format()->setBackGroundBrushStyle(Qt::SolidPattern);
  1141. }
  1142. else if (shade == "25")
  1143. {
  1144. /* 25 87.5% */
  1145. kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense2Pattern);
  1146. }
  1147. }
  1148. if ( style_element.hasAttribute( "Rotation" ) )
  1149. {
  1150. int rot = style_element.attribute( "Rotation" ).toInt();
  1151. kspread_cell->format()->setAngle( -1* rot );
  1152. }
  1153. if (style_element.hasAttribute("Indent"))
  1154. {
  1155. double indent = style_element.attribute("Indent").toDouble();
  1156. // gnumeric saves indent in characters, we in points:
  1157. kspread_cell->format()->setIndent( indent * 10.0 );
  1158. }
  1159. if (style_element.hasAttribute("HAlign"))
  1160. {
  1161. TQString halign_string=style_element.attribute("HAlign");
  1162. if (halign_string == "1")
  1163. {
  1164. /* General: No equivalent in Kspread. */
  1165. }
  1166. else if (halign_string == "2")
  1167. {
  1168. kspread_cell->format()->setAlign(Format::Left);
  1169. }
  1170. else if (halign_string == "4")
  1171. {
  1172. kspread_cell->format()->setAlign(Format::Right);
  1173. }
  1174. else if (halign_string == "8")
  1175. {
  1176. kspread_cell->format()->setAlign(Format::Center);
  1177. }
  1178. else if (halign_string == "16")
  1179. {
  1180. /* Fill: No equivalent in Kspread. */
  1181. }
  1182. else if (halign_string == "32")
  1183. {
  1184. /* Justify: No equivalent in Kspread */
  1185. }
  1186. else if (halign_string == "64")
  1187. {
  1188. /* Centered across selection*/
  1189. }
  1190. }
  1191. if (style_element.hasAttribute("VAlign"))
  1192. {
  1193. TQString valign_string=style_element.attribute("VAlign");
  1194. if (valign_string == "1")
  1195. {
  1196. /* General: No equivalent in Kspread. */
  1197. kspread_cell->format()->setAlignY(Format::Top);
  1198. }
  1199. else if (valign_string == "2")
  1200. {
  1201. kspread_cell->format()->setAlignY(Format::Bottom);
  1202. }
  1203. else if (valign_string == "4")
  1204. {
  1205. kspread_cell->format()->setAlignY(Format::Middle);
  1206. }
  1207. else if (valign_string == "8")
  1208. {
  1209. /* Justify: No equivalent in Kspread */
  1210. }
  1211. }
  1212. if (style_element.hasAttribute("WrapText"))
  1213. {
  1214. TQString multiRow = style_element.attribute("WrapText");
  1215. if ( multiRow == "1" )
  1216. kspread_cell->format()->setMultiRow( true );
  1217. }
  1218. if (style_element.hasAttribute("Format"))
  1219. {
  1220. TQString formatString = style_element.attribute("Format");
  1221. kdDebug(30521) << "Format: " << formatString << endl;
  1222. ParseFormat(formatString, kspread_cell);
  1223. } // End "Format"
  1224. if (!gmr_styleborder.isNull())
  1225. {
  1226. TQDomElement style_element = gmr_styleborder.toElement(); // try to convert the node to an element.
  1227. ParseBorder( style_element, kspread_cell );
  1228. }
  1229. if ( !validation.isNull() )
  1230. {
  1231. TQDomElement validation_element = validation.toElement();
  1232. if ( !validation_element.isNull() )
  1233. {
  1234. kdDebug(30521)<<" Cell validation \n";
  1235. Validity* kspread_validity = kspread_cell->getValidity();
  1236. if ( validation_element.hasAttribute( "AllowBlank" ) && validation_element.attribute( "AllowBlank" )=="true" )
  1237. {
  1238. kspread_validity->allowEmptyCell=true;
  1239. }
  1240. if ( validation_element.hasAttribute( "Title" ))
  1241. {
  1242. kspread_validity->title=validation_element.attribute( "Title" );
  1243. }
  1244. if ( validation_element.hasAttribute( "Message" ))
  1245. {
  1246. kspread_validity->message=validation_element.attribute( "Message" );
  1247. }
  1248. if ( validation_element.hasAttribute( "Style" ) )
  1249. {
  1250. int value = validation_element.attribute( "Style" ).toInt();
  1251. switch( value )
  1252. {
  1253. case 0:
  1254. kspread_validity->displayMessage=false;
  1255. break;
  1256. case 1:
  1257. kspread_validity->m_action=Action::Stop;
  1258. break;
  1259. case 2:
  1260. kspread_validity->m_action=Action::Warning;
  1261. break;
  1262. case 3:
  1263. kspread_validity->m_action=Action::Information;
  1264. break;
  1265. default:
  1266. kdDebug()<<" Error in validation style :"<<value<<endl;
  1267. break;
  1268. }
  1269. }
  1270. TQDomNode expression0 = validation_element.namedItem( "gmr:Expression0" );
  1271. TQDomNode expression1 = validation_element.namedItem( "gmr:Expression1" );
  1272. //kdDebug()<<" expression0.isNull() "<<expression0.isNull()<<endl;
  1273. //kdDebug()<<" expression1.isNull() "<<expression1.isNull()<<endl;
  1274. if ( validation_element.hasAttribute( "Type" ) )
  1275. {
  1276. int valueOp = validation_element.attribute( "Type" ).toInt();
  1277. switch( valueOp )
  1278. {
  1279. case 0:
  1280. kspread_validity->m_restriction=Restriction::None;
  1281. break;
  1282. case 1:
  1283. {
  1284. kspread_validity->m_restriction=Restriction::Integer;
  1285. if ( validation_element.hasAttribute( "Operator" ) )
  1286. {
  1287. int value = validation_element.attribute( "Operator" ).toInt();
  1288. switch( value )
  1289. {
  1290. case 0:
  1291. kspread_validity->m_cond=Conditional::Between;
  1292. if ( !expression0.isNull() )
  1293. kspread_validity->valMin=expression0.toElement().text().toInt();
  1294. if ( !expression1.isNull() )
  1295. kspread_validity->valMax=expression1.toElement().text().toInt();
  1296. break;
  1297. case 1:
  1298. kspread_validity->m_cond=Conditional::DifferentTo;
  1299. if ( !expression0.isNull() )
  1300. kspread_validity->valMin=expression0.toElement().text().toInt();
  1301. if ( !expression1.isNull() )
  1302. kspread_validity->valMax=expression1.toElement().text().toInt();
  1303. break;
  1304. case 2:
  1305. kspread_validity->m_cond=Conditional::Equal;
  1306. if ( !expression0.isNull() )
  1307. kspread_validity->valMin=expression0.toElement().text().toInt();
  1308. break;
  1309. case 3:
  1310. kspread_validity->m_cond=Conditional::Different;
  1311. if ( !expression0.isNull() )
  1312. kspread_validity->valMin=expression0.toElement().text().toInt();
  1313. break;
  1314. case 4:
  1315. kspread_validity->m_cond=Conditional::Superior;
  1316. if ( !expression0.isNull() )
  1317. kspread_validity->valMin=expression0.toElement().text().toInt();
  1318. break;
  1319. case 5:
  1320. kspread_validity->m_cond=Conditional::Inferior;
  1321. if ( !expression0.isNull() )
  1322. kspread_validity->valMin=expression0.toElement().text().toInt();
  1323. break;
  1324. case 6:
  1325. kspread_validity->m_cond=Conditional::SuperiorEqual;
  1326. if ( !expression0.isNull() )
  1327. kspread_validity->valMin=expression0.toElement().text().toInt();
  1328. break;
  1329. case 7:
  1330. kspread_validity->m_cond=Conditional::InferiorEqual;
  1331. if ( !expression0.isNull() )
  1332. kspread_validity->valMin=expression0.toElement().text().toInt();
  1333. break;
  1334. default:
  1335. kdDebug()<<" Error in validation Operator :"<<value<<endl;
  1336. break;
  1337. }
  1338. }
  1339. }
  1340. break;
  1341. case 2:
  1342. kspread_validity->m_restriction=Restriction::Number;
  1343. if ( validation_element.hasAttribute( "Operator" ) )
  1344. {
  1345. int value = validation_element.attribute( "Operator" ).toInt();
  1346. switch( value )
  1347. {
  1348. case 0:
  1349. kspread_validity->m_cond=Conditional::Between;
  1350. if ( !expression0.isNull() )
  1351. kspread_validity->valMin=expression0.toElement().text().toInt();
  1352. if ( !expression1.isNull() )
  1353. kspread_validity->valMax=expression1.toElement().text().toInt();
  1354. break;
  1355. case 1:
  1356. kspread_validity->m_cond=Conditional::DifferentTo;
  1357. if ( !expression0.isNull() )
  1358. kspread_validity->valMin=expression0.toElement().text().toInt();
  1359. if ( !expression1.isNull() )
  1360. kspread_validity->valMax=expression1.toElement().text().toInt();
  1361. break;
  1362. case 2:
  1363. kspread_validity->m_cond=Conditional::Equal;
  1364. if ( !expression0.isNull() )
  1365. kspread_validity->valMin=expression0.toElement().text().toInt();
  1366. break;
  1367. case 3:
  1368. kspread_validity->m_cond=Conditional::Different;
  1369. if ( !expression0.isNull() )
  1370. kspread_validity->valMin=expression0.toElement().text().toInt();
  1371. break;
  1372. case 4:
  1373. kspread_validity->m_cond=Conditional::Superior;
  1374. if ( !expression0.isNull() )
  1375. kspread_validity->valMin=expression0.toElement().text().toInt();
  1376. break;
  1377. case 5:
  1378. if ( !expression0.isNull() )
  1379. kspread_validity->valMin=expression0.toElement().text().toInt();
  1380. kspread_validity->m_cond=Conditional::Inferior;
  1381. break;
  1382. case 6:
  1383. kspread_validity->m_cond=Conditional::SuperiorEqual;
  1384. if ( !expression0.isNull() )
  1385. kspread_validity->valMin=expression0.toElement().text().toInt();
  1386. break;
  1387. case 7:
  1388. kspread_validity->m_cond=Conditional::InferiorEqual;
  1389. if ( !expression0.isNull() )
  1390. kspread_validity->valMin=expression0.toElement().text().toInt();
  1391. break;
  1392. default:
  1393. kdDebug()<<" Error in validation Operator :"<<value<<endl;
  1394. break;
  1395. }
  1396. }
  1397. break;
  1398. case 3:
  1399. kspread_validity->m_restriction=Restriction::List;
  1400. break;
  1401. case 4:
  1402. kspread_validity->m_restriction=Restriction::Date;
  1403. if ( validation_element.hasAttribute( "Operator" ) )
  1404. {
  1405. int value = validation_element.attribute( "Operator" ).toInt();
  1406. switch( value )
  1407. {
  1408. case 0:
  1409. kspread_validity->m_cond=Conditional::Between;
  1410. if ( !expression0.isNull() )
  1411. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1412. if ( !expression1.isNull() )
  1413. kspread_validity->dateMax=TQDate::fromString( expression1.toElement().text() );
  1414. break;
  1415. case 1:
  1416. if ( !expression0.isNull() )
  1417. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1418. if ( !expression1.isNull() )
  1419. kspread_validity->dateMax=TQDate::fromString( expression1.toElement().text() );
  1420. kspread_validity->m_cond=Conditional::DifferentTo;
  1421. break;
  1422. case 2:
  1423. if ( !expression0.isNull() )
  1424. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1425. kspread_validity->m_cond=Conditional::Equal;
  1426. break;
  1427. case 3:
  1428. if ( !expression0.isNull() )
  1429. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1430. kspread_validity->m_cond=Conditional::Different;
  1431. break;
  1432. case 4:
  1433. if ( !expression0.isNull() )
  1434. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1435. kspread_validity->m_cond=Conditional::Superior;
  1436. break;
  1437. case 5:
  1438. if ( !expression0.isNull() )
  1439. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1440. kspread_validity->m_cond=Conditional::Inferior;
  1441. break;
  1442. case 6:
  1443. if ( !expression0.isNull() )
  1444. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1445. kspread_validity->m_cond=Conditional::SuperiorEqual;
  1446. break;
  1447. case 7:
  1448. if ( !expression0.isNull() )
  1449. kspread_validity->dateMin=TQDate::fromString( expression0.toElement().text() );
  1450. kspread_validity->m_cond=Conditional::InferiorEqual;
  1451. break;
  1452. default:
  1453. kdDebug()<<" Error in validation Operator :"<<value<<endl;
  1454. break;
  1455. }
  1456. }
  1457. break;
  1458. case 5:
  1459. kspread_validity->m_restriction=Restriction::Time;
  1460. if ( validation_element.hasAttribute( "Operator" ) )
  1461. {
  1462. int value = validation_element.attribute( "Operator" ).toInt();
  1463. switch( value )
  1464. {
  1465. case 0:
  1466. kspread_validity->m_cond=Conditional::Between;
  1467. if ( !expression0.isNull() )
  1468. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1469. if ( !expression1.isNull() )
  1470. kspread_validity->timeMax=TQTime::fromString( expression1.toElement().text() );
  1471. break;
  1472. case 1:
  1473. if ( !expression0.isNull() )
  1474. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1475. if ( !expression1.isNull() )
  1476. kspread_validity->timeMax=TQTime::fromString( expression1.toElement().text() );
  1477. kspread_validity->m_cond=Conditional::DifferentTo;
  1478. break;
  1479. case 2:
  1480. if ( !expression0.isNull() )
  1481. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1482. kspread_validity->m_cond=Conditional::Equal;
  1483. break;
  1484. case 3:
  1485. kspread_validity->m_cond=Conditional::Different;
  1486. break;
  1487. case 4:
  1488. if ( !expression0.isNull() )
  1489. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1490. kspread_validity->m_cond=Conditional::Superior;
  1491. break;
  1492. case 5:
  1493. kspread_validity->m_cond=Conditional::Inferior;
  1494. if ( !expression0.isNull() )
  1495. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1496. break;
  1497. case 6:
  1498. kspread_validity->m_cond=Conditional::SuperiorEqual;
  1499. if ( !expression0.isNull() )
  1500. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1501. break;
  1502. case 7:
  1503. if ( !expression0.isNull() )
  1504. kspread_validity->timeMin=TQTime::fromString( expression0.toElement().text() );
  1505. kspread_validity->m_cond=Conditional::InferiorEqual;
  1506. break;
  1507. default:
  1508. kdDebug()<<" Error in validation Operator :"<<value<<endl;
  1509. break;
  1510. }
  1511. }
  1512. break;
  1513. case 6:
  1514. kspread_validity->m_restriction=Restriction::TextLength;
  1515. if ( validation_element.hasAttribute( "Operator" ) )
  1516. {
  1517. int value = validation_element.attribute( "Operator" ).toInt();
  1518. switch( value )
  1519. {
  1520. case 0:
  1521. kspread_validity->m_cond=Conditional::Between;
  1522. if ( !expression0.isNull() )
  1523. kspread_validity->valMin=expression0.toElement().text().toInt();
  1524. if ( !expression1.isNull() )
  1525. kspread_validity->valMax=expression1.toElement().text().toInt();
  1526. break;
  1527. case 1:
  1528. kspread_validity->m_cond=Conditional::DifferentTo;
  1529. if ( !expression0.isNull() )
  1530. kspread_validity->valMin=expression0.toElement().text().toInt();
  1531. if ( !expression1.isNull() )
  1532. kspread_validity->valMax=expression1.toElement().text().toInt();
  1533. break;
  1534. case 2:
  1535. kspread_validity->m_cond=Conditional::Equal;
  1536. if ( !expression0.isNull() )
  1537. kspread_validity->valMin=expression0.toElement().text().toInt();
  1538. break;
  1539. case 3:
  1540. kspread_validity->m_cond=Conditional::Different;
  1541. if ( !expression0.isNull() )
  1542. kspread_validity->valMin=expression0.toElement().text().toInt();
  1543. break;
  1544. case 4:
  1545. if ( !expression0.isNull() )
  1546. kspread_validity->valMin=expression0.toElement().text().toInt();
  1547. kspread_validity->m_cond=Conditional::Superior;
  1548. break;
  1549. case 5:
  1550. if ( !expression0.isNull() )
  1551. kspread_validity->valMin=expression0.toElement().text().toInt();
  1552. kspread_validity->m_cond=Conditional::Inferior;
  1553. break;
  1554. case 6:
  1555. if ( !expression0.isNull() )
  1556. kspread_validity->valMin=expression0.toElement().text().toInt();
  1557. kspread_validity->m_cond=Conditional::SuperiorEqual;
  1558. break;
  1559. case 7:
  1560. if ( !expression0.isNull() )
  1561. kspread_validity->valMin=expression0.toElement().text().toInt();
  1562. kspread_validity->m_cond=Conditional::InferiorEqual;
  1563. break;
  1564. default:
  1565. kdDebug()<<" Error in validation Operator :"<<value<<endl;
  1566. break;
  1567. }
  1568. }
  1569. break;
  1570. default:
  1571. kdDebug()<<" Error in Type element : "<<valueOp<<endl;
  1572. }
  1573. }
  1574. //<gmr:Validation Style="0" Type="1" Operator="0" AllowBlank="true" UseDropdown="false">
  1575. //<gmr:Expression0>745</gmr:Expression0>
  1576. //<gmr:Expression1>4546</gmr:Expression1>
  1577. }
  1578. }
  1579. if (!font.isNull())
  1580. {
  1581. TQDomElement font_element = font.toElement();
  1582. kspread_cell->format()->setTextFontFamily( font_element.text() );
  1583. if (!font_element.isNull())
  1584. {
  1585. if (font_element.attribute("Italic") == "1")
  1586. { kspread_cell->format()->setTextFontItalic(true); }
  1587. if (font_element.attribute("Bold") == "1")
  1588. { kspread_cell->format()->setTextFontBold(true); }
  1589. if (font_element.hasAttribute("Underline") && ( font_element.attribute("Underline") != "0") )
  1590. { kspread_cell->format()->setTextFontUnderline(true); }
  1591. if (font_element.hasAttribute("StrikeThrough" ) && ( font_element.attribute("StrikeThrough") != "0") )
  1592. { kspread_cell->format()->setTextFontStrike(true); }
  1593. if (font_element.hasAttribute("Unit"))
  1594. { kspread_cell->format()->setTextFontSize(font_element.attribute("Unit").toInt()); }
  1595. }
  1596. if ( !hyperlink.isNull() )
  1597. {
  1598. //<gmr:HyperLink type="GnmHLinkURL" target="www.kde.org"/>
  1599. if ( hyperlink.toElement().hasAttribute( "type" ) )
  1600. {
  1601. TQString linkType= hyperlink.toElement().attribute( "type" );
  1602. TQString target = hyperlink.toElement().attribute( "target" );
  1603. TQString tip = hyperlink.toElement().attribute( "tip" );
  1604. if ( !tip.isEmpty() )
  1605. kspread_cell->setCellText( tip );
  1606. if ( linkType=="GnmHLinkURL" )
  1607. {
  1608. if ( !target.startsWith( "http://" ) )
  1609. target="http://"+target;
  1610. kspread_cell->setLink( target );
  1611. }
  1612. else if ( linkType=="GnmHLinkEMail" )
  1613. {
  1614. if ( !target.startsWith( "mailto:/" ) )
  1615. target="mailto:/"+target;
  1616. kspread_cell->setLink( target );
  1617. }
  1618. else if ( linkType=="GnmHLinkExternal" )
  1619. {
  1620. if ( !target.startsWith( "file://" ) )
  1621. target="file://"+target;
  1622. kspread_cell->setLink( target );
  1623. }
  1624. else if ( linkType=="GnmHLinkCurWB" )
  1625. {
  1626. kspread_cell->setLink( target );
  1627. }
  1628. else
  1629. kdDebug()<<" linkType not defined : "<<linkType<<endl;
  1630. }
  1631. }
  1632. }
  1633. }
  1634. }
  1635. style_region = style_region.nextSibling();
  1636. }
  1637. }
  1638. }
  1639. /* NOTE: As of now everything is in a single huge function. This is
  1640. very ugly. It should all be broken up into smaller
  1641. functions, probably one for each GNUMeric section. It kind
  1642. of grew out of control. It could probably be cleaned up in
  1643. an hour or so. --PGE
  1644. */
  1645. KoFilter::ConversionStatus GNUMERICFilter::convert( const TQCString & from, const TQCString & to )
  1646. {
  1647. dateInit();
  1648. bool bSuccess=true;
  1649. kdDebug(30521) << "Entering GNUmeric Import filter." << endl;
  1650. KoDocument * document = m_chain->outputDocument();
  1651. if ( !document )
  1652. return KoFilter::StupidError;
  1653. kdDebug(30521) << "here we go... " << document->className() << endl;
  1654. if ( !::tqqt_cast<const KSpread::Doc *>( document ) ) // it's safer that way :)
  1655. {
  1656. kdWarning(30521) << "document isn't a KSpread::Doc but a " << document->className() << endl;
  1657. return KoFilter::NotImplemented;
  1658. }
  1659. if ( from != "application/x-gnumeric" || to != "application/x-kspread" )
  1660. {
  1661. kdWarning(30521) << "Invalid mimetypes " << from << " " << to << endl;
  1662. return KoFilter::NotImplemented;
  1663. }
  1664. kdDebug(30521) << "...still here..." << endl;
  1665. // No need for a dynamic cast here, since we use TQt's moc magic
  1666. Doc * ksdoc = ( Doc * ) document;
  1667. if ( ksdoc->mimeType() != "application/x-kspread" )
  1668. {
  1669. kdWarning(30521) << "Invalid document mimetype " << ksdoc->mimeType() << endl;
  1670. return KoFilter::NotImplemented;
  1671. }
  1672. TQIODevice* in = KFilterDev::deviceForFile(m_chain->inputFile(),"application/x-gzip");
  1673. if ( !in )
  1674. {
  1675. kdError(30521) << "Cannot create device for uncompressing! Aborting!" << endl;
  1676. return KoFilter::FileNotFound;
  1677. }
  1678. if (!in->open(IO_ReadOnly))
  1679. {
  1680. kdError(30521) << "Cannot open file for uncompressing! Aborting!" << endl;
  1681. delete in;
  1682. return KoFilter::FileNotFound;
  1683. }
  1684. TQDomDocument doc;
  1685. TQString errorMsg;
  1686. int errorLine, errorColumn;
  1687. if ( !doc.setContent(in, &errorMsg, &errorLine, &errorColumn) )
  1688. {
  1689. kdError(30521) << "Parsing error in " << from << "! Aborting!" << endl
  1690. << " In line: " << errorLine << ", column: " << errorColumn << endl
  1691. << " Error message: " << errorMsg << endl;
  1692. in->close();
  1693. return KoFilter::ParsingError;
  1694. }
  1695. in->close();
  1696. delete in;
  1697. int row, column;
  1698. int value = 0;
  1699. int currentTab = -1;
  1700. int selectedTab = 0;
  1701. Sheet * selTable = 0;
  1702. TQDomElement docElem = doc.documentElement();
  1703. TQDomElement uiData = docElem.namedItem("gmr:UIData").toElement();
  1704. if ( !uiData.isNull() )
  1705. {
  1706. if ( uiData.hasAttribute( "SelectedTab" ) )
  1707. {
  1708. bool ok = false;
  1709. int n = uiData.attribute( "SelectedTab" ).toInt( &ok );
  1710. if ( ok )
  1711. {
  1712. selectedTab = n;
  1713. }
  1714. }
  1715. }
  1716. TQDomNode sheets = docElem.namedItem("gmr:Sheets");
  1717. if ( sheets.isNull() )
  1718. {
  1719. //avoid crash with new file format.
  1720. //TODO allow to load new file format
  1721. return KoFilter::ParsingError;
  1722. }
  1723. TQDomNode sheet = sheets.namedItem("gmr:Sheet");
  1724. /* This sets the Document information. */
  1725. set_document_info( document, &docElem );
  1726. /* This sets the Document attributes */
  1727. set_document_attributes( ksdoc, &docElem );
  1728. /* This sets the Area Names */
  1729. set_document_area_names( ksdoc, &docElem );
  1730. Sheet * table;
  1731. // This is a mapping of exprID to expressions.
  1732. TQDict<char> exprID_dict( 17, FALSE );
  1733. int num = 1;
  1734. while (!sheet.isNull())
  1735. {
  1736. ++currentTab;
  1737. table = ksdoc->map()->addNewSheet();
  1738. if ( currentTab == selectedTab )
  1739. selTable = table;
  1740. TQDomElement name = sheet.namedItem( "gmr:Name" ).toElement();
  1741. TQDomElement sheetElement = sheet.toElement();
  1742. if ( !name.isNull() )
  1743. table->setSheetName( name.text(), false, false );
  1744. else
  1745. table->setSheetName( "Sheet" + TQString::number( num ), false, false );
  1746. table->enableScrollBarUpdates( false );
  1747. //kdDebug()<<" sheetElement.hasAttribute( DisplayFormulas ) :"<<sheetElement.hasAttribute( "DisplayFormulas" )<<endl;
  1748. TQString tmp;
  1749. if ( sheetElement.hasAttribute( "DisplayFormulas" ) )
  1750. {
  1751. tmp=sheetElement.attribute( "DisplayFormulas");
  1752. table->setShowFormula( ( tmp=="true" )||( tmp=="1" ) );
  1753. }
  1754. if ( sheetElement.hasAttribute( "HideZero" ) )
  1755. {
  1756. tmp = sheetElement.attribute( "HideZero" );
  1757. table->setHideZero( ( tmp=="true" )||( tmp=="1" ) );
  1758. }
  1759. if ( sheetElement.hasAttribute( "HideGrid" ) )
  1760. {
  1761. tmp = sheetElement.attribute( "HideGrid" );
  1762. table->setShowGrid( ( tmp=="false" )||( tmp=="0" ) );
  1763. }
  1764. if ( sheetElement.hasAttribute( "HideColHeader" ) )
  1765. {
  1766. tmp = sheetElement.attribute( "HideColHeader" );
  1767. ksdoc->setShowColumnHeader( ( tmp=="false" )||( tmp=="0" ) );
  1768. }
  1769. if ( sheetElement.hasAttribute( "HideRowHeader" ) )
  1770. {
  1771. tmp =sheetElement.attribute( "HideRowHeader" );
  1772. ksdoc->setShowRowHeader( ( tmp=="false" )||( tmp=="0" ) );
  1773. }
  1774. setObjectInfo(&sheet, table);
  1775. setColInfo(&sheet, table);
  1776. setRowInfo(&sheet, table);
  1777. setSelectionInfo(&sheet, table);
  1778. /* handling print information */
  1779. TQDomNode printInfo = sheet.namedItem("gmr:PrintInformation");
  1780. if ( !printInfo.isNull() )
  1781. ParsePrintInfo( printInfo, table );
  1782. kdDebug(30521) << "Reading in cells" << endl;
  1783. /* CELL handling START */
  1784. TQDomNode cells = sheet.namedItem( "gmr:Cells" );
  1785. TQDomNode cell = cells.namedItem( "gmr:Cell" );
  1786. TQDomNode mergedCells = sheet.namedItem( "gmr:MergedRegions" );
  1787. TQDomNode mergedRegion = mergedCells.namedItem( "gmr:Merge" );
  1788. if ( cell.isNull() )
  1789. {
  1790. kdWarning(30521) << "No cells" << endl;
  1791. }
  1792. while ( !cell.isNull() )
  1793. {
  1794. value += 2;
  1795. emit sigProgress(value);
  1796. TQDomElement e = cell.toElement(); // try to convert the node to an element.
  1797. if ( !e.isNull() )
  1798. { // the node was really an element.
  1799. kdDebug(30521) << "New Cell " << endl;
  1800. TQDomNode content_node = cell.namedItem("gmr:Content");
  1801. if (!content_node.isNull())
  1802. {
  1803. TQDomElement content = content_node.toElement();
  1804. if( !content.isNull() )
  1805. { // the node was really an element.
  1806. column = e.attribute( "Col" ).toInt() + 1;
  1807. row = e.attribute( "Row" ).toInt() + 1;
  1808. TQString cell_content( content.text() );
  1809. //kdDebug()<<"cell_content :!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :"<<cell_content<<endl;
  1810. if ( cell_content[0] == '=' )
  1811. convertFormula( cell_content );
  1812. Cell * kspread_cell = table->nonDefaultCell( column, row );
  1813. if (e.hasAttribute("ValueType"))
  1814. {
  1815. // TODO: what is this for?
  1816. // <xsd:enumeration value="10"/> <!-- empty -->
  1817. // <xsd:enumeration value="20"/> <!-- boolean -->
  1818. // <xsd:enumeration value="30"/> <!-- integer -->
  1819. // <xsd:enumeration value="40"/> <!-- float -->
  1820. // <xsd:enumeration value="50"/> <!-- error -->
  1821. // <xsd:enumeration value="60"/> <!-- string -->
  1822. // <xsd:enumeration value="70"/> <!-- cellrange -->
  1823. // <xsd:enumeration value="80"/> <!-- array -->
  1824. TQString valuetype = e.attribute( "ValueType" );
  1825. if ( valuetype == "40" )//percentage
  1826. {
  1827. kspread_cell->format()->setFormatType( Percentage_format );
  1828. kspread_cell->setValue( cell_content );
  1829. }
  1830. else if ( valuetype =="60" )//string
  1831. {
  1832. kspread_cell->format()->setFormatType( Text_format );
  1833. kspread_cell->setValue( cell_content );
  1834. }
  1835. }
  1836. if (e.hasAttribute( "ValueFormat" ))
  1837. {
  1838. TQString formatString = e.attribute( "ValueFormat" );
  1839. if ( !setType( kspread_cell, formatString, cell_content ) )
  1840. table->setText(row, column, cell_content, false);
  1841. }
  1842. else
  1843. table->setText(row, column, cell_content, false);
  1844. if (e.hasAttribute("ExprID"))
  1845. {
  1846. // TQString encoded_string(table->cellAt( column, row, false)->encodeFormula( row, column ).utf8());
  1847. TQString encoded_string(table->cellAt( column, row, false )->encodeFormula().latin1());
  1848. char * tmp_string = ( char * ) malloc( strlen( encoded_string.latin1() ) );
  1849. strcpy( tmp_string, encoded_string.latin1() );
  1850. kdDebug(30521) << encoded_string.latin1() << endl;
  1851. exprID_dict.insert(e.attribute("ExprID"), tmp_string);
  1852. kdDebug(30521) << exprID_dict[e.attribute("ExprID")] << endl;
  1853. kdDebug(30521) << exprID_dict[TQString("1")] << endl;
  1854. kdDebug(30521) << e.attribute("ExprID") << endl;
  1855. }
  1856. }
  1857. }
  1858. else
  1859. {
  1860. column = e.attribute( "Col" ).toInt() + 1;
  1861. row = e.attribute( "Row" ).toInt() + 1;
  1862. TQString cell_content( e.text() );
  1863. //kdDebug()<<"cell_content :!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :"<<cell_content<<endl;
  1864. if ( cell_content[0] == '=' )
  1865. convertFormula( cell_content );
  1866. Cell * kspread_cell = table->nonDefaultCell( column, row );
  1867. if (e.hasAttribute("ValueType"))
  1868. {
  1869. // TODO: Defined type of cell
  1870. //<xsd:enumeration value="10"/> <!-- empty -->
  1871. //<xsd:enumeration value="20"/> <!-- boolean -->
  1872. //<xsd:enumeration value="30"/> <!-- integer -->
  1873. //<xsd:enumeration value="40"/> <!-- float -->
  1874. //<xsd:enumeration value="50"/> <!-- error -->
  1875. //<xsd:enumeration value="60"/> <!-- string -->
  1876. //<xsd:enumeration value="70"/> <!-- cellrange -->
  1877. //<xsd:enumeration value="80"/> <!-- array -->
  1878. //kspread_cell->setValue( date );
  1879. //kspread_cell->format()->setFormatType( type );
  1880. TQString valuetype = e.attribute( "ValueType" );
  1881. if ( valuetype == "40" )//percentage
  1882. {
  1883. kspread_cell->format()->setFormatType( Percentage_format );
  1884. kspread_cell->setValue( cell_content );
  1885. }
  1886. else if ( valuetype =="60" )//string
  1887. {
  1888. kspread_cell->format()->setFormatType( Text_format );
  1889. kspread_cell->setValue( cell_content );
  1890. }
  1891. }
  1892. if (e.hasAttribute( "ValueFormat" ))
  1893. {
  1894. TQString formatString = e.attribute( "ValueFormat" );
  1895. if ( !setType( kspread_cell, formatString, cell_content ) )
  1896. table->setText(row, column, cell_content, false);
  1897. }
  1898. else
  1899. table->setText(row, column, cell_content, false);
  1900. if (e.hasAttribute("ExprID"))
  1901. {
  1902. column = e.attribute("Col").toInt() + 1;
  1903. row = e.attribute("Row").toInt() + 1;
  1904. char * expr;
  1905. expr = exprID_dict[e.attribute("ExprID")];
  1906. // expr = exprID_dict[TQString("1")];
  1907. kdDebug(30521) << "FOO:" << column << row << endl;
  1908. kdDebug(30521) <<
  1909. table->cellAt( column, row, false )->decodeFormula( expr, column, row ).latin1() << endl;
  1910. kdDebug(30521) << expr << endl;
  1911. table->setText(row, column,
  1912. table->cellAt( column, row, false )->decodeFormula( expr, column, row ),
  1913. false);
  1914. }
  1915. }
  1916. }
  1917. cell = cell.nextSibling();
  1918. }
  1919. kdDebug(30521) << "Reading in cells done" << endl;
  1920. if ( mergedRegion.isNull() )
  1921. {
  1922. kdWarning(30521) << "No cells merged !" << endl;
  1923. }
  1924. while ( !mergedRegion.isNull() )
  1925. {
  1926. TQDomElement e = mergedRegion.toElement(); // try to convert the node to an element.
  1927. TQString cell_merge_area( e.text() );
  1928. Range range(cell_merge_area);
  1929. //kdDebug()<<"text !!! :"<<cell_merge_area<< "range :start row : "<<range.startRow ()<<" start col :"<<range.startCol ()<<" end row :"<<range.endRow ()<<" end col :"<<range.endCol ()<<endl;
  1930. Cell * cell = table->nonDefaultCell( range.startCol (), range.startRow () );
  1931. cell->mergeCells( range.startCol (), range.startRow (), range.endCol ()-range.startCol (), range.endRow ()-range.startRow ());
  1932. mergedRegion = mergedRegion.nextSibling();
  1933. }
  1934. /* There is a memory leak here...
  1935. * The strings in the exprID_dict have been allocated, but they have not been freed.
  1936. */
  1937. /* exprID_dict.statistics(); */
  1938. /* CELL handling STOP */
  1939. /* STYLE handling START */
  1940. //Laurent - 2001-12-07 desactivate this code : otherwise we
  1941. //create 65535*255 cells (Styleregion is define for a area and
  1942. //not for cell, so gnumeric define a style as : col start=0 col end=255
  1943. //rowstart=0 rowend=255 => we create 255*255 cells
  1944. //and gnumeric stocke all area and not just modify area
  1945. //=> not good for kspread.
  1946. // Norbert: activated again, only cells with texts get modified, nothing else created
  1947. setStyleInfo(&sheet, table);
  1948. /* STYLE handling STOP */
  1949. table->enableScrollBarUpdates( true );
  1950. sheet = sheet.nextSibling();
  1951. ++num;
  1952. }
  1953. if ( selTable )
  1954. ksdoc->setDisplaySheet( selTable );
  1955. emit sigProgress(100);
  1956. if ( bSuccess )
  1957. return KoFilter::OK;
  1958. else
  1959. return KoFilter::StupidError;
  1960. }
  1961. #include <gnumericimport.moc>