Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

styleset.h

Go to the documentation of this file.
00001 
00002 
00003 #ifndef STYLESET_H
00004 #define STYLESET_H
00005 
00006 #include <assert.h>
00007 #include "style.h"
00008 //Added by qt3to4:
00009 #include <QList>
00010 
00011 
00012 template<class STYLE>
00013 class StyleSet : public StyleContext {
00014 public:
00015     STYLE& operator[] (int index) { 
00016         assert(index < styles.count()); 
00017         return * styles[index]; 
00018     }
00019     
00020     const STYLE& get(const QString& name) const { 
00021         return * dynamic_cast<const STYLE*>(resolve(name)); 
00022     }
00023     
00024     const STYLE& operator[] (int index) const { 
00025         assert(index < styles.count()); 
00026         return * styles[index]; 
00027     }
00028     
00029     inline int find(const QString& name) const;
00030 
00031     inline const Style* resolve(const QString& name) const;
00032     
00033     int count() const {
00034         return styles.count();
00035     }
00036     
00037     STYLE* append(STYLE* style) { 
00038         styles.append(style); 
00039         style->setContext(this); 
00040         return style; 
00041     }
00042     
00043     inline void remove(int index);
00044     
00045     inline void redefine(const StyleSet<STYLE>& defs, bool removeUnused=false);
00046     
00047     inline void rename(const QMap<QString,QString>& newNames);
00048     
00049     STYLE* create(const STYLE& proto) { 
00050         return append(new STYLE(proto)); 
00051     }
00052     
00053     void makeDefault(STYLE* def) { 
00054         m_default = def; 
00055         if(def) 
00056             def->setContext(this);
00057         invalidate();
00058     }
00059     
00060     bool isDefault(const STYLE& style) const {
00061         return &style == m_default;
00062     }
00063     
00064     StyleSet() : styles(), m_context(NULL), m_default(NULL) {}
00065     
00066     ~StyleSet() { 
00067         clear(); 
00068     }
00069     
00070     void clear() { 
00071         while(styles.count()>0) 
00072         { 
00073             delete styles.front(); 
00074             styles.pop_front(); 
00075         }
00076         invalidate();
00077     }
00078 
00079     void setContext(const StyleContext* context) {
00080         bool reallyNew = m_context != context;
00081         m_context = context; 
00082         if (reallyNew)
00083             invalidate();
00084     }
00085     
00086     const StyleContext* context() const { 
00087         return m_context; 
00088     }
00089     
00090 private:
00091     StyleSet(const StyleSet&)             { assert(false); }
00092     StyleSet& operator= (const StyleSet&) { assert(false); return *this; }
00093 
00094     QList<STYLE*> styles;
00095     const StyleContext* m_context;
00096     STYLE* m_default;
00097 };
00098 
00099 template<class STYLE>
00100 inline void StyleSet<STYLE>::remove(int index)
00101 {
00102     assert(index>=0 && index < styles.count()); 
00103 //  QList<STYLE*> it = styles.at(index);
00104     if (styles.at(index) == m_default)
00105         return;
00106 //  delete (*it);
00107 //  styles.erase(it);
00108     styles.removeAt(index);
00109 }
00110 
00111 template<class STYLE>
00112 inline int StyleSet<STYLE>::find(const QString& name) const
00113 {
00114     for (int i=0; i < styles.count(); ++i)
00115         if (styles[i]->name() == name)
00116             return i;
00117     return -1;
00118 }
00119 
00120 template<class STYLE>
00121 inline const Style* StyleSet<STYLE>::resolve(const QString& name) const
00122 {
00123     if (name.isEmpty())
00124         return m_default;
00125     for (int i=0; i < styles.count(); ++i)
00126     {
00127         if (styles[i]->name() == name)
00128             return styles[i];
00129     }
00130     return m_context ? m_context->resolve(name) : NULL;
00131 }
00132 
00133 template<class STYLE>
00134 inline void StyleSet<STYLE>::redefine(const StyleSet<STYLE>& defs, bool removeUnused)
00135 {
00136     for (int i=signed(styles.count())-1; i >= 0; --i) 
00137     {
00138         bool found = false;
00139         for (int j=0; j < defs.count(); ++j)
00140         {
00141             if (styles[i]->name() == defs[j].name()) 
00142             {
00143                 found = true;
00144                 (*styles[i]) = defs[j];
00145                 (*styles[i]).setContext(this);
00146                 if (defs.m_default == defs.styles[j])
00147                     makeDefault(styles[i]);
00148                 break;
00149             }
00150         }
00151         if (!found && removeUnused) 
00152         {
00153             if (styles[i] == m_default)
00154                 makeDefault(NULL);
00155             remove(i);
00156         }
00157     }
00158     for (int j=0; j < defs.count(); ++j)
00159     {
00160         if (find(defs[j].name()) < 0) 
00161         {
00162             STYLE* newStyle = create(defs[j]);
00163             if (defs.m_default == defs.styles[j])
00164                 makeDefault(newStyle);
00165         }
00166     }
00167     invalidate();
00168 }
00169 
00170 template<class STYLE>
00171 inline void StyleSet<STYLE>::rename(const QMap<QString,QString>& newNames)
00172 {
00173     for (int i=0; i < styles.count(); ++i)
00174     { 
00175         QMap<QString,QString>::ConstIterator it;
00176         
00177         it = newNames.find(styles[i]->name());
00178         if (it != newNames.end())
00179             styles[i]->setName(it.value());
00180     
00181         it = newNames.find(styles[i]->parent());
00182         if (it != newNames.end())
00183             styles[i]->setParent(it.value());
00184     }
00185     invalidate();
00186 }
00187     
00188 #endif
00189 
00190 
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.