QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgis_core.h"
21 #include "qgis_sip.h"
22 #include "qgis.h"
23 #include "qgscolorramp.h"
24 
25 #include <QAbstractItemModel>
26 #include <QIcon>
27 #include <QMimeData>
28 #include <QAction>
29 
31 class QgsCptCityDataItem;
33 
34 #define DEFAULT_CPTCITY_ARCHIVE "cpt-city-qgis-min"
35 
40 class CORE_EXPORT QgsCptCityArchive
41 {
42  public:
43  QgsCptCityArchive( const QString &archiveName = DEFAULT_CPTCITY_ARCHIVE,
44  const QString &baseDir = QString() );
46 
48  QgsCptCityArchive( const QgsCptCityArchive &rh ) = delete;
50  QgsCptCityArchive &operator=( const QgsCptCityArchive &rh ) = delete;
51 
52  // basic dir info
53  QString baseDir() const;
54  static QString baseDir( QString archiveName );
55  static QString defaultBaseDir();
56  void setBaseDir( const QString &dirName ) { mBaseDir = dirName; }
57 
58  // collection + selection info
59  QString copyingFileName( const QString &dirName ) const;
60  QString descFileName( const QString &dirName ) const;
61  static QString findFileName( const QString &target, const QString &startDir, const QString &baseDir );
62  static QMap< QString, QString > copyingInfo( const QString &fileName );
63  static QMap< QString, QString > description( const QString &fileName );
65  static QMap< double, QPair<QColor, QColor> > gradientColorMap( const QString &fileName ) SIP_SKIP;
66 
67  // archive management
68  bool isEmpty();
69  QString archiveName() const { return mArchiveName; }
70  static void initArchives( bool loadAll = false );
71  static void initArchive( const QString &archiveName, const QString &archiveBaseDir );
72  static void initDefaultArchive();
73  static void clearArchives();
74  static QgsCptCityArchive *defaultArchive();
75  static QMap< QString, QgsCptCityArchive * > archiveRegistry();
76 
77  // items
78  QVector< QgsCptCityDataItem * > rootItems() const { return mRootItems; }
79  QVector< QgsCptCityDataItem * > selectionItems() const { return mSelectionItems; }
80 
81  protected:
82 
83  QString mArchiveName;
84  QString mBaseDir;
85  static QString sDefaultArchiveName;
86  static QMap< QString, QgsCptCityArchive * > sArchiveRegistry;
87  // root items, namely directories at root of archive
88  QVector< QgsCptCityDataItem * > mRootItems;
89  QVector<QgsCptCityDataItem *> mSelectionItems;
90  // mapping of copyinginfo, key is fileName
91  static QMap< QString, QMap< QString, QString > > sCopyingInfoMap;
92 
93  private:
94 #ifdef SIP_RUN
96 #endif
97 
98 };
99 
104 class CORE_EXPORT QgsCptCityDataItem : public QObject
105 {
106  Q_OBJECT
107  public:
108  enum Type
109  {
114  AllRamps
115  };
116 
118  const QString &name, const QString &path );
119 
120  bool hasChildren();
121 
122  int rowCount();
123  // retrieve total count of "leaf" items (all children which are end nodes)
124  virtual int leafCount() const;
125 
126  //
127 
128  virtual void refresh();
129 
130  // Create vector of children
131  virtual QVector<QgsCptCityDataItem *> createChildren();
132 
133  // Populate children using children vector created by createChildren()
134  virtual void populate();
135  bool isPopulated() { return mPopulated; }
136 
137  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
138  // refresh - refresh populated item, emit signals to model
139  virtual void addChildItem( QgsCptCityDataItem *child SIP_TRANSFER, bool refresh = false );
140 
141  // remove and delete child item, signals to browser are emitted
142  virtual void deleteChildItem( QgsCptCityDataItem *child );
143 
144  // remove child item but don't delete it, signals to browser are emitted
145  // returns pointer to the removed item or null if no such item was found
146  virtual QgsCptCityDataItem *removeChildItem( QgsCptCityDataItem *child ) SIP_TRANSFERBACK;
147 
148  virtual bool equal( const QgsCptCityDataItem *other );
149 
150  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
151 
152  // list of actions provided by this item - usually used for popup menu on right-click
153  virtual QList<QAction *> actions() { return QList<QAction *>(); }
154 
155  // whether accepts drag&drop'd layers - e.g. for import
156  virtual bool acceptDrop() { return false; }
157 
158  // try to process the data dropped on this item
159  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
160 
161  // static methods
162 
163  // Find child index in vector of items using '==' operator
164  static int findItem( QVector<QgsCptCityDataItem *> items, QgsCptCityDataItem *item );
165 
166  // members
167 
168  Type type() const { return mType; }
169  QgsCptCityDataItem *parent() const { return mParent; }
170  void setParent( QgsCptCityDataItem *parent ) { mParent = parent; }
171  QVector<QgsCptCityDataItem *> children() const { return mChildren; }
172  virtual QIcon icon() { return mIcon; }
173  virtual QIcon icon( QSize size ) { Q_UNUSED( size ) ; return icon(); }
174  QString name() const { return mName; }
175  QString path() const { return mPath; }
176  QString info() const { return mInfo; }
177  QString shortInfo() const { return mShortInfo; }
178 
179  void setIcon( const QIcon &icon ) { mIcon = icon; }
180 
181  void setToolTip( const QString &msg ) { mToolTip = msg; }
182  QString toolTip() const { return mToolTip; }
183 
184  bool isValid() { return mValid; }
185 
186  protected:
187 
189  QgsCptCityDataItem *mParent = nullptr;
190  QVector<QgsCptCityDataItem *> mChildren; // easier to have it always
192  QString mName;
193  QString mPath; // it is also used to identify item in tree
194  QString mInfo;
195  QString mShortInfo;
196  QString mToolTip;
197  QIcon mIcon;
198  bool mValid;
199 
200  signals:
201  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
202  void endInsertItems();
203  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
204  void endRemoveItems();
205 };
206 
211 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
212 {
213  Q_OBJECT
214  public:
216  const QString &name, const QString &path,
217  const QString &variantName = QString(),
218  bool initialize = false );
220  const QString &name, const QString &path,
221  const QStringList &variantList,
222  bool initialize = false );
223 
224  // --- reimplemented from QgsCptCityDataItem ---
225 
226  bool equal( const QgsCptCityDataItem *other ) override;
227  int leafCount() const override { return 1; }
228 
229  // --- New virtual methods for layer item derived classes ---
230  const QgsCptCityColorRamp &ramp() const { return mRamp; }
231  QIcon icon() override;
232  QIcon icon( QSize size ) override;
233  void init();
234 
235  protected:
236 
239  QList< QIcon > mIcons;
240 };
241 
242 
248 {
249  Q_OBJECT
250  public:
252  const QString &name, const QString &path );
253  ~QgsCptCityCollectionItem() override;
254 
255  void setPopulated() { mPopulated = true; }
256  void addChild( QgsCptCityDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
257  QVector<QgsCptCityDataItem *> childrenRamps( bool recursive );
258 
259  protected:
261 };
262 
268 {
269  Q_OBJECT
270  public:
272  const QString &name, const QString &path );
273 
274  QVector<QgsCptCityDataItem *> createChildren() override;
275 
276  bool equal( const QgsCptCityDataItem *other ) override;
277 
278  static QgsCptCityDataItem *dataItem( QgsCptCityDataItem *parent,
279  const QString &name, const QString &path );
280 
281  protected:
282  QMap< QString, QStringList > rampsMap();
283  QStringList dirEntries() const;
284  QMap< QString, QStringList > mRampsMap;
285 };
286 
293 {
294  Q_OBJECT
295  public:
296  QgsCptCitySelectionItem( QgsCptCityDataItem *parent, const QString &name, const QString &path );
297 
298  QVector<QgsCptCityDataItem *> createChildren() override;
299 
300  bool equal( const QgsCptCityDataItem *other ) override;
301 
302  QStringList selectionsList() const { return mSelectionsList; }
303 
304  protected:
305  void parseXml();
306  QStringList mSelectionsList;
307 };
308 
313 {
314  Q_OBJECT
315  public:
316  QgsCptCityAllRampsItem( QgsCptCityDataItem *parent, const QString &name,
317  const QVector<QgsCptCityDataItem *> &items );
318 
319  QVector<QgsCptCityDataItem *> createChildren() override;
320 
321  protected:
322  QVector<QgsCptCityDataItem *> mItems;
323 };
324 
329 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
330 {
331  Q_OBJECT
332 
333  public:
334 
335  enum ViewType
336  {
337  Authors = 0,
338  Selections = 1,
339  List = 2 // not used anymore
340  };
341 
342  QgsCptCityBrowserModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
344  ViewType Type = Authors );
345  ~QgsCptCityBrowserModel() override;
346 
347  // implemented methods from QAbstractItemModel for read-only access
348  Qt::ItemFlags flags( const QModelIndex &index ) const override;
349  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
350  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
351  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
352  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
353  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
354 
355  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
356 
357  QModelIndex parent( const QModelIndex &index ) const override;
358 
360  /* virtual QStringList mimeTypes() const; */
361 
363  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
364 
366  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
367 
368  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
369 
370  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
371 
372  // Reload the whole model
373  void reload();
374 
375  // Refresh item specified by path
376  void refresh( const QString &path );
377 
378  // Refresh item children
379  void refresh( const QModelIndex &index = QModelIndex() );
380 
382  QModelIndex findPath( const QString &path );
383 
384  void connectItem( QgsCptCityDataItem *item );
385 
386  bool canFetchMore( const QModelIndex &parent ) const override;
387  void fetchMore( const QModelIndex &parent ) override;
388 
389  signals:
390 
391  public slots:
392  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
393  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
394  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
395 
396  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
397  void endInsertItems();
398  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
399  void endRemoveItems();
400 
401  protected:
402 
403  // populates the model
404  void addRootItems();
405  void removeRootItems();
406 
407  QVector<QgsCptCityDataItem *> mRootItems;
408  QgsCptCityArchive *mArchive = nullptr;
410  QSize mIconSize;
411 };
412 
413 // clazy:excludeall=qstring-allocations
414 
415 #endif
QStringList selectionsList() const
virtual QList< QAction * > actions()
An "All ramps item", which contains all items in a flat hierarchy.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QVector< QgsCptCityDataItem * > mItems
static QgsCptCityArchive * defaultArchive()
QgsCptCityColorRamp mRamp
QString name() const
Item that represents a layer that can be opened with one of the providers.
QMap< QString, QStringList > mRampsMap
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
static QMap< QString, QMap< QString, QString > > sCopyingInfoMap
QString toolTip() const
virtual QVector< QgsCptCityDataItem * > createChildren()
QVector< QgsCptCityDataItem * > children() const
void setIcon(const QIcon &icon)
const QgsCptCityColorRamp & ramp() const
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
virtual bool equal(const QgsCptCityDataItem *other)
#define SIP_SKIP
Definition: qgis_sip.h:119
QVector< QgsCptCityDataItem *> rootItems() const
virtual bool acceptDrop()
A directory: contains subdirectories and color ramps.
int leafCount() const override
void setToolTip(const QString &msg)
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Base class for all items in the model.
QString path() const
#define SIP_FACTORY
Definition: qgis_sip.h:69
A Collection: logical collection of subcollections and color ramps.
QString shortInfo() const
virtual QIcon icon()
QString archiveName() const
#define DEFAULT_CPTCITY_ARCHIVE
QVector< QgsCptCityDataItem * > mSelectionItems
void addChild(QgsCptCityDataItem *item)
void setParent(QgsCptCityDataItem *parent)
static QMap< QString, QgsCptCityArchive *> sArchiveRegistry
QString info() const
virtual QWidget * paramWidget()
virtual QIcon icon(QSize size)
void setBaseDir(const QString &dirName)
A selection: contains subdirectories and color ramps.
QgsCptCityDataItem * parent() const
QVector< QgsCptCityDataItem *> mRootItems
QVector< QgsCptCityDataItem * > mRootItems
QVector< QgsCptCityDataItem *> selectionItems() const
QVector< QgsCptCityDataItem * > mChildren
static QString sDefaultArchiveName