00001
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
00024
00025
00026 KoUserStyleCollection::KoUserStyleCollection( const QString& prefix )
00027 : m_prefix( prefix ),
00028 m_lastStyle( 0 ),
00029 m_default( false )
00030 {
00031 }
00032
00033 KoUserStyle* KoUserStyleCollection::findStyle( const QString & _name, const QString& defaultStyleName ) const
00034 {
00035
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();
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
00114 KoUserStyle* p = 0;
00115 foreach( p, m_styleList )
00116 {
00117 if ( p->name() == sty->name() ) {
00118 if ( p->displayName() == sty->displayName() ) {
00119
00120 if ( sty != p )
00121 {
00122 *p = *sty;
00123 delete sty;
00124 }
00125 return p;
00126 } else {
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
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
00158 Q_ASSERT( m_styleList.count() == orderStyle.count() );
00159 m_styleList.clear();
00160 m_styleList = orderStyle;
00161 }