QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
qgscptcityarchive.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscptcityarchive.h
3  ---------------------
4  begin : August 2012
5  copyright : (C) 2009 by Martin Dobias
6  copyright : (C) 2012 by Etienne Tourigny
7  email : etourigny.dev at gmail.com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSCPTCITYARCHIVE_H
18 #define QGSCPTCITYARCHIVE_H
19 
20 #include "qgsvectorcolorrampv2.h"
21 
22 #include <QAbstractItemModel>
23 #include <QIcon>
24 #include <QMimeData>
25 #include <QAction>
26 
28 class QgsCptCityDataItem;
30 
31 #define DEFAULT_CPTCITY_ARCHIVE "cpt-city-qgis-min"
32 
33 class CORE_EXPORT QgsCptCityArchive
34 {
35  public:
36  QgsCptCityArchive( QString archiveName = DEFAULT_CPTCITY_ARCHIVE,
37  QString baseDir = QString() );
39 
40  // basic dir info
41  QString baseDir() const;
42  static QString baseDir( QString archiveName );
43  static QString defaultBaseDir();
44  void setBaseDir( QString dirName ) { mBaseDir = dirName; }
45 
46  // collection + selection info
47  QString copyingFileName( const QString& dirName ) const;
48  QString descFileName( const QString& dirName ) const;
49  static QString findFileName( const QString & target, const QString & startDir, const QString & baseDir );
50  static QMap< QString, QString > copyingInfo( const QString& fileName );
51  static QMap< QString, QString > description( const QString& fileName );
53  static QMap< double, QPair<QColor, QColor> > gradientColorMap( const QString& fileName );
54 
55  // archive management
56  bool isEmpty();
57  QString archiveName() const { return mArchiveName; }
58  static void initArchives( bool loadAll = false );
59  static void initArchive( QString archiveName, QString archiveBaseDir );
60  static void initDefaultArchive();
61  static void clearArchives();
62  static QgsCptCityArchive* defaultArchive();
63  static QMap< QString, QgsCptCityArchive* > archiveRegistry();
64 
65  // items
66  QVector< QgsCptCityDataItem* > rootItems() const { return mRootItems; }
67  QVector< QgsCptCityDataItem* > selectionItems() const { return mSelectionItems; }
68 
69  protected:
70 
71  QString mArchiveName;
72  QString mBaseDir;
73  static QString mDefaultArchiveName;
74  static QMap< QString, QgsCptCityArchive* > mArchiveRegistry;
75  // root items, namely directories at root of archive
76  QVector< QgsCptCityDataItem* > mRootItems;
77  QVector<QgsCptCityDataItem*> mSelectionItems;
78  // mapping of copyinginfo, key is fileName
79  static QMap< QString, QMap< QString, QString > > mCopyingInfoMap;
80 
81 };
82 
84 class CORE_EXPORT QgsCptCityDataItem : public QObject
85 {
86  Q_OBJECT
87  public:
88  enum Type
89  {
94  AllRamps
95  };
96 
98  QString name, QString path );
99  virtual ~QgsCptCityDataItem();
100 
101  bool hasChildren();
102 
103  int rowCount();
104  // retrieve total count of "leaf" items (all children which are end nodes)
105  virtual int leafCount() const;
106 
107  //
108 
109  virtual void refresh();
110 
111  // Create vector of children
112  virtual QVector<QgsCptCityDataItem*> createChildren();
113 
114  // Populate children using children vector created by createChildren()
115  virtual void populate();
116  bool isPopulated() { return mPopulated; }
117 
118  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
119  // refresh - refresh populated item, emit signals to model
120  virtual void addChildItem( QgsCptCityDataItem * child, bool refresh = false );
121 
122  // remove and delete child item, signals to browser are emitted
123  virtual void deleteChildItem( QgsCptCityDataItem * child );
124 
125  // remove child item but don't delete it, signals to browser are emitted
126  // returns pointer to the removed item or null if no such item was found
127  virtual QgsCptCityDataItem * removeChildItem( QgsCptCityDataItem * child );
128 
129  virtual bool equal( const QgsCptCityDataItem *other );
130 
131  virtual QWidget *paramWidget() { return 0; }
132 
133  // list of actions provided by this item - usually used for popup menu on right-click
134  virtual QList<QAction*> actions() { return QList<QAction*>(); }
135 
136  // whether accepts drag&drop'd layers - e.g. for import
137  virtual bool acceptDrop() { return false; }
138 
139  // try to process the data dropped on this item
140  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
141 
142  // static methods
143 
144  // Find child index in vector of items using '==' operator
145  static int findItem( QVector<QgsCptCityDataItem*> items, QgsCptCityDataItem * item );
146 
147  // members
148 
149  Type type() const { return mType; }
150  QgsCptCityDataItem* parent() const { return mParent; }
151  void setParent( QgsCptCityDataItem* parent ) { mParent = parent; }
152  QVector<QgsCptCityDataItem*> children() const { return mChildren; }
153  virtual QIcon icon() { return mIcon; }
154  virtual QIcon icon( const QSize& size ) { Q_UNUSED( size ) ; return icon(); }
155  QString name() const { return mName; }
156  QString path() const { return mPath; }
157  QString info() const { return mInfo; }
158  QString shortInfo() const { return mShortInfo; }
159 
160  void setIcon( QIcon icon ) { mIcon = icon; }
161 
162  void setToolTip( QString msg ) { mToolTip = msg; }
163  QString toolTip() const { return mToolTip; }
164 
165  bool isValid() { return mValid; }
166 
167  protected:
168 
171  QVector<QgsCptCityDataItem*> mChildren; // easier to have it always
173  QString mName;
174  QString mPath; // it is also used to identify item in tree
175  QString mInfo;
176  QString mShortInfo;
177  QString mToolTip;
178  QIcon mIcon;
179  bool mValid;
180 
181  public slots:
182  void emitBeginInsertItems( QgsCptCityDataItem* parent, int first, int last );
183  void emitEndInsertItems();
184  void emitBeginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
185  void emitEndRemoveItems();
186 
187  signals:
188  void beginInsertItems( QgsCptCityDataItem* parent, int first, int last );
189  void endInsertItems();
190  void beginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
191  void endRemoveItems();
192 };
193 
195 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
196 {
197  Q_OBJECT
198  public:
200  QString name, QString path,
201  QString variantName = QString(),
202  bool initialize = false );
204  QString name, QString path,
205  QStringList variantList,
206  bool initialize = false );
208 
209  // --- reimplemented from QgsCptCityDataItem ---
210 
211  virtual bool equal( const QgsCptCityDataItem *other );
212  virtual int leafCount() const { return 1; }
213 
214  // --- New virtual methods for layer item derived classes ---
215  const QgsCptCityColorRampV2& ramp() const { return mRamp; }
216  QIcon icon();
217  QIcon icon( const QSize& size );
218  void init();
219 
220  protected:
221 
224  QList< QIcon > mIcons;
225 };
226 
227 
230 {
231  Q_OBJECT
232  public:
234  QString name, QString path );
236 
237  void setPopulated() { mPopulated = true; }
238  void addChild( QgsCptCityDataItem *item ) { mChildren.append( item ); }
239  QVector<QgsCptCityDataItem*> childrenRamps( bool recursive );
240 
241  protected:
243 };
244 
247 {
248  Q_OBJECT
249  public:
251  QString name, QString path );
253 
254  QVector<QgsCptCityDataItem*> createChildren();
255 
256  virtual bool equal( const QgsCptCityDataItem *other );
257 
258  static QgsCptCityDataItem* dataItem( QgsCptCityDataItem* parent,
259  QString name, QString path );
260 
261  protected:
262  QMap< QString, QStringList > rampsMap();
263  QStringList dirEntries() const;
264  QMap< QString, QStringList > mRampsMap;
265 };
266 
269 {
270  Q_OBJECT
271  public:
272  QgsCptCitySelectionItem( QgsCptCityDataItem* parent, QString name, QString path );
274 
275  QVector<QgsCptCityDataItem*> createChildren();
276 
277  virtual bool equal( const QgsCptCityDataItem *other );
278 
279  QStringList selectionsList() const { return mSelectionsList; }
280 
281  protected:
282  void parseXML();
283  QStringList mSelectionsList;
284 };
285 
288 {
289  Q_OBJECT
290  public:
291  QgsCptCityAllRampsItem( QgsCptCityDataItem* parent, QString name,
292  QVector<QgsCptCityDataItem*> items );
294 
295  QVector<QgsCptCityDataItem*> createChildren();
296 
297  protected:
298  QVector<QgsCptCityDataItem*> mItems;
299 };
300 
301 
302 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
303 {
304  Q_OBJECT
305 
306  public:
307 
308  enum ViewType
309  {
310  Authors = 0,
311  Selections = 1,
312  List = 2 // not used anymore
313  };
314 
315  QgsCptCityBrowserModel( QObject* parent = 0,
317  ViewType Type = Authors );
319 
320  // implemented methods from QAbstractItemModel for read-only access
321 
324  virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
325 
330  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
331 
334  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
335 
337  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
338 
341  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const;
342 
344  virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
345 
346  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = 0 ) const;
347 
351  virtual QModelIndex parent( const QModelIndex &index ) const;
352 
354  /* virtual QStringList mimeTypes() const; */
355 
357  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
358 
360  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
361 
362  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
363 
364  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const;
365 
366  // Reload the whole model
367  void reload();
368 
369  // Refresh item specified by path
370  void refresh( QString path );
371 
372  // Refresh item childs
373  void refresh( const QModelIndex &index = QModelIndex() );
374 
376  QModelIndex findPath( QString path );
377 
378  void connectItem( QgsCptCityDataItem *item );
379 
380  bool canFetchMore( const QModelIndex & parent ) const;
381  void fetchMore( const QModelIndex & parent );
382 
383  signals:
384 
385  public slots:
386  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
387  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
388  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
389 
390  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
391  void endInsertItems();
392  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
393  void endRemoveItems();
394 
395  protected:
396 
397  // populates the model
398  void addRootItems( );
399  void removeRootItems();
400 
401  QVector<QgsCptCityDataItem*> mRootItems;
404  QSize mIconSize;
405 };
406 
407 #endif
QVector< QgsCptCityDataItem * > children() const
Definition: qgscptcityarchive.h:152
ViewType mViewType
Definition: qgscptcityarchive.h:403
static unsigned index
Definition: mersenne-twister.cpp:36
Definition: qgscptcityarchive.h:93
QgsCptCityDataItem * mParent
Definition: qgscptcityarchive.h:170
QString toolTip() const
Definition: qgscptcityarchive.h:163
Definition: qgscptcityarchive.h:91
Definition: qgsvectorcolorrampv2.h:228
QVector< QgsCptCityDataItem * > mItems
Definition: qgscptcityarchive.h:298
An "All ramps item", which contains all items in a flat hierarchy.
Definition: qgscptcityarchive.h:287
void setIcon(QIcon icon)
Definition: qgscptcityarchive.h:160
QList< QIcon > mIcons
Definition: qgscptcityarchive.h:224
static QgsCptCityArchive * defaultArchive()
Definition: qgscptcityarchive.cpp:413
QString mArchiveName
Definition: qgscptcityarchive.h:71
QString mBaseDir
Definition: qgscptcityarchive.h:72
virtual QList< QAction * > actions()
Definition: qgscptcityarchive.h:134
QString archiveName() const
Definition: qgscptcityarchive.h:57
QgsCptCityDataItem * parent() const
Definition: qgscptcityarchive.h:150
Item that represents a layer that can be opened with one of the providers.
Definition: qgscptcityarchive.h:195
QMap< QString, QStringList > mRampsMap
Definition: qgscptcityarchive.h:264
static QIcon icon(QString icon)
Definition: qgsmessagelogviewer.cpp:32
Definition: qgscptcityarchive.h:90
virtual QVector< QgsCptCityDataItem * > createChildren()
Definition: qgscptcityarchive.cpp:526
QgsCptCityArchive * mArchive
Definition: qgscptcityarchive.h:402
void setPopulated()
Definition: qgscptcityarchive.h:237
QVector< QgsCptCityDataItem * > rootItems() const
Definition: qgscptcityarchive.h:66
QString mShortInfo
Definition: qgscptcityarchive.h:176
static QMap< QString, QgsCptCityArchive * > mArchiveRegistry
Definition: qgscptcityarchive.h:74
bool mValid
Definition: qgscptcityarchive.h:179
bool isValid()
Definition: qgscptcityarchive.h:165
Type mType
Definition: qgscptcityarchive.h:169
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgscptcityarchive.h:140
QStringList selectionsList() const
Definition: qgscptcityarchive.h:279
virtual bool equal(const QgsCptCityDataItem *other)
Definition: qgscptcityarchive.cpp:698
QString mPath
Definition: qgscptcityarchive.h:174
QIcon mIcon
Definition: qgscptcityarchive.h:178
Definition: qgscptcityarchive.h:302
virtual bool acceptDrop()
Definition: qgscptcityarchive.h:137
A directory: contains subdirectories and color ramps.
Definition: qgscptcityarchive.h:246
virtual QIcon icon(const QSize &size)
Definition: qgscptcityarchive.h:154
QString mInfo
Definition: qgscptcityarchive.h:175
base class for all items in the model
Definition: qgscptcityarchive.h:84
QVector< QgsCptCityDataItem * > mSelectionItems
Definition: qgscptcityarchive.h:77
void setBaseDir(QString dirName)
Definition: qgscptcityarchive.h:44
~QgsCptCityColorRampItem()
Definition: qgscptcityarchive.h:207
ViewType
Definition: qgscptcityarchive.h:308
QgsCptCityColorRampV2 mRamp
Definition: qgscptcityarchive.h:223
Definition: qgscptcityarchive.h:33
A Collection: logical collection of subcollections and color ramps.
Definition: qgscptcityarchive.h:229
QVector< QgsCptCityDataItem * > mChildren
Definition: qgscptcityarchive.h:171
QString shortInfo() const
Definition: qgscptcityarchive.h:158
bool mInitialised
Definition: qgscptcityarchive.h:222
QString path() const
Definition: qgscptcityarchive.h:156
bool mPopulatedRamps
Definition: qgscptcityarchive.h:242
bool isPopulated()
Definition: qgscptcityarchive.h:116
virtual QIcon icon()
Definition: qgscptcityarchive.h:153
#define DEFAULT_CPTCITY_ARCHIVE
Definition: qgscptcityarchive.h:31
Type type() const
Definition: qgscptcityarchive.h:149
bool mPopulated
Definition: qgscptcityarchive.h:172
QStringList mSelectionsList
Definition: qgscptcityarchive.h:283
static QMap< QString, QMap< QString, QString > > mCopyingInfoMap
Definition: qgscptcityarchive.h:79
void addChild(QgsCptCityDataItem *item)
Definition: qgscptcityarchive.h:238
void setParent(QgsCptCityDataItem *parent)
Definition: qgscptcityarchive.h:151
Type
Definition: qgscptcityarchive.h:88
static QString mDefaultArchiveName
Definition: qgscptcityarchive.h:73
QVector< QgsCptCityDataItem * > mRootItems
Definition: qgscptcityarchive.h:401
virtual int leafCount() const
Definition: qgscptcityarchive.h:212
virtual QWidget * paramWidget()
Definition: qgscptcityarchive.h:131
void setToolTip(QString msg)
Definition: qgscptcityarchive.h:162
QString mName
Definition: qgscptcityarchive.h:173
Definition: qgscptcityarchive.h:92
A selection: contains subdirectories and color ramps.
Definition: qgscptcityarchive.h:268
QSize mIconSize
Definition: qgscptcityarchive.h:404
double size
Definition: qgssvgcache.cpp:77
QString info() const
Definition: qgscptcityarchive.h:157
QString name() const
Definition: qgscptcityarchive.h:155
QVector< QgsCptCityDataItem * > mRootItems
Definition: qgscptcityarchive.h:76
const QgsCptCityColorRampV2 & ramp() const
Definition: qgscptcityarchive.h:215
QVector< QgsCptCityDataItem * > selectionItems() const
Definition: qgscptcityarchive.h:67
QString mToolTip
Definition: qgscptcityarchive.h:177