kspread

kspread_style_manager.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Norbert Andres, nandres@web.de
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qdom.h>
00021 #include <qstringlist.h>
00022 
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 
00026 #include <KoOasisStyles.h>
00027 #include <KoXmlNS.h>
00028 
00029 #include "kspread_doc.h"
00030 #include "kspread_style.h"
00031 #include "kspread_style_manager.h"
00032 
00033 using namespace KSpread;
00034 
00035 StyleManager::StyleManager()
00036   : m_defaultStyle( new CustomStyle() )
00037 {
00038 }
00039 
00040 StyleManager::~StyleManager()
00041 {
00042   delete m_defaultStyle;
00043 
00044   Styles::iterator iter = m_styles.begin();
00045   Styles::iterator end  = m_styles.end();
00046 
00047   while ( iter != end )
00048   {
00049     delete iter.data();
00050 
00051     ++iter;
00052   }
00053 }
00054 
00055 void StyleManager::saveOasis( KoGenStyles &mainStyles )
00056 {
00057     kdDebug() << "Saving default oasis style" << endl;
00058     KoGenStyle defaultStyle = KoGenStyle( Doc::STYLE_CELL_USER, "table-cell" );
00059     m_defaultStyle->saveOasis( defaultStyle, mainStyles );
00060 
00061     Styles::iterator iter = m_styles.begin();
00062     Styles::iterator end  = m_styles.end();
00063 
00064     while ( iter != end )
00065     {
00066         kdDebug() << "Saving style" << endl;
00067         CustomStyle * styleData = iter.data();
00068 
00069         KoGenStyle customStyle = KoGenStyle( Doc::STYLE_CELL_USER, "table-cell" );
00070         styleData->saveOasis( customStyle, mainStyles );
00071 
00072         ++iter;
00073     }
00074 }
00075 
00076 void StyleManager::loadOasisStyleTemplate( KoOasisStyles& oasisStyles )
00077 {
00078     // loading default style first
00079     const QDomElement* defaultStyle = oasisStyles.defaultStyle( "table-cell" );
00080     if ( defaultStyle )
00081     {
00082       m_defaultStyle->loadOasis( oasisStyles, *defaultStyle, "Default" );
00083       m_defaultStyle->setType( Style::BUILTIN );
00084       kdDebug() << "StyleManager: default cell style loaded!" << endl;
00085     }
00086     else
00087     {
00088       delete m_defaultStyle;
00089       m_defaultStyle = new CustomStyle();
00090     }
00091 
00092     uint nStyles = oasisStyles.userStyles().count();
00093     kdDebug() << " number of template style to load : " << nStyles << endl;
00094     for (unsigned int item = 0; item < nStyles; item++) {
00095         QDomElement styleElem = oasisStyles.userStyles()[item];
00096 
00097         // assume the name assigned by the application
00098         QString name = styleElem.attributeNS( KoXmlNS::style, "name", QString::null );
00099 
00100         // then replace by user-visible one (if any)
00101         name = styleElem.attributeNS( KoXmlNS::style, "display-name", name );
00102 
00103         if ( !name.isEmpty() )
00104         {
00105             CustomStyle * style = 0;
00106             if ( styleElem.hasAttributeNS( KoXmlNS::style, "parent-style-name" ) )
00107                 // The style's parent name will be set in Style::loadOasis(..).
00108                 // After all styles are loaded the pointer to the parent is set.
00109                 style = new CustomStyle( name, 0 );
00110             else
00111                 style = new CustomStyle( name, m_defaultStyle );
00112 
00113             //fixme test return;
00114             style->loadOasis( oasisStyles, styleElem, name );
00115             style->setType( Style::CUSTOM );
00116             m_styles[name] = style;
00117             kdDebug() << "Style " << name << ": " << style << endl;
00118         }
00119     }
00120 
00121     // set the parent pointers after we loaded all styles
00122     Styles::iterator iter = m_styles.begin();
00123     Styles::iterator end  = m_styles.end();
00124     while ( iter != end )
00125     {
00126         CustomStyle * styleData = iter.data();
00127 
00128         if ( !styleData->parent() && !styleData->parentName().isNull() )
00129             styleData->setParent( m_styles[ styleData->parentName() ] );
00130 
00131         ++iter;
00132     }
00133 }
00134 
00135 QDomElement StyleManager::save( QDomDocument & doc )
00136 {
00137   kdDebug() << "Saving styles" << endl;
00138   QDomElement styles = doc.createElement( "styles" );
00139 
00140   kdDebug() << "Saving default style" << endl;
00141   m_defaultStyle->save( doc, styles );
00142 
00143   Styles::iterator iter = m_styles.begin();
00144   Styles::iterator end  = m_styles.end();
00145 
00146   while ( iter != end )
00147   {
00148     kdDebug() << "Saving style" << endl;
00149     CustomStyle * styleData = iter.data();
00150 
00151     styleData->save( doc, styles );
00152 
00153     ++iter;
00154   }
00155 
00156   kdDebug() << "End saving styles" << endl;
00157   return styles;
00158 }
00159 
00160 bool StyleManager::loadXML( QDomElement const & styles )
00161 {
00162   QDomElement e = styles.firstChild().toElement();
00163   while ( !e.isNull() )
00164   {
00165     QString name;
00166     if ( e.hasAttribute( "name" ) )
00167       name = e.attribute( "name" );
00168 
00169     if ( name == "Default" )
00170     {
00171       if ( !m_defaultStyle->loadXML( e, name ) )
00172         return false;
00173       m_defaultStyle->setType( Style::BUILTIN );
00174     }
00175     else if ( !name.isNull() )
00176     {
00177       CustomStyle * style = 0;
00178       if ( e.hasAttribute( "parent" ) && e.attribute( "parent" ) == "Default" )
00179         style = new CustomStyle( name, m_defaultStyle );
00180       else
00181         style = new CustomStyle( name, 0 );
00182 
00183       if ( !style->loadXML( e, name ) )
00184       {
00185         delete style;
00186         return false;
00187       }
00188 
00189       if ( style->type() == Style::AUTO )
00190         style->setType( Style::CUSTOM );
00191       m_styles[name] = style;
00192       kdDebug() << "Style " << name << ": " << style << endl;
00193     }
00194 
00195     e = e.nextSibling().toElement();
00196   }
00197 
00198   Styles::iterator iter = m_styles.begin();
00199   Styles::iterator end  = m_styles.end();
00200 
00201   while ( iter != end )
00202   {
00203     CustomStyle * styleData = iter.data();
00204 
00205     if ( !styleData->parent() && !styleData->parentName().isNull() )
00206       styleData->setParent( m_styles[ styleData->parentName() ] );
00207 
00208     ++iter;
00209   }
00210 
00211   m_defaultStyle->setName( "Default" );
00212   m_defaultStyle->setType( Style::BUILTIN );
00213 
00214   return true;
00215 }
00216 
00217 void StyleManager::createBuiltinStyles()
00218 {
00219   CustomStyle * header1 = new CustomStyle( i18n( "Header" ), m_defaultStyle );
00220   QFont f( header1->font() );
00221   f.setItalic( true );
00222   f.setPointSize( f.pointSize() + 2 );
00223   f.setBold( true );
00224   header1->changeFont( f );
00225   header1->setType( Style::BUILTIN );
00226   m_styles[ header1->name() ] = header1;
00227 
00228   CustomStyle * header2 = new CustomStyle( i18n( "Header1" ), header1 );
00229   QColor color( "#F0F0FF" );
00230   header2->changeBgColor( color );
00231   QPen pen( Qt::black, 1, Qt::SolidLine );
00232   header2->changeBottomBorderPen( pen );
00233   header2->setType( Style::BUILTIN );
00234 
00235   m_styles[ header2->name() ] = header2;
00236 }
00237 
00238 CustomStyle * StyleManager::style( QString const & name ) const
00239 {
00240   Styles::const_iterator iter( m_styles.find( name ) );
00241 
00242   if ( iter != m_styles.end() )
00243     return iter.data();
00244 
00245   if ( name == "Default" )
00246     return m_defaultStyle;
00247 
00248   return 0;
00249 }
00250 
00251 void StyleManager::takeStyle( CustomStyle * style )
00252 {
00253   CustomStyle * parent = style->parent();
00254 
00255   Styles::iterator iter = m_styles.begin();
00256   Styles::iterator end  = m_styles.end();
00257 
00258   while ( iter != end )
00259   {
00260     if ( iter.data()->parent() == style )
00261       iter.data()->setParent( parent );
00262 
00263     ++iter;
00264   }
00265 
00266   Styles::iterator i( m_styles.find( style->name() ) );
00267 
00268   if ( i != m_styles.end() )
00269   {
00270     kdDebug() << "Erasing style entry for " << style->name() << endl;
00271     m_styles.erase( i );
00272   }
00273 }
00274 
00275 bool StyleManager::checkCircle( QString const & name, QString const & parent )
00276 {
00277   CustomStyle * s = style( parent );
00278   if ( !s || s->parent() == 0 )
00279     return true;
00280   if ( s->parentName() == name )
00281     return false;
00282   else
00283     return checkCircle( name, s->parentName() );
00284 }
00285 
00286 bool StyleManager::validateStyleName( QString const & name, CustomStyle * style )
00287 {
00288   if ( m_defaultStyle->name() == name || name == "Default" )
00289     return false;
00290 
00291   Styles::const_iterator iter = m_styles.begin();
00292   Styles::const_iterator end  = m_styles.end();
00293 
00294   while ( iter != end )
00295   {
00296     if ( iter.key() == name && iter.data() != style )
00297       return false;
00298 
00299     ++iter;
00300   }
00301 
00302   return true;
00303 }
00304 
00305 void StyleManager::changeName( QString const & oldName, QString const & newName )
00306 {
00307   Styles::iterator iter = m_styles.begin();
00308   Styles::iterator end  = m_styles.end();
00309 
00310   while ( iter != end )
00311   {
00312     if ( iter.data()->parentName() == oldName )
00313       iter.data()->refreshParentName();
00314 
00315     ++iter;
00316   }
00317 
00318   iter = m_styles.find( oldName );
00319   if ( iter != end )
00320   {
00321     CustomStyle * s = iter.data();
00322     m_styles.erase( iter );
00323     m_styles[newName] = s;
00324   }
00325 }
00326 
00327 QStringList StyleManager::styleNames() const
00328 {
00329   QStringList list;
00330 
00331   list.push_back( i18n("Default") );
00332 
00333   Styles::const_iterator iter = m_styles.begin();
00334   Styles::const_iterator end  = m_styles.end();
00335 
00336   while ( iter != end )
00337   {
00338     list.push_back( iter.key() );
00339 
00340     ++iter;
00341   }
00342 
00343   return list;
00344 }
00345 
KDE Home | KDE Accessibility Home | Description of Access Keys