Scribus
Open source desktop publishing at your fingertips
styleset.h
1 
2 
3 #ifndef STYLESET_H
4 #define STYLESET_H
5 
6 #include <QList>
7 
8 #include <assert.h>
9 #include "style.h"
10 
11 
12 template<class STYLE>
13 class StyleSet : public StyleContext {
14 public:
15  STYLE& operator[] (int index) {
16  assert(index < styles.count());
17  return * styles[index];
18  }
19 
20  STYLE* getDefault(){ return m_default; }
21 
22  const STYLE& get(const QString& name) const {
23  return * dynamic_cast<const STYLE*>(resolve(name));
24  }
25 
26  const STYLE& operator[] (int index) const {
27  assert(index < styles.count());
28  return * styles[index];
29  }
30 
31  inline bool contains(const QString& name) const;
32 
33  inline int find(const QString& name) const;
34 
35  inline const Style* resolve(const QString& name) const;
36 
37  int count() const {
38  return styles.count();
39  }
40 
41  STYLE* append(STYLE* style) {
42  styles.append(style);
43  style->setContext(this);
44  return style;
45  }
46 
47  inline void remove(int index);
48 
49  inline void redefine(const StyleSet<STYLE>& defs, bool removeUnused=false);
50 
51  inline void rename(const QMap<QString,QString>& newNames);
52 
53  STYLE* create(const STYLE& proto) {
54  return append(new STYLE(proto));
55  }
56 
57  void makeDefault(STYLE* def) {
58  m_default = def;
59  if(def)
60  def->setContext(this);
61  invalidate();
62  }
63 
64  bool isDefault(const STYLE& style) const {
65  return &style == m_default;
66  }
67 
68 
69  StyleSet() : styles(), m_context(NULL), m_default(NULL) {}
70 
71  ~StyleSet() {
72  clear(false);
73  }
74 
80  void clear(bool invalid = true) {
81  while(styles.count()>0)
82  {
83  delete styles.front();
84  styles.pop_front();
85  }
86  if (invalid)
87  invalidate();
88  }
89 
90  void setContext(const StyleContext* context) {
91  bool reallyNew = m_context != context;
92  m_context = context;
93  if (reallyNew)
94  invalidate();
95  }
96 
97  const StyleContext* context() const {
98  return m_context;
99  }
100 
101 
102 private:
103  StyleSet(const StyleSet&) { assert(false); }
104  StyleSet& operator= (const StyleSet&) { assert(false); return *this; }
105 
106  QList<STYLE*> styles;
107  const StyleContext* m_context;
108  STYLE* m_default;
109 };
110 
111 template<class STYLE>
112 inline void StyleSet<STYLE>::remove(int index)
113 {
114  assert(index>=0 && index < styles.count());
115 // QList<STYLE*> it = styles.at(index);
116  if (styles.at(index) == m_default)
117  return;
118 // delete (*it);
119 // styles.erase(it);
120  styles.removeAt(index);
121 }
122 
123 template<class STYLE>
124 inline bool StyleSet<STYLE>::contains(const QString& name) const
125 {
126  for (int i=0; i < styles.count(); ++i)
127  if (styles[i]->name() == name)
128  return true;
129  return false;
130 }
131 
132 template<class STYLE>
133 inline int StyleSet<STYLE>::find(const QString& name) const
134 {
135  for (int i=0; i < styles.count(); ++i)
136  if (styles[i]->name() == name)
137  return i;
138  return -1;
139 }
140 
141 template<class STYLE>
142 inline const Style* StyleSet<STYLE>::resolve(const QString& name) const
143 {
144  if (name.isEmpty())
145  return m_default;
146  for (int i=0; i < styles.count(); ++i)
147  {
148  if (styles[i]->name() == name)
149  return styles[i];
150  }
151  return m_context ? m_context->resolve(name) : NULL;
152 }
153 
154 template<class STYLE>
155 inline void StyleSet<STYLE>::redefine(const StyleSet<STYLE>& defs, bool removeUnused)
156 {
157  for (int i=signed(styles.count())-1; i >= 0; --i)
158  {
159  bool found = false;
160  for (int j=0; j < defs.count(); ++j)
161  {
162  if (styles[i]->name() == defs[j].name())
163  {
164  found = true;
165  (*styles[i]) = defs[j];
166  (*styles[i]).setContext(this);
167  if (defs.m_default == defs.styles[j])
168  makeDefault(styles[i]);
169  break;
170  }
171  }
172  if (!found && removeUnused)
173  {
174  if (styles[i] == m_default)
175  makeDefault(NULL);
176  remove(i);
177  }
178  }
179  for (int j=0; j < defs.count(); ++j)
180  {
181  if (find(defs[j].name()) < 0)
182  {
183  STYLE* newStyle = create(defs[j]);
184  if (defs.m_default == defs.styles[j])
185  makeDefault(newStyle);
186  }
187  }
188  invalidate();
189 }
190 
191 template<class STYLE>
192 inline void StyleSet<STYLE>::rename(const QMap<QString,QString>& newNames)
193 {
194  for (int i=0; i < styles.count(); ++i)
195  {
196  QMap<QString,QString>::ConstIterator it;
197 
198  it = newNames.find(styles[i]->name());
199  if (it != newNames.end())
200  styles[i]->setName(it.value());
201 
202  it = newNames.find(styles[i]->parent());
203  if (it != newNames.end())
204  styles[i]->setParent(it.value());
205  }
206  invalidate();
207 }
208 
209 #endif
210 
211 
Definition: stylecontext.h:35
void clear(bool invalid=true)
Definition: styleset.h:80
Definition: styleset.h:13
Definition: style.h:37