lib

KoUserStyleCollection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "KoUserStyleCollection.h"
20#include "KoUserStyle.h"
21#include <kdebug.h>
22
24 : m_prefix( prefix ),
25 m_lastStyle( 0 ),
26 m_default( false /*to be safe*/ )
27{
28}
29
30KoUserStyle* KoUserStyleCollection::findStyle( const TQString & _name, const TQString& defaultStyleName ) const
31{
32 // Caching, to speed things up
33 if ( m_lastStyle && m_lastStyle->name() == _name )
34 return m_lastStyle;
35
36 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt ) {
37 if ( (*styleIt)->name() == _name ) {
38 m_lastStyle = *styleIt;
39 return m_lastStyle;
40 }
41 }
42
43 if( !defaultStyleName.isEmpty() && _name == defaultStyleName && !m_styleList.isEmpty() )
44 return m_styleList.first(); // fallback..
45
46 return 0;
47}
48
50{
51 if ( m_lastStyle && m_lastStyle->displayName() == displayName )
52 return m_lastStyle;
53
54 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt ) {
55 if ( (*styleIt)->displayName() == displayName ) {
56 m_lastStyle = *styleIt;
57 return m_lastStyle;
58 }
59 }
60
61 return 0;
62}
63
65{
66 int count = 1;
67 TQString name;
68 do {
69 name = m_prefix + TQString::number( count++ );
70 } while ( findStyle( name, TQString() ) );
71 return name;
72}
73
78
80{
81 // KDE4: qDeleteAll
82 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
83 delete *styleIt;
84 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_deletedStyles.begin(), styleEnd = m_deletedStyles.end() ; styleIt != styleEnd ; ++styleIt )
85 delete *styleIt;
86 m_styleList.clear();
87 m_deletedStyles.clear();
88 m_lastStyle = 0;
89}
90
92{
93 TQStringList lst;
94 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
95 lst.append( (*styleIt)->displayName() );
96 return lst;
97}
98
100{
101 // First check for duplicates.
102 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
103 {
104 KoUserStyle* p = *styleIt;
105 if ( p->name() == sty->name() ) {
106 if ( p->displayName() == sty->displayName() ) {
107 // Replace existing style
108 if ( sty != p )
109 {
110 *p = *sty;
111 delete sty;
112 }
113 return p;
114 } else { // internal name conflict, but it's not the same style as far as the user is concerned
115 sty->setName( generateUniqueName() );
116 }
117 }
118 }
119 m_styleList.append( sty );
120 return sty;
121}
122
124 if( m_styleList.remove(style)) {
125 if ( m_lastStyle == style )
126 m_lastStyle = 0;
127 // Remember to delete this style when deleting the document
128 m_deletedStyles.append(style);
129 }
130}
131
132void KoUserStyleCollection::updateStyleListOrder( const TQStringList &lst )
133{
134 TQValueList<KoUserStyle *> orderStyle;
135 for ( TQStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
136 {
137 KoUserStyle* style = findStyle( *it, TQString() );
138 if ( style )
139 orderStyle.append( style );
140 else
141 kdWarning(32500) << "updateStyleListOrder: style " << *it << " not found!" << endl;
142 }
143 // we'll lose (and leak) styles if the list didn't have all the styles
144 Q_ASSERT( m_styleList.count() == orderStyle.count() );
145 m_styleList.clear();
146 m_styleList = orderStyle;
147}
TQString generateUniqueName() const
Generate a new unique name, to create a style whose internal name differs from the internal name of a...
~KoUserStyleCollection()
Destructor Deletes all styles.
void updateStyleListOrder(const TQStringList &list)
Reorder the styles in the collection.
KoUserStyle * findStyleByDisplayName(const TQString &displayName) const
Find style based on the display name displayName.
TQStringList displayNameList() const
Return the list composed of the display-name of each style in the collection.
KoUserStyle * findStyle(const TQString &name, const TQString &defaultStyleName) const
Find style based on the internal name name.
KoUserStyle * addStyle(KoUserStyle *sty)
Try adding sty to the collection.
void clear()
Erase all styles.
KoUserStyleCollection(const TQString &prefix)
Constructor.
void removeStyle(KoUserStyle *style)
Remove style from the collection.
Base class for any kind of style that the user can create/modify/delete/display.
Definition KoUserStyle.h:34
void setName(const TQString &name)
Set the internal name - see generateUniqueName() if needed Should be unique in a given style collecti...
Definition KoUserStyle.h:43
TQString displayName() const
The user-visible name (e.g. translated, or set by the user).
TQString name() const
The internal name (used for loading/saving, but not shown to the user) Should be unique in a given st...
Definition KoUserStyle.h:40