~ubuntu-branches/ubuntu/hardy/qgis/hardy

« back to all changes in this revision

Viewing changes to src/core/qgsprojectproperty.h

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                                  qgsproject.h
 
3
 
 
4
                      Implements persistent project state.
 
5
 
 
6
                              -------------------
 
7
  begin                : February 24, 2005
 
8
  copyright            : (C) 2005 by Mark Coletti
 
9
  email                : mcoletti at gmail.com
 
10
***************************************************************************/
 
11
 
 
12
/***************************************************************************
 
13
 *                                                                         *
 
14
 *   This program is free software; you can redistribute it and/or modify  *
 
15
 *   it under the terms of the GNU General Public License as published by  *
 
16
 *   the Free Software Foundation; either version 2 of the License, or     *
 
17
 *   (at your option) any later version.                                   *
 
18
 *                                                                         *
 
19
 ***************************************************************************/
 
20
 
 
21
/* $Id: qgsprojectproperty.h 4502 2006-01-08 01:18:20Z timlinux $ */
 
22
 
 
23
#ifndef QGSPROJECTPROPERTY_H
 
24
#define QGSPROJECTPROPERTY_H
 
25
 
 
26
#include <qvariant.h>
 
27
#include <qstring.h>
 
28
#include <q3dict.h>
 
29
 
 
30
class QDomNode;
 
31
class QDomElement;
 
32
class QDomDocument;
 
33
class QStringList;
 
34
 
 
35
 
 
36
/**
 
37
   ABC for property hierarchies
 
38
 
 
39
   Each sub-class is either a QgsPropertyKey or QgsPropertyValue.  QgsPropertyKeys can
 
40
   contain either QgsPropertyKeys or QgsPropertyValues, thus describing an
 
41
   hierarchy.  QgsPropertyValues are always graph leaves.
 
42
 
 
43
   @note 
 
44
 
 
45
   This is really a hidden QgsProject implementation class.  It was getting
 
46
   too large and unwieldy, so it's broken out here into separate files.
 
47
 
 
48
*/
 
49
class QgsProperty
 
50
{
 
51
public:
 
52
 
 
53
    QgsProperty()
 
54
    {}
 
55
    
 
56
    virtual ~ QgsProperty()
 
57
    {}
 
58
 
 
59
    /** dumps out the keys and values 
 
60
 
 
61
    @param tabs is number of tabs to print; used for pretty-printing
 
62
    hierarchy
 
63
    */
 
64
    virtual void dump( size_t tabs = 0 ) const = 0;
 
65
    
 
66
    /** returns true if is a QgsPropertyKey */
 
67
    virtual bool isKey() const = 0;
 
68
 
 
69
    /** returns true if is a QgsPropertyValue */
 
70
    virtual bool isValue() const = 0;
 
71
 
 
72
    /** returns true if a leaf node 
 
73
 
 
74
    A leaf node is a key node that has either no value or a single value.
 
75
    A non-leaf node would be a key node with key sub-nodes.
 
76
 
 
77
    This is used for entryList() and subkeyList() implementation.
 
78
    */
 
79
    virtual bool isLeaf() const = 0;
 
80
 
 
81
    /**
 
82
       restores property hierarchy to given DOM node
 
83
 
 
84
       Used for restoring properties from project file
 
85
    */
 
86
    virtual bool readXML(QDomNode & keyNode) = 0;
 
87
 
 
88
    /**
 
89
       adds property hierarchy to given DOM node
 
90
 
 
91
       Used for saving properties to project file.
 
92
 
 
93
       @param nodeName the tag name associated with this element
 
94
       @param node     the parent (or encompassing) property node
 
95
       @param documetn the overall project file DOM document
 
96
    */
 
97
    virtual bool writeXML(QString const & nodeName, 
 
98
                          QDomElement   & element,
 
99
                          QDomDocument  & document) = 0;
 
100
 
 
101
    /** return the node's value
 
102
 
 
103
       For QgsPropertyValue nodes, this is straightforward -- just return the
 
104
       embedded QVariant, _value.  For QgsPropertyKey, this means returning
 
105
       the QgsPropertyValue _value that is keyed by its name, if it exists;
 
106
       i.e., QgsPropertyKey "foo" will return the property value mapped to its
 
107
       name, "foo", in its QDict of QProperties.
 
108
 
 
109
     */
 
110
    virtual QVariant value() const = 0;
 
111
 
 
112
}; // class QgsProperty
 
