Rev 22724 | Rev 23240 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5981 | avox | 1 | |
2 | |||
3 | #ifndef STYLESET_H |
||
4 | #define STYLESET_H |
||
5 | |||
13650 | cbradney | 6 | #include <QList> |
7 | |||
5990 | jghali | 8 | #include <assert.h> |
6733 | avox | 9 | #include "style.h" |
5981 | avox | 10 | |
7059 | avox | 11 | |
5981 | avox | 12 | template<class STYLE> |
22724 | jghali | 13 | class StyleSet : public StyleContext |
14 | { |
||
5981 | avox | 15 | public: |
22930 | jghali | 16 | STYLE* getDefault() { return m_default; } |
17 | const STYLE* getDefault() const { return m_default; } |
||
7059 | avox | 18 | |
22724 | jghali | 19 | const STYLE& get(const QString& name) const |
20 | { |
||
7059 | avox | 21 | return * dynamic_cast<const STYLE*>(resolve(name)); |
22 | } |
||
23 | |||
22930 | jghali | 24 | STYLE& operator[] (int index) |
25 | { |
||
26 | assert(index < styles.count()); |
||
27 | return *styles[index]; |
||
28 | } |
||
29 | |||
22724 | jghali | 30 | const STYLE& operator[] (int index) const |
31 | { |
||
7059 | avox | 32 | assert(index < styles.count()); |
33 | return * styles[index]; |
||
34 | } |
||
35 | |||
19089 | jghali | 36 | inline bool contains(const QString& name) const; |
37 | |||
7001 | avox | 38 | inline int find(const QString& name) const; |
7059 | avox | 39 | |
21816 | craig | 40 | inline const BaseStyle* resolve(const QString& name) const; |
7059 | avox | 41 | |
22724 | jghali | 42 | int count() const |
43 | { |
||
10400 | subik | 44 | return styles.count(); |
7059 | avox | 45 | } |
46 | |||
22724 | jghali | 47 | STYLE* append(STYLE* style) |
48 | { |
||
7059 | avox | 49 | styles.append(style); |
8134 | avox | 50 | style->setContext(this); |
7059 | avox | 51 | return style; |
52 | } |
||
53 | |||
8638 | cbradney | 54 | inline void remove(int index); |
7059 | avox | 55 | |
6953 | avox | 56 | inline void redefine(const StyleSet<STYLE>& defs, bool removeUnused=false); |
7059 | avox | 57 | |
8455 | avox | 58 | inline void rename(const QMap<QString,QString>& newNames); |
59 | |||
22724 | jghali | 60 | STYLE* create(const STYLE& proto) |
61 | { |
||
7059 | avox | 62 | return append(new STYLE(proto)); |
63 | } |
||
64 | |||
22724 | jghali | 65 | void makeDefault(STYLE* def) |
66 | { |
||
7059 | avox | 67 | m_default = def; |
22721 | jghali | 68 | if (def) |
8134 | avox | 69 | def->setContext(this); |
7942 | avox | 70 | invalidate(); |
7059 | avox | 71 | } |
72 | |||
22724 | jghali | 73 | bool isDefault(const STYLE& style) const |
74 | { |
||
7193 | avox | 75 | return &style == m_default; |
76 | } |
||
77 | |||
12549 | pierre | 78 | |
8134 | avox | 79 | StyleSet() : styles(), m_context(NULL), m_default(NULL) {} |
7059 | avox | 80 | |
7942 | avox | 81 | ~StyleSet() { |
16856 | craig | 82 | clear(false); |
7942 | avox | 83 | } |
7059 | avox | 84 | |
16856 | craig | 85 | /** |
86 | * Deletes all styles in the style set. |
||
87 | * |
||
88 | * If @a invalid is <code>true</code>, this function also invalidates the style set. |
||
89 | */ |
||
22724 | jghali | 90 | void clear(bool invalid = true) |
91 | { |
||
92 | while (styles.count() > 0) |
||
7059 | avox | 93 | { |
94 | delete styles.front(); |
||
95 | styles.pop_front(); |
||
7942 | avox | 96 | } |
16856 | craig | 97 | if (invalid) |
98 | invalidate(); |
||
7059 | avox | 99 | } |
6733 | avox | 100 | |
22724 | jghali | 101 | void setContext(const StyleContext* context) |
102 | { |
||
8134 | avox | 103 | bool reallyNew = m_context != context; |
104 | m_context = context; |
||
7945 | avox | 105 | if (reallyNew) |
106 | invalidate(); |
||
7059 | avox | 107 | } |
6733 | avox | 108 | |
22724 | jghali | 109 | const StyleContext* context() const |
110 | { |
||
8134 | avox | 111 | return m_context; |
7059 | avox | 112 | } |
113 | |||
12549 | pierre | 114 | |
5981 | avox | 115 | private: |
7059 | avox | 116 | StyleSet(const StyleSet&) { assert(false); } |
117 | StyleSet& operator= (const StyleSet&) { assert(false); return *this; } |
||
118 | |||
9803 | fschmid | 119 | QList<STYLE*> styles; |
8134 | avox | 120 | const StyleContext* m_context; |
7018 | avox | 121 | STYLE* m_default; |
5981 | avox | 122 | }; |
123 | |||
124 | template<class STYLE> |
||
8638 | cbradney | 125 | inline void StyleSet<STYLE>::remove(int index) |
5981 | avox | 126 | { |
8638 | cbradney | 127 | assert(index>=0 && index < styles.count()); |
9803 | fschmid | 128 | // QList<STYLE*> it = styles.at(index); |
129 | if (styles.at(index) == m_default) |
||
7183 | avox | 130 | return; |
9803 | fschmid | 131 | // delete (*it); |
132 | // styles.erase(it); |
||
133 | styles.removeAt(index); |
||
5981 | avox | 134 | } |
135 | |||
136 | template<class STYLE> |
||
19089 | jghali | 137 | inline bool StyleSet<STYLE>::contains(const QString& name) const |
138 | { |
||
139 | for (int i=0; i < styles.count(); ++i) |
||
140 | if (styles[i]->name() == name) |
||
141 | return true; |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | template<class STYLE> |
||
7001 | avox | 146 | inline int StyleSet<STYLE>::find(const QString& name) const |
5981 | avox | 147 | { |
8546 | cbradney | 148 | for (int i=0; i < styles.count(); ++i) |
5981 | avox | 149 | if (styles[i]->name() == name) |
150 | return i; |
||
151 | return -1; |
||
152 | } |
||
153 | |||
154 | template<class STYLE> |
||
21816 | craig | 155 | inline const BaseStyle* StyleSet<STYLE>::resolve(const QString& name) const |
6733 | avox | 156 | { |
7018 | avox | 157 | if (name.isEmpty()) |
158 | return m_default; |
||
8523 | cbradney | 159 | for (int i=0; i < styles.count(); ++i) |
7059 | avox | 160 | { |
6733 | avox | 161 | if (styles[i]->name() == name) |
162 | return styles[i]; |
||
7059 | avox | 163 | } |
8134 | avox | 164 | return m_context ? m_context->resolve(name) : NULL; |
6733 | avox | 165 | } |
166 | |||
167 | template<class STYLE> |
||
6953 | avox | 168 | inline void StyleSet<STYLE>::redefine(const StyleSet<STYLE>& defs, bool removeUnused) |
5981 | avox | 169 | { |
7059 | avox | 170 | for (int i=signed(styles.count())-1; i >= 0; --i) |
171 | { |
||
5981 | avox | 172 | bool found = false; |
10400 | subik | 173 | for (int j=0; j < defs.count(); ++j) |
7059 | avox | 174 | { |
175 | if (styles[i]->name() == defs[j].name()) |
||
176 | { |
||
5981 | avox | 177 | found = true; |
178 | (*styles[i]) = defs[j]; |
||
8134 | avox | 179 | (*styles[i]).setContext(this); |
7018 | avox | 180 | if (defs.m_default == defs.styles[j]) |
181 | makeDefault(styles[i]); |
||
5981 | avox | 182 | break; |
183 | } |
||
184 | } |
||
7059 | avox | 185 | if (!found && removeUnused) |
186 | { |
||
187 | if (styles[i] == m_default) |
||
188 | makeDefault(NULL); |
||
5981 | avox | 189 | remove(i); |
190 | } |
||
191 | } |
||
10400 | subik | 192 | for (int j=0; j < defs.count(); ++j) |
7059 | avox | 193 | { |
194 | if (find(defs[j].name()) < 0) |
||
195 | { |
||
7018 | avox | 196 | STYLE* newStyle = create(defs[j]); |
197 | if (defs.m_default == defs.styles[j]) |
||
198 | makeDefault(newStyle); |
||
5981 | avox | 199 | } |
200 | } |
||
6865 | avox | 201 | invalidate(); |
5981 | avox | 202 | } |
203 | |||
8455 | avox | 204 | template<class STYLE> |
205 | inline void StyleSet<STYLE>::rename(const QMap<QString,QString>& newNames) |
||
206 | { |
||
8643 | cbradney | 207 | for (int i=0; i < styles.count(); ++i) |
8455 | avox | 208 | { |
209 | QMap<QString,QString>::ConstIterator it; |
||
210 | |||
211 | it = newNames.find(styles[i]->name()); |
||
212 | if (it != newNames.end()) |
||
10394 | cbradney | 213 | styles[i]->setName(it.value()); |
8455 | avox | 214 | |
215 | it = newNames.find(styles[i]->parent()); |
||
216 | if (it != newNames.end()) |
||
10394 | cbradney | 217 | styles[i]->setParent(it.value()); |
8455 | avox | 218 | } |
219 | invalidate(); |
||
220 | } |
||
221 | |||
5984 | avox | 222 | #endif |
223 | |||
224 |