filters

gnumericimport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1999 David Faure <faure@kde.org>
00003    Copyright (C) 2005 Laurent Montel <montel@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 /* GNUmeric import filter by Phillip Ezolt 6-2-2001 */
00022 /*                        phillipezolt@hotmail.com  */
00023 /* additions: Norbert Andres nandres@web.de         */
00024 
00025 #include <qdict.h>
00026 #include <qfile.h>
00027 #include <qstringlist.h>
00028 
00029 #include <gnumericimport.h>
00030 #include <kmessagebox.h>
00031 #include <kfilterdev.h>
00032 #include <kdebug.h>
00033 #include <kgenericfactory.h>
00034 #include <KoFilterChain.h>
00035 #include <KoGlobal.h>
00036 
00037 // hehe >:->
00038 #include <kspread_doc.h>
00039 #include <kspread_map.h>
00040 #include <kspread_sheet.h>
00041 #include <kspread_sheetprint.h>
00042 #include <kspread_cell.h>
00043 #include <kspread_util.h>
00044 #include <KoDocumentInfo.h>
00045 
00046 #include <math.h>
00047 
00048 #define SECS_PER_DAY 86400
00049 #define HALF_SEC (0.5 / SECS_PER_DAY)
00050 
00051 using namespace KSpread;
00052 
00053 // copied from gnumeric: src/format.c:
00054 static const int g_dateSerial_19000228 = 59;
00055 /* One less that the Julian day number of 19000101.  */
00056 static int g_dateOrigin = 0;
00057 
00058 // copied from gnumeric: src/formats.c:
00059 static char const * cell_format_date [] = {
00060     "m/d/yy",       /* 0 Cell::date_format5*/
00061     "m/d/yyyy",     /* 1 Cell::date_format6*/
00062     "d-mmm-yy",     /* 2 Cell::date_format1 18-Feb-99 */
00063     "d-mmm-yyyy",       /* 3 Cell::date_format2 18-Feb-1999 */
00064     "d-mmm",        /* 4 Cell::date_format3 18-Feb */
00065     "d-mm",         /* 5 Cell::date_format4 18-05 */
00066     "mmm/d",        /* 6 Cell::date_format11*/
00067     "mm/d",         /* 7 Cell::date_format12*/
00068     "mm/dd/yy",     /* 8 Cell::date_format19*/
00069     "mm/dd/yyyy",       /* 9 Cell::date_format18*/
00070     "mmm/dd/yy",        /* 10 Cell::date_format20*/
00071     "mmm/dd/yyyy",      /* 11 Cell::date_format21*/
00072     "mmm/ddd/yy",       /* 12 */
00073     "mmm/ddd/yyyy",     /* 13 */
00074     "mm/ddd/yy",        /* 14 */
00075     "mm/ddd/yyyy",      /* 15 */
00076     "mmm-yy",       /* 16 Cell::date_format7*/
00077     "mmm-yyyy",     /* 17 Cell::date_format22*/
00078     "mmmm-yy",      /* 18 Cell::date_format8*/
00079     "mmmm-yyyy",        /* 19 Cell::date_format9*/
00080     "m/d/yy h:mm",      /* 20 */
00081     "m/d/yyyy h:mm",    /* 21 */
00082     "yyyy/mm/d",        /* 22 Cell::date_format25*/
00083     "yyyy/mmm/d",       /* 23 Cell::date_format14*/
00084     "yyyy/mm/dd",       /* 24 Cell::date_format25*/
00085     "yyyy/mmm/dd",      /* 25 Cell::date_format26*/
00086     "yyyy-mm-d",        /* 26 Cell::date_format16*/
00087     "yyyy-mmm-d",       /* 27 Cell::date_format15*/
00088     "yyyy-mm-dd",       /* 28 Cell::date_format16*/
00089     "yyyy-mmm-dd",      /* 29 Cell::date_format15*/
00090     "yy",           /* 30 Cell::date_format24*/
00091     "yyyy",         /* 31 Cell::date_format23*/
00092     NULL
00093 };
00094 
00095 // copied from gnumeric: src/formats.c:
00096 static char const * cell_format_time [] = {
00097   "h:mm AM/PM",    // Cell::Time_format1 9 : 01 AM
00098   "h:mm:ss AM/PM", // Cell::Time_format2 9:01:05 AM
00099   "h:mm",          // Cell::Time_format4 9:01
00100   "h:mm:ss",       // Cell::Time_format5 9:01:12
00101   "m/d/yy h:mm",
00102   "mm:ss",         // Cell::Time_format6 01:12
00103   "mm:ss.0",       // Cell::Time_format6 01:12
00104   "[h]:mm:ss",
00105   "[h]:mm",
00106   "[mm]:ss",
00107   "[ss]",
00108   NULL
00109 };
00110 
00111 namespace gnumeric_import_LNS
00112 {
00113   QStringList list1;
00114   QStringList list2;
00115 }
00116 
00117 using namespace gnumeric_import_LNS;
00118 
00119 void GNUMERICFilter::dateInit()
00120 {
00121   // idea copied form gnumeric src/format.c:
00122   /* Day 1 means 1st of January of 1900 */
00123   g_dateOrigin = GnumericDate::greg2jul( 1900, 1, 1 ) - 1;
00124 }
00125 
00126 uint GNUMERICFilter::GnumericDate::greg2jul( int y, int m, int d )
00127 {
00128   return QDate::gregorianToJulian( y, m, d );
00129 }
00130 
00131 void GNUMERICFilter::GnumericDate::jul2greg( double num, int & y, int & m, int & d )
00132 {
00133   int i = (int) floor( num + HALF_SEC );
00134   if (i > g_dateSerial_19000228)
00135     --i;
00136   else if (i == g_dateSerial_19000228 + 1)
00137     kdWarning(30521) << "Request for date 02/29/1900." << endl;
00138 
00139   kdDebug(30521) << "***** Num: " << num << ", i: " << i  << endl;
00140 
00141   QDate::julianToGregorian( i + g_dateOrigin, y, m, d );
00142   kdDebug(30521) << "y: " << y << ", m: " << m << ", d: " << d << endl;
00143 }
00144 
00145 QTime GNUMERICFilter::GnumericDate::getTime( double num )
00146 {
00147   // idea copied from gnumeric: src/datetime.c
00148   num += HALF_SEC;
00149   int secs = qRound( (num - floor(num)) * SECS_PER_DAY );
00150 
00151   kdDebug(30521) << "***** Num: " << num << ", secs " << secs << endl;
00152 
00153   const int h = secs / 3600;
00154   secs -= h * 3600;
00155   const int m = secs / 60;
00156   secs -= h * 60;
00157 
00158   kdDebug(30521) << "****** h: " << h << ", m: " << m << ", secs: " << secs << endl;
00159   const QTime time( h, m, ( secs < 0 || secs > 59 ? 0 : secs ) );
00160 
00161   return time;
00162 }
00163 
00164 typedef KGenericFactory<GNUMERICFilter, KoFilter> GNUMERICFilterFactory;
00165 K_EXPORT_COMPONENT_FACTORY( libgnumericimport, GNUMERICFilterFactory( "kofficefilters" ) )
00166 
00167 GNUMERICFilter::GNUMERICFilter( KoFilter *, const char *, const QStringList & )
00168   : KoFilter()
00169 {
00170 }
00171 
00172 /* This converts GNUmeric's color string "0:0:0" to a QColor. */
00173 void  convert_string_to_qcolor(QString color_string, QColor * color)
00174 {
00175   int red, green, blue, first_col_pos, second_col_pos;
00176 
00177   bool number_ok;
00178 
00179   first_col_pos  = color_string.find(":", 0);
00180   second_col_pos = color_string.find(":", first_col_pos + 1);
00181 
00182   /* Fore="0:0:FF00" */
00183   /* If GNUmeric kicks out some invalid colors, we could crash. */
00184   /* GNUmeric gives us two bytes of color data per element. */
00185   /* We only care about the top byte. */
00186 
00187   red   = color_string.mid( 0, first_col_pos).toInt( &number_ok, 16 ) >> 8;
00188   green = color_string.mid(first_col_pos + 1,
00189                            (second_col_pos-first_col_pos) - 1).toInt( &number_ok, 16) >> 8;
00190   blue  = color_string.mid(second_col_pos + 1,
00191                            (color_string.length() - first_col_pos) - 1).toInt( &number_ok, 16) >> 8;
00192   color->setRgb(red, green, blue);
00193 }
00194 
00195 void areaNames( Doc * ksdoc, const QString &_name, QString _zone )
00196 {
00197 //Sheet2!$A$2:$D$8
00198     QString tableName;
00199     int pos = _zone.find( '!' );
00200     if ( pos != -1 )
00201     {
00202         tableName = _zone.left( pos );
00203         _zone = _zone.right( _zone.length()-pos-1 );
00204          pos = _zone.find( ':' );
00205         QRect rect;
00206         if ( pos != -1 )
00207         {
00208             QString left = _zone.mid( 1, pos-1 );
00209             QString right = _zone.mid( pos+2, _zone.length()-pos-2 );
00210             int pos = left.find( '$' );
00211 
00212             rect.setLeft( util_decodeColumnLabelText(left.left(pos ) ) );
00213             rect.setTop( left.right( left.length()-pos-1 ).toInt() );
00214 
00215             pos = right.find( '$' );
00216             rect.setRight( util_decodeColumnLabelText(right.left(pos ) ) );
00217             rect.setBottom( right.right( right.length()-pos-1 ).toInt() );
00218        }
00219         else
00220         {
00221             QString left = _zone;
00222             int pos = left.find( '$' );
00223             int leftPos = util_decodeColumnLabelText(left.left(pos ) );
00224             rect.setLeft( leftPos );
00225             rect.setRight( leftPos );
00226 
00227             int top = left.right( left.length()-pos-1 ).toInt();
00228             rect.setTop( top );
00229             rect.setBottom( top );
00230         }
00231         ksdoc->addAreaName( rect, _name ,tableName);
00232     }
00233 }
00234 
00235 
00236 void set_document_area_names( Doc * ksdoc, QDomElement * docElem )
00237 {
00238     QDomNode areaNamesElement = docElem->namedItem( "gmr:Names" );
00239     if ( areaNamesElement.isNull() )
00240         return;
00241     QDomNode areaNameItem = areaNamesElement.namedItem( "gmr:Name" );
00242     while ( !areaNameItem.isNull() )
00243     {
00244         QDomNode gmr_name  = areaNameItem.namedItem("gmr:name");
00245         QDomNode gmr_value = areaNameItem.namedItem("gmr:value");
00246         QString name = gmr_name.toElement().text();
00247         areaNames( ksdoc, name, gmr_value.toElement().text() );
00248         areaNameItem = areaNameItem.nextSibling();
00249     }
00250 }
00251 
00252 
00253 
00254 void set_document_attributes( Doc * ksdoc, QDomElement * docElem)
00255 {
00256     ksdoc->loadConfigFromFile();
00257     QDomNode attributes  = docElem->namedItem("gmr:Attributes");
00258     if ( attributes.isNull() )
00259         return;
00260 
00261     QDomNode attributeItem = attributes.namedItem("gmr:Attribute");
00262     while( !attributeItem.isNull() )
00263     {
00264         QDomNode gmr_name  = attributeItem.namedItem("gmr:name");
00265         QDomNode gmr_value = attributeItem.namedItem("gmr:value");
00266         if (gmr_name.toElement().text() == "WorkbookView::show_horizontal_scrollbar")
00267         {
00268             ksdoc->setShowHorizontalScrollBar( gmr_value.toElement().text().lower()=="true"? true : false );
00269         }
00270         else if ( gmr_name.toElement().text() == "WorkbookView::show_vertical_scrollbar")
00271         {
00272             ksdoc->setShowVerticalScrollBar( gmr_value.toElement().text().lower()=="true"? true : false );
00273         }
00274         else if ( gmr_name.toElement().text() == "WorkbookView::show_notebook_tabs")
00275         {
00276             ksdoc->setShowTabBar(gmr_value.toElement().text().lower()=="true"? true : false );
00277         }
00278         else if ( gmr_name.toElement().text() == "WorkbookView::do_auto_completion")
00279         {
00280             ksdoc->setCompletionMode( KGlobalSettings::CompletionAuto);
00281         }
00282         else if ( gmr_name.toElement().text() == "WorkbookView::is_protected")
00283         {
00284             //TODO protect document ???
00285             //ksdoc->map()->isProtected()
00286         }
00287 
00288         attributeItem = attributeItem.nextSibling();
00289     }
00290 }
00291 
00292 /* This sets the documentation information from the information stored in
00293    the GNUmeric file. Particularly in the "gmr:Summary" subcategory.
00294 */
00295 void set_document_info(KoDocument * document, QDomElement * docElem)
00296 {
00297   /* Summary Handling START */
00298   QDomNode summary  = docElem->namedItem("gmr:Summary");
00299   QDomNode gmr_item = summary.namedItem("gmr:Item");
00300 
00301   while( !gmr_item.isNull() )
00302   {
00303     QDomNode gmr_name  = gmr_item.namedItem("gmr:name");
00304     QDomNode gmr_value = gmr_item.namedItem("gmr:val-string");
00305 
00306     KoDocumentInfo * DocumentInfo     = document->documentInfo();
00307     KoDocumentInfoAbout  * aboutPage  = static_cast<KoDocumentInfoAbout *>(DocumentInfo->page( "about" ));
00308     KoDocumentInfoAuthor * authorPage = static_cast<KoDocumentInfoAuthor*>(DocumentInfo->page( "author" ));
00309 
00310 
00311     if (gmr_name.toElement().text() == "title")
00312     {
00313       aboutPage->setTitle(gmr_value.toElement().text());
00314     }
00315     else if (gmr_name.toElement().text() == "keywords")
00316     {
00317         aboutPage->setKeywords( gmr_value.toElement().text());
00318     }
00319     else if (gmr_name.toElement().text() == "comments")
00320     {
00321       aboutPage->setAbstract(gmr_value.toElement().text());
00322     }
00323     else if (gmr_name.toElement().text() == "category")
00324     {
00325       /* Not supported by KSpread */
00326     }
00327     else if (gmr_name.toElement().text() == "manager")
00328     {
00329       /* Not supported by KSpread */
00330     }
00331     else if (gmr_name.toElement().text() == "application")
00332     {
00333       /* Not supported by KSpread */
00334     }
00335     else if (gmr_name.toElement().text() == "author")
00336     {
00337       authorPage->setFullName(gmr_value.toElement().text());
00338     }
00339     else if (gmr_name.toElement().text() == "company")
00340     {
00341       authorPage->setCompany(gmr_value.toElement().text());
00342     }
00343 
00344     gmr_item = gmr_item.nextSibling();
00345   }
00346 
00347   /* Summany Handling STOP */
00348 }
00349 
00350 
00351 void setColInfo(QDomNode * sheet, Sheet * table)
00352 {
00353   QDomNode columns =  sheet->namedItem("gmr:Cols");
00354   QDomNode columninfo = columns.namedItem("gmr:ColInfo");
00355 
00356   QDomElement def = columns.toElement();
00357   if ( def.hasAttribute( "DefaultSizePts" ) )
00358   {
00359     bool ok = false;
00360     double d = def.attribute( "DefaultSizePts" ).toDouble( &ok );
00361     if ( ok )
00362     {
00363       Format::setGlobalColWidth( d );
00364       table->setDefaultWidth( d );
00365     }
00366   }
00367 
00368   while( !columninfo.isNull() )
00369   {
00370     QDomElement e = columninfo.toElement(); // try to convert the node to an element.
00371     int column_number;
00372 
00373     column_number = e.attribute("No").toInt()+1;
00374     ColumnFormat *cl = new ColumnFormat(table, column_number);
00375     if (e.hasAttribute("Hidden"))
00376     {
00377       if (e.attribute("Hidden")=="1")
00378       {
00379         cl->setHide(true);
00380       }
00381     }
00382     if (e.hasAttribute("Unit"))
00383     {
00384       //  xmm = (x_points) * (1 inch / 72 points) * (25.4 mm/ 1 inch)
00385       cl->setDblWidth(e.attribute("Unit").toDouble());
00386       //cl->setWidth(e.attribute("Unit").toInt());
00387     }
00388     table->insertColumnFormat(cl);
00389     columninfo = columninfo.nextSibling();
00390   }
00391 }
00392 
00393 void setRowInfo(QDomNode *sheet, Sheet *table)
00394 {
00395   QDomNode rows =  sheet->namedItem("gmr:Rows");
00396   QDomNode rowinfo = rows.namedItem("gmr:RowInfo");
00397 
00398   double d;
00399   bool ok = false;
00400 
00401   QDomElement def = rows.toElement();
00402   if ( def.hasAttribute( "DefaultSizePts" ) )
00403   {
00404     d = def.attribute( "DefaultSizePts" ).toDouble( &ok );
00405     if ( ok )
00406     {
00407       Format::setGlobalRowHeight( d );
00408       table->setDefaultHeight( d );
00409     }
00410   }
00411 
00412   while( !rowinfo.isNull() )
00413   {
00414     QDomElement e = rowinfo.toElement(); // try to convert the node to an element.
00415     int row_number;
00416     row_number = e.attribute("No").toInt() + 1;
00417     RowFormat *rl = new RowFormat(table, row_number);
00418 
00419     if (e.hasAttribute("Hidden"))
00420     {
00421       if (e.attribute("Hidden") == "1")
00422       {
00423         rl->setHide(true);
00424       }
00425     }
00426     if (e.hasAttribute("Unit"))
00427     {
00428       double dbl = e.attribute( "Unit" ).toDouble( &ok );
00429       if ( ok )
00430         rl->setDblHeight( dbl );
00431     }
00432     table->insertRowFormat(rl);
00433     rowinfo = rowinfo.nextSibling();
00434   }
00435 }
00436 
00437 void setSelectionInfo( QDomNode * sheet, Sheet * /* table */ )
00438 {
00439   QDomNode selections =  sheet->namedItem("gmr:Selections");
00440   QDomNode selection = selections.namedItem("gmr:Selection");
00441 
00442   /* Kspread does not support mutiple selections.. */
00443   /* This code will set the selection to the last one GNUmeric's multiple
00444      selections. */
00445   while( !selection.isNull() )
00446   {
00447     QDomElement e = selection.toElement(); // try to convert the node to an element.
00448     QRect kspread_selection;
00449 
00450     kspread_selection.setLeft((e.attribute("startCol").toInt() + 1));
00451     kspread_selection.setTop((e.attribute("startRow").toInt() + 1));
00452     kspread_selection.setRight((e.attribute("endCol").toInt() + 1));
00453     kspread_selection.setBottom((e.attribute("endRow").toInt() + 1));
00454 
00455     /* can't set it in the table -- must set it to a view */
00456     //    table->setSelection(kspread_selection);
00457 
00458     selection = selection.nextSibling();
00459   }
00460 }
00461 
00462 
00463 void setObjectInfo(QDomNode * sheet, Sheet * table)
00464 {
00465   QDomNode gmr_objects =  sheet->namedItem("gmr:Objects");
00466   QDomNode gmr_cellcomment = gmr_objects.namedItem("gmr:CellComment");
00467   while( !gmr_cellcomment.isNull() )
00468   {
00469     QDomElement e = gmr_cellcomment.toElement(); // try to convert the node to an element.
00470     if (e.hasAttribute("Text"))
00471     {
00472       if (e.hasAttribute("ObjectBound"))
00473       {
00474         Point point(e.attribute("ObjectBound"));
00475         Cell * cell = table->nonDefaultCell( point.pos().x(), point.pos().y() );
00476         cell->format()->setComment(e.attribute("Text"));
00477       }
00478     }
00479 
00480     gmr_cellcomment  = gmr_cellcomment.nextSibling();
00481   }
00482 }
00483 
00484 void convertToPen( QPen & pen, int style )
00485 {
00486   switch( style )
00487   {
00488    case 0:
00489     break;
00490    case 1:
00491     pen.setStyle( Qt::SolidLine );
00492     pen.setWidth( 1 );
00493     break;
00494    case 2:
00495     pen.setStyle( Qt::SolidLine );
00496     pen.setWidth( 2 );
00497     break;
00498    case 3:
00499     pen.setStyle( Qt::DashLine );
00500     pen.setWidth( 1 );
00501     break;
00502    case 4:
00503     // width should be 1 but otherwise it would be the same as 7
00504     pen.setStyle( Qt::DotLine );
00505     pen.setWidth( 2 );
00506     break;
00507    case 5:
00508     pen.setStyle( Qt::SolidLine );
00509     pen.setWidth( 3 );
00510     break;
00511    case 6:
00512     // TODO should be double
00513     pen.setStyle( Qt::SolidLine );
00514     pen.setWidth( 1 );
00515     break;
00516    case 7:
00517     // very thin dots => no match in KSpread
00518     pen.setStyle( Qt::DotLine );
00519     pen.setWidth( 1 );
00520     break;
00521    case 8:
00522     pen.setStyle( Qt::DashLine );
00523     pen.setWidth( 2 );
00524     break;
00525    case 9:
00526     pen.setStyle( Qt::DashDotLine );
00527     pen.setWidth( 1 );
00528     break;
00529    case 10:
00530     pen.setStyle( Qt::DashDotLine );
00531     pen.setWidth( 2 );
00532     break;
00533    case 11:
00534     pen.setStyle( Qt::DashDotDotLine );
00535     pen.setWidth( 1 );
00536     break;
00537    case 12:
00538     pen.setStyle( Qt::DashDotDotLine );
00539     pen.setWidth( 2 );
00540     break;
00541    case 13:
00542     // TODO: long dash, short dash, long dash,...
00543     pen.setStyle( Qt::DashDotLine );
00544     pen.setWidth( 3 );
00545     break;
00546    default:
00547     pen.setStyle( Qt::SolidLine );
00548     pen.setWidth( 1 );
00549   }
00550 }
00551 
00552 void GNUMERICFilter::ParseBorder( QDomElement & gmr_styleborder, Cell * kspread_cell )
00553 {
00554   QDomNode gmr_diagonal = gmr_styleborder.namedItem("gmr:Diagonal");
00555   QDomNode gmr_rev_diagonal = gmr_styleborder.namedItem("gmr:Rev-Diagonal");
00556   QDomNode gmr_top = gmr_styleborder.namedItem("gmr:Top");
00557   QDomNode gmr_bottom = gmr_styleborder.namedItem("gmr:Bottom");
00558   QDomNode gmr_left = gmr_styleborder.namedItem("gmr:Left");
00559   QDomNode gmr_right = gmr_styleborder.namedItem("gmr:Right");
00560 
00561   // NoPen - no line at all. For example,
00562   // QPainter::drawRect() fills but does not
00563   // draw any explicit boundary
00564   // line. SolidLine - a simple line. DashLine
00565   // - dashes, separated by a few
00566   // pixels. DotLine - dots, separated by a
00567   // few pixels. DashDotLine - alternately
00568   // dots and dashes. DashDotDotLine - one dash, two dots, one dash, two dots...
00569 
00570   if ( !gmr_left.isNull() )
00571   {
00572     QDomElement e = gmr_left.toElement(); // try to convert the node to an element.
00573     importBorder( e, Left, kspread_cell);
00574    }
00575 
00576   if ( !gmr_right.isNull() )
00577   {
00578     QDomElement e = gmr_right.toElement(); // try to convert the node to an element.
00579     importBorder( e, Right, kspread_cell);
00580   }
00581 
00582   if ( !gmr_top.isNull() )
00583   {
00584     QDomElement e = gmr_top.toElement(); // try to convert the node to an element.
00585     importBorder( e, Top,  kspread_cell);
00586   }
00587 
00588   if ( !gmr_bottom.isNull() )
00589   {
00590     QDomElement e = gmr_bottom.toElement(); // try to convert the node to an element.
00591     importBorder( e, Bottom, kspread_cell);
00592   }
00593 
00594   if ( !gmr_diagonal.isNull() )
00595   {
00596     QDomElement e = gmr_diagonal.toElement(); // try to convert the node to an element.
00597     importBorder( e, Diagonal, kspread_cell);
00598   }
00599 
00600   if ( !gmr_rev_diagonal.isNull() )
00601   {
00602     QDomElement e = gmr_rev_diagonal.toElement(); // try to convert the node to an element.
00603     importBorder( e, Revdiagonal, kspread_cell);
00604   }
00605 
00606   //  QDomElement gmr_styleborder_element = gmr_styleborder.toElement();
00607 }
00608 
00609 
00610 void GNUMERICFilter::importBorder( QDomElement border, borderStyle _style,  Cell *cell)
00611 {
00612     if ( !border.isNull() )
00613     {
00614         QDomElement e = border.toElement(); // try to convert the node to an element.
00615         if ( e.hasAttribute( "Style" ) )
00616         {
00617             int style = e.attribute( "Style" ).toInt();
00618 
00619             QPen pen;
00620             convertToPen( pen, style );
00621 
00622             if ( style > 0 )
00623             {
00624                 switch( _style )
00625                 {
00626                 case Left:
00627                     cell->setLeftBorderPen( pen );
00628                     break;
00629                 case Right:
00630                     cell->setRightBorderPen( pen );
00631                     break;
00632                 case Top:
00633                     cell->setTopBorderPen( pen );
00634                     break;
00635                 case Bottom:
00636                     cell->setBottomBorderPen( pen );
00637                     break;
00638                 case Diagonal:
00639                     cell->format()->setFallDiagonalPen( pen ); // check if this is really Fall border
00640                     break;
00641                 case Revdiagonal:
00642                     cell->format()->setGoUpDiagonalPen( pen ); // check if this is really GoUp
00643                     break;
00644                 }
00645             }
00646             if ( e.hasAttribute( "Color" ) )
00647             {
00648                 QColor color;
00649                 QString colorString = e.attribute( "Color" );
00650                 convert_string_to_qcolor( colorString, &color );
00651                 {
00652                     switch( _style )
00653                     {
00654                     case Left:
00655                         cell->format()->setLeftBorderColor( color );
00656                         break;
00657                     case Right:
00658                         cell->format()->setRightBorderColor( color );
00659                         break;
00660                     case Top:
00661                         cell->format()->setTopBorderColor( color );
00662                         break;
00663                     case Bottom:
00664                         cell->format()->setBottomBorderColor( color );
00665                         break;
00666                     case Diagonal:
00667                         cell->format()->setFallDiagonalColor( color );
00668                         break;
00669                     case Revdiagonal:
00670                         cell->format()->setGoUpDiagonalPen( color );
00671                         break;
00672                     }
00673                 }
00674             }
00675         }
00676     }
00677 
00678 }
00679 
00680 bool GNUMERICFilter::setType( Cell * kspread_cell,
00681                               QString const & formatString,
00682                               QString & cell_content )
00683 {
00684   int i = 0;
00685   for ( i = 0; cell_format_date[i] ; ++i )
00686   {
00687     kdDebug(30521) << "Cell_format: " << cell_format_date[i] << ", FormatString: " << formatString << endl;
00688     if ( ( formatString == "d/m/yy" ) || ( formatString == cell_format_date[i] ) )
00689     {
00690       kdDebug(30521) << "   FormatString: Date: " << formatString << ", CellContent: " << cell_content << endl;
00691       QDate date;
00692       if ( !kspread_cell->isDate() )
00693       {
00694         // convert cell_content to date
00695         int y, m, d;
00696         bool ok = true;
00697         int val  = cell_content.toInt( &ok );
00698 
00699         kdDebug(30521) << "!!!   FormatString: Date: " << formatString << ", CellContent: " << cell_content
00700                   << ", Double: " << val << endl;
00701         if ( !ok )
00702           return false;
00703 
00704         GnumericDate::jul2greg( val, y, m, d );
00705         kdDebug(30521) << "     num: " << val << ", y: " << y << ", m: " << m << ", d: " << d << endl;
00706 
00707         date.setYMD( y, m, d );
00708       }
00709       else
00710         date = kspread_cell->value().asDate();
00711 
00712       FormatType type;
00713       switch( i )
00714       {
00715        case 0:  type = date_format5;  break;
00716        case 1:  type = date_format6;  break;
00717        case 2:  type = date_format1;  break;
00718        case 3:  type = date_format2;  break;
00719        case 4:  type = date_format3;  break;
00720        case 5:  type = date_format4;  break;
00721        case 6:  type = date_format11; break;
00722        case 7:  type = date_format12; break;
00723        case 8:  type = date_format19; break;
00724        case 9:  type = date_format18; break;
00725        case 10: type = date_format20; break;
00726        case 11: type = date_format21; break;
00727        case 16: type = date_format7;  break;
00728        case 17: type = date_format22; break;
00729        case 18: type = date_format8;  break;
00730        case 19: type = date_format9;  break;
00731        case 22: type = date_format25; break;
00732        case 23: type = date_format14; break;
00733        case 24: type = date_format25; break;
00734        case 25: type = date_format26; break;
00735        case 26: type = date_format16; break;
00736        case 27: type = date_format15; break;
00737        case 28: type = date_format16; break;
00738        case 29: type = date_format15; break;
00739        case 30: type = date_format24; break;
00740        case 31: type = date_format23; break;
00741        default:
00742         type = ShortDate_format;
00743         break;
00744         /* 12, 13, 14, 15, 20, 21 */
00745       }
00746 
00747       kdDebug(30521) << "i: " << i << ", Type: " << type << ", Date: " << date.toString() << endl;
00748 
00749       kspread_cell->setValue( date );
00750       kspread_cell->format()->setFormatType( type );
00751 
00752       return true;
00753     }
00754   }
00755 
00756   for ( i = 0; cell_format_time[i] ; ++i )
00757   {
00758     if (formatString == cell_format_time[i])
00759     {
00760       QTime time;
00761 
00762       if ( !kspread_cell->isTime() )
00763       {
00764         bool ok = true;
00765         double content = cell_content.toDouble( &ok );
00766 
00767         kdDebug(30521) << "   FormatString: Time: " << formatString << ", CellContent: " << cell_content
00768                   << ", Double: " << content << endl;
00769 
00770         if ( !ok )
00771           return false;
00772 
00773         time = GnumericDate::getTime( content );
00774       }
00775       else
00776         time = kspread_cell->value().asTime();
00777 
00778       FormatType type;
00779       switch( i )
00780       {
00781        case 0: type = Time_format1; break;
00782        case 1: type = Time_format2; break;
00783        case 2: type = Time_format4; break;
00784        case 3: type = Time_format5; break;
00785        case 5: type = Time_format6; break;
00786        case 6: type = Time_format6; break;
00787        default:
00788         type = Time_format1; break;
00789       }
00790 
00791       kdDebug(30521) << "i: " << i << ", Type: " << type << endl;
00792       kspread_cell->setValue( time );
00793       kspread_cell->format()->setFormatType( type );
00794 
00795       return true;
00796     }
00797   }
00798 
00799   return false; // no date or time
00800 }
00801 
00802 QString GNUMERICFilter::convertVars( QString const & str, Sheet * table ) const
00803 {
00804   QString result( str );
00805   uint count = list1.count();
00806   if ( count == 0 )
00807   {
00808     list1 << "&[TAB]" << "&[DATE]" << "&[PAGE]"
00809           << "&[PAGES]"<<"&[TIME]" << "&[FILE]";
00810     list2 << "<sheet>" << "<date>" << "<page>"
00811           << "<pages>" << "<time>" << "<file>";
00812     count = list1.count();
00813   }
00814 
00815   for ( uint i = 0; i < count; ++i )
00816   {
00817     int n = result.find( list1[i] );
00818 
00819     if ( n != -1 )
00820     {
00821         kdDebug(30521) << "Found var: " << list1[i] << endl;
00822       if ( i == 0 )
00823         result = result.replace( list1[i], table->tableName() );
00824       else
00825         result = result.replace( list1[i], list2[i] );
00826     }
00827   }
00828 
00829   return result;
00830 }
00831 
00832 double GNUMERICFilter::parseAttribute( const QDomElement &_element )
00833 {
00834     QString unit = _element.attribute( "PrefUnit" );
00835     bool ok;
00836     double value = _element.attribute("Points").toFloat( &ok );
00837     if ( !ok )
00838         value = 2.0;
00839     if ( unit == "mm" )
00840         return value;
00841     else if ( unit == "cm" )
00842         return ( value/10.0 );
00843     else if ( unit == "in" )
00844         return MM_TO_INCH( value );
00845     else if ( unit == "Pt" || unit == "Px" || unit == "points" )
00846         return MM_TO_POINT( value );
00847     else
00848         return value;
00849 }
00850 
00851 void GNUMERICFilter::ParsePrintInfo( QDomNode const & printInfo, Sheet * table )
00852 {
00853   kdDebug(30521) << "Parsing print info " << endl;
00854 
00855   float fleft = 2.0;
00856   float fright = 2.0;
00857   float ftop = 2.0;
00858   float fbottom = 2.0;
00859 
00860   QString paperSize("A4");
00861   QString orientation("Portrait");
00862   QString footLeft, footMiddle, footRight;
00863   QString headLeft, headMiddle, headRight; // no we are zombies :-)
00864 
00865   QDomNode margins( printInfo.namedItem("gmr:Margins") );
00866   if ( !margins.isNull() )
00867   {
00868     QDomElement top( margins.namedItem( "gmr:top" ).toElement() );
00869     if ( !top.isNull() )
00870         ftop = parseAttribute( top );
00871 
00872     QDomElement bottom( margins.namedItem( "gmr:bottom" ).toElement() );
00873     if ( !bottom.isNull() )
00874         fbottom= parseAttribute( bottom );
00875 
00876     QDomElement left( margins.namedItem( "gmr:left" ).toElement() );
00877     if ( !left.isNull() )
00878         fleft = parseAttribute( left );
00879 
00880     QDomElement right( margins.namedItem( "gmr:right" ).toElement() );
00881     if ( !right.isNull() )
00882         fright = parseAttribute( right );
00883   }
00884 
00885   QDomElement foot( printInfo.namedItem("gmr:Footer").toElement() );
00886   if ( !foot.isNull() )
00887   {
00888     kdDebug(30521) << "Parsing footer: " << foot.attribute("Left") << ", " << foot.attribute("Middle") << ", "
00889               << foot.attribute("Right") << ", " <<endl;
00890     if ( foot.hasAttribute("Left") )
00891       footLeft = convertVars( foot.attribute("Left"), table );
00892     if ( foot.hasAttribute("Middle") )
00893       footMiddle = convertVars( foot.attribute("Middle"), table );
00894     if ( foot.hasAttribute("Right") )
00895       footRight = convertVars( foot.attribute("Right"), table );
00896   }
00897 
00898   QDomElement head( printInfo.namedItem("gmr:Header").toElement() );
00899   if ( !head.isNull() )
00900   {
00901     kdDebug(30521) << "Parsing header: " << head.attribute("Left") << ", " << head.attribute("Middle") << ", " << head.attribute("Right") << ", "<< endl;
00902     if ( head.hasAttribute("Left") )
00903       headLeft = convertVars( head.attribute("Left"), table );
00904     if ( head.hasAttribute("Middle") )
00905       headMiddle = convertVars( head.attribute("Middle"), table );
00906     if ( head.hasAttribute("Right") )
00907       headRight = convertVars( head.attribute("Right"), table );
00908   }
00909 
00910   QDomElement repeateColumn( printInfo.namedItem("gmr:repeat_top").toElement() );
00911   if ( !repeateColumn.isNull() )
00912   {
00913       QString repeate = repeateColumn.attribute( "value" );
00914       if ( !repeate.isEmpty() )
00915       {
00916           Range range(repeate);
00917           //kdDebug()<<" repeate :"<<repeate<<"range. ::start row : "<<range.startRow ()<<" start col :"<<range.startCol ()<<" end row :"<<range.endRow ()<<" end col :"<<range.endCol ()<<endl;
00918           table->print()->setPrintRepeatRows( qMakePair( range.startRow (),range.endRow ()) );
00919       }
00920   }
00921 
00922   QDomElement repeateRow( printInfo.namedItem("gmr:repeat_left").toElement() );
00923   if ( !repeateRow.isNull() )
00924   {
00925       QString repeate = repeateRow.attribute( "value" );
00926       if ( !repeate.isEmpty() )
00927       {
00928           //fix row too high
00929           repeate = repeate.replace( "65536", "32500" );
00930           Range range(repeate);
00931           //kdDebug()<<" repeate :"<<repeate<<"range. ::start row : "<<range.startRow ()<<" start col :"<<range.startCol ()<<" end row :"<<range.endRow ()<<" end col :"<<range.endCol ()<<endl;
00932           table->print()->setPrintRepeatColumns( qMakePair( range.startCol (),range.endCol ()) );
00933       }
00934   }
00935 
00936   QDomElement orient( printInfo.namedItem("gmr:orientation").toElement() );
00937   if ( !orient.isNull() )
00938     orientation = orient.text();
00939 
00940   QDomElement size( printInfo.namedItem("gmr:paper").toElement() );
00941   if ( !size.isNull() )
00942     paperSize = size.text();
00943 
00944   table->print()->setPaperLayout( fleft, ftop, fright, fbottom,
00945                          paperSize, orientation );
00946 
00947   table->print()->setHeadFootLine( headLeft, headMiddle, headRight,
00948                                    footLeft, footMiddle, footRight );
00949 }
00950 
00951 void GNUMERICFilter::ParseFormat(QString const & formatString, Cell * kspread_cell)
00952 {
00953   int l = formatString.length();
00954   int lastPos = 0;
00955 
00956   if (formatString[l - 1] == '%')
00957     kspread_cell->format()->setFormatType(Percentage_format);
00958   else if (formatString[0] == '$')
00959   {
00960     kspread_cell->format()->setFormatType(Money_format);
00961     kspread_cell->format()->setCurrency( 1, "$" );
00962     lastPos = 1;
00963   }
00964   else if (formatString[0] == '£')
00965   {
00966     kspread_cell->format()->setFormatType(Money_format);
00967     kspread_cell->format()->setCurrency( 1, "£" );
00968     lastPos = 1;
00969   }
00970   else if (formatString[0] == '¥')
00971   {
00972     kspread_cell->format()->setFormatType(Money_format);
00973     kspread_cell->format()->setCurrency( 1, "¥" );
00974     lastPos = 1;
00975   }
00976   else if (formatString[0] == '¤')
00977   {
00978     kspread_cell->format()->setFormatType(Money_format);
00979     kspread_cell->format()->setCurrency( 1, "¤" );
00980     lastPos = 1;
00981   }
00982   else if (l > 1)
00983   {
00984     if ((formatString[0] == '[') && (formatString[1] == '$'))
00985     {
00986       int n = formatString.find(']');
00987       if (n != -1)
00988       {
00989         QString currency = formatString.mid(2, n - 2);
00990         kspread_cell->format()->setFormatType(Money_format);
00991         kspread_cell->format()->setCurrency( 1, currency );
00992       }
00993       lastPos = ++n;
00994     }
00995     else if (formatString.find("E+0") != -1)
00996     {
00997       kspread_cell->format()->setFormatType(Scientific_format);
00998     }
00999     else
01000     {
01001       // do pattern matching with gnumeric formats
01002       QString content(kspread_cell->value().asString());
01003 
01004       if ( setType(kspread_cell, formatString, content) )
01005         return;
01006 
01007       if (formatString.find("?/?") != -1)
01008       {
01009         // TODO: fixme!
01010         kspread_cell->format()->setFormatType( fraction_three_digits );
01011         return;
01012       }
01013       // so it's nothing we want to understand:-)
01014       return;
01015     }
01016   }
01017 
01018   while (formatString[lastPos] == ' ')
01019     ++lastPos;
01020 
01021   // GetPrecision and decimal point, format of negative items...
01022 
01023   // thousands separator
01024   if (formatString[lastPos] == '#')
01025   {
01026     bool sep = true;
01027     if (formatString[lastPos + 1] == ',')
01028       lastPos += 2;
01029     else
01030       sep = false;
01031     // since KSpread 1.3
01032     // kspread_cell->setThousandsSeparator( sep );
01033   }
01034 
01035   while (formatString[lastPos] == ' ')
01036     ++lastPos;
01037 
01038   int n = formatString.find( '.', lastPos );
01039   if ( n != -1)
01040   {
01041     lastPos = n + 1;
01042     int precision = lastPos;
01043     while (formatString[precision] == '0')
01044       ++precision;
01045 
01046     int tmp = lastPos;
01047     lastPos = precision;
01048     precision -= tmp;
01049 
01050     kspread_cell->format()->setPrecision( precision );
01051   }
01052 
01053   bool red = false;
01054   if (formatString.find("[RED]", lastPos) != -1)
01055   {
01056     red = true;
01057     kspread_cell->format()->setFloatColor( Format::NegRed );
01058   }
01059   if ( formatString.find('(', lastPos) != -1 )
01060   {
01061     if ( red )
01062       kspread_cell->format()->setFloatColor( Format::NegRedBrackets );
01063     else
01064       kspread_cell->format()->setFloatColor( Format::NegBrackets );
01065   }
01066 }
01067 
01068 void GNUMERICFilter::convertFormula( QString & formula ) const
01069 {
01070   int n = formula.find( '=', 1 );
01071 
01072   // TODO: check if we do not screw something up here...
01073   if ( n != -1 )
01074     formula = formula.replace( n, 1, "==" );
01075 
01076   bool inQuote1 = false;
01077   bool inQuote2 = false;
01078   int l = formula.length();
01079   for ( int i = 0; i < l; ++i )
01080   {
01081     if ( formula[i] == '\'' )
01082       inQuote1 = !inQuote1;
01083     else if ( formula[i] == '"' )
01084       inQuote2 = !inQuote2;
01085     else if ( formula[i] == ',' && !inQuote1 && !inQuote2 )
01086       formula = formula.replace( i, 1, ";" );
01087   }
01088 }
01089 
01090 void GNUMERICFilter::setStyleInfo(QDomNode * sheet, Sheet * table)
01091 {
01092     kdDebug(30521) << "SetStyleInfo entered " << endl;
01093 
01094     int row, column;
01095     QDomNode styles =  sheet->namedItem( "gmr:Styles" );
01096     if ( !styles.isNull() )
01097     {
01098         // Get a style region within that sheet.
01099         QDomNode style_region =  styles.namedItem( "gmr:StyleRegion" );
01100 
01101         while ( !style_region.isNull() )
01102         {
01103             QDomElement e = style_region.toElement(); // try to convert the node to an element.
01104 
01105             QDomNode style = style_region.namedItem( "gmr:Style" );
01106             QDomNode font = style.namedItem( "gmr:Font" );
01107             QDomNode validation = style.namedItem( "gmr:Validation" );
01108             QDomNode gmr_styleborder = style.namedItem( "gmr:StyleBorder" );
01109             QDomNode hyperlink = style.namedItem( "gmr:HyperLink" );
01110             int startCol = e.attribute( "startCol" ).toInt() + 1;
01111             int endCol   = e.attribute( "endCol" ).toInt() + 1;
01112             int startRow = e.attribute( "startRow" ).toInt() + 1;
01113             int endRow   = e.attribute( "endRow" ).toInt() + 1;
01114 
01115             kdDebug(30521) << "------Style: " << startCol << ", "
01116                            << startRow << " - " << endCol << ", " << endRow << endl;
01117 
01118             if ( endCol - startCol > 200 || endRow - startRow > 200 )
01119             {
01120                 style_region = style_region.nextSibling();
01121                 continue;
01122             }
01123 
01124             for ( column = startCol; column <= endCol; ++column )
01125             {
01126                 for ( row = startRow; row <= endRow; ++row )
01127                 {
01128                     kdDebug(30521) << "Cell: " << column << ", " << row << endl;
01129                     Cell * kspread_cell = table->cellAt( column, row, false );
01130 
01131                     // don't create new cells -> don't apply format on empty cells, if bigger region
01132                     if ( ( kspread_cell->isDefault() || kspread_cell->isEmpty() )
01133                          && ( ( endCol - startCol > 2 ) || ( endRow - startRow > 2 ) ) )
01134                     {
01135                         kdDebug(30521) << "CELL EMPTY OR RANGE TOO BIG " << endl;
01136                         continue;
01137                     }
01138 
01139                     QDomElement style_element = style.toElement(); // try to convert the node to an element.
01140 
01141                     kdDebug(30521) << "Style valid for kspread" << endl;
01142                     kspread_cell = table->nonDefaultCell( column, row, false );
01143 
01144                     if (style_element.hasAttribute("Fore"))
01145                     {
01146                         QString color_string = style_element.attribute("Fore");
01147                         QColor color;
01148                         convert_string_to_qcolor(color_string, &color);
01149                         kspread_cell->format()->setTextColor(color);
01150                     }
01151 
01152                     if (style_element.hasAttribute("Back"))
01153                     {
01154                         QString color_string = style_element.attribute("Back");
01155                         QColor color;
01156                         convert_string_to_qcolor(color_string, &color);
01157                         kspread_cell->format()->setBgColor(color);
01158                     }
01159 
01160                     if (style_element.hasAttribute("PatternColor"))
01161                     {
01162                         QString color_string = style_element.attribute("PatternColor");
01163                         QColor color;
01164                         convert_string_to_qcolor(color_string, &color);
01165                         kspread_cell->format()->setBackGroundBrushColor( color );
01166                     }
01167 
01168                     if (style_element.hasAttribute("Shade"))
01169                     {
01170                         /* Pattern's taken from: gnumeric's pattern.c */
01171                         /* if "TODO" added: doesn't match exactly the gnumeric one */
01172 
01173                         QString shade = style_element.attribute("Shade");
01174                         if (shade == "0")
01175                         {
01176                             // nothing to do
01177                         }
01178                         else if (shade == "1")
01179                         {
01180                             /* 1 Solid */
01181                             //kspread_cell->format()->setBackGroundBrushStyle(Qt::SolidPattern);
01182                             //This is as empty
01183                             /* What should this be? */
01184 
01185                         }
01186                         else if (shade == "2")
01187                         {
01188                             /* 2 75% */
01189                             kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense2Pattern);
01190                         }
01191                         else if (shade == "3")
01192                         {
01193                             /* 3 50% */
01194                             kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense4Pattern);
01195                         }
01196                         else if (shade == "4")
01197                         {
01198                             kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense5Pattern);
01199                             /* This should be 25%... All qt has is 37% */
01200 
01201                             /* 4 25% */
01202                         }
01203                         else if (shade == "5")
01204                         {
01205                             kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense6Pattern);
01206                             /* 5 12.5% */
01207                         }
01208                         else if (shade == "6")
01209                         {
01210                             kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense7Pattern);
01211                             /* 6 6.25% */
01212 
01213                         }
01214                         else if (shade == "7")
01215                         {
01216                             kspread_cell->format()->setBackGroundBrushStyle(Qt::HorPattern);
01217                             /* 7 Horizontal Stripe */
01218                         }
01219                         else if (shade == "8")
01220                         {
01221                             kspread_cell->format()->setBackGroundBrushStyle(Qt::VerPattern);
01222                             /* 8 Vertical Stripe */
01223                         }
01224                         else if (shade == "9")
01225                         {
01226                             kspread_cell->format()->setBackGroundBrushStyle(Qt::BDiagPattern);
01227                             /* 9 Reverse Diagonal Stripe */
01228                         }
01229                         else if (shade == "10")
01230                         {
01231                             /* 10 Diagonal Stripe */
01232                             kspread_cell->format()->setBackGroundBrushStyle(Qt::FDiagPattern);
01233                         }
01234                         else if (shade == "11")
01235                         {
01236                             /* 11 Diagonal Crosshatch */
01237                             kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
01238                         }
01239                         else if (shade == "12")
01240                         {
01241                             /* 12 Thick Diagonal Crosshatch TODO!*/
01242                             kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
01243                         }
01244                         else if (shade == "13")
01245                         {
01246                             /* 13 Thin Horizontal Stripe TODO: wrong: this is thick!*/
01247                             kspread_cell->format()->setBackGroundBrushStyle(Qt::HorPattern);
01248                         }
01249                         else if (shade == "14")
01250                         {
01251                             kspread_cell->format()->setBackGroundBrushStyle(Qt::VerPattern);
01252                         }
01253                         else if (shade == "15")
01254                         {
01255                             kspread_cell->format()->setBackGroundBrushStyle(Qt::FDiagPattern);
01256                         }
01257                         else if (shade == "16")
01258                         {
01259                             /* 16 Thick Reverse Stripe TODO:*/
01260                             kspread_cell->format()->setBackGroundBrushStyle(Qt::BDiagPattern);
01261                         }
01262                         else if (shade == "17")
01263                         {
01264                             kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
01265                         }
01266                         else if (shade == "18")
01267                         {
01268                             kspread_cell->format()->setBackGroundBrushStyle(Qt::DiagCrossPattern);
01269                         }
01270                         else if (shade == "19")
01271                         {
01272                             /* 19 Applix small circle */
01273                         }
01274                         else if (shade == "20")
01275                         {
01276                             /* 20 Applix semicircle */
01277                         }
01278                         else if (shade == "21")
01279                         {
01280                             /* 21 Applix small thatch */
01281                         }
01282                         else if (shade == "22")
01283                         {
01284                             /* 22 Applix round thatch */
01285                         }
01286                         else if (shade == "23")
01287                         {
01288                             /* 23 Applix Brick */
01289                         }
01290                         else if (shade == "24")
01291                         {
01292                             /* 24 100% */
01293                             kspread_cell->format()->setBackGroundBrushStyle(Qt::SolidPattern);
01294                         }
01295                         else if (shade == "25")
01296                         {
01297                             /* 25 87.5% */
01298                             kspread_cell->format()->setBackGroundBrushStyle(Qt::Dense2Pattern);
01299                         }
01300                     }
01301 
01302                     if ( style_element.hasAttribute( "Rotation" ) )
01303                     {
01304                         int rot = style_element.attribute( "Rotation" ).toInt();
01305                         kspread_cell->format()->setAngle( -1* rot );
01306                     }
01307                     if (style_element.hasAttribute("Indent"))
01308                     {
01309                         double indent = style_element.attribute("Indent").toDouble();
01310                         // gnumeric saves indent in characters, we in points:
01311                         kspread_cell->format()->setIndent( indent * 10.0 );
01312                     }
01313 
01314                     if (style_element.hasAttribute("HAlign"))
01315                     {
01316                         QString halign_string=style_element.attribute("HAlign");
01317 
01318                         if (halign_string == "1")
01319                         {
01320                             /* General: No equivalent in Kspread. */
01321                         }
01322                         else if (halign_string == "2")
01323                         {
01324                             kspread_cell->format()->setAlign(Format::Left);
01325                         }
01326                         else if (halign_string == "4")
01327                         {
01328                             kspread_cell->format()->setAlign(Format::Right);
01329                         }
01330                         else if (halign_string == "8")
01331                         {
01332                             kspread_cell->format()->setAlign(Format::Center);
01333                         }
01334                         else if (halign_string == "16")
01335                         {
01336                             /* Fill: No equivalent in Kspread. */
01337                         }
01338                         else if (halign_string == "32")
01339                         {
01340                             /* Justify: No equivalent in Kspread */
01341                         }
01342                         else if (halign_string == "64")
01343                         {
01344                             /* Centered across selection*/
01345                         }
01346 
01347                     }
01348 
01349                     if (style_element.hasAttribute("VAlign"))
01350                     {
01351                         QString valign_string=style_element.attribute("VAlign");
01352 
01353                         if (valign_string == "1")
01354                         {
01355                             /* General: No equivalent in Kspread. */
01356                             kspread_cell->format()->setAlignY(Format::Top);
01357                         }
01358                         else if (valign_string == "2")
01359                         {
01360                             kspread_cell->format()->setAlignY(Format::Bottom);
01361                         }
01362                         else if (valign_string == "4")
01363                         {
01364                             kspread_cell->format()->setAlignY(Format::Middle);
01365                         }
01366                         else if (valign_string == "8")
01367                         {
01368                             /* Justify: No equivalent in Kspread */
01369                         }
01370                     }
01371 
01372                     if (style_element.hasAttribute("WrapText"))
01373                     {
01374                         QString multiRow = style_element.attribute("WrapText");
01375 
01376                         if ( multiRow == "1" )
01377                             kspread_cell->format()->setMultiRow( true );
01378                     }
01379 
01380                     if (style_element.hasAttribute("Format"))
01381                     {
01382                         QString formatString = style_element.attribute("Format");
01383 
01384                         kdDebug(30521) << "Format: " << formatString << endl;
01385                         ParseFormat(formatString, kspread_cell);
01386 
01387                     } // End "Format"
01388 
01389                     if (!gmr_styleborder.isNull())
01390                     {
01391                         QDomElement style_element = gmr_styleborder.toElement(); // try to convert the node to an element.
01392                         ParseBorder( style_element, kspread_cell );
01393                     }
01394                     if ( !validation.isNull() )
01395                     {
01396                         QDomElement validation_element = validation.toElement();
01397                         if ( !validation_element.isNull() )
01398                         {
01399                             kdDebug(30521)<<" Cell validation \n";
01400                             Validity* kspread_validity = kspread_cell->getValidity();
01401                             if ( validation_element.hasAttribute( "AllowBlank" ) && validation_element.attribute( "AllowBlank" )=="true" )
01402                             {
01403                                 kspread_validity->allowEmptyCell=true;
01404                             }
01405                             if ( validation_element.hasAttribute( "Title" ))
01406                             {
01407                                 kspread_validity->title=validation_element.attribute( "Title" );
01408                             }
01409                             if ( validation_element.hasAttribute( "Message" ))
01410                             {
01411                                 kspread_validity->message=validation_element.attribute( "Message" );
01412                             }
01413                             if ( validation_element.hasAttribute( "Style" ) )
01414                             {
01415                                 int value = validation_element.attribute( "Style" ).toInt();
01416                                 switch( value )
01417                                 {
01418                                 case 0:
01419                                     kspread_validity->displayMessage=false;
01420                                     break;
01421                                 case 1:
01422                                   kspread_validity->m_action=Action::Stop;
01423                                     break;
01424                                 case 2:
01425                                   kspread_validity->m_action=Action::Warning;
01426                                     break;
01427                                 case 3:
01428                                   kspread_validity->m_action=Action::Information;
01429                                     break;
01430                                 default:
01431                                     kdDebug()<<" Error in validation style :"<<value<<endl;
01432                                     break;
01433                                 }
01434                             }
01435                             QDomNode expression0 = validation_element.namedItem( "gmr:Expression0" );
01436                             QDomNode expression1 = validation_element.namedItem( "gmr:Expression1" );
01437                             //kdDebug()<<" expression0.isNull() "<<expression0.isNull()<<endl;
01438                             //kdDebug()<<" expression1.isNull() "<<expression1.isNull()<<endl;
01439                             if ( validation_element.hasAttribute( "Type" ) )
01440                             {
01441                                 int valueOp = validation_element.attribute( "Type" ).toInt();
01442                                 switch( valueOp )
01443                                 {
01444                                 case 0:
01445                                     kspread_validity->m_restriction=Restriction::None;
01446                                     break;
01447                                 case 1:
01448                                 {
01449                                     kspread_validity->m_restriction=Restriction::Integer;
01450                                     if ( validation_element.hasAttribute( "Operator" ) )
01451                                     {
01452                                         int value = validation_element.attribute( "Operator" ).toInt();
01453 
01454                                         switch( value )
01455                                         {
01456                                         case 0:
01457                                           kspread_validity->m_cond=Conditional::Between;
01458                                             if ( !expression0.isNull() )
01459                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01460                                             if ( !expression1.isNull() )
01461                                                 kspread_validity->valMax=expression1.toElement().text().toInt();
01462                                             break;
01463                                         case 1:
01464                                           kspread_validity->m_cond=Conditional::DifferentTo;
01465                                             if ( !expression0.isNull() )
01466                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01467                                             if ( !expression1.isNull() )
01468                                                 kspread_validity->valMax=expression1.toElement().text().toInt();
01469                                             break;
01470                                         case 2:
01471                                           kspread_validity->m_cond=Conditional::Equal;
01472                                             if ( !expression0.isNull() )
01473                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01474                                             break;
01475                                         case 3:
01476                                           kspread_validity->m_cond=Conditional::Different;
01477                                             if ( !expression0.isNull() )
01478                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01479                                             break;
01480                                         case 4:
01481                                           kspread_validity->m_cond=Conditional::Superior;
01482                                             if ( !expression0.isNull() )
01483                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01484                                             break;
01485                                         case 5:
01486                                           kspread_validity->m_cond=Conditional::Inferior;
01487                                             if ( !expression0.isNull() )
01488                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01489                                             break;
01490                                         case 6:
01491                                           kspread_validity->m_cond=Conditional::SuperiorEqual;
01492                                             if ( !expression0.isNull() )
01493                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01494                                             break;
01495                                         case 7:
01496                                           kspread_validity->m_cond=Conditional::InferiorEqual;
01497                                             if ( !expression0.isNull() )
01498                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01499                                             break;
01500                                         default:
01501                                             kdDebug()<<" Error in validation Operator :"<<value<<endl;
01502                                             break;
01503                                         }
01504                                     }
01505                                 }
01506                                 break;
01507                                 case 2:
01508                                     kspread_validity->m_restriction=Restriction::Number;
01509                                     if ( validation_element.hasAttribute( "Operator" ) )
01510                                     {
01511                                         int value = validation_element.attribute( "Operator" ).toInt();
01512                                         switch( value )
01513                                         {
01514                                         case 0:
01515                                           kspread_validity->m_cond=Conditional::Between;
01516                                             if ( !expression0.isNull() )
01517                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01518                                             if ( !expression1.isNull() )
01519                                                 kspread_validity->valMax=expression1.toElement().text().toInt();
01520                                             break;
01521                                         case 1:
01522                                           kspread_validity->m_cond=Conditional::DifferentTo;
01523                                             if ( !expression0.isNull() )
01524                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01525                                             if ( !expression1.isNull() )
01526                                                 kspread_validity->valMax=expression1.toElement().text().toInt();
01527                                             break;
01528                                         case 2:
01529                                           kspread_validity->m_cond=Conditional::Equal;
01530                                             if ( !expression0.isNull() )
01531                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01532                                             break;
01533                                         case 3:
01534                                           kspread_validity->m_cond=Conditional::Different;
01535                                             if ( !expression0.isNull() )
01536                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01537                                             break;
01538                                         case 4:
01539                                           kspread_validity->m_cond=Conditional::Superior;
01540                                             if ( !expression0.isNull() )
01541                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01542                                             break;
01543                                         case 5:
01544                                             if ( !expression0.isNull() )
01545                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01546                                             kspread_validity->m_cond=Conditional::Inferior;
01547                                             break;
01548                                         case 6:
01549                                           kspread_validity->m_cond=Conditional::SuperiorEqual;
01550                                             if ( !expression0.isNull() )
01551                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01552                                             break;
01553                                         case 7:
01554                                           kspread_validity->m_cond=Conditional::InferiorEqual;
01555                                             if ( !expression0.isNull() )
01556                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01557                                             break;
01558                                         default:
01559                                             kdDebug()<<" Error in validation Operator :"<<value<<endl;
01560                                             break;
01561                                         }
01562                                     }
01563                                     break;
01564                                 case 3:
01565                                     kspread_validity->m_restriction=Restriction::List;
01566                                     break;
01567                                 case 4:
01568                                     kspread_validity->m_restriction=Restriction::Date;
01569                                     if ( validation_element.hasAttribute( "Operator" ) )
01570                                     {
01571                                         int value = validation_element.attribute( "Operator" ).toInt();
01572                                         switch( value )
01573                                         {
01574                                         case 0:
01575                                           kspread_validity->m_cond=Conditional::Between;
01576                                             if ( !expression0.isNull() )
01577                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01578                                             if ( !expression1.isNull() )
01579                                                 kspread_validity->dateMax=QDate::fromString( expression1.toElement().text() );
01580 
01581                                             break;
01582                                         case 1:
01583                                             if ( !expression0.isNull() )
01584                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01585                                             if ( !expression1.isNull() )
01586                                                 kspread_validity->dateMax=QDate::fromString( expression1.toElement().text() );
01587                                             kspread_validity->m_cond=Conditional::DifferentTo;
01588                                             break;
01589                                         case 2:
01590                                             if ( !expression0.isNull() )
01591                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01592                                             kspread_validity->m_cond=Conditional::Equal;
01593                                             break;
01594                                         case 3:
01595                                             if ( !expression0.isNull() )
01596                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01597                                             kspread_validity->m_cond=Conditional::Different;
01598                                             break;
01599                                         case 4:
01600                                             if ( !expression0.isNull() )
01601                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01602                                             kspread_validity->m_cond=Conditional::Superior;
01603                                             break;
01604                                         case 5:
01605                                             if ( !expression0.isNull() )
01606                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01607                                             kspread_validity->m_cond=Conditional::Inferior;
01608                                             break;
01609                                         case 6:
01610                                             if ( !expression0.isNull() )
01611                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01612                                             kspread_validity->m_cond=Conditional::SuperiorEqual;
01613                                             break;
01614                                         case 7:
01615                                             if ( !expression0.isNull() )
01616                                                 kspread_validity->dateMin=QDate::fromString( expression0.toElement().text() );
01617                                             kspread_validity->m_cond=Conditional::InferiorEqual;
01618                                             break;
01619                                         default:
01620                                             kdDebug()<<" Error in validation Operator :"<<value<<endl;
01621                                             break;
01622                                         }
01623                                     }
01624                                     break;
01625                                 case 5:
01626                                     kspread_validity->m_restriction=Restriction::Time;
01627                                     if ( validation_element.hasAttribute( "Operator" ) )
01628                                     {
01629                                         int value = validation_element.attribute( "Operator" ).toInt();
01630                                         switch( value )
01631                                         {
01632                                         case 0:
01633                                           kspread_validity->m_cond=Conditional::Between;
01634                                             if ( !expression0.isNull() )
01635                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01636                                             if ( !expression1.isNull() )
01637                                                 kspread_validity->timeMax=QTime::fromString( expression1.toElement().text() );
01638                                             break;
01639                                         case 1:
01640                                             if ( !expression0.isNull() )
01641                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01642                                             if ( !expression1.isNull() )
01643                                                 kspread_validity->timeMax=QTime::fromString( expression1.toElement().text() );
01644                                             kspread_validity->m_cond=Conditional::DifferentTo;
01645                                             break;
01646                                         case 2:
01647                                             if ( !expression0.isNull() )
01648                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01649                                             kspread_validity->m_cond=Conditional::Equal;
01650                                             break;
01651                                         case 3:
01652                                           kspread_validity->m_cond=Conditional::Different;
01653                                             break;
01654                                         case 4:
01655                                             if ( !expression0.isNull() )
01656                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01657                                             kspread_validity->m_cond=Conditional::Superior;
01658                                             break;
01659                                         case 5:
01660                                           kspread_validity->m_cond=Conditional::Inferior;
01661                                             if ( !expression0.isNull() )
01662                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01663                                             break;
01664                                         case 6:
01665                                           kspread_validity->m_cond=Conditional::SuperiorEqual;
01666                                             if ( !expression0.isNull() )
01667                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01668                                             break;
01669                                         case 7:
01670                                             if ( !expression0.isNull() )
01671                                                 kspread_validity->timeMin=QTime::fromString( expression0.toElement().text() );
01672                                             kspread_validity->m_cond=Conditional::InferiorEqual;
01673                                             break;
01674                                         default:
01675                                             kdDebug()<<" Error in validation Operator :"<<value<<endl;
01676                                             break;
01677                                         }
01678                                     }
01679                                     break;
01680                                 case 6:
01681                                     kspread_validity->m_restriction=Restriction::TextLength;
01682                                     if ( validation_element.hasAttribute( "Operator" ) )
01683                                     {
01684                                         int value = validation_element.attribute( "Operator" ).toInt();
01685                                         switch( value )
01686                                         {
01687                                         case 0:
01688                                           kspread_validity->m_cond=Conditional::Between;
01689                                             if ( !expression0.isNull() )
01690                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01691                                             if ( !expression1.isNull() )
01692                                                 kspread_validity->valMax=expression1.toElement().text().toInt();
01693                                             break;
01694                                         case 1:
01695                                           kspread_validity->m_cond=Conditional::DifferentTo;
01696                                             if ( !expression0.isNull() )
01697                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01698                                             if ( !expression1.isNull() )
01699                                                 kspread_validity->valMax=expression1.toElement().text().toInt();
01700                                             break;
01701                                         case 2:
01702                                           kspread_validity->m_cond=Conditional::Equal;
01703                                             if ( !expression0.isNull() )
01704                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01705                                             break;
01706                                         case 3:
01707                                           kspread_validity->m_cond=Conditional::Different;
01708                                             if ( !expression0.isNull() )
01709                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01710                                             break;
01711                                         case 4:
01712                                             if ( !expression0.isNull() )
01713                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01714                                             kspread_validity->m_cond=Conditional::Superior;
01715                                             break;
01716                                         case 5:
01717                                             if ( !expression0.isNull() )
01718                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01719                                             kspread_validity->m_cond=Conditional::Inferior;
01720                                             break;
01721                                         case 6:
01722                                             if ( !expression0.isNull() )
01723                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01724                                             kspread_validity->m_cond=Conditional::SuperiorEqual;
01725                                             break;
01726                                         case 7:
01727                                             if ( !expression0.isNull() )
01728                                                 kspread_validity->valMin=expression0.toElement().text().toInt();
01729                                             kspread_validity->m_cond=Conditional::InferiorEqual;
01730                                             break;
01731                                         default:
01732                                             kdDebug()<<" Error in validation Operator :"<<value<<endl;
01733                                             break;
01734                                         }
01735                                     }
01736                                     break;
01737                                 default:
01738                                     kdDebug()<<" Error in Type element : "<<valueOp<<endl;
01739                                 }
01740 
01741                             }
01742                             //<gmr:Validation Style="0" Type="1" Operator="0" AllowBlank="true" UseDropdown="false">
01743                             //<gmr:Expression0>745</gmr:Expression0>
01744                             //<gmr:Expression1>4546</gmr:Expression1>
01745                         }
01746                     }
01747                     if (!font.isNull())
01748                     {
01749                         QDomElement font_element = font.toElement();
01750 
01751                         kspread_cell->format()->setTextFontFamily( font_element.text() );
01752 
01753                         if (!font_element.isNull())
01754                         {
01755                             if (font_element.attribute("Italic") == "1")
01756                             { kspread_cell->format()->setTextFontItalic(true); }
01757 
01758                             if (font_element.attribute("Bold") == "1")
01759                             { kspread_cell->format()->setTextFontBold(true); }
01760 
01761                             if (font_element.hasAttribute("Underline") && ( font_element.attribute("Underline") != "0") )
01762                             { kspread_cell->format()->setTextFontUnderline(true); }
01763 
01764                             if (font_element.hasAttribute("StrikeThrough" ) && ( font_element.attribute("StrikeThrough") != "0") )
01765                             { kspread_cell->format()->setTextFontStrike(true); }
01766 
01767                             if (font_element.hasAttribute("Unit"))
01768                             { kspread_cell->format()->setTextFontSize(font_element.attribute("Unit").toInt()); }
01769 
01770                         }
01771                         if ( !hyperlink.isNull() )
01772                         {
01773                             //<gmr:HyperLink type="GnmHLinkURL" target="www.kde.org"/>
01774                             if ( hyperlink.toElement().hasAttribute( "type" ) )
01775                             {
01776                                 QString linkType= hyperlink.toElement().attribute( "type" );
01777                                 QString target = hyperlink.toElement().attribute( "target" );
01778                                 QString tip = hyperlink.toElement().attribute( "tip" );
01779                                 if ( !tip.isEmpty() )
01780                                     kspread_cell->setCellText( tip );
01781                                 if ( linkType=="GnmHLinkURL" )
01782                                 {
01783                                     if ( !target.startsWith( "http://" ) )
01784                                         target="http://"+target;
01785                                     kspread_cell->setLink( target );
01786                                 }
01787                                 else if ( linkType=="GnmHLinkEMail" )
01788                                 {
01789                                     if ( !target.startsWith( "mailto:/" ) )
01790                                         target="mailto:/"+target;
01791                                     kspread_cell->setLink( target );
01792                                 }
01793                                 else if ( linkType=="GnmHLinkExternal" )
01794                                 {
01795                                     if ( !target.startsWith( "file://" ) )
01796                                         target="file://"+target;
01797 
01798                                     kspread_cell->setLink( target );
01799                                 }
01800                                 else if ( linkType=="GnmHLinkCurWB" )
01801                                 {
01802                                     kspread_cell->setLink( target );
01803                                 }
01804                                 else
01805                                     kdDebug()<<" linkType not defined : "<<linkType<<endl;
01806                             }
01807                         }
01808                     }
01809                 }
01810             }
01811             style_region = style_region.nextSibling();
01812         }
01813 
01814     }
01815 }
01816 
01817 /* NOTE: As of now everything is in a single huge function.  This is
01818      very ugly.  It should all be broken up into smaller
01819      functions, probably one for each GNUMeric section.  It kind
01820      of grew out of control.  It could probably be cleaned up in
01821      an hour or so. --PGE
01822   */
01823 
01824 
01825 KoFilter::ConversionStatus GNUMERICFilter::convert( const QCString & from, const QCString & to )
01826 {
01827     dateInit();
01828     bool bSuccess=true;
01829 
01830     kdDebug(30521) << "Entering GNUmeric Import filter." << endl;
01831 
01832     KoDocument * document = m_chain->outputDocument();
01833     if ( !document )
01834         return KoFilter::StupidError;
01835 
01836     kdDebug(30521) << "here we go... " << document->className() << endl;
01837 
01838     if ( !::qt_cast<const KSpread::Doc *>( document ) )  // it's safer that way :)
01839     {
01840         kdWarning(30521) << "document isn't a KSpread::Doc but a " << document->className() << endl;
01841         return KoFilter::NotImplemented;
01842     }
01843     if ( from != "application/x-gnumeric" || to != "application/x-kspread" )
01844     {
01845         kdWarning(30521) << "Invalid mimetypes " << from << " " << to << endl;
01846         return KoFilter::NotImplemented;
01847     }
01848 
01849     kdDebug(30521) << "...still here..." << endl;
01850 
01851     // No need for a dynamic cast here, since we use Qt's moc magic
01852     Doc * ksdoc = ( Doc * ) document;
01853 
01854     if ( ksdoc->mimeType() != "application/x-kspread" )
01855     {
01856         kdWarning(30521) << "Invalid document mimetype " << ksdoc->mimeType() << endl;
01857         return KoFilter::NotImplemented;
01858     }
01859 
01860 
01861     QIODevice* in = KFilterDev::deviceForFile(m_chain->inputFile(),"application/x-gzip");
01862 
01863     if ( !in )
01864     {
01865         kdError(30521) << "Cannot create device for uncompressing! Aborting!" << endl;
01866         return KoFilter::FileNotFound;
01867     }
01868 
01869     if (!in->open(IO_ReadOnly))
01870     {
01871         kdError(30521) << "Cannot open file for uncompressing! Aborting!" << endl;
01872         delete in;
01873         return KoFilter::FileNotFound;
01874     }
01875 
01876     QDomDocument doc;
01877     QString errorMsg;
01878     int errorLine, errorColumn;
01879     if ( !doc.setContent(in, &errorMsg, &errorLine, &errorColumn) )
01880     {
01881         kdError(30521) << "Parsing error in " << from << "! Aborting!" << endl
01882             << " In line: " << errorLine << ", column: " << errorColumn << endl
01883             << " Error message: " << errorMsg << endl;
01884         in->close();
01885         return KoFilter::ParsingError;
01886     }
01887 
01888     in->close();
01889     delete in;
01890 
01891     int row, column;
01892     int value = 0;
01893     int currentTab = -1;
01894     int selectedTab = 0;
01895     Sheet * selTable = 0;
01896 
01897     QDomElement docElem = doc.documentElement();
01898     QDomElement uiData  = docElem.namedItem("gmr:UIData").toElement();
01899     if ( !uiData.isNull() )
01900     {
01901       if ( uiData.hasAttribute( "SelectedTab" ) )
01902       {
01903         bool ok = false;
01904         int n = uiData.attribute( "SelectedTab" ).toInt( &ok );
01905         if ( ok )
01906         {
01907           selectedTab = n;
01908         }
01909       }
01910     }
01911     QDomNode sheets = docElem.namedItem("gmr:Sheets");
01912     if ( sheets.isNull() )
01913     {
01914         //avoid crash with new file format.
01915         //TODO allow to load new file format
01916         return KoFilter::ParsingError;
01917     }
01918     QDomNode sheet =  sheets.namedItem("gmr:Sheet");
01919 
01920     /* This sets the Document information. */
01921     set_document_info( document, &docElem );
01922 
01923     /* This sets the Document attributes */
01924     set_document_attributes( ksdoc, &docElem );
01925 
01926     /* This sets the Area Names */
01927     set_document_area_names( ksdoc, &docElem );
01928 
01929     Sheet * table;
01930 
01931     // This is a mapping of exprID to expressions.
01932 
01933     QDict<char> exprID_dict( 17, FALSE );
01934     int num = 1;
01935 
01936     while (!sheet.isNull())
01937     {
01938         ++currentTab;
01939         table = ksdoc->map()->addNewSheet();
01940 
01941         if ( currentTab == selectedTab )
01942           selTable = table;
01943 
01944         QDomElement name = sheet.namedItem( "gmr:Name" ).toElement();
01945         QDomElement sheetElement = sheet.toElement();
01946 
01947         if ( !name.isNull() )
01948           table->setSheetName( name.text(), false, false );
01949         else
01950           table->setSheetName( "Sheet" + QString::number( num ), false, false );
01951         table->enableScrollBarUpdates( false );
01952 
01953         //kdDebug()<<" sheetElement.hasAttribute( DisplayFormulas ) :"<<sheetElement.hasAttribute( "DisplayFormulas" )<<endl;
01954         QString tmp;
01955         if ( sheetElement.hasAttribute( "DisplayFormulas" ) )
01956         {
01957             tmp=sheetElement.attribute( "DisplayFormulas");
01958             table->setShowFormula( ( tmp=="true" )||( tmp=="1" ) );
01959         }
01960         if ( sheetElement.hasAttribute( "HideZero" ) )
01961         {
01962             tmp = sheetElement.attribute( "HideZero" );
01963             table->setHideZero( ( tmp=="true" )||( tmp=="1" ) );
01964         }
01965         if ( sheetElement.hasAttribute( "HideGrid" ) )
01966         {
01967             tmp = sheetElement.attribute( "HideGrid" );
01968             table->setShowGrid( ( tmp=="false" )||( tmp=="0" ) );
01969         }
01970         if ( sheetElement.hasAttribute( "HideColHeader" ) )
01971         {
01972             tmp = sheetElement.attribute( "HideColHeader" );
01973             ksdoc->setShowColumnHeader( ( tmp=="false" )||( tmp=="0" ) );
01974         }
01975         if ( sheetElement.hasAttribute( "HideRowHeader" ) )
01976         {
01977             tmp =sheetElement.attribute( "HideRowHeader" );
01978             ksdoc->setShowRowHeader( ( tmp=="false" )||( tmp=="0" ) );
01979         }
01980 
01981 
01982     setObjectInfo(&sheet, table);
01983     setColInfo(&sheet, table);
01984     setRowInfo(&sheet, table);
01985     setSelectionInfo(&sheet, table);
01986 
01987         /* handling print information */
01988     QDomNode printInfo = sheet.namedItem("gmr:PrintInformation");
01989         if ( !printInfo.isNull() )
01990           ParsePrintInfo( printInfo, table );
01991 
01992         kdDebug(30521) << "Reading in cells" << endl;
01993 
01994     /* CELL handling START */
01995     QDomNode cells = sheet.namedItem( "gmr:Cells" );
01996     QDomNode cell  = cells.namedItem( "gmr:Cell" );
01997         QDomNode mergedCells = sheet.namedItem( "gmr:MergedRegions" );
01998         QDomNode mergedRegion = mergedCells.namedItem( "gmr:Merge" );
01999         if ( cell.isNull() )
02000         {
02001           kdWarning(30521) << "No cells" << endl;
02002         }
02003 
02004     while ( !cell.isNull() )
02005         {
02006       value += 2;
02007       emit sigProgress(value);
02008 
02009       QDomElement e = cell.toElement(); // try to convert the node to an element.
02010       if ( !e.isNull() )
02011           { // the node was really an element.
02012             kdDebug(30521) << "New Cell " << endl;
02013         QDomNode content_node = cell.namedItem("gmr:Content");
02014 
02015         if (!content_node.isNull())
02016             {
02017           QDomElement content = content_node.toElement();
02018 
02019           if( !content.isNull() )
02020               { // the node was really an element.
02021         column = e.attribute( "Col" ).toInt() + 1;
02022         row    = e.attribute( "Row" ).toInt() + 1;
02023 
02024         QString cell_content( content.text() );
02025                 //kdDebug()<<"cell_content :!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :"<<cell_content<<endl;
02026                 if ( cell_content[0] == '=' )
02027                   convertFormula( cell_content );
02028 
02029                 Cell * kspread_cell = table->nonDefaultCell( column, row );
02030 
02031                 if (e.hasAttribute("ValueType"))
02032                 {
02033                   // TODO: what is this for?
02034                   // <xsd:enumeration value="10"/> <!-- empty     -->
02035                   // <xsd:enumeration value="20"/> <!-- boolean   -->
02036                   // <xsd:enumeration value="30"/> <!-- integer   -->
02037                   // <xsd:enumeration value="40"/> <!-- float     -->
02038                   // <xsd:enumeration value="50"/> <!-- error     -->
02039                   // <xsd:enumeration value="60"/> <!-- string    -->
02040                   // <xsd:enumeration value="70"/> <!-- cellrange -->
02041                   // <xsd:enumeration value="80"/> <!-- array     -->
02042                     QString valuetype = e.attribute( "ValueType" );
02043                     if ( valuetype == "40" )//percentage
02044                     {
02045                         kspread_cell->format()->setFormatType( Percentage_format );
02046                         kspread_cell->setValue( cell_content );
02047                     }
02048                     else if ( valuetype =="60" )//string
02049                     {
02050                         kspread_cell->format()->setFormatType( Text_format );
02051                         kspread_cell->setValue( cell_content );
02052                     }
02053                 }
02054 
02055                 if (e.hasAttribute( "ValueFormat" ))
02056                 {
02057                   QString formatString = e.attribute( "ValueFormat" );
02058                   if ( !setType( kspread_cell, formatString, cell_content ) )
02059                     table->setText(row, column, cell_content, false);
02060                 }
02061                 else
02062                   table->setText(row, column, cell_content, false);
02063 
02064         if (e.hasAttribute("ExprID"))
02065                 {
02066             // QString encoded_string(table->cellAt( column, row, false)->encodeFormula( row, column ).utf8());
02067             QString encoded_string(table->cellAt( column, row, false )->encodeFormula().latin1());
02068 
02069 
02070             char * tmp_string = ( char * ) malloc( strlen( encoded_string.latin1() ) );
02071             strcpy( tmp_string, encoded_string.latin1() );
02072 
02073             kdDebug(30521) << encoded_string.latin1() << endl;
02074 
02075             exprID_dict.insert(e.attribute("ExprID"), tmp_string);
02076 
02077             kdDebug(30521) << exprID_dict[e.attribute("ExprID")] << endl;
02078             kdDebug(30521) << exprID_dict[QString("1")] << endl;
02079             kdDebug(30521) << e.attribute("ExprID") << endl;
02080 
02081           }
02082           }
02083         }
02084         else
02085             {
02086 
02087                 column = e.attribute( "Col" ).toInt() + 1;
02088         row    = e.attribute( "Row" ).toInt() + 1;
02089 
02090         QString cell_content( e.text() );
02091                 //kdDebug()<<"cell_content :!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :"<<cell_content<<endl;
02092                 if ( cell_content[0] == '=' )
02093                   convertFormula( cell_content );
02094 
02095                 Cell * kspread_cell = table->nonDefaultCell( column, row );
02096 
02097                 if (e.hasAttribute("ValueType"))
02098                 {
02099                   // TODO: Defined type of cell
02100                     //<xsd:enumeration value="10"/> <!-- empty     -->
02101                     //<xsd:enumeration value="20"/> <!-- boolean   -->
02102                     //<xsd:enumeration value="30"/> <!-- integer   -->
02103                     //<xsd:enumeration value="40"/> <!-- float     -->
02104                     //<xsd:enumeration value="50"/> <!-- error     -->
02105                     //<xsd:enumeration value="60"/> <!-- string    -->
02106                     //<xsd:enumeration value="70"/> <!-- cellrange -->
02107                     //<xsd:enumeration value="80"/> <!-- array     -->
02108                     //kspread_cell->setValue( date );
02109                     //kspread_cell->format()->setFormatType( type );
02110                     QString valuetype = e.attribute( "ValueType" );
02111                     if ( valuetype == "40" )//percentage
02112                     {
02113                         kspread_cell->format()->setFormatType( Percentage_format );
02114                         kspread_cell->setValue( cell_content );
02115                     }
02116                     else if ( valuetype =="60" )//string
02117                     {
02118                         kspread_cell->format()->setFormatType( Text_format );
02119                         kspread_cell->setValue( cell_content );
02120                     }
02121 
02122                 }
02123 
02124                 if (e.hasAttribute( "ValueFormat" ))
02125                 {
02126                   QString formatString = e.attribute( "ValueFormat" );
02127                   if ( !setType( kspread_cell, formatString, cell_content ) )
02128                     table->setText(row, column, cell_content, false);
02129                 }
02130                 else
02131                   table->setText(row, column, cell_content, false);
02132 
02133 
02134                 if (e.hasAttribute("ExprID"))
02135                 {
02136             column = e.attribute("Col").toInt() + 1;
02137             row    = e.attribute("Row").toInt() + 1;
02138             char * expr;
02139             expr = exprID_dict[e.attribute("ExprID")];
02140             // expr = exprID_dict[QString("1")];
02141 
02142             kdDebug(30521) << "FOO:" << column << row << endl;
02143             kdDebug(30521) <<
02144                            table->cellAt( column, row, false )->decodeFormula( expr, column, row ).latin1() << endl;
02145             kdDebug(30521) << expr << endl;
02146 
02147             table->setText(row, column,
02148                                    table->cellAt( column, row, false )->decodeFormula( expr, column, row ),
02149                                    false);
02150           }
02151           }
02152       }
02153       cell = cell.nextSibling();
02154     }
02155 
02156         kdDebug(30521) << "Reading in cells done" << endl;
02157 
02158         if ( mergedRegion.isNull() )
02159         {
02160           kdWarning(30521) << "No cells merged !" << endl;
02161         }
02162     while ( !mergedRegion.isNull() )
02163         {
02164             QDomElement e = mergedRegion.toElement(); // try to convert the node to an element.
02165             QString cell_merge_area( e.text() );
02166             Range range(cell_merge_area);
02167             //kdDebug()<<"text !!! :"<<cell_merge_area<< "range :start row : "<<range.startRow ()<<" start col :"<<range.startCol ()<<" end row :"<<range.endRow ()<<" end col :"<<range.endCol ()<<endl;
02168             Cell * cell = table->nonDefaultCell( range.startCol (), range.startRow () );
02169             cell->mergeCells( range.startCol (), range.startRow (), range.endCol ()-range.startCol (),  range.endRow ()-range.startRow ());
02170             mergedRegion = mergedRegion.nextSibling();
02171         }
02172     /* There is a memory leak here...
02173      * The strings in the exprID_dict have been allocated, but they have not been freed.
02174      */
02175 
02176     /* exprID_dict.statistics(); */
02177 
02178     /* CELL handling STOP */
02179 
02180     /* STYLE handling START */
02181         //Laurent - 2001-12-07  desactivate this code : otherwise we
02182         //create 65535*255 cells (Styleregion is define for a area and
02183         //not for cell, so gnumeric define a style as : col start=0 col end=255
02184         //rowstart=0 rowend=255 => we create 255*255 cells
02185         //and gnumeric stocke all area and not just modify area
02186         //=> not good for kspread.
02187         // Norbert: activated again, only cells with texts get modified, nothing else created
02188     setStyleInfo(&sheet, table);
02189 
02190     /* STYLE handling STOP */
02191         table->enableScrollBarUpdates( true );
02192 
02193     sheet = sheet.nextSibling();
02194         ++num;
02195       }
02196 
02197     if ( selTable )
02198       ksdoc->setDisplaySheet( selTable );
02199 
02200     emit sigProgress(100);
02201     if ( bSuccess )
02202         return KoFilter::OK;
02203     else
02204         return KoFilter::StupidError;
02205 }
02206 
02207 #include <gnumericimport.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys