QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
314 {
315  Q_OBJECT
316  public:
317  QgsCptCityAllRampsItem( QgsCptCityDataItem *parent, const QString &name,
318  const QVector<QgsCptCityDataItem *> &items );
319 
320  QVector<QgsCptCityDataItem *> createChildren() override;
321 
322  protected:
323  QVector<QgsCptCityDataItem *> mItems;
324 };
325 
330 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
331 {
332  Q_OBJECT
333 
334  public:
335 
336  enum ViewType
337  {
338  Authors = 0,
339  Selections = 1,
340  List = 2 // not used anymore
341  };
342 
343  QgsCptCityBrowserModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
345  ViewType Type = Authors );
346  ~QgsCptCityBrowserModel() override;
347 
348  // implemented methods from QAbstractItemModel for read-only access
349  Qt::ItemFlags flags( const QModelIndex &index ) const override;
350  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
351  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
352  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
353  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
354  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
355 
356  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
357 
358  QModelIndex parent( const QModelIndex &index ) const override;
359 
361  /* virtual QStringList mimeTypes() const; */
362 
364  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
365 
367  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
368 
369  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
370 
371  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
372 
373  // Reload the whole model
374  void reload();
375 
376  // Refresh item specified by path
377  void refresh( const QString &path );
378 
379  // Refresh item children
380  void refresh( const QModelIndex &index = QModelIndex() );
381 
383  QModelIndex findPath( const QString &path );
384 
385  void connectItem( QgsCptCityDataItem *item );
386 
387  bool canFetchMore( const QModelIndex &parent ) const override;
388  void fetchMore( const QModelIndex &parent ) override;
389 
390  signals:
391 
392  public slots:
393  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
394  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
395  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
396 
397  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
398  void endInsertItems();
399  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
400  void endRemoveItems();
401 
402  protected:
403 
404  // populates the model
405  void addRootItems();
406  void removeRootItems();
407 
408  QVector<QgsCptCityDataItem *> mRootItems;
409  QgsCptCityArchive *mArchive = nullptr;
411  QSize mIconSize;
412 };
413 
414 // clazy:excludeall=qstring-allocations
415 
416 #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:53
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:48
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:126
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:76
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