QGIS API Documentation  2.99.0-Master (13a3f2f)
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 };
94 
98 class CORE_EXPORT QgsCptCityDataItem : public QObject
99 {
100  Q_OBJECT
101  public:
102  enum Type
103  {
108  AllRamps
109  };
110 
112  const QString &name, const QString &path );
113 
114  bool hasChildren();
115 
116  int rowCount();
117  // retrieve total count of "leaf" items (all children which are end nodes)
118  virtual int leafCount() const;
119 
120  //
121 
122  virtual void refresh();
123 
124  // Create vector of children
125  virtual QVector<QgsCptCityDataItem *> createChildren();
126 
127  // Populate children using children vector created by createChildren()
128  virtual void populate();
129  bool isPopulated() { return mPopulated; }
130 
131  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
132  // refresh - refresh populated item, emit signals to model
133  virtual void addChildItem( QgsCptCityDataItem *child SIP_TRANSFER, bool refresh = false );
134 
135  // remove and delete child item, signals to browser are emitted
136  virtual void deleteChildItem( QgsCptCityDataItem *child );
137 
138  // remove child item but don't delete it, signals to browser are emitted
139  // returns pointer to the removed item or null if no such item was found
140  virtual QgsCptCityDataItem *removeChildItem( QgsCptCityDataItem *child ) SIP_TRANSFERBACK;
141 
142  virtual bool equal( const QgsCptCityDataItem *other );
143 
144  virtual QWidget *paramWidget() SIP_FACTORY { return nullptr; }
145 
146  // list of actions provided by this item - usually used for popup menu on right-click
147  virtual QList<QAction *> actions() { return QList<QAction *>(); }
148 
149  // whether accepts drag&drop'd layers - e.g. for import
150  virtual bool acceptDrop() { return false; }
151 
152  // try to process the data dropped on this item
153  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
154 
155  // static methods
156 
157  // Find child index in vector of items using '==' operator
158  static int findItem( QVector<QgsCptCityDataItem *> items, QgsCptCityDataItem *item );
159 
160  // members
161 
162  Type type() const { return mType; }
163  QgsCptCityDataItem *parent() const { return mParent; }
164  void setParent( QgsCptCityDataItem *parent ) { mParent = parent; }
165  QVector<QgsCptCityDataItem *> children() const { return mChildren; }
166  virtual QIcon icon() { return mIcon; }
167  virtual QIcon icon( QSize size ) { Q_UNUSED( size ) ; return icon(); }
168  QString name() const { return mName; }
169  QString path() const { return mPath; }
170  QString info() const { return mInfo; }
171  QString shortInfo() const { return mShortInfo; }
172 
173  void setIcon( const QIcon &icon ) { mIcon = icon; }
174 
175  void setToolTip( const QString &msg ) { mToolTip = msg; }
176  QString toolTip() const { return mToolTip; }
177 
178  bool isValid() { return mValid; }
179 
180  protected:
181 
183  QgsCptCityDataItem *mParent = nullptr;
184  QVector<QgsCptCityDataItem *> mChildren; // easier to have it always
186  QString mName;
187  QString mPath; // it is also used to identify item in tree
188  QString mInfo;
189  QString mShortInfo;
190  QString mToolTip;
191  QIcon mIcon;
192  bool mValid;
193 
194  signals:
195  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
196  void endInsertItems();
197  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
198  void endRemoveItems();
199 };
200 
204 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
205 {
206  Q_OBJECT
207  public:
209  const QString &name, const QString &path,
210  const QString &variantName = QString(),
211  bool initialize = false );
213  const QString &name, const QString &path,
214  const QStringList &variantList,
215  bool initialize = false );
216 
217  // --- reimplemented from QgsCptCityDataItem ---
218 
219  virtual bool equal( const QgsCptCityDataItem *other ) override;
220  virtual int leafCount() const override { return 1; }
221 
222  // --- New virtual methods for layer item derived classes ---
223  const QgsCptCityColorRamp &ramp() const { return mRamp; }
224  QIcon icon() override;
225  QIcon icon( QSize size ) override;
226  void init();
227 
228  protected:
229 
232  QList< QIcon > mIcons;
233 };
234 
235 
240 {
241  Q_OBJECT
242  public:
244  const QString &name, const QString &path );
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 
259 {
260  Q_OBJECT
261  public:
263  const QString &name, const QString &path );
264 
265  QVector<QgsCptCityDataItem *> createChildren() override;
266 
267  virtual bool equal( const QgsCptCityDataItem *other ) override;
268 
269  static QgsCptCityDataItem *dataItem( QgsCptCityDataItem *parent,
270  const QString &name, const QString &path );
271 
272  protected:
273  QMap< QString, QStringList > rampsMap();
274  QStringList dirEntries() const;
275  QMap< QString, QStringList > mRampsMap;
276 };
277 
283 {
284  Q_OBJECT
285  public:
286  QgsCptCitySelectionItem( QgsCptCityDataItem *parent, const QString &name, const QString &path );
287 
288  QVector<QgsCptCityDataItem *> createChildren() override;
289 
290  virtual bool equal( const QgsCptCityDataItem *other ) override;
291 
292  QStringList selectionsList() const { return mSelectionsList; }
293 
294  protected:
295  void parseXml();
296  QStringList mSelectionsList;
297 };
298 
302 {
303  Q_OBJECT
304  public:
305  QgsCptCityAllRampsItem( QgsCptCityDataItem *parent, const QString &name,
306  const QVector<QgsCptCityDataItem *> &items );
307 
308  QVector<QgsCptCityDataItem *> createChildren() override;
309 
310  protected:
311  QVector<QgsCptCityDataItem *> mItems;
312 };
313 
317 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
318 {
319  Q_OBJECT
320 
321  public:
322 
323  enum ViewType
324  {
325  Authors = 0,
326  Selections = 1,
327  List = 2 // not used anymore
328  };
329 
330  QgsCptCityBrowserModel( QObject *parent = nullptr,
332  ViewType Type = Authors );
334 
335  // implemented methods from QAbstractItemModel for read-only access
336  virtual Qt::ItemFlags flags( const QModelIndex &index ) const override;
337  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
338  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
339  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
340  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
341  virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
342 
343  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
344 
345  virtual QModelIndex parent( const QModelIndex &index ) const override;
346 
348  /* virtual QStringList mimeTypes() const; */
349 
351  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
352 
354  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
355 
356  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
357 
358  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
359 
360  // Reload the whole model
361  void reload();
362 
363  // Refresh item specified by path
364  void refresh( const QString &path );
365 
366  // Refresh item children
367  void refresh( const QModelIndex &index = QModelIndex() );
368 
370  QModelIndex findPath( const QString &path );
371 
372  void connectItem( QgsCptCityDataItem *item );
373 
374  bool canFetchMore( const QModelIndex &parent ) const override;
375  void fetchMore( const QModelIndex &parent ) override;
376 
377  signals:
378 
379  public slots:
380  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
381  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
382  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
383 
384  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
385  void endInsertItems();
386  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
387  void endRemoveItems();
388 
389  protected:
390 
391  // populates the model
392  void addRootItems();
393  void removeRootItems();
394 
395  QVector<QgsCptCityDataItem *> mRootItems;
396  QgsCptCityArchive *mArchive = nullptr;
398  QSize mIconSize;
399 };
400 
401 #endif
QStringList selectionsList() const
virtual QList< QAction * > actions()
An "All ramps item", which contains all items in a flat hierarchy.
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:29
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:102
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:24
Base class for all items in the model.
QString path() const
#define SIP_FACTORY
Definition: qgis_sip.h:57
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