lib
KoUserStyleCollection.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 )
00027 {
00028 }
00029
00030 KoUserStyle* KoUserStyleCollection::findStyle( const QString & _name, const QString& defaultStyleName ) const
00031 {
00032
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();
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
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
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
00108 if ( sty != p )
00109 {
00110 *p = *sty;
00111 delete sty;
00112 }
00113 return p;
00114 } else {
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
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
00144 Q_ASSERT( m_styleList.count() == orderStyle.count() );
00145 m_styleList.clear();
00146 m_styleList = orderStyle;
00147 }
|