summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoUserStyleCollection.cpp
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 01:29:50 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-01-20 01:29:50 +0000
commit8362bf63dea22bbf6736609b0f49c152f975eb63 (patch)
tree0eea3928e39e50fae91d4e68b21b1e6cbae25604 /lib/kotext/KoUserStyleCollection.cpp
downloadkoffice-8362bf63dea22bbf6736609b0f49c152f975eb63.tar.gz
koffice-8362bf63dea22bbf6736609b0f49c152f975eb63.zip
Added old abandoned KDE3 version of koffice
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/koffice@1077364 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'lib/kotext/KoUserStyleCollection.cpp')
-rw-r--r--lib/kotext/KoUserStyleCollection.cpp147
1 files changed, 147 insertions, 0 deletions
diff --git a/lib/kotext/KoUserStyleCollection.cpp b/lib/kotext/KoUserStyleCollection.cpp
new file mode 100644
index 00000000..1b9b8ccf
--- /dev/null
+++ b/lib/kotext/KoUserStyleCollection.cpp
@@ -0,0 +1,147 @@
+/* This file is part of the KDE project
+ Copyright (C) 2005 David Faure <[email protected]>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include "KoUserStyleCollection.h"
+#include "KoUserStyle.h"
+#include <kdebug.h>
+
+KoUserStyleCollection::KoUserStyleCollection( const QString& prefix )
+ : m_prefix( prefix ),
+ m_lastStyle( 0 ),
+ m_default( false /*to be safe*/ )
+{
+}
+
+KoUserStyle* KoUserStyleCollection::findStyle( const QString & _name, const QString& defaultStyleName ) const
+{
+ // Caching, to speed things up
+ if ( m_lastStyle && m_lastStyle->name() == _name )
+ return m_lastStyle;
+
+ for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt ) {
+ if ( (*styleIt)->name() == _name ) {
+ m_lastStyle = *styleIt;
+ return m_lastStyle;
+ }
+ }
+
+ if( !defaultStyleName.isEmpty() && _name == defaultStyleName && !m_styleList.isEmpty() )
+ return m_styleList.first(); // fallback..
+
+ return 0;
+}
+
+KoUserStyle* KoUserStyleCollection::findStyleByDisplayName( const QString& displayName ) const
+{
+ if ( m_lastStyle && m_lastStyle->displayName() == displayName )
+ return m_lastStyle;
+
+ for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt ) {
+ if ( (*styleIt)->displayName() == displayName ) {
+ m_lastStyle = *styleIt;
+ return m_lastStyle;
+ }
+ }
+
+ return 0;
+}
+
+QString KoUserStyleCollection::generateUniqueName() const
+{
+ int count = 1;
+ QString name;
+ do {
+ name = m_prefix + QString::number( count++ );
+ } while ( findStyle( name, QString::null ) );
+ return name;
+}
+
+KoUserStyleCollection::~KoUserStyleCollection()
+{
+ clear();
+}
+
+void KoUserStyleCollection::clear()
+{
+ // KDE4: qDeleteAll
+ for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
+ delete *styleIt;
+ for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_deletedStyles.begin(), styleEnd = m_deletedStyles.end() ; styleIt != styleEnd ; ++styleIt )
+ delete *styleIt;
+ m_styleList.clear();
+ m_deletedStyles.clear();
+ m_lastStyle = 0;
+}
+
+QStringList KoUserStyleCollection::displayNameList() const
+{
+ QStringList lst;
+ for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
+ lst.append( (*styleIt)->displayName() );
+ return lst;
+}
+
+KoUserStyle* KoUserStyleCollection::addStyle( KoUserStyle* sty )
+{
+ // First check for duplicates.
+ for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
+ {
+ KoUserStyle* p = *styleIt;
+ if ( p->name() == sty->name() ) {
+ if ( p->displayName() == sty->displayName() ) {
+ // Replace existing style
+ if ( sty != p )
+ {
+ *p = *sty;
+ delete sty;
+ }
+ return p;
+ } else { // internal name conflict, but it's not the same style as far as the user is concerned
+ sty->setName( generateUniqueName() );
+ }
+ }
+ }
+ m_styleList.append( sty );
+ return sty;
+}
+
+void KoUserStyleCollection::removeStyle ( KoUserStyle *style ) {
+ if( m_styleList.remove(style)) {
+ if ( m_lastStyle == style )
+ m_lastStyle = 0;
+ // Remember to delete this style when deleting the document
+ m_deletedStyles.append(style);
+ }
+}
+
+void KoUserStyleCollection::updateStyleListOrder( const QStringList &lst )
+{
+ QValueList<KoUserStyle *> orderStyle;
+ for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
+ {
+ KoUserStyle* style = findStyle( *it, QString::null );
+ if ( style )
+ orderStyle.append( style );
+ else
+ kdWarning(32500) << "updateStyleListOrder: style " << *it << " not found!" << endl;
+ }
+ // we'll lose (and leak) styles if the list didn't have all the styles
+ Q_ASSERT( m_styleList.count() == orderStyle.count() );
+ m_styleList.clear();
+ m_styleList = orderStyle;
+}