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