113
 
 
114
 
 
115
 
 
116
 
 
117
/** QgsPropertyValue node
 
118
 
 
119
Contains a QgsPropertyKey's value
 
120
*/
 
121
class QgsPropertyValue : public QgsProperty
 
122
{
 
123
public:
 
124
    QgsPropertyValue()
 
125
    {} 
 
126
 
 
127
    QgsPropertyValue(QVariant const &value)
 
128
        : value_(value)
 
129
    {}
 
130
 
 
131
    virtual ~ QgsPropertyValue()
 
132
    {}
 
133
 
 
134
    /** returns true if is a QgsPropertyKey */
 
135
    virtual bool isKey() const
 
136
    { return false; }
 
137
 
 
138
    /** returns true if is a QgsPropertyValue */ 
 
139
    virtual bool isValue() const
 
140
    { return true; }
 
141
 
 
142
    QVariant value() const
 
143
    { return value_; }
 
144
 
 
145
    /** returns true if is a leaf node
 
146
 
 
147
    @note I suppose, in a way, value nodes can also be qualified as leaf
 
148
    nodes even though we're only counting key nodes.
 
149
    */ 
 
150
    bool isLeaf() const
 
151
    { return true; }
 
152
 
 
153
    void dump( size_t tabs = 0) const;
 
154
 
 
155
    bool readXML(QDomNode & keyNode);
 
156
 
 
157
    bool writeXML( QString const & nodeName, 
 
158
                   QDomElement   & element,
 
159
                   QDomDocument  & document );
 
160
 
 
161
    size_t count() const
 
162
    { return 0; }
 
163
 
 
164
 
 
165
    /** return keys that do not contain other keys
 
166
 
 
167
    Since QgsPropertyValue isn't a key, don't do anything.
 
168
    */
 
169
    void entryList( QStringList & keyName, QStringList & entries ) const
 
170
    { /* NOP */ }
 
171
 
 
172
private:
 
173
 
 
174
    /** We use QVariant as it's very handy to keep multiple types and provides
 
175
        type conversions
 
176
    */
 
177
    QVariant value_;
 
178
    
 
179
}; // class QgsPropertyValue
 
180
 
 
181
 
 
182
 
 
183
 
 
184
/**
 
185
   QgsPropertyKey node
 
186
 
 
187
   Can, itself, contain QgsPropertyKeys and QgsPropertyValues.
 
188
 
 
189
   The internal QDict, properties_, maps key names to their respective
 
190
   QgsPropertyValue or next QgsPropertyKey in the key name sequence.  The key with
 
191
   the current name should contain its QgsPropertyValue.
 
192
 
 
193
   E.g., given the key sequence "/foo/bar", "foo" will have a corresponding
 
194
   QgsPropertyKey with a name "foo".  It will contain an element in its
 
195
   properties_ that maps to "bar", which is another QgsPropertyKey.  The "bar"
 
196
   QgsPropertyKey will, in turn, have an element that maps to itself, i.e. "bar",
 
197
   that will contain a QgsPropertyValue.
 
198
 
 
199
*/
 
200
class QgsPropertyKey : public QgsProperty
 
