QGIS API Documentation  2.15.0-Master (02a0ebe)
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:
37  const 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( const 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( const QString& archiveName, const 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 
75  // root items, namely directories at root of archive
78  // mapping of copyinginfo, key is fileName
80 
81  private:
82 
84  QgsCptCityArchive& operator=( const QgsCptCityArchive& rh );
85 };
86 
88 class CORE_EXPORT QgsCptCityDataItem : public QObject
89 {
90  Q_OBJECT
91  public:
92  enum Type
93  {
98  AllRamps
99  };
100 
102  const QString& name, const QString& path );
103  virtual ~QgsCptCityDataItem();
104 
105  bool hasChildren();
106 
107  int rowCount();
108  // retrieve total count of "leaf" items (all children which are end nodes)
109  virtual int leafCount() const;
110 
111  //
112 
113  virtual void refresh();
114 
115  // Create vector of children
116  virtual QVector<QgsCptCityDataItem*> createChildren();
117 
118  // Populate children using children vector created by createChildren()
119  virtual void populate();
120  bool isPopulated() { return mPopulated; }
121 
122  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
123  // refresh - refresh populated item, emit signals to model
124  virtual void addChildItem( QgsCptCityDataItem * child, bool refresh = false );
125 
126  // remove and delete child item, signals to browser are emitted
127  virtual void deleteChildItem( QgsCptCityDataItem * child );
128 
129  // remove child item but don't delete it, signals to browser are emitted
130  // returns pointer to the removed item or null if no such item was found
131  virtual QgsCptCityDataItem * removeChildItem( QgsCptCityDataItem * child );
132 
133  virtual bool equal( const QgsCptCityDataItem *other );
134 
135  virtual QWidget *paramWidget() { return nullptr; }
136 
137  // list of actions provided by this item - usually used for popup menu on right-click
138  virtual QList<QAction*> actions() { return QList<QAction*>(); }
139 
140  // whether accepts drag&drop'd layers - e.g. for import
141  virtual bool acceptDrop() { return false; }
142 
143  // try to process the data dropped on this item
144  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
145 
146  // static methods
147 
148  // Find child index in vector of items using '==' operator
149  static int findItem( QVector<QgsCptCityDataItem*> items, QgsCptCityDataItem * item );
150 
151  // members
152 
153  Type type() const { return mType; }
154  QgsCptCityDataItem* parent() const { return mParent; }
155  void setParent( QgsCptCityDataItem* parent ) { mParent = parent; }
156  QVector<QgsCptCityDataItem*> children() const { return mChildren; }
157  virtual QIcon icon() { return mIcon; }
158  virtual QIcon icon( QSize size ) { Q_UNUSED( size ) ; return icon(); }
159  QString name() const { return mName; }
160  QString path() const { return mPath; }
161  QString info() const { return mInfo; }
162  QString shortInfo() const { return mShortInfo; }
163 
164  void setIcon( const QIcon& icon ) { mIcon = icon; }
165 
166  void setToolTip( const QString& msg ) { mToolTip = msg; }
167  QString toolTip() const { return mToolTip; }
168 
169  bool isValid() { return mValid; }
170 
171  protected:
172 
175  QVector<QgsCptCityDataItem*> mChildren; // easier to have it always
178  QString mPath; // it is also used to identify item in tree
183  bool mValid;
184 
185  public slots:
186  void emitBeginInsertItems( QgsCptCityDataItem* parent, int first, int last );
187  void emitEndInsertItems();
188  void emitBeginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
189  void emitEndRemoveItems();
190 
191  signals:
192  void beginInsertItems( QgsCptCityDataItem* parent, int first, int last );
193  void endInsertItems();
194  void beginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
195  void endRemoveItems();
196 };
197 
199 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
200 {
201  Q_OBJECT
202  public:
204  const QString& name, const QString& path,
205  const QString& variantName = QString(),
206  bool initialize = false );
208  const QString& name, const QString& path,
209  const QStringList& variantList,
210  bool initialize = false );
212 
213  // --- reimplemented from QgsCptCityDataItem ---
214 
215  virtual bool equal( const QgsCptCityDataItem *other ) override;
216  virtual int leafCount() const override { return 1; }
217 
218  // --- New virtual methods for layer item derived classes ---
219  const QgsCptCityColorRampV2& ramp() const { return mRamp; }
220  QIcon icon() override;
221  QIcon icon( QSize size ) override;
222  void init();
223 
224  protected:
225 
229 };
230 
231 
234 {
235  Q_OBJECT
236  public:
238  const QString& name, const QString& path );
240 
241  void setPopulated() { mPopulated = true; }
242  void addChild( QgsCptCityDataItem *item ) { mChildren.append( item ); }
243  QVector<QgsCptCityDataItem*> childrenRamps( bool recursive );
244 
245  protected:
247 };
248 
251 {
252  Q_OBJECT
253  public:
255  const QString& name, const QString& path );
257 
259 
260  virtual bool equal( const QgsCptCityDataItem *other ) override;
261 
262  static QgsCptCityDataItem* dataItem( QgsCptCityDataItem* parent,
263  const QString& name, const QString& path );
264 
265  protected:
266  QMap< QString, QStringList > rampsMap();
267  QStringList dirEntries() const;
269 };
270 
273 {
274  Q_OBJECT
275  public:
276  QgsCptCitySelectionItem( QgsCptCityDataItem* parent, const QString& name, const QString& path );
278 
280 
281  virtual bool equal( const QgsCptCityDataItem *other ) override;
282 
283  QStringList selectionsList() const { return mSelectionsList; }
284 
285  protected:
286  void parseXML();
288 };
289 
292 {
293  Q_OBJECT
294  public:
295  QgsCptCityAllRampsItem( QgsCptCityDataItem* parent, const QString& name,
296  const QVector<QgsCptCityDataItem*>& items );
298 
300 
301  protected:
303 };
304 
305 
306 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
307 {
308  Q_OBJECT
309 
310  public:
311 
312  enum ViewType
313  {
314  Authors = 0,
315  Selections = 1,
316  List = 2 // not used anymore
317  };
318 
319  QgsCptCityBrowserModel( QObject* parent = nullptr,
321  ViewType Type = Authors );
323 
324  // implemented methods from QAbstractItemModel for read-only access
325 
328  virtual Qt::ItemFlags flags( const QModelIndex &index ) const override;
329 
334  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
335 
338  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
339 
341  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
342 
345  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
346 
348  virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const override;
349 
350  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
351 
355  virtual QModelIndex parent( const QModelIndex &index ) const override;
356 
358  /* virtual QStringList mimeTypes() const; */
359 
361  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
362 
364  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
365 
366  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
367 
368  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
369 
370  // Reload the whole model
371  void reload();
372 
373  // Refresh item specified by path
374  void refresh( const QString& path );
375 
376  // Refresh item childs
377  void refresh( const QModelIndex &index = QModelIndex() );
378 
380  QModelIndex findPath( const QString& path );
381 
382  void connectItem( QgsCptCityDataItem *item );
383 
384  bool canFetchMore( const QModelIndex & parent ) const override;
385  void fetchMore( const QModelIndex & parent ) override;
386 
387  signals:
388 
389  public slots:
390  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
391  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
392  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
393 
394  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
395  void endInsertItems();
396  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
397  void endRemoveItems();
398 
399  protected:
400 
401  // populates the model
402  void addRootItems();
403  void removeRootItems();
404 
409 };
410 
411 #endif
QVector< QgsCptCityDataItem * > children() const
static unsigned index
virtual int rowCount(const QModelIndex &parent) const =0
QgsCptCityDataItem * mParent
QString toolTip() const
QVector< QgsCptCityDataItem * > mItems
An "All ramps item", which contains all items in a flat hierarchy.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
static QgsCptCityArchive * defaultArchive()
virtual bool canFetchMore(const QModelIndex &parent) const
virtual QList< QAction * > actions()
QString archiveName() const
QgsCptCityDataItem * parent() const
Item that represents a layer that can be opened with one of the providers.
QMap< QString, QStringList > mRampsMap
virtual QVector< QgsCptCityDataItem * > createChildren()
QgsCptCityArchive * mArchive
QVector< QgsCptCityDataItem * > rootItems() const
void setIcon(const QIcon &icon)
static QMap< QString, QgsCptCityArchive * > mArchiveRegistry
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
QStringList selectionsList() const
virtual bool equal(const QgsCptCityDataItem *other)
virtual bool acceptDrop()
A directory: contains subdirectories and color ramps.
void setToolTip(const QString &msg)
Base class for all items in the model.
QVector< QgsCptCityDataItem * > mSelectionItems
QgsCptCityColorRampV2 mRamp
virtual QVariant data(const QModelIndex &index, int role) const =0
A Collection: logical collection of subcollections and color ramps.
QVector< QgsCptCityDataItem * > mChildren
QString shortInfo() const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
QString path() const
virtual QIcon icon()
#define DEFAULT_CPTCITY_ARCHIVE
static QMap< QString, QMap< QString, QString > > mCopyingInfoMap
void addChild(QgsCptCityDataItem *item)
void setParent(QgsCptCityDataItem *parent)
static QString mDefaultArchiveName
virtual bool hasChildren(const QModelIndex &parent) const
virtual int columnCount(const QModelIndex &parent) const =0
QVector< QgsCptCityDataItem * > mRootItems
virtual void fetchMore(const QModelIndex &parent)
virtual QWidget * paramWidget()
virtual QIcon icon(QSize size)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual int leafCount() const override
void setBaseDir(const QString &dirName)
A selection: contains subdirectories and color ramps.
QObject * parent() const
QString info() const
QString name() const
QVector< QgsCptCityDataItem * > mRootItems
const QgsCptCityColorRampV2 & ramp() const
QVector< QgsCptCityDataItem * > selectionItems() const
typedef ItemFlags