F:/KPlato/koffice/libs/kotext/KoUserStyleCollection.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 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 "KoUserStyleCollection.h"
00020 #include "KoUserStyle.h"
00021 #include <kdebug.h>
00022 
00023 
00024 
00025 
00026 KoUserStyleCollection::KoUserStyleCollection( const QString& prefix )
00027     : m_prefix( prefix ),
00028       m_lastStyle( 0 ),
00029       m_default( false /*to be safe*/ )
00030 {
00031 }
00032 
00033 KoUserStyle* KoUserStyleCollection::findStyle( const QString & _name, const QString& defaultStyleName ) const
00034 {
00035     // Caching, to speed things up
00036     if ( m_lastStyle && m_lastStyle->name() == _name )
00037         return m_lastStyle;
00038 
00039     KoUserStyle* style = 0;
00040     foreach( style, m_styleList )
00041     {
00042         if ( style->name() == _name )
00043         {
00044             m_lastStyle = style;
00045             return m_lastStyle;
00046         }
00047     }
00048 
00049     if( !defaultStyleName.isEmpty() && _name == defaultStyleName && !m_styleList.isEmpty() )
00050         return m_styleList.first(); // fallback..
00051 
00052     return 0;
00053 }
00054 
00055 KoUserStyle* KoUserStyleCollection::findStyleByDisplayName( const QString& displayName ) const
00056 {
00057     if ( m_lastStyle && m_lastStyle->displayName() == displayName )
00058         return m_lastStyle;
00059 
00060     KoUserStyle* style = 0;
00061     foreach( style, m_styleList )
00062     {
00063         if ( style->displayName() == displayName )
00064         {
00065             m_lastStyle = style;
00066             return m_lastStyle;
00067         }
00068     }
00069 
00070     return 0;
00071 }
00072 
00073 QString KoUserStyleCollection::generateUniqueName() const
00074 {
00075     int count = 1;
00076     QString name;
00077     do {
00078         name = m_prefix + QString::number( count++ );
00079     } while ( findStyle( name, QString::null ) );
00080     return name;
00081 }
00082 
00083 KoUserStyleCollection::~KoUserStyleCollection()
00084 {
00085     clear();
00086 }
00087 
00088 void KoUserStyleCollection::clear()
00089 {
00090     KoUserStyle* style = 0;
00091     foreach( style, m_styleList )
00092         delete style;
00093     foreach( style, m_deletedStyles )
00094         delete style;
00095     
00096     m_styleList.clear();
00097     m_deletedStyles.clear();
00098     m_lastStyle = 0;
00099 }
00100 
00101 QStringList KoUserStyleCollection::displayNameList() const
00102 {
00103     QStringList lst;
00104     KoUserStyle* style = 0;
00105     foreach( style, m_styleList )
00106        lst.append( style->displayName() );
00107     
00108     return lst;
00109 }
00110 
00111 KoUserStyle* KoUserStyleCollection::addStyle( KoUserStyle* sty )
00112 {
00113     // First check for duplicates.
00114     KoUserStyle* p = 0;
00115     foreach( p, m_styleList )
00116     {
00117         if ( p->name() == sty->name() ) {
00118             if ( p->displayName() == sty->displayName() ) {
00119                 // Replace existing style
00120                 if ( sty != p )
00121                 {
00122                     *p = *sty;
00123                     delete sty;
00124                 }
00125                 return p;
00126             } else { // internal name conflict, but it's not the same style as far as the user is concerned
00127                 sty->setName( generateUniqueName() );
00128             }
00129         }
00130     }
00131     m_styleList.append( sty );
00132     return sty;
00133 }
00134 
00135 void KoUserStyleCollection::removeStyle ( KoUserStyle *style ) {
00136     if( m_styleList.removeAll(style)) {
00137         if ( m_lastStyle == style )
00138             m_lastStyle = 0;
00139         // Remember to delete this style when deleting the document
00140         m_deletedStyles.append(style);
00141     }
00142 }
00143 
00144 void KoUserStyleCollection::updateStyleListOrder( const QStringList &lst )
00145 {
00146     QList<KoUserStyle *> orderStyle;
00147     QString tmpString;
00148     KoUserStyle* style = 0;
00149     foreach( tmpString, lst )
00150     {
00151         style = findStyle( tmpString, QString::null );
00152         if( style )
00153             orderStyle.append( style );
00154         else
00155             kWarning(32500) << "updateStyleListOrder: style " << tmpString << " not found!" << endl;
00156     }
00157     // we'll lose (and leak) styles if the list didn't have all the styles
00158     Q_ASSERT( m_styleList.count() == orderStyle.count() );
00159     m_styleList.clear();
00160     m_styleList = orderStyle;
00161 }

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