QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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
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