QGIS API Documentation  2.99.0-Master (5b186ae)
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 
103 class CORE_EXPORT QgsCptCityDataItem : public QObject
104 {
105  Q_OBJECT
106  public:
107  enum Type
108  {
113  AllRamps
114  };
115 
117  const QString &name, const QString &path );
118 
119  bool hasChildren();
120 
121  int rowCount();
122  // retrieve total count of "leaf" items (all children which are end nodes)
123  virtual int leafCount() const;
124 
125  //
126 
127  virtual void refresh();
128 
129  // Create vector of children
130  virtual QVector<QgsCptCityDataItem *> createChildren();
131 
132  // Populate children using children vector created by createChildren()
133  virtual void populate();
134  bool isPopulated() { return mPopulated; }
135 
136  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
137  // refresh - refresh populated item, emit signals to model
138  virtual void addChildItem( QgsCptCityDataItem *child SIP_TRANSFER, bool refresh = false );
139 
140  // remove and delete child item, signals to browser are emitted
141  virtual void deleteChildItem( QgsCptCityDataItem *child );
142 
143  // remove child item but don't delete it, signals to browser are emitted
144  // returns pointer to the removed item or null if no such item was found
145  virtual QgsCptCityDataItem *removeChildItem( QgsCptCityDataItem *child ) SIP_TRANSFERBACK;
146 
147  virtual bool equal( const QgsCptCityDataItem *other );
148 
149  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
150 
151  // list of actions provided by this item - usually used for popup menu on right-click
152  virtual QList<QAction *> actions() { return QList<QAction *>(); }
153 
154  // whether accepts drag&drop'd layers - e.g. for import
155  virtual bool acceptDrop() { return false; }
156 
157  // try to process the data dropped on this item
158  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
159 
160  // static methods
161 
162  // Find child index in vector of items using '==' operator
163  static int findItem( QVector<QgsCptCityDataItem *> items, QgsCptCityDataItem *item );
164 
165  // members
166 
167  Type type() const { return mType; }
168  QgsCptCityDataItem *parent() const { return mParent; }
169  void setParent( QgsCptCityDataItem *parent ) { mParent = parent; }
170  QVector<QgsCptCityDataItem *> children() const { return mChildren; }
171  virtual QIcon icon() { return mIcon; }
172  virtual QIcon icon( QSize size ) { Q_UNUSED( size ) ; return icon(); }
173  QString name() const { return mName; }
174  QString path() const { return mPath; }
175  QString info() const { return mInfo; }
176  QString shortInfo() const { return mShortInfo; }
177 
178  void setIcon( const QIcon &icon ) { mIcon = icon; }
179 
180  void setToolTip( const QString &msg ) { mToolTip = msg; }
181  QString toolTip() const { return mToolTip; }
182 
183  bool isValid() { return mValid; }
184 
185  protected:
186 
188  QgsCptCityDataItem *mParent = nullptr;
189  QVector<QgsCptCityDataItem *> mChildren; // easier to have it always
191  QString mName;
192  QString mPath; // it is also used to identify item in tree
193  QString mInfo;
194  QString mShortInfo;
195  QString mToolTip;
196  QIcon mIcon;
197  bool mValid;
198 
199  signals:
200  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
201  void endInsertItems();
202  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
203  void endRemoveItems();
204 };
205 
209 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
210 {
211  Q_OBJECT
212  public:
214  const QString &name, const QString &path,
215  const QString &variantName = QString(),
216  bool initialize = false );
218  const QString &name, const QString &path,
219  const QStringList &variantList,
220  bool initialize = false );
221 
222  // --- reimplemented from QgsCptCityDataItem ---
223 
224  virtual bool equal( const QgsCptCityDataItem *other ) override;
225  virtual int leafCount() const override { return 1; }
226 
227  // --- New virtual methods for layer item derived classes ---
228  const QgsCptCityColorRamp &ramp() const { return mRamp; }
229  QIcon icon() override;
230  QIcon icon( QSize size ) override;
231  void init();
232 
233  protected:
234 
237  QList< QIcon > mIcons;
238 };
239 
240 
245 {
246  Q_OBJECT
247  public:
249  const QString &name, const QString &path );
251 
252  void setPopulated() { mPopulated = true; }
253  void addChild( QgsCptCityDataItem *item SIP_TRANSFER ) { mChildren.append( item ); }
254  QVector<QgsCptCityDataItem *> childrenRamps( bool recursive );
255 
256  protected:
258 };
259 
264 {
265  Q_OBJECT
266  public:
268  const QString &name, const QString &path );
269 
270  QVector<QgsCptCityDataItem *> createChildren() override;
271 
272  virtual 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 
288 {
289  Q_OBJECT
290  public:
291  QgsCptCitySelectionItem( QgsCptCityDataItem *parent, const QString &name, const QString &path );
292 
293  QVector<QgsCptCityDataItem *> createChildren() override;
294 
295  virtual bool equal( const QgsCptCityDataItem *other ) override;
296 
297  QStringList selectionsList() const { return mSelectionsList; }
298 
299  protected:
300  void parseXml();
301  QStringList mSelectionsList;
302 };
303 
307 {
308  Q_OBJECT
309  public:
310  QgsCptCityAllRampsItem( QgsCptCityDataItem *parent, const QString &name,
311  const QVector<QgsCptCityDataItem *> &items );
312 
313  QVector<QgsCptCityDataItem *> createChildren() override;
314 
315  protected:
316  QVector<QgsCptCityDataItem *> mItems;
317 };
318 
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 );
339 
340  // implemented methods from QAbstractItemModel for read-only access
341  virtual Qt::ItemFlags flags( const QModelIndex &index ) const override;
342  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
343  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
344  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
345  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
346  virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
347 
348  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
349 
350  virtual 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 #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.
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)
virtual int leafCount() const override
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