QGIS API Documentation  2.99.0-Master (314842d)
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 "qgscolorramp.h"
22 
23 #include <QAbstractItemModel>
24 #include <QIcon>
25 #include <QMimeData>
26 #include <QAction>
27 
29 class QgsCptCityDataItem;
31 
32 #define DEFAULT_CPTCITY_ARCHIVE "cpt-city-qgis-min"
33 
38 class CORE_EXPORT QgsCptCityArchive
39 {
40  public:
41  QgsCptCityArchive( const QString &archiveName = DEFAULT_CPTCITY_ARCHIVE,
42  const QString &baseDir = QString() );
44 
46  QgsCptCityArchive( const QgsCptCityArchive &rh ) = delete;
48  QgsCptCityArchive &operator=( const QgsCptCityArchive &rh ) = delete;
49 
50  // basic dir info
51  QString baseDir() const;
52  static QString baseDir( QString archiveName );
53  static QString defaultBaseDir();
54  void setBaseDir( const QString &dirName ) { mBaseDir = dirName; }
55 
56  // collection + selection info
57  QString copyingFileName( const QString &dirName ) const;
58  QString descFileName( const QString &dirName ) const;
59  static QString findFileName( const QString &target, const QString &startDir, const QString &baseDir );
60  static QMap< QString, QString > copyingInfo( const QString &fileName );
61  static QMap< QString, QString > description( const QString &fileName );
63  static QMap< double, QPair<QColor, QColor> > gradientColorMap( const QString &fileName );
64 
65  // archive management
66  bool isEmpty();
67  QString archiveName() const { return mArchiveName; }
68  static void initArchives( bool loadAll = false );
69  static void initArchive( const QString &archiveName, const QString &archiveBaseDir );
70  static void initDefaultArchive();
71  static void clearArchives();
72  static QgsCptCityArchive *defaultArchive();
73  static QMap< QString, QgsCptCityArchive * > archiveRegistry();
74 
75  // items
76  QVector< QgsCptCityDataItem * > rootItems() const { return mRootItems; }
77  QVector< QgsCptCityDataItem * > selectionItems() const { return mSelectionItems; }
78 
79  protected:
80 
81  QString mArchiveName;
82  QString mBaseDir;
83  static QString sDefaultArchiveName;
84  static QMap< QString, QgsCptCityArchive * > sArchiveRegistry;
85  // root items, namely directories at root of archive
86  QVector< QgsCptCityDataItem * > mRootItems;
87  QVector<QgsCptCityDataItem *> mSelectionItems;
88  // mapping of copyinginfo, key is fileName
89  static QMap< QString, QMap< QString, QString > > sCopyingInfoMap;
90 
91 };
92 
96 class CORE_EXPORT QgsCptCityDataItem : public QObject
97 {
98  Q_OBJECT
99  public:
100  enum Type
101  {
106  AllRamps
107  };
108 
110  const QString &name, const QString &path );
111 
112  bool hasChildren();
113 
114  int rowCount();
115  // retrieve total count of "leaf" items (all children which are end nodes)
116  virtual int leafCount() const;
117 
118  //
119 
120  virtual void refresh();
121 
122  // Create vector of children
123  virtual QVector<QgsCptCityDataItem *> createChildren();
124 
125  // Populate children using children vector created by createChildren()
126  virtual void populate();
127  bool isPopulated() { return mPopulated; }
128 
129  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
130  // refresh - refresh populated item, emit signals to model
131  virtual void addChildItem( QgsCptCityDataItem *child, bool refresh = false );
132 
133  // remove and delete child item, signals to browser are emitted
134  virtual void deleteChildItem( QgsCptCityDataItem *child );
135 
136  // remove child item but don't delete it, signals to browser are emitted
137  // returns pointer to the removed item or null if no such item was found
138  virtual QgsCptCityDataItem *removeChildItem( QgsCptCityDataItem *child );
139 
140  virtual bool equal( const QgsCptCityDataItem *other );
141 
142  virtual QWidget *paramWidget() { return nullptr; }
143 
144  // list of actions provided by this item - usually used for popup menu on right-click
145  virtual QList<QAction *> actions() { return QList<QAction *>(); }
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 
202 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
203 {
204  Q_OBJECT
205  public:
207  const QString &name, const QString &path,
208  const QString &variantName = QString(),
209  bool initialize = false );
211  const QString &name, const QString &path,
212  const QStringList &variantList,
213  bool initialize = false );
214 
215  // --- reimplemented from QgsCptCityDataItem ---
216 
217  virtual bool equal( const QgsCptCityDataItem *other ) override;
218  virtual int leafCount() const override { return 1; }
219 
220  // --- New virtual methods for layer item derived classes ---
221  const QgsCptCityColorRamp &ramp() const { return mRamp; }
222  QIcon icon() override;
223  QIcon icon( QSize size ) override;
224  void init();
225 
226  protected:
227 
230  QList< QIcon > mIcons;
231 };
232 
233 
238 {
239  Q_OBJECT
240  public:
242  const QString &name, const QString &path );
244 
245  void setPopulated() { mPopulated = true; }
246  void addChild( QgsCptCityDataItem *item ) { mChildren.append( item ); }
247  QVector<QgsCptCityDataItem *> childrenRamps( bool recursive );
248 
249  protected:
251 };
252 
257 {
258  Q_OBJECT
259  public:
261  const QString &name, const QString &path );
262 
263  QVector<QgsCptCityDataItem *> createChildren() override;
264 
265  virtual bool equal( const QgsCptCityDataItem *other ) override;
266 
267  static QgsCptCityDataItem *dataItem( QgsCptCityDataItem *parent,
268  const QString &name, const QString &path );
269 
270  protected:
271  QMap< QString, QStringList > rampsMap();
272  QStringList dirEntries() const;
273  QMap< QString, QStringList > mRampsMap;
274 };
275 
281 {
282  Q_OBJECT
283  public:
284  QgsCptCitySelectionItem( QgsCptCityDataItem *parent, const QString &name, const QString &path );
285 
286  QVector<QgsCptCityDataItem *> createChildren() override;
287 
288  virtual bool equal( const QgsCptCityDataItem *other ) override;
289 
290  QStringList selectionsList() const { return mSelectionsList; }
291 
292  protected:
293  void parseXml();
294  QStringList mSelectionsList;
295 };
296 
300 {
301  Q_OBJECT
302  public:
303  QgsCptCityAllRampsItem( QgsCptCityDataItem *parent, const QString &name,
304  const QVector<QgsCptCityDataItem *> &items );
305 
306  QVector<QgsCptCityDataItem *> createChildren() override;
307 
308  protected:
309  QVector<QgsCptCityDataItem *> mItems;
310 };
311 
315 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
316 {
317  Q_OBJECT
318 
319  public:
320 
321  enum ViewType
322  {
323  Authors = 0,
324  Selections = 1,
325  List = 2 // not used anymore
326  };
327 
328  QgsCptCityBrowserModel( QObject *parent = nullptr,
330  ViewType Type = Authors );
332 
333  // implemented methods from QAbstractItemModel for read-only access
334  virtual Qt::ItemFlags flags( const QModelIndex &index ) const override;
335  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
336  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
337  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
338  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
339  virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
340 
341  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
342 
343  virtual QModelIndex parent( const QModelIndex &index ) const override;
344 
346  /* virtual QStringList mimeTypes() const; */
347 
349  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
350 
352  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
353 
354  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
355 
356  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
357 
358  // Reload the whole model
359  void reload();
360 
361  // Refresh item specified by path
362  void refresh( const QString &path );
363 
364  // Refresh item children
365  void refresh( const QModelIndex &index = QModelIndex() );
366 
368  QModelIndex findPath( const QString &path );
369 
370  void connectItem( QgsCptCityDataItem *item );
371 
372  bool canFetchMore( const QModelIndex &parent ) const override;
373  void fetchMore( const QModelIndex &parent ) override;
374 
375  signals:
376 
377  public slots:
378  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
379  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
380  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
381 
382  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
383  void endInsertItems();
384  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
385  void endRemoveItems();
386 
387  protected:
388 
389  // populates the model
390  void addRootItems();
391  void removeRootItems();
392 
393  QVector<QgsCptCityDataItem *> mRootItems;
394  QgsCptCityArchive *mArchive = nullptr;
396  QSize mIconSize;
397 };
398 
399 #endif
QStringList selectionsList() const
virtual QList< QAction * > actions()
static unsigned index
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
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)
QVector< QgsCptCityDataItem *> rootItems() const
virtual bool acceptDrop()
A directory: contains subdirectories and color ramps.
void setToolTip(const QString &msg)
Base class for all items in the model.
QString path() const
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