QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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;
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  private:
82 
83  QString mArchiveName;
84  QString mBaseDir;
85  // root items, namely directories at root of archive
86  QVector< QgsCptCityDataItem * > mRootItems;
87  QVector<QgsCptCityDataItem *> mSelectionItems;
88 
89  private:
90 #ifdef SIP_RUN
92 #endif
93 
94 };
95 
100 class CORE_EXPORT QgsCptCityDataItem : public QObject
101 {
102  Q_OBJECT
103  public:
104  enum Type
105  {
110  AllRamps
111  };
112 
114  const QString &name, const QString &path );
115 
116  bool hasChildren();
117 
118  int rowCount();
119  // retrieve total count of "leaf" items (all children which are end nodes)
120  virtual int leafCount() const;
121 
122  //
123 
124  virtual void refresh();
125 
126  // Create vector of children
127  virtual QVector<QgsCptCityDataItem *> createChildren();
128 
129  // Populate children using children vector created by createChildren()
130  virtual void populate();
131  bool isPopulated() { return mPopulated; }
132 
133  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
134  // refresh - refresh populated item, emit signals to model
135  virtual void addChildItem( QgsCptCityDataItem *child SIP_TRANSFER, bool refresh = false );
136 
137  // remove and delete child item, signals to browser are emitted
138  virtual void deleteChildItem( QgsCptCityDataItem *child );
139 
140  // remove child item but don't delete it, signals to browser are emitted
141  // returns pointer to the removed item or null if no such item was found
142  virtual QgsCptCityDataItem *removeChildItem( QgsCptCityDataItem *child ) SIP_TRANSFERBACK;
143 
144  virtual bool equal( const QgsCptCityDataItem *other );
145 
146  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
147 
148  // list of actions provided by this item - usually used for popup menu on right-click
149  virtual QList<QAction *> actions() { return QList<QAction *>(); }
150 
151  // whether accepts drag&drop'd layers - e.g. for import
152  virtual bool acceptDrop() { return false; }
153 
154  // try to process the data dropped on this item
155  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
156 
157  // static methods
158 
159  // Find child index in vector of items using '==' operator
160  static int findItem( QVector<QgsCptCityDataItem *> items, QgsCptCityDataItem *item );
161 
162  // members
163 
164  Type type() const { return mType; }
165  QgsCptCityDataItem *parent() const { return mParent; }
166  void setParent( QgsCptCityDataItem *parent ) { mParent = parent; }
167  QVector<QgsCptCityDataItem *> children() const { return mChildren; }
168  virtual QIcon icon() { return mIcon; }
169  virtual QIcon icon( QSize size ) { Q_UNUSED( size ) return icon(); }
170  QString name() const { return mName; }
171  QString path() const { return mPath; }
172  QString info() const { return mInfo; }
173  QString shortInfo() const { return mShortInfo; }
174 
175  void setIcon( const QIcon &icon ) { mIcon = icon; }
176 
177  void setToolTip( const QString &msg ) { mToolTip = msg; }
178  QString toolTip() const { return mToolTip; }
179 
180  bool isValid() { return mValid; }
181 
182  protected:
183 
185  QgsCptCityDataItem *mParent = nullptr;
186  QVector<QgsCptCityDataItem *> mChildren; // easier to have it always
188  QString mName;
189  QString mPath; // it is also used to identify item in tree
190  QString mInfo;
191  QString mShortInfo;
192  QString mToolTip;
193  QIcon mIcon;
194  bool mValid;
195 
196  signals:
197  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
199  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
201 };
202 
207 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
208 {
209  Q_OBJECT
210  public:
212  const QString &name, const QString &path,
213  const QString &variantName = QString(),
214  bool initialize = false );
216  const QString &name, const QString &path,
217  const QStringList &variantList,
218  bool initialize = false );
219 
220  // --- reimplemented from QgsCptCityDataItem ---
221 
222  bool equal( const QgsCptCityDataItem *other ) override;
223  int leafCount() const override { return 1; }
224 
225  // --- New virtual methods for layer item derived classes ---
226  const QgsCptCityColorRamp &ramp() const { return mRamp; }
227  QIcon icon() override;
228  QIcon icon( QSize size ) override;
229  void init();
230 
231  protected:
232 
235  QList< QIcon > mIcons;
236 };
237 
238 
244 {
245  Q_OBJECT
246  public:
248  const QString &name, const QString &path );
249  ~QgsCptCityCollectionItem() override;
250 
251  void setPopulated() { mPopulated = true; }
252  void addChild( QgsCptCityDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
253  QVector<QgsCptCityDataItem *> childrenRamps( bool recursive );
254 
255  protected:
257 };
258 
264 {
265  Q_OBJECT
266  public:
268  const QString &name, const QString &path );
269 
270  QVector<QgsCptCityDataItem *> createChildren() override;
271 
272  bool equal( const QgsCptCityDataItem *other ) override;
273 
274  static QgsCptCityDataItem *dataItem( QgsCptCityDataItem *parent,
275  const QString &name, const QString &path );
276 
277  protected:
278  QMap< QString, QStringList > rampsMap();
279  QStringList dirEntries() const;
280  QMap< QString, QStringList > mRampsMap;
281 };
282 
289 {
290  Q_OBJECT
291  public:
292  QgsCptCitySelectionItem( QgsCptCityDataItem *parent, const QString &name, const QString &path );
293 
294  QVector<QgsCptCityDataItem *> createChildren() override;
295 
296  bool equal( const QgsCptCityDataItem *other ) override;
297 
298  QStringList selectionsList() const { return mSelectionsList; }
299 
300  protected:
301  void parseXml();
302  QStringList mSelectionsList;
303 };
304 
310 {
311  Q_OBJECT
312  public:
313  QgsCptCityAllRampsItem( QgsCptCityDataItem *parent, const QString &name,
314  const QVector<QgsCptCityDataItem *> &items );
315 
316  QVector<QgsCptCityDataItem *> createChildren() override;
317 
318  protected:
319  QVector<QgsCptCityDataItem *> mItems;
320 };
321 
326 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
327 {
328  Q_OBJECT
329 
330  public:
331 
332  enum ViewType
333  {
334  Authors = 0,
335  Selections = 1,
336  List = 2 // not used anymore
337  };
338 
339  QgsCptCityBrowserModel( QObject *parent SIP_TRANSFERTHIS = nullptr,
341  ViewType Type = Authors );
342  ~QgsCptCityBrowserModel() override;
343 
344  // implemented methods from QAbstractItemModel for read-only access
345  Qt::ItemFlags flags( const QModelIndex &index ) const override;
346  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
347  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
348  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
349  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
350  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
351 
352  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
353 
354  QModelIndex parent( const QModelIndex &index ) const override;
355 
357  /* virtual QStringList mimeTypes() const; */
358 
360  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
361 
363  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
364 
365  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
366 
367  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
368 
369  // Reload the whole model
370  void reload();
371 
372  // Refresh item specified by path
373  void refresh( const QString &path );
374 
375  // Refresh item children
376  void refresh( const QModelIndex &index = QModelIndex() );
377 
379  QModelIndex findPath( const QString &path );
380 
381  void connectItem( QgsCptCityDataItem *item );
382 
383  bool canFetchMore( const QModelIndex &parent ) const override;
384  void fetchMore( const QModelIndex &parent ) override;
385 
386  signals:
387 
388  public slots:
389  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
390  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
391  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
392 
393  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
394  void endInsertItems();
395  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
396  void endRemoveItems();
397 
398  protected:
399 
400  // populates the model
401  void addRootItems();
402  void removeRootItems();
403 
404  QVector<QgsCptCityDataItem *> mRootItems;
405  QgsCptCityArchive *mArchive = nullptr;
407  QSize mIconSize;
408 };
409 
410 // clazy:excludeall=qstring-allocations
411 
412 #endif
QgsCptCityDataItem::mInfo
QString mInfo
Definition: qgscptcityarchive.h:190
QgsCptCityDataItem::isValid
bool isValid()
Definition: qgscptcityarchive.h:180
QgsCptCityDataItem::icon
virtual QIcon icon(QSize size)
Definition: qgscptcityarchive.h:169
QgsCptCityColorRampItem::mRamp
QgsCptCityColorRamp mRamp
Definition: qgscptcityarchive.h:234
QgsCptCityDataItem::path
QString path() const
Definition: qgscptcityarchive.h:171
QgsCptCityColorRampItem
Item that represents a layer that can be opened with one of the providers.
Definition: qgscptcityarchive.h:208
QgsCptCityDataItem::isPopulated
bool isPopulated()
Definition: qgscptcityarchive.h:131
QgsCptCityDataItem::icon
virtual QIcon icon()
Definition: qgscptcityarchive.h:168
QgsCptCityDataItem::Collection
@ Collection
Definition: qgscptcityarchive.h:107
QgsCptCityColorRampItem::mIcons
QList< QIcon > mIcons
Definition: qgscptcityarchive.h:235
QgsCptCityDataItem::mToolTip
QString mToolTip
Definition: qgscptcityarchive.h:192
QgsCptCityArchive::operator=
QgsCptCityArchive & operator=(const QgsCptCityArchive &rh)=delete
QgsCptCityArchive cannot be copied.
QgsCptCityColorRampItem::mInitialized
bool mInitialized
Definition: qgscptcityarchive.h:233
QgsCptCitySelectionItem
A selection: contains subdirectories and color ramps.
Definition: qgscptcityarchive.h:289
QgsCptCityDataItem::setParent
void setParent(QgsCptCityDataItem *parent)
Definition: qgscptcityarchive.h:166
qgis.h
DEFAULT_CPTCITY_ARCHIVE
#define DEFAULT_CPTCITY_ARCHIVE
Definition: qgscptcityarchive.h:34
QgsCptCityArchive
Definition: qgscptcityarchive.h:41
QgsCptCityArchive::defaultArchive
static QgsCptCityArchive * defaultArchive()
Definition: qgscptcityarchive.cpp:426
QgsCptCityDataItem::acceptDrop
virtual bool acceptDrop()
Definition: qgscptcityarchive.h:152
QgsCptCityDataItem::beginRemoveItems
void beginRemoveItems(QgsCptCityDataItem *parent, int first, int last)
QgsCptCityBrowserModel::mIconSize
QSize mIconSize
Definition: qgscptcityarchive.h:407
QgsCptCityDataItem::setIcon
void setIcon(const QIcon &icon)
Definition: qgscptcityarchive.h:175
QgsCptCityBrowserModel::mRootItems
QVector< QgsCptCityDataItem * > mRootItems
Definition: qgscptcityarchive.h:404
QgsCptCityDataItem::mValid
bool mValid
Definition: qgscptcityarchive.h:194
QgsCptCityBrowserModel::mViewType
ViewType mViewType
Definition: qgscptcityarchive.h:406
QgsCptCityDataItem::Selection
@ Selection
Definition: qgscptcityarchive.h:109
QgsCptCityDataItem::handleDrop
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Definition: qgscptcityarchive.h:155
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QgsCptCityDataItem::setToolTip
void setToolTip(const QString &msg)
Definition: qgscptcityarchive.h:177
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsCptCityDataItem::mName
QString mName
Definition: qgscptcityarchive.h:188
QgsCptCityColorRampItem::ramp
const QgsCptCityColorRamp & ramp() const
Definition: qgscptcityarchive.h:226
QgsCptCityArchive::rootItems
QVector< QgsCptCityDataItem * > rootItems() const
Definition: qgscptcityarchive.h:78
QgsCptCityDataItem::paramWidget
virtual QWidget * paramWidget()
Definition: qgscptcityarchive.h:146
QgsCptCityDataItem::mPopulated
bool mPopulated
Definition: qgscptcityarchive.h:187
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsCptCityDataItem::name
QString name() const
Definition: qgscptcityarchive.h:170
QgsCptCityColorRampItem::leafCount
int leafCount() const override
Definition: qgscptcityarchive.h:223
QgsCptCityCollectionItem
A Collection: logical collection of subcollections and color ramps.
Definition: qgscptcityarchive.h:244
QgsCptCityDataItem::createChildren
virtual QVector< QgsCptCityDataItem * > createChildren()
Definition: qgscptcityarchive.cpp:520
QgsCptCityDataItem::endRemoveItems
void endRemoveItems()
qgscolorramp.h
QgsCptCityDataItem::mChildren
QVector< QgsCptCityDataItem * > mChildren
Definition: qgscptcityarchive.h:186
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsCptCityDataItem
Base class for all items in the model.
Definition: qgscptcityarchive.h:101
QgsCptCityAllRampsItem::mItems
QVector< QgsCptCityDataItem * > mItems
Definition: qgscptcityarchive.h:319
QgsCptCityDataItem::ColorRamp
@ ColorRamp
Definition: qgscptcityarchive.h:106
QgsCptCityDataItem::beginInsertItems
void beginInsertItems(QgsCptCityDataItem *parent, int first, int last)
QgsCptCityDataItem::actions
virtual QList< QAction * > actions()
Definition: qgscptcityarchive.h:149
QgsCptCityCollectionItem::mPopulatedRamps
bool mPopulatedRamps
Definition: qgscptcityarchive.h:256
QgsCptCityArchive::archiveName
QString archiveName() const
Definition: qgscptcityarchive.h:69
QgsCptCityBrowserModel
Definition: qgscptcityarchive.h:327
QgsCptCityDataItem::parent
QgsCptCityDataItem * parent() const
Definition: qgscptcityarchive.h:165
QgsCptCityDirectoryItem
A directory: contains subdirectories and color ramps.
Definition: qgscptcityarchive.h:264
QgsCptCityCollectionItem::setPopulated
void setPopulated()
Definition: qgscptcityarchive.h:251
QgsCptCityDataItem::Directory
@ Directory
Definition: qgscptcityarchive.h:108
QgsCptCityArchive::QgsCptCityArchive
QgsCptCityArchive(const QgsCptCityArchive &rh)=delete
QgsCptCityArchive cannot be copied.
QgsCptCityDataItem::endInsertItems
void endInsertItems()
QgsCptCityDirectoryItem::mRampsMap
QMap< QString, QStringList > mRampsMap
Definition: qgscptcityarchive.h:280
QgsCptCitySelectionItem::mSelectionsList
QStringList mSelectionsList
Definition: qgscptcityarchive.h:302
QgsCptCityCollectionItem::addChild
void addChild(QgsCptCityDataItem *item)
Definition: qgscptcityarchive.h:252
QgsCptCityDataItem::shortInfo
QString shortInfo() const
Definition: qgscptcityarchive.h:173
QgsCptCityAllRampsItem
An "All ramps item", which contains all items in a flat hierarchy.
Definition: qgscptcityarchive.h:310
QgsCptCityColorRamp
Definition: qgscolorramp.h:677
QgsCptCitySelectionItem::selectionsList
QStringList selectionsList() const
Definition: qgscptcityarchive.h:298
QgsCptCityBrowserModel::ViewType
ViewType
Definition: qgscptcityarchive.h:333
QgsCptCityDataItem::children
QVector< QgsCptCityDataItem * > children() const
Definition: qgscptcityarchive.h:167
QgsCptCityDataItem::type
Type type() const
Definition: qgscptcityarchive.h:164
QgsCptCityDataItem::mIcon
QIcon mIcon
Definition: qgscptcityarchive.h:193
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsCptCityArchive::setBaseDir
void setBaseDir(const QString &dirName)
Definition: qgscptcityarchive.h:56
QgsCptCityDataItem::toolTip
QString toolTip() const
Definition: qgscptcityarchive.h:178
QgsCptCityDataItem::mPath
QString mPath
Definition: qgscptcityarchive.h:189
QgsCptCityDataItem::info
QString info() const
Definition: qgscptcityarchive.h:172
QgsCptCityDataItem::mShortInfo
QString mShortInfo
Definition: qgscptcityarchive.h:191
QgsCptCityDataItem::equal
virtual bool equal(const QgsCptCityDataItem *other)
Definition: qgscptcityarchive.cpp:689
QgsCptCityDataItem::Type
Type
Definition: qgscptcityarchive.h:105
QgsCptCityDataItem::mType
Type mType
Definition: qgscptcityarchive.h:184
QgsCptCityArchive::selectionItems
QVector< QgsCptCityDataItem * > selectionItems() const
Definition: qgscptcityarchive.h:79