gnumericexport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2000 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 export filter by Phillip Ezolt (phillipezolt@hotmail.com)
22 2004 - Some updates by Tim Beaulen (tbscope@gmail.com) */
23
24#include <gnumericexport.h>
25#include <kdebug.h>
26#include <kfilterdev.h>
27#include <tdemessagebox.h>
28#include <kgenericfactory.h>
29#include <KoFilterChain.h>
30#include <tqapplication.h>
31#include <tqptrlist.h>
32#include <tqsortedlist.h>
33#include <tqfile.h>
34#include <tqtextstream.h>
35
36#include <kspread_map.h>
37#include <kspread_sheet.h>
38#include <kspread_doc.h>
39#include <kspread_view.h>
40#include <kspread_canvas.h>
41#include <kspread_sheetprint.h>
42#include <KoDocumentInfo.h>
43
44using namespace KSpread;
45
46typedef KGenericFactory<GNUMERICExport, KoFilter> GNUMERICExportFactory;
47K_EXPORT_COMPONENT_FACTORY( libgnumericexport, GNUMERICExportFactory( "kofficefilters" ) )
48
49GNUMERICExport::GNUMERICExport(KoFilter *, const char *, const TQStringList&) :
50KoFilter()
51{
52 isLink = false;
53 isLinkBold = false;
54 isLinkItalic = false;
55}
56
60bool GNUMERICExport::hasBorder(Cell *cell, int currentcolumn, int currentrow)
61{
62 if ( ( (cell->format()->leftBorderWidth(currentcolumn, currentrow) != 0) &&
63 (cell->format()->leftBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) ) ||
64 ( (cell->format()->rightBorderWidth(currentcolumn, currentrow) != 0) &&
65 (cell->format()->rightBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) ) ||
66 ( (cell->format()->topBorderWidth(currentcolumn, currentrow) != 0) &&
67 (cell->format()->topBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) ) ||
68 ( (cell->format()->bottomBorderWidth(currentcolumn, currentrow) != 0) &&
69 (cell->format()->bottomBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) ) ||
70 ( (cell->format()->fallDiagonalWidth(currentcolumn, currentrow) != 0) &&
71 (cell->format()->fallDiagonalStyle(currentcolumn, currentrow) != TQt::NoPen ) ) ||
72 ( (cell->format()->goUpDiagonalWidth(currentcolumn, currentrow) != 0) &&
73 (cell->format()->goUpDiagonalStyle(currentcolumn, currentrow) != TQt::NoPen ) ) )
74 return true;
75 else
76 return false;
77}
78
79const TQString GNUMERICExport::ColorToString(int red, int green, int blue)
80{
81 return TQString::number(red,16)+":"+TQString::number(green,16)+":"+TQString::number(blue,16);
82}
83
84TQDomElement GNUMERICExport::GetBorderStyle(TQDomDocument gnumeric_doc,Cell * cell, int currentcolumn, int currentrow)
85{
86 TQDomElement border_style;
87 TQDomElement border;
88
89 int red, green, blue;
90 TQColor color;
91
92 border_style = gnumeric_doc.createElement("gmr:StyleBorder");
93
94 if ( (cell->format()->leftBorderWidth(currentcolumn, currentrow) != 0) &&
95 (cell->format()->leftBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) )
96 {
97 border = gnumeric_doc.createElement("gmr:Left");
98 border.setAttribute("Style","1");
99
100 color = cell->format()->leftBorderColor(currentcolumn, currentrow);
101 red = color.red()<<8;
102 green = color.green()<<8;
103 blue = color.blue()<<8;
104
105 border.setAttribute("Color", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
106 }
107 else
108 {
109 border = gnumeric_doc.createElement("gmr:Left");
110 border.setAttribute("Style","0");
111 }
112
113 border_style.appendChild(border);
114
115 if ( (cell->format()->rightBorderWidth(currentcolumn, currentrow) != 0) &&
116 (cell->format()->rightBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) )
117 {
118 border = gnumeric_doc.createElement("gmr:Right");
119 border.setAttribute("Style","1");
120
121 color = cell->format()->rightBorderColor(currentcolumn, currentrow);
122 red = color.red()<<8;
123 green = color.green()<<8;
124 blue = color.blue()<<8;
125
126 border.setAttribute("Color", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
127 }
128 else
129 {
130 border = gnumeric_doc.createElement("gmr:Right");
131 border.setAttribute("Style","0");
132 }
133
134 border_style.appendChild(border);
135
136 if ( (cell->format()->topBorderWidth(currentcolumn, currentrow) != 0) &&
137 (cell->format()->topBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) )
138 {
139 border = gnumeric_doc.createElement("gmr:Top");
140 border.setAttribute("Style","1");
141
142 color = cell->format()->topBorderColor(currentcolumn, currentrow);
143 red = color.red()<<8;
144 green = color.green()<<8;
145 blue = color.blue()<<8;
146
147 border.setAttribute("Color", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
148 }
149 else
150 {
151 border = gnumeric_doc.createElement("gmr:Top");
152 border.setAttribute("Style","0");
153 }
154
155 border_style.appendChild(border);
156
157 if ( (cell->format()->bottomBorderWidth(currentcolumn, currentrow) != 0) &&
158 (cell->format()->bottomBorderStyle(currentcolumn, currentrow) != TQt::NoPen ) )
159 {
160 border = gnumeric_doc.createElement("gmr:Bottom");
161 border.setAttribute("Style","1");
162
163 color = cell->format()->bottomBorderColor(currentcolumn, currentrow);
164 red = color.red()<<8;
165 green = color.green()<<8;
166 blue = color.blue()<<8;
167
168 border.setAttribute("Color", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
169 }
170 else
171 {
172 border = gnumeric_doc.createElement("gmr:Bottom");
173 border.setAttribute("Style","0");
174 }
175
176 border_style.appendChild(border);
177
178 if ( (cell->format()->fallDiagonalWidth(currentcolumn, currentrow) != 0) &&
179 (cell->format()->fallDiagonalStyle(currentcolumn, currentrow) != TQt::NoPen ) )
180 {
181 border = gnumeric_doc.createElement("gmr:Diagonal");
182 border.setAttribute("Style","1");
183
184 color = cell->format()->fallDiagonalColor(currentcolumn, currentrow);
185 red = color.red()<<8;
186 green = color.green()<<8;
187 blue = color.blue()<<8;
188
189 border.setAttribute("Color", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
190 }
191 else
192 {
193 border = gnumeric_doc.createElement("gmr:Diagonal");
194 border.setAttribute("Style","0");
195 }
196
197 border_style.appendChild(border);
198
199 if ( (cell->format()->goUpDiagonalWidth(currentcolumn, currentrow) != 0) &&
200 (cell->format()->goUpDiagonalStyle(currentcolumn, currentrow) != TQt::NoPen ) )
201 {
202 border = gnumeric_doc.createElement("gmr:Rev-Diagonal");
203 border.setAttribute("Style","1");
204
205 color = cell->format()->goUpDiagonalColor(currentcolumn, currentrow);
206 red = color.red()<<8;
207 green = color.green()<<8;
208 blue = color.blue()<<8;
209
210 border.setAttribute("Color", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
211 }
212 else
213 {
214 border = gnumeric_doc.createElement("gmr:Rev-Diagonal");
215 border.setAttribute("Style","0");
216 }
217
218 border_style.appendChild(border);
219
220 return border_style;
221}
222
223TQDomElement GNUMERICExport::GetValidity( TQDomDocument gnumeric_doc, Cell * cell )
224{
225 //<gmr:Validation Style="1" Type="1" Operator="7" AllowBlank="true" UseDropdown="false" Title="ghhg" Message="ghghhhjfhfghjfghj&#10;fg&#10;hjgf&#10;hj">
226 // <gmr:Expression0>45</gmr:Expression0>
227 // </gmr:Validation>
228 Validity *kspread_validity = cell->getValidity();
229 TQDomElement val = gnumeric_doc.createElement( "gmr:Validation" );
230 val.setAttribute( "Title", kspread_validity->title );
231 val.setAttribute( "Message", kspread_validity->message );
232 val.setAttribute( "AllowBlank", kspread_validity->allowEmptyCell ? "true":"false" );
233 if ( !kspread_validity->displayMessage )
234 {
235 val.setAttribute("Style", "0" );
236 }
237 else
238 {
239 switch( kspread_validity->m_action )
240 {
241 case Action::Stop:
242 val.setAttribute("Style", "1" );
243 break;
244 case Action::Warning:
245 val.setAttribute("Style", "2" );
246 break;
247 case Action::Information:
248 val.setAttribute("Style", "3" );
249 break;
250 }
251 }
252
253 switch( kspread_validity->m_cond )
254 {
255 case Conditional::None:
256 //Nothing
257 break;
258 case Conditional::Equal:
259 val.setAttribute("Operator", "2" );
260 break;
261 case Conditional::Superior:
262 val.setAttribute("Operator", "4" );
263 break;
264 case Conditional::Inferior:
265 val.setAttribute("Operator", "5" );
266 break;
267 case Conditional::SuperiorEqual:
268 val.setAttribute("Operator", "6" );
269 break;
270 case Conditional::InferiorEqual:
271 val.setAttribute("Operator", "7" );
272 break;
273 case Conditional::Between:
274 val.setAttribute("Operator", "0" );
275 break;
276 case Conditional::Different:
277 val.setAttribute("Operator", "3" );
278 break;
279 case Conditional::DifferentTo:
280 val.setAttribute("Operator", "1" );
281 break;
282 }
283 switch( kspread_validity->m_restriction )
284 {
285 case Restriction::None:
286 val.setAttribute("Type", "0" );
287 break;
288 case Restriction::Number:
289 {
290 val.setAttribute("Type", "2" );
291 switch( kspread_validity->m_cond )
292 {
293 case Conditional::None:
294 //Nothing
295 break;
296 case Conditional::Equal:
297 case Conditional::Superior:
298 case Conditional::Inferior:
299 case Conditional::SuperiorEqual:
300 case Conditional::InferiorEqual:
301 case Conditional::Different:
302 {
303 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
304 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number( kspread_validity->valMin ) ) );
305 val.appendChild( tmp );
306 }
307 break;
308 case Conditional::Between:
309 case Conditional::DifferentTo:
310 {
311 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
312 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMin ) ) );
313 val.appendChild( tmp );
314 tmp = gnumeric_doc.createElement( "gmr:Expression1" );
315 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMax ) ) );
316 val.appendChild( tmp );
317 }
318 break;
319 }
320
321 break;
322 }
323 case Restriction::Text:
324 //Not supported into gnumeric
325 //val.setAttribute("Type", "1" );
326 break;
327 case Restriction::Time:
328 val.setAttribute("Type", "5" );
329 switch( kspread_validity->m_cond )
330 {
331 case Conditional::None:
332 //Nothing
333 break;
334 case Conditional::Equal:
335 case Conditional::Superior:
336 case Conditional::Inferior:
337 case Conditional::SuperiorEqual:
338 case Conditional::InferiorEqual:
339 case Conditional::Different:
340 {
341 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
342 tmp.appendChild( gnumeric_doc.createTextNode( kspread_validity->timeMin.toString() ) );
343 val.appendChild( tmp );
344 }
345 break;
346 case Conditional::Between:
347 case Conditional::DifferentTo:
348 {
349 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
350 tmp.appendChild( gnumeric_doc.createTextNode( kspread_validity->timeMin.toString() ) );
351 val.appendChild( tmp );
352 tmp = gnumeric_doc.createElement( "gmr:Expression1" );
353 tmp.appendChild( gnumeric_doc.createTextNode( kspread_validity->timeMax.toString() ) );
354 val.appendChild( tmp );
355 }
356 break;
357 }
358
359 break;
360 case Restriction::Date:
361 val.setAttribute("Type", "4" );
362 switch( kspread_validity->m_cond )
363 {
364 case Conditional::None:
365 //Nothing
366 break;
367 case Conditional::Equal:
368 case Conditional::Superior:
369 case Conditional::Inferior:
370 case Conditional::SuperiorEqual:
371 case Conditional::InferiorEqual:
372 case Conditional::Different:
373 {
374 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
375 tmp.appendChild( gnumeric_doc.createTextNode( kspread_validity->dateMin.toString() ) );
376 val.appendChild( tmp );
377 }
378 break;
379 case Conditional::Between:
380 case Conditional::DifferentTo:
381 {
382 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
383 tmp.appendChild( gnumeric_doc.createTextNode( kspread_validity->dateMin.toString() ) );
384 val.appendChild( tmp );
385 tmp = gnumeric_doc.createElement( "gmr:Expression1" );
386 tmp.appendChild( gnumeric_doc.createTextNode( kspread_validity->dateMax.toString() ) );
387 val.appendChild( tmp );
388 }
389 break;
390 }
391
392 break;
393 case Restriction::Integer:
394 val.setAttribute("Type", "1" );
395 switch( kspread_validity->m_cond )
396 {
397 case Conditional::None:
398 //Nothing
399 break;
400 case Conditional::Equal:
401 case Conditional::Superior:
402 case Conditional::Inferior:
403 case Conditional::SuperiorEqual:
404 case Conditional::InferiorEqual:
405 case Conditional::Different:
406 {
407 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
408 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMin ) ) );
409 val.appendChild( tmp );
410 }
411 break;
412 case Conditional::Between:
413 case Conditional::DifferentTo:
414 {
415 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
416 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMin ) ) );
417 val.appendChild( tmp );
418 tmp = gnumeric_doc.createElement( "gmr:Expression1" );
419 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMax ) ) );
420 val.appendChild( tmp );
421 }
422 break;
423 }
424 break;
425 case Restriction::TextLength:
426 val.setAttribute("Type", "6" );
427 switch( kspread_validity->m_cond )
428 {
429 case Conditional::None:
430 //Nothing
431 break;
432 case Conditional::Equal:
433 case Conditional::Superior:
434 case Conditional::Inferior:
435 case Conditional::SuperiorEqual:
436 case Conditional::InferiorEqual:
437 case Conditional::Different:
438 {
439 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
440 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMin ) ) );
441 val.appendChild( tmp );
442 }
443 break;
444 case Conditional::Between:
445 case Conditional::DifferentTo:
446 {
447 TQDomElement tmp = gnumeric_doc.createElement( "gmr:Expression0" );
448 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMin ) ) );
449 val.appendChild( tmp );
450 tmp = gnumeric_doc.createElement( "gmr:Expression1" );
451 tmp.appendChild( gnumeric_doc.createTextNode( TQString::number(kspread_validity->valMax ) ) );
452 val.appendChild( tmp );
453 }
454 break;
455 }
456 break;
457 case Restriction::List:
458 val.setAttribute("Type", "3" );
459 switch( kspread_validity->m_cond )
460 {
461 case Conditional::None:
462 //Nothing
463 break;
464 case Conditional::Equal:
465 case Conditional::Superior:
466 case Conditional::Inferior:
467 case Conditional::SuperiorEqual:
468 case Conditional::InferiorEqual:
469 case Conditional::Different:
470 break;
471 case Conditional::Between:
472 case Conditional::DifferentTo:
473 break;
474 }
475 break;
476 }
477
478 return val;
479}
480
481TQDomElement GNUMERICExport::GetFontStyle( TQDomDocument gnumeric_doc,Cell * cell, int currentcolumn, int currentrow)
482{
483 TQDomElement font_style;
484 kdDebug()<<" currentcolumn :"<<currentcolumn<<" currentrow :"<<currentrow<<endl;
485 font_style = gnumeric_doc.createElement("gmr:Font");
486 font_style.appendChild(gnumeric_doc.createTextNode(cell->format()->textFontFamily(currentcolumn, currentrow)));
487
488 if (cell->format()->textFontItalic(currentcolumn,currentrow) || (isLink && isLinkItalic))
489 {
490 font_style.setAttribute("Italic","1");
491 }
492 if (cell->format()->textFontBold(currentcolumn,currentrow) || (isLink && isLinkBold))
493 {
494 font_style.setAttribute("Bold","1");
495 }
496 if (cell->format()->textFontUnderline(currentcolumn,currentrow))
497 {
498 font_style.setAttribute("Underline","1");
499 }
500 if (cell->format()->textFontStrike(currentcolumn,currentrow))
501 {
502 font_style.setAttribute("StrikeThrough","1");
503 }
504 if (cell->format()->textFontSize(currentcolumn,currentrow))
505 {
506 font_style.setAttribute("Unit",TQString::number(cell->format()->textFontSize(currentcolumn,currentrow)));
507 }
508
509 return font_style;
510}
511
512TQDomElement GNUMERICExport::GetLinkStyle(TQDomDocument gnumeric_doc)
513{
514 TQDomElement link_style;
515
516 link_style = gnumeric_doc.createElement("gmr:HyperLink");
517
518 TQString path;
519
520 path = linkUrl;
521
522 if (path.section(":",0,0).lower() == "http")
523 link_style.setAttribute("type","GnmHLinkURL");
524 else if (path.section(":",0,0).lower() == "mailto")
525 link_style.setAttribute("type","GnmHLinkEMail");
526 else if (path.section(":",0,0).lower() == "file")
527 link_style.setAttribute("type","GnmHLinkExternal");
528 else if (path.left(5).lower() == "sheet")
529 link_style.setAttribute("type","GnmHLinkCurWB");
530 else
531 link_style.setAttribute("type","");
532
533 link_style.setAttribute("target",path);
534
535 // KSpread doesn't support link tips.
536 link_style.setAttribute("tip","");
537
538 return link_style;
539}
540
541TQDomElement GNUMERICExport::GetCellStyle(TQDomDocument gnumeric_doc,Cell * cell, int currentcolumn, int currentrow)
542{
543 TQColorGroup defaultColorGroup = TQApplication::palette().active();
544
545 TQDomElement cell_style;
546
547 cell_style = gnumeric_doc.createElement("gmr:Style");
548
549 int red, green, blue;
550
551 TQColor bgColor = cell->bgColor(currentcolumn, currentrow);
552 red = bgColor.red()<<8;
553 green = bgColor.green()<<8;
554 blue = bgColor.blue()<<8;
555
556 switch (cell->format()->backGroundBrushStyle(currentcolumn, currentrow))
557 {
558 case TQt::NoBrush:
559 cell_style.setAttribute("Shade","0");
560 break;
561 case TQt::SolidPattern:
562 // 100%
563 cell_style.setAttribute("Shade","1");
564 break;
565 case TQt::Dense1Pattern:
566 // 87.5%
567 cell_style.setAttribute("Shade","25");
568 break;
569 case TQt::Dense2Pattern:
570 // 75%
571 cell_style.setAttribute("Shade","2");
572 break;
573 case TQt::Dense3Pattern:
574 // 62.5%
575 // Not supported by GNumeric
576 // Fall back to 50%
577 cell_style.setAttribute("Shade","3");
578 break;
579 case TQt::Dense4Pattern:
580 // 50%
581 cell_style.setAttribute("Shade","3");
582 break;
583 case TQt::Dense5Pattern:
584 // 25%
585 cell_style.setAttribute("Shade","4");
586 break;
587 case TQt::Dense6Pattern:
588 // 12.5%
589 cell_style.setAttribute("Shade","5");
590 break;
591 case TQt::Dense7Pattern:
592 // 6.25%
593 cell_style.setAttribute("Shade","6");
594 break;
595 case TQt::HorPattern:
596 cell_style.setAttribute("Shade","13");
597 break;
598 case TQt::VerPattern:
599 cell_style.setAttribute("Shade","14");
600 break;
601 case TQt::CrossPattern:
602 cell_style.setAttribute("Shade","17");
603 break;
604 case TQt::BDiagPattern:
605 cell_style.setAttribute("Shade","16");
606 break;
607 case TQt::FDiagPattern:
608 cell_style.setAttribute("Shade","15");
609 break;
610 case TQt::DiagCrossPattern:
611 cell_style.setAttribute("Shade","18");
612 break;
613 case TQt::CustomPattern:
614 // Not supported by Gnumeric
615 cell_style.setAttribute("Shade","0");
616 break;
617 }
618
619 cell_style.setAttribute("Back",TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16) );
620
621
622 TQColor textColor = cell->format()->textColor(currentcolumn, currentrow);
623 red = textColor.red()<<8;
624 green = textColor.green()<<8;
625 blue = textColor.blue()<<8;
626
627 cell_style.setAttribute("Fore",TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16) );
628
629 if (cell->format()->align(currentcolumn,currentrow) == Format::Undefined)
630 {
631 cell_style.setAttribute("HAlign","1");
632 }
633 else if (cell->format()->align(currentcolumn,currentrow) == Format::Left)
634 {
635 cell_style.setAttribute("HAlign","2");
636 }
637 else if (cell->format()->align(currentcolumn,currentrow) == Format::Right)
638 {
639 cell_style.setAttribute("HAlign","4");
640 }
641 else if (cell->format()->align(currentcolumn,currentrow) == Format::Center)
642 {
643 cell_style.setAttribute("HAlign","8");
644 }
645
646 if (cell->format()->alignY(currentcolumn,currentrow) == Format::Top)
647 {
648 cell_style.setAttribute("VAlign","1");
649 }
650 else if (cell->format()->alignY(currentcolumn,currentrow) == Format::Bottom)
651 {
652 cell_style.setAttribute("VAlign","2");
653 }
654 else if (cell->format()->alignY(currentcolumn,currentrow) == Format::Middle)
655 {
656 cell_style.setAttribute("VAlign","4");
657 }
658
659 if (cell->format()->multiRow(currentcolumn,currentrow))
660 cell_style.setAttribute("WrapText","1");
661 else
662 cell_style.setAttribute("WrapText","0");
663
664 // ShrinkToFit not supported by KSpread (?)
665 cell_style.setAttribute("ShrinkToFit","0");
666
667 // I'm not sure about the rotation values.
668 // I never got it to work in GNumeric.
669 cell_style.setAttribute("Rotation", TQString::number(-1*cell->format()->getAngle(currentcolumn,currentrow)));
670
671 // The indentation in GNumeric is an integer value. In KSpread, it's a double.
672 // Save the double anyway, makes it even better when importing the document back in KSpread.
673 // TODO verify if it's correct, in import we "* 10.0"
674 cell_style.setAttribute("Indent", TQString::number(cell->format()->getIndent(currentcolumn,currentrow)));
675
676 cell_style.setAttribute("Locked", !cell->format()->notProtected(currentcolumn,currentrow));
677
678 // A KSpread cell can have two options to hide: only formula hidden, or everything hidden.
679 // I only consider a cell with everything hidden as hidden.
680 // Gnumeric hides everything or nothing.
681 cell_style.setAttribute("Hidden", cell->format()->isHideAll(currentcolumn,currentrow));
682
683 TQColor patColor = cell->format()->backGroundBrushColor(currentcolumn, currentrow);
684 red = patColor.red()<<8;
685 green = patColor.green()<<8;
686 blue = patColor.blue()<<8;
687
688 cell_style.setAttribute("PatternColor", TQString::number(red,16)+":"+TQString::number(green,16) +":"+TQString::number(blue,16));
689
690 if (isLink)
691 cell_style.appendChild(GetLinkStyle(gnumeric_doc));
692
693 cell_style.appendChild(GetFontStyle(gnumeric_doc, cell, currentcolumn, currentrow));
694
695 if ( cell->getValidity() )
696 {
697 cell_style.appendChild( GetValidity( gnumeric_doc, cell ) );
698 }
699
700 TQString stringFormat;
701
702 Format::Currency c;
703 Currency currency;
704
705 switch( cell->format()->getFormatType(currentcolumn, currentrow))
706 {
707 case Number_format:
708 stringFormat="0.00";
709 break;
710 case Text_format:
711 stringFormat="general";
712 break;
713 case Money_format:
714
715 if (!cell->format()->currencyInfo(c))
716 {
717 stringFormat = "0.00";
718 break;
719 }
720
721 if (currency.getCurrencyCode(c.type).isEmpty())
722 stringFormat = "0.00";
723 else if (currency.getCurrencyCode(c.type) == "$")
724 stringFormat = "$0.00";
725 else if (currency.getCurrencyCode(c.type) == TQString::fromUtf8("€"))
726 stringFormat = "[$€-2]0.00";
727 else if (currency.getCurrencyCode(c.type) == TQString::fromUtf8("£"))
728 stringFormat = "£0.00";
729 else if (currency.getCurrencyCode(c.type) == TQString::fromUtf8("Â¥"))
730 stringFormat = "Â¥0.00";
731 else
732 stringFormat="[$" + currency.getCurrencyCode(c.type) + "]0.00";
733
734 break;
735 case Percentage_format:
736 stringFormat="0.00%";
737 break;
738 case Scientific_format:
739 stringFormat="0.00E+00";
740 break;
741 case ShortDate_format:
742 stringFormat=cell->locale()->dateFormatShort();
743 break;
744 case TextDate_format:
745 stringFormat=cell->locale()->dateFormat();
746 break;
747 case date_format1:
748 stringFormat="dd-mmm-yy";
749 break;
750 case date_format2:
751 stringFormat="dd-mmm-yyyy";
752 break;
753 case date_format3:
754 stringFormat="dd-mmm";
755 break;
756 case date_format4:
757 stringFormat="dd-mm";
758 break;
759 case date_format5:
760 stringFormat="dd/mm/yy";
761 break;
762 case date_format6:
763 stringFormat="dd/mm/yyyy";
764 break;
765 case date_format7:
766 stringFormat="mmm-yy";
767 break;
768 case date_format8:
769 stringFormat="mmmm-yy";
770 break;
771 case date_format9:
772 stringFormat="mmmm-yyyy";
773 break;
774 case date_format10:
775 stringFormat="m-yy";
776 break;
777 case date_format11:
778 stringFormat="dd/mmm";
779 break;
780 case date_format12:
781 stringFormat="dd/mm";
782 break;
783 case date_format13:
784 stringFormat="dd/mmm/yyyy";
785 break;
786 case date_format14:
787 stringFormat="yyyy/mmm/dd";
788 break;
789 case date_format15:
790 stringFormat="yyyy-mmm-dd";
791 break;
792 case date_format16:
793 stringFormat="yyyy-mm-dd";
794 break;
795 case date_format17:
796 stringFormat="d mmmm yyyy";
797 break;
798 case date_format18:
799 stringFormat="mm/dd/yyyy";
800 break;
801 case date_format19:
802 stringFormat="mm/dd/yy";
803 break;
804 case date_format20:
805 stringFormat="mmm/dd/yy";
806 break;
807 case date_format21:
808 stringFormat="mmm/dd/yyyy";
809 break;
810 case date_format22:
811 stringFormat="mmm-yyyy";
812 break;
813 case date_format23:
814 stringFormat="yyyy";
815 break;
816 case date_format24:
817 stringFormat="yy";
818 break;
819 case date_format25:
820 stringFormat="yyyy/mm/dd";
821 break;
822 case date_format26:
823 stringFormat="yyyy/mmm/dd";
824 break;
825 case Time_format:
826 case SecondeTime_format:
827 stringFormat=cell->locale()->timeFormat();
828 break;
829 case Time_format1:
830 stringFormat = "h:mm AM/PM";
831 break;
832 case Time_format2:
833 stringFormat = "h:mm:ss AM/PM";
834 break;
835 case Time_format3:
836 stringFormat = "h \"h\" mm \"min\" ss \"s\"";
837 break;
838 case Time_format4:
839 stringFormat = "h:mm";
840 break;
841 case Time_format5:
842 stringFormat = "h:mm:ss";
843 break;
844 case Time_format6:
845 stringFormat = "mm:ss";
846 break;
847 case Time_format7:
848 stringFormat = "[h]:mm:ss";
849 break;
850 case Time_format8:
851 stringFormat = "[h]:mm";
852 break;
853 case fraction_half:
854 stringFormat="# ?/2";
855 break;
856 case fraction_quarter:
857 stringFormat="# ?/4";
858 break;
859 case fraction_eighth:
860 stringFormat="# ?/8";
861 break;
862 case fraction_sixteenth:
863 stringFormat="# ?/16";
864 break;
865 case fraction_tenth:
866 stringFormat="# ?/10";
867 break;
868 case fraction_hundredth:
869 stringFormat="# ?/100";
870 break;
871 case fraction_one_digit:
872 stringFormat="# ?/?";
873 break;
874 case fraction_two_digits:
875 stringFormat="# ?\?/?\?";
876 break;
877 case fraction_three_digits:
878 stringFormat="# ?\?\?/?\?\?";
879 break;
880 case Custom_format:
881 stringFormat = cell->format()->getFormatString(currentcolumn,currentrow);
882 break;
883 default:
884 break;
885 }
886 cell_style.setAttribute("Format",stringFormat);
887
888 if(hasBorder(cell, currentcolumn, currentrow))
889 cell_style.appendChild(GetBorderStyle(gnumeric_doc, cell, currentcolumn, currentrow));
890
891 return cell_style;
892}
893
894
895void GNUMERICExport::addAttributeItem(TQDomDocument gnumeric_doc, TQDomElement attributes, const TQString& type, const TQString& name, bool value)
896{
897 TQDomElement gmr_attribute, gmr_type, gmr_name, gmr_value;
898
899 gmr_attribute = gnumeric_doc.createElement("gmr:Attribute");
900 attributes.appendChild(gmr_attribute);
901
902 gmr_type = gnumeric_doc.createElement("gmr:type");
903 gmr_type.appendChild(gnumeric_doc.createTextNode(type));
904 gmr_attribute.appendChild(gmr_type);
905
906 gmr_name = gnumeric_doc.createElement("gmr:name");
907 gmr_name.appendChild(gnumeric_doc.createTextNode(name));
908 gmr_attribute.appendChild(gmr_name);
909
910 TQString txtValue;
911 if (value)
912 txtValue = "true";
913 else
914 txtValue = "false";
915
916 gmr_value = gnumeric_doc.createElement("gmr:value");
917 gmr_value.appendChild(gnumeric_doc.createTextNode(txtValue));
918 gmr_attribute.appendChild(gmr_value);
919}
920
921void GNUMERICExport::addSummaryItem(TQDomDocument gnumeric_doc, TQDomElement summary, const TQString& name, const TQString& value)
922{
923 if ( value.isEmpty() )
924 return;
925 TQDomElement gmr_item, gmr_name, gmr_val_string;
926
927 gmr_item = gnumeric_doc.createElement("gmr:Item");
928 summary.appendChild(gmr_item);
929
930 gmr_name = gnumeric_doc.createElement("gmr:name");
931 gmr_name.appendChild(gnumeric_doc.createTextNode(name));
932 gmr_item.appendChild(gmr_name);
933
934 gmr_val_string = gnumeric_doc.createElement("gmr:val-string");
935 gmr_val_string.appendChild(gnumeric_doc.createTextNode(value));
936 gmr_item.appendChild(gmr_val_string);
937}
938
939// The reason why we use the KoDocument* approach and not the TQDomDocument
940// approach is because we don't want to export formulas but values !
941KoFilter::ConversionStatus GNUMERICExport::convert( const TQCString& from, const TQCString& to )
942{
943 kdDebug(30521) << "Exporting GNUmeric" << endl;
944
945 TQDomDocument gnumeric_doc=TQDomDocument();
946
947 Sheet* table;
948 KoDocument* document = m_chain->inputDocument();
949
950 if (!document)
951 return KoFilter::StupidError;
952
953 if ( !::tqt_cast<const KSpread::Doc *>( document ) ) // it's safer that way :)
954 {
955 kdWarning(30521) << "document isn't a KSpread::Doc but a " << document->className() << endl;
956 return KoFilter::NotImplemented;
957 }
958 if (to != "application/x-gnumeric" || from != "application/x-kspread")
959 {
960 kdWarning(30521) << "Invalid mimetypes " << to << " " << from << endl;
961 return KoFilter::NotImplemented;
962 }
963
964 Doc* ksdoc = (Doc*)document;
965
966 if (ksdoc->mimeType() != "application/x-kspread")
967 {
968 kdWarning(30521) << "Invalid document mimetype " << ksdoc->mimeType() << endl;
969 return KoFilter::NotImplemented;
970 }
971
972 /* This could be Made into a function */
973
974 gnumeric_doc.appendChild( gnumeric_doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
975
976 TQDomElement workbook = gnumeric_doc.createElement("gmr:Workbook");
977 workbook.setAttribute("xmlns:gmr","http://www.gnumeric.org/v10.dtd");
978 workbook.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
979 workbook.setAttribute("xmlns:schemaLocation", "http://www.gnumeric.org/v8.xsd");
980 gnumeric_doc.appendChild(workbook);
981
982 /* End Made into a function */
983
984 TQDomElement sheets,sheet,tmp,cells,selections, cols,rows,styles,merged, margins, top, left, bottom, right, orientation, paper, header, footer, customSize, cellComment, objects, repeatColumns, repeatRows;
985
986 KoDocumentInfo *DocumentInfo = document->documentInfo();
987 KoDocumentInfoAbout *aboutPage = static_cast<KoDocumentInfoAbout *>(DocumentInfo->page( "about" ));
988
989 KoDocumentInfoAuthor *authorPage = static_cast<KoDocumentInfoAuthor*>(DocumentInfo->page( "author" ));
990
991 /*
992 * Attributes
993 */
994 TQDomElement attributes = gnumeric_doc.createElement("gmr:Attributes");
995 workbook.appendChild(attributes);
996
997 addAttributeItem(gnumeric_doc, attributes, "4", "WorkbookView::show_horizontal_scrollbar", ksdoc->showHorizontalScrollBar());
998 addAttributeItem(gnumeric_doc, attributes, "4", "WorkbookView::show_vertical_scrollbar", ksdoc->showVerticalScrollBar());
999 addAttributeItem(gnumeric_doc, attributes, "4", "WorkbookView::show_notebook_tabs", ksdoc->showTabBar());
1000 if (ksdoc->completionMode() == TDEGlobalSettings::CompletionAuto)
1001 addAttributeItem(gnumeric_doc, attributes, "4", "WorkbookView::do_auto_completion", "true");
1002 else
1003 addAttributeItem(gnumeric_doc, attributes, "4", "WorkbookView::do_auto_completion", "false");
1004 addAttributeItem(gnumeric_doc, attributes, "4", "WorkbookView::is_protected", ksdoc->map()->isProtected());
1005
1006 /*
1007 * Doccument summary
1008 */
1009 TQDomElement summary = gnumeric_doc.createElement("gmr:Summary");
1010 workbook.appendChild(summary);
1011
1012 addSummaryItem(gnumeric_doc, summary, "title", aboutPage->title());
1013 addSummaryItem(gnumeric_doc, summary, "company", authorPage->company());
1014 addSummaryItem(gnumeric_doc, summary, "author", authorPage->fullName());
1015 addSummaryItem(gnumeric_doc, summary, "comments", aboutPage->abstract());
1016 addSummaryItem(gnumeric_doc, summary, "keywords", aboutPage->keywords());
1017
1018 addSummaryItem(gnumeric_doc, summary, "application", "KSpread");
1019
1020 /*
1021 * Sheet name index (necessary for the gnumeric xml_sax importer)
1022 */
1023 TQDomElement sheetNameIndex = gnumeric_doc.createElement("gmr:SheetNameIndex");
1024 workbook.appendChild(sheetNameIndex);
1025
1026 for (table = ksdoc->map()->firstSheet(); table != 0L; table =ksdoc->map()->nextSheet())
1027 {
1028 TQDomElement sheetName = gnumeric_doc.createElement("gmr:SheetName");
1029 sheetName.appendChild(gnumeric_doc.createTextNode(table->tableName()));
1030 sheetNameIndex.appendChild(sheetName);
1031 }
1032
1033 /*
1034 * Area Names
1035 */
1036 /*
1037 <gmr:Names>
1038 <gmr:Name>
1039 <gmr:name>test</gmr:name>
1040 <gmr:value>Sheet2!$A$2:$D$10</gmr:value>
1041 <gmr:position>A1</gmr:position>
1042 </gmr:Name>
1043 <gmr:Name>
1044 <gmr:name>voiture</gmr:name>
1045 <gmr:value>Sheet2!$A$2:$D$8</gmr:value>
1046 <gmr:position>A1</gmr:position>
1047 </gmr:Name>
1048 </gmr:Names>
1049 */
1050 if ( ksdoc->listArea().count()>0 )
1051 {
1052 TQDomElement areaNames = gnumeric_doc.createElement("gmr:Names");
1053 const TQValueList<Reference> &area = ksdoc->listArea(); // copying by value is slow!
1054 TQValueList<Reference>::ConstIterator it = area.begin();
1055 TQValueList<Reference>::ConstIterator end = area.end();
1056 for ( ; it != end; ++it )
1057 {
1058 TQDomElement areaName = gnumeric_doc.createElement("gmr:Name");
1059 TQDomElement areaNameElement = gnumeric_doc.createElement("gmr:name");
1060 areaNameElement.appendChild(gnumeric_doc.createTextNode(( *it ).ref_name) );
1061 areaName.appendChild( areaNameElement );
1062 TQDomElement areaNameValue = gnumeric_doc.createElement("gmr:value");
1063 areaNameValue.appendChild(gnumeric_doc.createTextNode( convertRefToRange( ( *it ).sheet_name, ( *it ).rect ) ) );
1064 areaName.appendChild( areaNameValue );
1065 areaNames.appendChild( areaName );
1066 //TODO <gmr:position>A1</gmr:position> I don't know what is it.
1067 }
1068 workbook.appendChild(areaNames);
1069 }
1070
1071
1072 /*
1073 * Sheets
1074 */
1075 sheets = gnumeric_doc.createElement("gmr:Sheets");
1076 workbook.appendChild(sheets);
1077
1078 TQString str;
1079
1080 View * view = static_cast<View*>( ksdoc->views().getFirst());
1081 Canvas * canvas=0L;
1082 TQString activeTableName;
1083 if (view)
1084 {
1085 canvas = view->canvasWidget();
1086 activeTableName = canvas->activeSheet()->sheetName();
1087 }
1088 int i = 0;
1089 int indexActiveTable=0;
1090 for (table = ksdoc->map()->firstSheet(); table != 0L; table =ksdoc->map()->nextSheet(), i++)
1091 {
1092 if ( table->print()->paperFormat()==PG_CUSTOM )
1093 {
1094 customSize = gnumeric_doc.createElement( "gmr:Geometry" );
1095 customSize.setAttribute( "Width", POINT_TO_MM ( table->print()->paperWidth() ));
1096 customSize.setAttribute( "Height", POINT_TO_MM (table->print()->paperWidth() ));
1097 sheets.appendChild(customSize);
1098 //<gmr:Geometry Width="768" Height="365"/>
1099 }
1100
1101 sheet = gnumeric_doc.createElement("gmr:Sheet");
1102 sheets.appendChild(sheet);
1103
1104 sheet.setAttribute("DisplayFormulas", table->getShowFormula() ? "true" : "false" );
1105 sheet.setAttribute("HideZero", table->getHideZero()? "true" : "false");
1106 sheet.setAttribute("HideGrid", !table->getShowGrid()? "true" : "false");
1107 sheet.setAttribute("HideColHeader", ( !ksdoc->showColumnHeader() ? "true" : "false" ));
1108 sheet.setAttribute("HideRowHeader", ( !ksdoc->showRowHeader()? "true" : "false" ));
1109 /* Not available in KSpread ?
1110 * sheet.setAttribute("DisplayOutlines", "true");
1111 * sheet.setAttribute("OutlineSymbolsBelow", "true");
1112 * sheet.setAttribute("OutlineSymbolsRight", "true");
1113 * sheet.setAttribute("TabColor", "");
1114 * sheet.setAttribute("TabTextColor", "");
1115 */
1116
1117 tmp = gnumeric_doc.createElement("gmr:Name");
1118 if ( table->tableName()==activeTableName )
1119 indexActiveTable = i;
1120
1121 tmp.appendChild(gnumeric_doc.createTextNode(table->tableName()));
1122
1123 sheet.appendChild(tmp);
1124
1125 tmp = gnumeric_doc.createElement("gmr:MaxCol");
1126 tmp.appendChild(gnumeric_doc.createTextNode(TQString::number(table->maxColumn())));
1127 sheet.appendChild(tmp);
1128
1129 tmp = gnumeric_doc.createElement("gmr:MaxRow");
1130
1131 tmp.appendChild(gnumeric_doc.createTextNode(TQString::number(table->maxRow())));
1132 sheet.appendChild(tmp);
1133
1134 // Zoom value doesn't appear to be correct
1135 // KSpread 200% gives zoom() = 2.5, this in GNumeric = 250%
1136 tmp = gnumeric_doc.createElement("gmr:Zoom");
1137 if (view)
1138 tmp.appendChild(gnumeric_doc.createTextNode(TQString::number(canvas->zoom())));
1139 else
1140 tmp.appendChild(gnumeric_doc.createTextNode("1.0"));
1141 sheet.appendChild(tmp);
1142
1143 //Print Info
1144 tmp = gnumeric_doc.createElement( "gmr:PrintInformation" );
1145 margins = gnumeric_doc.createElement( "gmr:Margins" );
1146
1147 top = gnumeric_doc.createElement( "gmr:top" );
1148 top.setAttribute("Points", table->print()->topBorder());
1149 top.setAttribute("PrefUnit", "mm");
1150 margins.appendChild( top );
1151
1152 bottom = gnumeric_doc.createElement( "gmr:bottom" );
1153 bottom.setAttribute("Points", table->print()->bottomBorder());
1154 bottom.setAttribute("PrefUnit", "mm");
1155 margins.appendChild( bottom );
1156
1157 left = gnumeric_doc.createElement( "gmr:left" );
1158 left.setAttribute("Points", table->print()->leftBorder());
1159 left.setAttribute("PrefUnit", "mm");
1160 margins.appendChild( left );
1161
1162 right = gnumeric_doc.createElement( "gmr:right" );
1163 right.setAttribute("Points", table->print()->rightBorder());
1164 right.setAttribute("PrefUnit", "mm");
1165 margins.appendChild( right );
1166
1167 tmp.appendChild( margins );
1168 sheet.appendChild(tmp);
1169
1170 orientation = gnumeric_doc.createElement( "gmr:orientation" );
1171 TQString orientString = table->print()->orientation() == PG_LANDSCAPE ? "landscape" : "portrait";
1172 orientation.appendChild( gnumeric_doc.createTextNode(orientString) );
1173 tmp.appendChild( orientation );
1174
1175 //TODO for future
1176 //<gmr:repeat_top value="A1:IV5"/>
1177 //<gmr:repeat_left value="B1:D65536"/>
1178
1179 int _tmpRepeatColumnStart = table->print()->printRepeatColumns().first;
1180 int _tmpRepeatColumnEnd = table->print()->printRepeatColumns().second;
1181 if ( _tmpRepeatColumnStart!=0 )
1182 {
1183 repeatColumns = gnumeric_doc.createElement( "gmr:repeat_left" );
1184 TQString value = Cell::columnName( _tmpRepeatColumnStart )+"1:"+Cell::columnName(_tmpRepeatColumnEnd )+"65536";
1185 repeatColumns.setAttribute( "value", value );
1186 tmp.appendChild( repeatColumns );
1187 }
1188 int _tmpRepeatRowStart = table->print()->printRepeatRows().first;
1189 int _tmpRepeatRowEnd = table->print()->printRepeatRows().second;
1190 if ( _tmpRepeatRowStart!=0 )
1191 {
1192 repeatRows = gnumeric_doc.createElement( "gmr:repeat_top" );
1193 TQString value = "A"+ TQString::number(_tmpRepeatRowStart ) +":IV"+TQString::number( _tmpRepeatRowEnd );
1194 repeatRows.setAttribute( "value", value );
1195 tmp.appendChild( repeatRows );
1196 }
1197
1198 header = gnumeric_doc.createElement( "gmr:Header" );
1199 header.setAttribute( "Left", convertVariable(table->print()->headLeft() ) );
1200 header.setAttribute( "Middle", convertVariable(table->print()->headMid() ) );
1201 header.setAttribute( "Right", convertVariable(table->print()->headRight() ) );
1202 tmp.appendChild( header );
1203
1204 footer = gnumeric_doc.createElement( "gmr:Footer" );
1205 footer.setAttribute( "Left", convertVariable( table->print()->footLeft() ) );
1206 footer.setAttribute( "Middle", convertVariable( table->print()->footMid() ) );
1207 footer.setAttribute( "Right", convertVariable( table->print()->footRight() ));
1208 tmp.appendChild( footer );
1209
1210 paper = gnumeric_doc.createElement( "gmr:paper" );
1211 paper.appendChild( gnumeric_doc.createTextNode( table->print()->paperFormatString() ) );
1212 tmp.appendChild( paper );
1213
1214 styles = gnumeric_doc.createElement("gmr:Styles");
1215 sheet.appendChild(styles);
1216
1217 cells = gnumeric_doc.createElement("gmr:Cells");
1218 sheet.appendChild(cells);
1219
1220 objects = gnumeric_doc.createElement("gmr:Objects");
1221 sheet.appendChild(objects);
1222
1223 merged = gnumeric_doc.createElement("gmr:MergedRegions");
1224 bool mergedCells = false; // if there are no merged cells in this sheet, don't write an
1225 // empty mergedRegions to the file.
1226 // So, depending on the value of mergedCells,
1227 // the merged dom element is added or not.
1228
1229 cols = gnumeric_doc.createElement("gmr:Cols");
1230 sheet.appendChild(cols);
1231
1232 rows = gnumeric_doc.createElement("gmr:Rows");
1233 sheet.appendChild(rows);
1234
1235 /*
1236 selections = gnumeric_doc.createElement("gmr:Selections");
1237 sheet.appendChild(selections);
1238 */
1239 // Ah ah ah - the document is const, but the map and table aren't. Safety: 0.
1240 // Either we get hold of Sheet::m_dctCells and apply the old method below
1241 // (for sorting) or, cleaner and already sorted, we use Sheet's API
1242 // (slower probably, though)
1243 int iMaxColumn = table->maxColumn();
1244 int iMaxRow = table->maxRow();
1245
1246 // this is just a bad approximation which fails for documents with less than 50 rows, but
1247 // we don't need any progress stuff there anyway :) (Werner)
1248 int value=0;
1249 int step=iMaxRow > 50 ? iMaxRow/50 : 1;
1250 int i=1;
1251
1252 TQString emptyLines;
1253
1254 /* Save selection info. */
1255
1256 /* can't save selection anymore -- part of the view, not table */
1257 /*
1258 TQDomElement selection = gnumeric_doc.createElement("gmr:Selection");
1259 TQRect table_selection(table->selection());
1260
1261 selections.appendChild(selection);
1262 */
1263 /* <gmr:Selection startCol="3" startRow="2" endCol="3" endRow="2"/>*/
1264 /*
1265 selection.setAttribute("startCol", TQString::number(table_selection.left()-1));
1266 selection.setAttribute("startRow", TQString::number(table_selection.top()-1));
1267
1268 selection.setAttribute("endCol", TQString::number(table_selection.right()-1));
1269 selection.setAttribute("endRow", TQString::number(table_selection.bottom()-1));
1270 */
1271 /* End selection info. */
1272
1273
1274 /* Start COLS */
1275 ColumnFormat *cl=table->firstCol();
1276 while (cl)
1277 {
1278 TQDomElement colinfo = gnumeric_doc.createElement("gmr:ColInfo");
1279 cols.appendChild(colinfo);
1280 colinfo.setAttribute("No", TQString::number(cl->column()-1));
1281 colinfo.setAttribute("Hidden", TQString::number(cl->isHide()));
1282 colinfo.setAttribute("Unit", TQString::number(cl->dblWidth()));
1283
1284 cl=cl->next();
1285 }
1286
1287 /* End COLS */
1288
1289 // RowFormat *rl=table->m_cells.firstCell;
1290 // <gmr:ColInfo No="1" Unit="96.75" MarginA="2" MarginB="2" HardSize="-1" Hidden="0"/>
1291
1292 /* Start ROWS */
1293 RowFormat *rl=table->firstRow();
1294 while (rl)
1295 {
1296 TQDomElement rowinfo = gnumeric_doc.createElement("gmr:RowInfo");
1297 rows.appendChild(rowinfo);
1298 rowinfo.setAttribute("No", TQString::number(rl->row()-1));
1299 rowinfo.setAttribute("Hidden", TQString::number(rl->isHide()));
1300 rowinfo.setAttribute("Unit", TQString::number(rl->dblHeight()));
1301
1302 rl=rl->next();
1303 }
1304
1305 /* End ROWS */
1306
1307 //rl->setHeight
1308 // colinfo.info();
1309 /*
1310 <gmr:ColInfo No="1" Unit="96.75" MarginA="2" MarginB="2" HardSize="-1" Hidden="0"/>
1311 <gmr:ColInfo No="3" Unit="113.25" MarginA="2" MarginB="2" HardSize="-1"
1312 Hidden="0"/>
1313 */
1314
1315 /* End COLS */
1316
1317 for (int currentrow = 1; currentrow <= iMaxRow; ++currentrow, ++i)
1318 {
1319 if(i>step)
1320 {
1321 value+=2;
1322 emit sigProgress(value);
1323 i=0;
1324 }
1325
1326 TQString line;
1327 for (int currentcolumn = 1; currentcolumn <= iMaxColumn; currentcolumn++)
1328 {
1329 TQDomElement cell_contents;
1330 Cell * cell = table->cellAt( currentcolumn, currentrow, false );
1331
1332 TQString text, style;
1333 TQDomDocument domLink;
1334 TQDomElement domRoot;
1335 TQDomNode domNode;
1336 TQDomNodeList childNodes;
1337
1338 if (!cell->isDefault() && !cell->isEmpty())
1339 {
1340 if ( cell->isFormula() )
1341 {
1342 TQString tmp = cell->text();
1343 if ( tmp.contains( "==" ) )
1344 tmp=tmp.replace( "==", "=" );
1345 text = tmp;
1346 isLink = false;
1347 }
1348 else if ( !cell->link().isEmpty() )
1349 {
1350 isLink = true;
1351 isLinkBold = false;
1352 isLinkItalic = false;
1353 //TODO FIXME
1354 linkUrl = cell->link();
1355 linkText = cell->text();
1356
1357 }
1358 else
1359 {
1360 text = cell->text();
1361 isLink = false;
1362 }
1363#if 0
1364 switch (cell->content())
1365 {
1366 case Cell::Text:
1367 text = cell->text();
1368 isLink = false;
1369 break;
1370 case Cell::RichText:
1371 // hyperlinks
1372 // Extract the cell text
1373 isLink = true;
1374 isLinkBold = false;
1375 isLinkItalic = false;
1376 domLink.setContent(cell->text().section("!",1,1));
1377
1378 domNode = domLink.firstChild();
1379 domRoot = domNode.toElement();
1380 text = domNode.toElement().text();
1381
1382 while (!domNode.isNull())
1383 {
1384 style = domNode.toElement().tagName();
1385
1386 if (style == "b")
1387 isLinkBold = true;
1388
1389 if (style == "i")
1390 isLinkItalic = true;
1391
1392 domNode = domNode.firstChild();
1393 }
1394
1395 //kdDebug(30521) << "---> link, text = " << text << endl;
1396
1397 linkUrl = domRoot.attribute("href");
1398 linkText = text;
1399
1400 break;
1401 case Cell::VisualFormula:
1402 isLink = false;
1403 text = cell->text(); // untested
1404 break;
1405 case Cell::Formula:
1406 isLink = false;
1407 TQString tmp = cell->text();
1408 if ( tmp =="==" )
1409 tmp=replace( "==", "=" );
1410 /* cell->calc( true ); // Incredible, cells are not calculated if the document was just opened text = cell->valueString(); */
1411 text = tmp;
1412 break;
1413 }
1414#endif
1415 }
1416
1417 if (!cell->isDefault())
1418 {
1419
1420 // Check if the cell is merged
1421 // Only cells with content are interesting?
1422 // Otherwise it can take a while to parse a large sheet
1423
1424 if (cell->doesMergeCells())
1425 {
1426 // The cell is forced to occupy other cells
1427 TQDomElement merge = gnumeric_doc.createElement("gmr:Merge");
1428
1429 // Set up the range
1430 TQString fromCol, toCol, fromRow, toRow;
1431 fromCol = cell->columnName(currentcolumn);
1432 fromRow = TQString::number(currentrow);
1433 toCol = cell->columnName(currentcolumn + cell->mergedXCells());
1434 toRow = TQString::number(currentrow + cell->mergedYCells());
1435
1436 merge.appendChild(gnumeric_doc.createTextNode(fromCol + fromRow + ":" + toCol + toRow));
1437 mergedCells = true;
1438 merged.appendChild(merge);
1439 }
1440 // ---
1441 if ( !cell->format()->comment( currentcolumn, currentrow ).isEmpty() )
1442 {
1443 //<gmr:CellComment Author="" Text="cvbcvbxcvb&#10;cb&#10;xc&#10;vbxcv&#10;" ObjectBound="A1" ObjectOffset="0 0 0 0" ObjectAnchorType="17 16 17 16" Direction="17"/>
1444 cellComment = gnumeric_doc.createElement("gmr:CellComment");
1445 cellComment.setAttribute( "Text", cell->format()->comment( currentcolumn, currentrow ) );
1446 TQString sCell=TQString( "%1%2" ).arg( Cell::columnName(currentcolumn ) ).arg( currentrow );
1447
1448 cellComment.setAttribute("ObjectBound", sCell );
1449 objects.appendChild(cellComment);
1450
1451 }
1452 TQDomElement gnumeric_cell = gnumeric_doc.createElement("gmr:Cell");
1453 TQDomElement cell_style;
1454
1455 TQDomElement style_region = gnumeric_doc.createElement("gmr:StyleRegion");
1456
1457 cells.appendChild(gnumeric_cell);
1458
1459 gnumeric_cell.setAttribute("Col", TQString::number(currentcolumn-1));
1460 gnumeric_cell.setAttribute("Row", TQString::number(currentrow-1));
1461
1462 /* Right now, we create a single region for each cell.. This is inefficient,
1463 * but the implementation is quicker.. Probably later we will have to
1464 * consolidate styles into style regions.
1465 */
1466
1467 style_region.setAttribute("startCol", TQString::number(currentcolumn-1));
1468 style_region.setAttribute("startRow", TQString::number(currentrow-1));
1469 style_region.setAttribute("endCol", TQString::number(currentcolumn-1));
1470 style_region.setAttribute("endRow", TQString::number(currentrow-1));
1471
1472 cell_style = GetCellStyle(gnumeric_doc,cell,currentcolumn,currentrow);
1473
1474 style_region.appendChild(cell_style);
1475
1476 styles.appendChild(style_region);
1477
1478 //cell_contents = gnumeric_doc.createElement("gmr:Content");
1479 gnumeric_cell.appendChild(gnumeric_doc.createTextNode(text));
1480 //gnumeric_cell.appendChild(cell_contents);
1481 }
1482
1483 // Append a delimiter, but in a temp string -> if no other real cell in this line,
1484 // then those will be dropped
1485 }
1486 }
1487
1488 if (mergedCells)
1489 sheet.appendChild(merged);
1490 }
1491 TQDomElement uidata = gnumeric_doc.createElement("gmr:UIData");
1492 uidata.setAttribute( "SelectedTab", indexActiveTable );
1493 workbook.appendChild(uidata);
1494
1495 str = gnumeric_doc.toString ();
1496
1497 emit sigProgress(100);
1498
1499 // Ok, now write to export file
1500
1501 TQIODevice* out = KFilterDev::deviceForFile(m_chain->outputFile(),"application/x-gzip");
1502
1503 if (!out)
1504 {
1505 kdError(30521) << "No output file! Aborting!" << endl;
1506 return KoFilter::FileNotFound;
1507 }
1508
1509 if (!out->open(IO_WriteOnly))
1510 {
1511 kdError(30521) << "Unable to open output file! Aborting!" << endl;
1512 delete out;
1513 return KoFilter::FileNotFound;
1514 }
1515
1516 TQTextStream streamOut(out);
1517
1518 streamOut << str;
1519
1520 out->close();
1521 delete out;
1522
1523 return KoFilter::OK;
1524}
1525
1526
1527TQString GNUMERICExport::convertRefToRange( const TQString & table, const TQRect & rect )
1528{
1529 TQPoint topLeft( rect.topLeft() );
1530 TQPoint bottomRight( rect.bottomRight() );
1531 if ( topLeft == bottomRight )
1532 return convertRefToBase( table, rect );
1533 TQString s;
1534 s += table;
1535 s += "!$";
1536 s += Cell::columnName( topLeft.x() );
1537 s += '$';
1538 s += TQString::number( topLeft.y() );
1539 s += ":$";
1540 s += Cell::columnName( bottomRight.x() );
1541 s += '$';
1542 s += TQString::number( bottomRight.y() );
1543
1544 return s;
1545}
1546
1547
1548TQString GNUMERICExport::convertRefToBase( const TQString & table, const TQRect & rect )
1549{
1550 TQPoint bottomRight( rect.bottomRight() );
1551
1552 TQString s;
1553 s = table;
1554 s += "!$";
1555 s += Cell::columnName( bottomRight.x() );
1556 s += '$';
1557 s += TQString::number( bottomRight.y() );
1558
1559 return s;
1560}
1561
1562TQString GNUMERICExport::convertVariable( TQString headerFooter )
1563{
1564 headerFooter = headerFooter.replace( "<sheet>", "&[TAB]" );
1565 headerFooter = headerFooter.replace( "<date>", "&[DATE]" );
1566 headerFooter = headerFooter.replace( "<page>", "&[PAGE]" );
1567 headerFooter = headerFooter.replace( "<pages>", "&[PAGES]" );
1568 headerFooter = headerFooter.replace( "<time>", "&[TIME]" );
1569 headerFooter = headerFooter.replace( "<file>", "&[FILE]" );
1570
1571 return headerFooter;
1572}
1573
1574#include "gnumericexport.moc"