201
{
 
202
public:
 
203
 
 
204
    QgsPropertyKey( QString const name = "" );
 
205
 
 
206
    virtual ~ QgsPropertyKey()
 
207
    { }
 
208
 
 
209
    /// every key has a name
 
210
    // @{
 
211
    QString const & name() const
 
212
    { return name_; }
 
213
 
 
214
    QString & name()
 
215
    { return name_; }
 
216
    // @}
 
217
 
 
218
 
 
219
    /** if this key has a value, it will be stored by its name in its
 
220
     * properties
 
221
     */
 
222
    QVariant value() const;
 
223
 
 
224
 
 
225
    /// add the given property key
 
226
    QgsPropertyKey * addKey( QString const & keyName )
 
227
    {
 
228
        properties_.replace( keyName, new QgsPropertyKey(keyName) );
 
229
 
 
230
        return dynamic_cast<QgsPropertyKey*>(properties_.find( keyName ));
 
231
    }
 
232
 
 
233
 
 
234
    /// remove the given key
 
235
    bool removeKey( QString const & keyName )
 
236
    {
 
237
        return properties_.remove( keyName );
 
238
    }
 
239
 
 
240
    /** set the value associated with this key
 
241
        @param name is the key name
 
242
    */
 
243
    QgsPropertyValue * setValue( QString const & name, QVariant const & value )
 
244
    {
 
245
        properties_.replace( name, new QgsPropertyValue( value ) );
 
246
 
 
247
        return dynamic_cast<QgsPropertyValue*>(properties_.find( name ));
 
248
    }
 
249
 
 
250
    /** set the value associated with this key
 
251
 
 
252
    @note that the single value node associated with each key is always
 
253
    stored keyed by the current key name
 
254
    */
 
255
    QgsPropertyValue * setValue( QVariant const & value )
 
256
    {
 
257
        return setValue( name(), value );
 
258
    }
 
259
 
 
260
 
 
261
 
 
262
    void dump( size_t tabs = 0 ) const;
 
263
 
 
264
    bool readXML(QDomNode & keyNode);
 
265
 
 
266
    bool writeXML(QString const &nodeName, QDomElement & element, QDomDocument & document);
 
267
 
 
268
    /// how many elements are contained within this one?
 
269
    size_t count() const
 
270
    { return properties_.count(); }
 
271
 
 
272
    /// Does this property not have any subkeys or values?
 
273
    /* virtual */ bool isEmpty() const 
 
274
    { return properties_.isEmpty(); }
 
275
 
 
276
    /** returns true if is a QgsPropertyKey */ 
 
277
    virtual bool isKey() const
 
278
    { return true; }
 
279
 
 
280
    /** returns true if is a QgsPropertyValue */ 
 
281
    virtual bool isValue() const
 
282
    { return false; }
 
283
 
 
284
    /// return keys that do not contain other keys
 
285
    void entryList( QStringList & entries ) const;
 
286
 
 
287
    /// return keys that contain other keys
 
288
    void subkeyList(QStringList & entries) const;
 
289
 
 
290
    /** returns true if a leaf node 
 
291
 
 
292
    A leaf node is a key node that has either no value or a single value.  A
 
293
    non-leaf node would be a key node with key sub-nodes.
 
294
    */
 
295
    bool isLeaf() const;
 
296
 
 
297
    /// reset the QgsProperty key to prestine state
 
298
    virtual void clear()
 
299
    {
 
300
        name_ = "";
 
301
        properties_.clear();
 
302
    }
 
303
 
 
304
    /// delete any sub-nodes
 
305
    virtual void clearKeys()
 
306
    {
 
307
        properties_.clear();
 
308
    }
 
309
 
 
310
    QgsProperty * find( QString & propertyName ) 
 
311
    {
 
312
        return properties_.find( propertyName );
 
313
    }
 
314
 
 
315
private:
 
316
 
 
317
    /// every key has a name
 
318
    QString name_;
 
319
 
 
320
    /// sub-keys
 
321
    Q3Dict < QgsProperty > properties_;
 
322
 
 
323
}; // class QgsPropertyKey
 
324
 
 
325
 
 
326
 
 
327
 
 
328
 
 
329
#endif