kspread

CalculationSettings.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
00003    Copyright 2007 Thorsten Zachmann <zachmann@kde.org>
00004    Copyright 2005-2006 Inge Wallin <inge@lysator.liu.se>
00005    Copyright 2004 Ariya Hidayat <ariya@kde.org>
00006    Copyright 2002-2003 Norbert Andres <nandres@web.de>
00007    Copyright 2000-2002 Laurent Montel <montel@kde.org>
00008    Copyright 2002 John Dailey <dailey@vt.edu>
00009    Copyright 2002 Phillip Mueller <philipp.mueller@gmx.de>
00010    Copyright 2000 Werner Trobin <trobin@kde.org>
00011    Copyright 1999-2000 Simon Hausmann <hausmann@kde.org>
00012    Copyright 1999 David Faure <faure@kde.org>
00013    Copyright 1998-2000 Torben Weis <weis@kde.org>
00014 
00015    This library is free software; you can redistribute it and/or
00016    modify it under the terms of the GNU Library General Public
00017    License as published by the Free Software Foundation; either
00018    version 2 of the License, or (at your option) any later version.
00019 
00020    This library is distributed in the hope that it will be useful,
00021    but WITHOUT ANY WARRANTY; without even the implied warranty of
00022    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023    Library General Public License for more details.
00024 
00025    You should have received a copy of the GNU Library General Public License
00026    along with this library; see the file COPYING.LIB.  If not, write to
00027    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00028    Boston, MA 02110-1301, USA.
00029 */
00030 
00031 // Local
00032 #include "CalculationSettings.h"
00033 
00034 #include "Localization.h"
00035 
00036 #include <KoXmlNS.h>
00037 
00038 #include <kdebug.h>
00039 
00040 using namespace KSpread;
00041 
00042 class CalculationSettings::Private
00043 {
00044 public:
00045     KLocale* locale;
00046     bool caseSensitiveComparisons : 1;
00047     bool precisionAsShown         : 1;
00048     bool wholeCellSearchCriteria  : 1;
00049     bool automaticFindLabels      : 1;
00050     bool useRegularExpressions    : 1;
00051     int refYear; // the reference year two-digit years are relative to
00052     QDate refDate; // the reference date all dates are relative to
00053     // The precision used for decimal numbers, if the default cell style's
00054     // precision is set to arbitrary.
00055     int precision;
00056     QString fileName;
00057 };
00058 
00059 /*****************************************************************************
00060  *
00061  * CalculationSettings
00062  *
00063  *****************************************************************************/
00064 
00065 CalculationSettings::CalculationSettings()
00066     : d(new Private)
00067 {
00068     d->locale = new Localization();
00069     d->caseSensitiveComparisons = true;
00070     d->precisionAsShown         = false;
00071     d->wholeCellSearchCriteria  = true;
00072     d->automaticFindLabels      = true;
00073     d->useRegularExpressions    = true;
00074     d->refYear = 1930;
00075     d->refDate = QDate( 1899, 12, 30 );
00076     d->precision = 8;
00077 }
00078 
00079 CalculationSettings::~CalculationSettings()
00080 {
00081     delete d->locale;
00082     delete d;
00083 }
00084 
00085 void CalculationSettings::loadOdf(const KoXmlElement& body)
00086 {
00087     KoXmlNode settings = KoXml::namedItemNS( body, KoXmlNS::table, "calculation-settings" );
00088     kDebug() <<"Calculation settings found?"<< !settings.isNull();
00089     if ( !settings.isNull() )
00090     {
00091         KoXmlElement element = settings.toElement();
00092         if ( element.hasAttributeNS( KoXmlNS::table,  "case-sensitive" ) )
00093         {
00094             d->caseSensitiveComparisons = true;
00095             QString value = element.attributeNS( KoXmlNS::table, "case-sensitive", "true" );
00096             if ( value == "false" )
00097                 d->caseSensitiveComparisons = false;
00098         }
00099         else if ( element.hasAttributeNS( KoXmlNS::table, "precision-as-shown" ) )
00100         {
00101             d->precisionAsShown = false;
00102             QString value = element.attributeNS( KoXmlNS::table, "precision-as-shown", "false" );
00103             if ( value == "true" )
00104                 d->precisionAsShown = true;
00105         }
00106         else if ( element.hasAttributeNS( KoXmlNS::table, "search-criteria-must-apply-to-whole-cell" ) )
00107         {
00108             d->wholeCellSearchCriteria = true;
00109             QString value = element.attributeNS( KoXmlNS::table, "search-criteria-must-apply-to-whole-cell", "true" );
00110             if ( value == "false" )
00111                 d->wholeCellSearchCriteria = false;
00112         }
00113         else if ( element.hasAttributeNS( KoXmlNS::table, "automatic-find-labels" ) )
00114         {
00115             d->automaticFindLabels = true;
00116             QString value = element.attributeNS( KoXmlNS::table, "automatic-find-labels", "true" );
00117             if ( value == "false" )
00118                 d->automaticFindLabels = false;
00119         }
00120         else if ( element.hasAttributeNS( KoXmlNS::table, "use-regular-expressions" ) )
00121         {
00122             d->useRegularExpressions = true;
00123             QString value = element.attributeNS( KoXmlNS::table, "use-regular-expressions", "true" );
00124             if ( value == "false" )
00125                 d->useRegularExpressions = false;
00126         }
00127         else if ( element.hasAttributeNS( KoXmlNS::table, "null-year" ) )
00128         {
00129             d->refYear = 1930;
00130             QString value = element.attributeNS( KoXmlNS::table, "null-year", "1930" );
00131             if ( value == "false" )
00132                 d->refYear = false;
00133         }
00134 
00135         forEachElement( element, settings )
00136         {
00137             if ( element.namespaceURI() != KoXmlNS::table )
00138                 continue;
00139             else if ( element.tagName() ==  "null-date" )
00140             {
00141                 d->refDate = QDate( 1899, 12, 30 );
00142                 QString valueType = element.attributeNS( KoXmlNS::table, "value-type", "date" );
00143                 if( valueType == "date" )
00144                 {
00145                     QString value = element.attributeNS( KoXmlNS::table, "date-value", "1899-12-30" );
00146                     QDate date = QDate::fromString( value, Qt::ISODate );
00147                     if ( date.isValid() )
00148                         d->refDate = date;
00149                 }
00150                 else
00151                 {
00152                     kDebug() <<"CalculationSettings: Error on loading null date."
00153                              << "Value type """ << valueType << """ not handled"
00154                              << ", falling back to default." << endl;
00155                     // NOTE Stefan: I don't know why different types are possible here!
00156                 }
00157             }
00158             else if ( element.tagName() ==  "iteration" )
00159             {
00160                 // TODO
00161             }
00162         }
00163     }
00164 }
00165 
00166 bool CalculationSettings::saveOdf(KoXmlWriter &settingsWriter) const
00167 {
00168     return true;
00169 }
00170 
00171 KLocale* CalculationSettings::locale() const
00172 {
00173     return d->locale;
00174 }
00175 
00176 void CalculationSettings::setReferenceYear( int year )
00177 {
00178     if ( year < 100)
00179        d->refYear = 1900 + year;
00180     else
00181        d->refYear = year;
00182 }
00183 
00184 int CalculationSettings::referenceYear() const
00185 {
00186     return d->refYear;
00187 }
00188 
00189 void CalculationSettings::setReferenceDate( const QDate& date )
00190 {
00191     if ( !date.isValid() ) return;
00192     d->refDate.setDate( date.year(), date.month(), date.day() );
00193 }
00194 
00195 QDate CalculationSettings::referenceDate() const
00196 {
00197     return d->refDate;
00198 }
00199 
00200 void CalculationSettings::setDefaultDecimalPrecision( int precision )
00201 {
00202     d->precision = ( precision < 0 ) ? 8 : precision;
00203 }
00204 
00205 int CalculationSettings::defaultDecimalPrecision() const
00206 {
00207     return d->precision;
00208 }
00209 
00210 void CalculationSettings::setFileName(const QString& fileName)
00211 {
00212     d->fileName = fileName;
00213 }
00214 
00215 const QString& CalculationSettings::fileName() const
00216 {
00217     return d->fileName;
00218 }