F:/KPlato/koffice/libs/kofficecore/KoGenStyles.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2004-2006 David Faure <faure@kde.org>
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 version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "KoGenStyles.h"
00020 #include <KoXmlWriter.h>
00021 #include <float.h>
00022 #include <kdebug.h>
00023 //Added by qt3to4:
00024 #include <Q3ValueList>
00025 
00026 KoGenStyles::KoGenStyles()
00027 {
00028 }
00029 
00030 KoGenStyles::~KoGenStyles()
00031 {
00032 }
00033 
00034 QString KoGenStyles::lookup( const KoGenStyle& style, const QString& name, int flags )
00035 {
00036     StyleMap::iterator it = m_styleMap.find( style );
00037     if ( it == m_styleMap.end() ) {
00038         // Not found, try if this style is in fact equal to its parent (the find above
00039         // wouldn't have found it, due to m_parentName being set).
00040         if ( !style.parentName().isEmpty() ) {
00041             KoGenStyle testStyle( style );
00042             const KoGenStyle* parentStyle = this->style( style.parentName() ); // ## linear search
00043             if( !parentStyle ) {
00044                 kDebug(30003) << "KoGenStyles::lookup(" << name << "): parent style '" << style.parentName() << "' not found in collection" << endl;
00045             } else {
00046                 if ( testStyle.m_familyName != parentStyle->m_familyName )
00047                 {
00048                     kWarning(30003) << "KoGenStyles::lookup(" << name << ", family=" << testStyle.m_familyName << ") parent style '" << style.parentName() << "' has a different family: " << parentStyle->m_familyName << endl;
00049                 }
00050 
00051                 testStyle.m_parentName = parentStyle->m_parentName;
00052                 // Exclude the type from the comparison. It's ok for an auto style
00053                 // to have a user style as parent; they can still be identical
00054                 testStyle.m_type = parentStyle->m_type;
00055                 // Also it's ok to not have the display name of the parent style
00056                 // in the auto style
00057                 QMap<QString, QString>::const_iterator it = parentStyle->m_attributes.find( "style:display-name" );
00058                 if ( it != parentStyle->m_attributes.end() )
00059                     testStyle.addAttribute( "style:display-name", *it );
00060 
00061                 if ( *parentStyle == testStyle )
00062                     return style.parentName();
00063             }
00064         }
00065 
00066         QString styleName( name );
00067         if ( styleName.isEmpty() ) {
00068             styleName = 'A'; // for "auto".
00069             flags &= ~DontForceNumbering; // i.e. force numbering
00070         }
00071         styleName = makeUniqueName( styleName, flags );
00072         if ( style.autoStyleInStylesDotXml() )
00073             m_autoStylesInStylesDotXml.insert( styleName, true /*unused*/ );
00074         else
00075             m_styleNames.insert( styleName, true /*unused*/ );
00076         it = m_styleMap.insert( style, styleName );
00077         NamedStyle s;
00078         s.style = &it.key();
00079         s.name = styleName;
00080         m_styleArray.append( s );
00081     }
00082     return it.value();
00083 }
00084 
00085 QString KoGenStyles::makeUniqueName( const QString& base, int flags ) const
00086 {
00087     // If this name is not used yet, and numbering isn't forced, then the given name is ok.
00088     if ( ( flags & DontForceNumbering )
00089          && m_autoStylesInStylesDotXml.find( base ) == m_autoStylesInStylesDotXml.end()
00090          && m_styleNames.find( base ) == m_styleNames.end() )
00091         return base;
00092     int num = 1;
00093     QString name;
00094     do {
00095         name = base;
00096         name += QString::number( num++ );
00097     } while ( m_autoStylesInStylesDotXml.find( name ) != m_autoStylesInStylesDotXml.end()
00098               || m_styleNames.find( name ) != m_styleNames.end() );
00099     return name;
00100 }
00101 
00102 Q3ValueList<KoGenStyles::NamedStyle> KoGenStyles::styles( int type, bool markedForStylesXml ) const
00103 {
00104     Q3ValueList<KoGenStyles::NamedStyle> lst;
00105     const NameMap& nameMap = markedForStylesXml ? m_autoStylesInStylesDotXml : m_styleNames;
00106     StyleArray::const_iterator it = m_styleArray.begin();
00107     const StyleArray::const_iterator end = m_styleArray.end();
00108     for ( ; it != end ; ++it ) {
00109         // Look up if it's marked for styles.xml or not by looking up in the corresponding style map.
00110         if ( (*it).style->type() == type && nameMap.find((*it).name) != nameMap.end() ) {
00111             lst.append( *it );
00112         }
00113     }
00114     return lst;
00115 }
00116 
00117 const KoGenStyle* KoGenStyles::style( const QString& name ) const
00118 {
00119     StyleArray::const_iterator it = m_styleArray.begin();
00120     const StyleArray::const_iterator end = m_styleArray.end();
00121     for ( ; it != end ; ++it ) {
00122         if ( (*it).name == name )
00123             return (*it).style;
00124     }
00125     return 0;
00126 }
00127 
00128 KoGenStyle* KoGenStyles::styleForModification( const QString& name )
00129 {
00130     return const_cast<KoGenStyle *>( style( name ) );
00131 }
00132 
00133 void KoGenStyles::markStyleForStylesXml( const QString& name )
00134 {
00135     Q_ASSERT( m_styleNames.find( name ) != m_styleNames.end() );
00136     m_styleNames.remove( name );
00137     m_autoStylesInStylesDotXml.insert( name, true );
00138     styleForModification( name )->setAutoStyleInStylesDotXml( true );
00139 }
00140 
00141 void KoGenStyles::dump()
00142 {
00143     kDebug() << "Style array:" << endl;
00144     StyleArray::const_iterator it = m_styleArray.begin();
00145     const StyleArray::const_iterator end = m_styleArray.end();
00146     for ( ; it != end ; ++it ) {
00147         kDebug() << (*it).name << endl;
00148     }
00149     for ( NameMap::const_iterator it = m_styleNames.begin(); it != m_styleNames.end(); ++it ) {
00150         kDebug() << "style: " << it.key() << endl;
00151     }
00152     for ( NameMap::const_iterator it = m_autoStylesInStylesDotXml.begin(); it != m_autoStylesInStylesDotXml.end(); ++it ) {
00153         kDebug() << "auto style for style.xml: " << it.key() << endl;
00154         const KoGenStyle* s = style( it.key() );
00155         Q_ASSERT( s );
00156         Q_ASSERT( s->autoStyleInStylesDotXml() );
00157     }
00158 }
00159 

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1