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