summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoUserStyleCollection.h
blob: e9a771a97ff99b4a1eb02c2460e60edb47133c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* This file is part of the KDE project
   Copyright (C) 2005 David Faure <faure@kde.org>

   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.
*/

#ifndef KOUSERSTYLECOLLECTION_H
#define KOUSERSTYLECOLLECTION_H

class KoUserStyle;
#include <koffice_export.h>
#include <tqstringlist.h>

/**
 * Generic style collection class, for all "user styles" (see KoUserStyle).
 * To use this for a particular kind of style, derive from KoUserStyle (to add the properties)
 * and derive from KoUserStyleCollection (to add loading, saving, as well as
 * re-defined findStyle and addStyle in order to cast to the correct style class).
 */
class KOTEXT_EXPORT KoUserStyleCollection
{
public:
    /**
     * Constructor
     * @param prefix used by generateUniqueName to prefix new style names
     * (to avoid clashes between different kinds of styles)
     */
    KoUserStyleCollection( const TQString& prefix );

    /**
     * Destructor
     * Deletes all styles.
     */
    ~KoUserStyleCollection();

    /**
     * Erase all styles
     */
    void clear();

    /**
     * @return true if the collection is empty
     */
    bool isEmpty() const { return m_styleList.isEmpty(); }
    /**
     * @return the number of items in the collection
     */
    int count() const { return m_styleList.count(); }
    /**
     * @return the index of @p style in the collection
     */
    int indexOf( KoUserStyle* style ) const { return m_styleList.findIndex( style ); }

    /**
     * Return the list of all styles in the collection.
     */
    TQValueList<KoUserStyle *> styleList() const { return m_styleList; }

    /**
     * Generate a new unique name, to create a style whose internal name
     * differs from the internal name of all existing styles.
     * The prefix passed to the constructor is used here.
     */
    TQString generateUniqueName() const;

    /**
     * Return the list composed of the display-name of each style in the collection
     */
    TQStringList displayNameList() const;

    /**
     * Find style based on the internal name @p name.
     * If the style with that name can't be found, then<br>
     * 1) if @p name equals @p defaultStyleName, return the first one, never 0<br>
     * 2) otherwise return 0
     */
    KoUserStyle* findStyle( const TQString & name, const TQString& defaultStyleName ) const;

    /**
     * Find style based on the display name @p displayName.
     * There could be 0, 1 or more than 1 style with that name,
     * the method simply returns the first one found, or 0 if none was found.
     * This is mostly useful to detect similar styles when importing styles
     * from another document.
     */
    KoUserStyle* findStyleByDisplayName( const TQString& displayName ) const;

    /**
     * Remove @p style from the collection. If the style isn't in the collection, nothing happens.
     * The style mustn't be deleted yet; it is stored into a list of styles to delete in clear().
     */
    void removeStyle( KoUserStyle *style );

    /**
     * Reorder the styles in the collection.
     * @param list the list of internal names of the styles
     * WARNING, if an existing style isn't listed, it will be lost
     */
    void updateStyleListOrder( const TQStringList& list );

    /**
     * Try adding @p sty to the collection.
     * From the moment of this call, the collection owns the style.
     *
     * Either this succeeds, and @p sty is returned, or a style with the exact same
     * internal name and display name is present already, in which case the existing style
     * is updated, @p sty is deleted, and the existing style is returned.
     *
     * WARNING: @p sty can be deleted; use the returned value for any further processing.
     */
    KoUserStyle* addStyle( KoUserStyle* sty );

    /**
     * @return true if this collection only holds the default styles provided by the application
     * When true, those styles don't need to be saved.
     */
    bool isDefault() const { return m_default; }
    /**
     * Set whether this collection only holds the default styles provided by the application
     */
    void setDefault( bool d ) { m_default = d; }

protected:
    KoUserStyleCollection( const KoUserStyleCollection& rhs ); ///< forbidden
    void operator=( const KoUserStyleCollection& rhs ); ///< forbidden

    TQValueList<KoUserStyle *> m_styleList;

private:
    TQValueList<KoUserStyle *> m_deletedStyles;
    const TQString m_prefix;
    // can become d pointer if needed
    mutable KoUserStyle *m_lastStyle; ///< Last style that was searched
    bool m_default;
    bool m_unused1;
    bool m_unused2;
    bool m_unused3;
};


#endif /* KOUSERSTYLECOLLECTION_H */