QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsvectorcolorrampv2.h"
21 
22 #include <QAbstractItemModel>
23 #include <QIcon>
24 #include <QMimeData>
25 #include <QAction>
26 
28 class QgsCptCityDataItem;
30 
31 #define DEFAULT_CPTCITY_ARCHIVE "cpt-city-qgis-min"
32 
37 class CORE_EXPORT QgsCptCityArchive
38 {
39  public:
41  const QString& baseDir = QString() );
43 
44  // basic dir info
45  QString baseDir() const;
46  static QString baseDir( QString archiveName );
47  static QString defaultBaseDir();
48  void setBaseDir( const QString& dirName ) { mBaseDir = dirName; }
49 
50  // collection + selection info
51  QString copyingFileName( const QString& dirName ) const;
52  QString descFileName( const QString& dirName ) const;
53  static QString findFileName( const QString & target, const QString & startDir, const QString & baseDir );
54  static QMap< QString, QString > copyingInfo( const QString& fileName );
55  static QMap< QString, QString > description( const QString& fileName );
57  static QMap< double, QPair<QColor, QColor> > gradientColorMap( const QString& fileName );
58 
59  // archive management
60  bool isEmpty();
61  QString archiveName() const { return mArchiveName; }
62  static void initArchives( bool loadAll = false );
63  static void initArchive( const QString& archiveName, const QString& archiveBaseDir );
64  static void initDefaultArchive();
65  static void clearArchives();
66  static QgsCptCityArchive* defaultArchive();
67  static QMap< QString, QgsCptCityArchive* > archiveRegistry();
68 
69  // items
70  QVector< QgsCptCityDataItem* > rootItems() const { return mRootItems; }
71  QVector< QgsCptCityDataItem* > selectionItems() const { return mSelectionItems; }
72 
73  protected:
74 
79  // root items, namely directories at root of archive
82  // mapping of copyinginfo, key is fileName
84 
85  private:
86 
88  QgsCptCityArchive& operator=( const QgsCptCityArchive& rh );
89 };
90 
94 class CORE_EXPORT QgsCptCityDataItem : public QObject
95 {
96  Q_OBJECT
97  public:
98  enum Type
99  {
104  AllRamps
105  };
106 
108  const QString& name, const QString& path );
109  virtual ~QgsCptCityDataItem();
110 
111  bool hasChildren();
112 
113  int rowCount();
114  // retrieve total count of "leaf" items (all children which are end nodes)
115  virtual int leafCount() const;
116 
117  //
118 
119  virtual void refresh();
120 
121  // Create vector of children
122  virtual QVector<QgsCptCityDataItem*> createChildren();
123 
124  // Populate children using children vector created by createChildren()
125  virtual void populate();
126  bool isPopulated() { return mPopulated; }
127 
128  // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
129  // refresh - refresh populated item, emit signals to model
130  virtual void addChildItem( QgsCptCityDataItem * child, bool refresh = false );
131 
132  // remove and delete child item, signals to browser are emitted
133  virtual void deleteChildItem( QgsCptCityDataItem * child );
134 
135  // remove child item but don't delete it, signals to browser are emitted
136  // returns pointer to the removed item or null if no such item was found
137  virtual QgsCptCityDataItem * removeChildItem( QgsCptCityDataItem * child );
138 
139  virtual bool equal( const QgsCptCityDataItem *other );
140 
141  virtual QWidget *paramWidget() { return nullptr; }
142 
143  // list of actions provided by this item - usually used for popup menu on right-click
144  virtual QList<QAction*> actions() { return QList<QAction*>(); }
145 
146  // whether accepts drag&drop'd layers - e.g. for import
147  virtual bool acceptDrop() { return false; }
148 
149  // try to process the data dropped on this item
150  virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
151 
152  // static methods
153 
154  // Find child index in vector of items using '==' operator
155  static int findItem( QVector<QgsCptCityDataItem*> items, QgsCptCityDataItem * item );
156 
157  // members
158 
159  Type type() const { return mType; }
160  QgsCptCityDataItem* parent() const { return mParent; }
161  void setParent( QgsCptCityDataItem* parent ) { mParent = parent; }
162  QVector<QgsCptCityDataItem*> children() const { return mChildren; }
163  virtual QIcon icon() { return mIcon; }
164  virtual QIcon icon( QSize size ) { Q_UNUSED( size ) ; return icon(); }
165  QString name() const { return mName; }
166  QString path() const { return mPath; }
167  QString info() const { return mInfo; }
168  QString shortInfo() const { return mShortInfo; }
169 
170  void setIcon( const QIcon& icon ) { mIcon = icon; }
171 
172  void setToolTip( const QString& msg ) { mToolTip = msg; }
173  QString toolTip() const { return mToolTip; }
174 
175  bool isValid() { return mValid; }
176 
177  protected:
178 
181  QVector<QgsCptCityDataItem*> mChildren; // easier to have it always
184  QString mPath; // it is also used to identify item in tree
189  bool mValid;
190 
191  public slots:
192  void emitBeginInsertItems( QgsCptCityDataItem* parent, int first, int last );
193  void emitEndInsertItems();
194  void emitBeginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
195  void emitEndRemoveItems();
196 
197  signals:
198  void beginInsertItems( QgsCptCityDataItem* parent, int first, int last );
199  void endInsertItems();
200  void beginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
201  void endRemoveItems();
202 };
203 
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 );
220 
221  // --- reimplemented from QgsCptCityDataItem ---
222 
223  virtual bool equal( const QgsCptCityDataItem *other ) override;
224  virtual int leafCount() const override { return 1; }
225 
226  // --- New virtual methods for layer item derived classes ---
227  const QgsCptCityColorRampV2& ramp() const { return mRamp; }
228  QIcon icon() override;
229  QIcon icon( QSize size ) override;
230  void init();
231 
232  protected:
233 
237 };
238 
239 
244 {
245  Q_OBJECT
246  public:
248  const QString& name, const QString& path );
250 
251  void setPopulated() { mPopulated = true; }
252  void addChild( QgsCptCityDataItem *item ) { mChildren.append( item ); }
253  QVector<QgsCptCityDataItem*> childrenRamps( bool recursive );
254 
255  protected:
257 };
258 
263 {
264  Q_OBJECT
265  public:
267  const QString& name, const QString& path );
269 
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;
281 };
282 
288 {
289  Q_OBJECT
290  public:
291  QgsCptCitySelectionItem( QgsCptCityDataItem* parent, const QString& name, const QString& path );
293 
295 
296  virtual bool equal( const QgsCptCityDataItem *other ) override;
297 
298  QStringList selectionsList() const { return mSelectionsList; }
299 
300  protected:
301  void parseXML();
303 };
304 
308 {
309  Q_OBJECT
310  public:
311  QgsCptCityAllRampsItem( QgsCptCityDataItem* parent, const QString& name,
312  const QVector<QgsCptCityDataItem*>& items );
314 
316 
317  protected:
319 };
320 
324 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
325 {
326  Q_OBJECT
327 
328  public:
329 
330  enum ViewType
331  {
332  Authors = 0,
333  Selections = 1,
334  List = 2 // not used anymore
335  };
336 
337  QgsCptCityBrowserModel( QObject* parent = nullptr,
339  ViewType Type = Authors );
341 
342  // implemented methods from QAbstractItemModel for read-only access
343 
346  virtual Qt::ItemFlags flags( const QModelIndex &index ) const override;
347 
352  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
353 
356  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
357 
359  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
360 
363  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
364 
366  virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const override;
367 
368  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
369 
373  virtual QModelIndex parent( const QModelIndex &index ) const override;
374 
376  /* virtual QStringList mimeTypes() const; */
377 
379  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
380 
382  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
383 
384  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
385 
386  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
387 
388  // Reload the whole model
389  void reload();
390 
391  // Refresh item specified by path
392  void refresh( const QString& path );
393 
394  // Refresh item childs
395  void refresh( const QModelIndex &index = QModelIndex() );
396 
398  QModelIndex findPath( const QString& path );
399 
400  void connectItem( QgsCptCityDataItem *item );
401 
402  bool canFetchMore( const QModelIndex & parent ) const override;
403  void fetchMore( const QModelIndex & parent ) override;
404 
405  signals:
406 
407  public slots:
408  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
409  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
410  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
411 
412  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
413  void endInsertItems();
414  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
415  void endRemoveItems();
416 
417  protected:
418 
419  // populates the model
420  void addRootItems();
421  void removeRootItems();
422 
427 };
428 
429 #endif
QStringList selectionsList() const
static unsigned index
virtual int rowCount(const QModelIndex &parent) const=0
QgsCptCityDataItem * mParent
QVector< QgsCptCityDataItem * > mItems
An "All ramps item", which contains all items in a flat hierarchy.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const=0
static QgsCptCityArchive * defaultArchive()
QVector< QgsCptCityDataItem * > children() const
static QMap< QString, QgsCptCityArchive *> mArchiveRegistry
virtual bool canFetchMore(const QModelIndex &parent) const
const QgsCptCityColorRampV2 & ramp() const
QString name() const
virtual QList< QAction * > actions()
Item that represents a layer that can be opened with one of the providers.
QMap< QString, QStringList > mRampsMap
QString toolTip() const
virtual QVector< QgsCptCityDataItem * > createChildren()
QgsCptCityArchive * mArchive
QVector< QgsCptCityDataItem *> selectionItems() const
void setIcon(const QIcon &icon)
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
virtual bool equal(const QgsCptCityDataItem *other)
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
QVector< QgsCptCityDataItem * > mSelectionItems
QgsCptCityColorRampV2 mRamp
virtual QVariant data(const QModelIndex &index, int role) const=0
A Collection: logical collection of subcollections and color ramps.
QVector< QgsCptCityDataItem * > mChildren
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
QString shortInfo() const
virtual QIcon icon()
QString archiveName() const
#define DEFAULT_CPTCITY_ARCHIVE
static QMap< QString, QMap< QString, QString > > mCopyingInfoMap
void addChild(QgsCptCityDataItem *item)
void setParent(QgsCptCityDataItem *parent)
static QString mDefaultArchiveName
virtual bool hasChildren(const QModelIndex &parent) const
virtual int columnCount(const QModelIndex &parent) const=0
QVector< QgsCptCityDataItem * > mRootItems
QString info() const
virtual void fetchMore(const QModelIndex &parent)
virtual QWidget * paramWidget()
virtual QIcon icon(QSize size)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual int leafCount() const override
void setBaseDir(const QString &dirName)
QVector< QgsCptCityDataItem *> rootItems() const
A selection: contains subdirectories and color ramps.
QObject * parent() const
QgsCptCityDataItem * parent() const
QVector< QgsCptCityDataItem *> mRootItems
typedef ItemFlags