lib

KoUserStyleCollection.cpp

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