QGIS API Documentation  2.99.0-Master (37c43df)
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 "qgscolorramp.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:
40  QgsCptCityArchive( const QString& archiveName = DEFAULT_CPTCITY_ARCHIVE,
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 
75  QString mArchiveName;
76  QString mBaseDir;
77  static QString mDefaultArchiveName;
78  static QMap< QString, QgsCptCityArchive* > mArchiveRegistry;
79  // root items, namely directories at root of archive
80  QVector< QgsCptCityDataItem* > mRootItems;
81  QVector<QgsCptCityDataItem*> mSelectionItems;
82  // mapping of copyinginfo, key is fileName
83  static QMap< QString, QMap< QString, QString > > mCopyingInfoMap;
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
183  QString mName;
184  QString mPath; // it is also used to identify item in tree
185  QString mInfo;
186  QString mShortInfo;
187  QString mToolTip;
188  QIcon mIcon;
189  bool mValid;
190 
191  signals:
192  void beginInsertItems( QgsCptCityDataItem* parent, int first, int last );
193  void endInsertItems();
194  void beginRemoveItems( QgsCptCityDataItem* parent, int first, int last );
195  void endRemoveItems();
196 };
197 
201 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
202 {
203  Q_OBJECT
204  public:
206  const QString& name, const QString& path,
207  const QString& variantName = QString(),
208  bool initialize = false );
210  const QString& name, const QString& path,
211  const QStringList& variantList,
212  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 );
263 
264  QVector<QgsCptCityDataItem*> createChildren() override;
265 
266  virtual bool equal( const QgsCptCityDataItem *other ) override;
267 
268  static QgsCptCityDataItem* dataItem( QgsCptCityDataItem* parent,
269  const QString& name, const QString& path );
270 
271  protected:
272  QMap< QString, QStringList > rampsMap();
273  QStringList dirEntries() const;
274  QMap< QString, QStringList > mRampsMap;
275 };
276 
282 {
283  Q_OBJECT
284  public:
285  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 );
308 
309  QVector<QgsCptCityDataItem*> createChildren() override;
310 
311  protected:
312  QVector<QgsCptCityDataItem*> mItems;
313 };
314 
318 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
319 {
320  Q_OBJECT
321 
322  public:
323 
324  enum ViewType
325  {
326  Authors = 0,
327  Selections = 1,
328  List = 2 // not used anymore
329  };
330 
331  QgsCptCityBrowserModel( QObject* parent = nullptr,
333  ViewType Type = Authors );
335 
336  // implemented methods from QAbstractItemModel for read-only access
337  virtual Qt::ItemFlags flags( const QModelIndex &index ) const override;
338  virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
339  virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
340  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
341  virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
342  virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const override;
343 
344  QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = nullptr ) const;
345 
346  virtual QModelIndex parent( const QModelIndex &index ) const override;
347 
349  /* virtual QStringList mimeTypes() const; */
350 
352  /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */
353 
355  /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */
356 
357  QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const;
358 
359  bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
360 
361  // Reload the whole model
362  void reload();
363 
364  // Refresh item specified by path
365  void refresh( const QString& path );
366 
367  // Refresh item childs
368  void refresh( const QModelIndex &index = QModelIndex() );
369 
371  QModelIndex findPath( const QString& path );
372 
373  void connectItem( QgsCptCityDataItem *item );
374 
375  bool canFetchMore( const QModelIndex & parent ) const override;
376  void fetchMore( const QModelIndex & parent ) override;
377 
378  signals:
379 
380  public slots:
381  //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
382  //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
383  //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items );
384 
385  void beginInsertItems( QgsCptCityDataItem *parent, int first, int last );
386  void endInsertItems();
387  void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last );
388  void endRemoveItems();
389 
390  protected:
391 
392  // populates the model
393  void addRootItems();
394  void removeRootItems();
395 
396  QVector<QgsCptCityDataItem*> mRootItems;
399  QSize mIconSize;
400 };
401 
402 #endif
QStringList selectionsList() const
static unsigned index
QgsCptCityDataItem * mParent
QVector< QgsCptCityDataItem * > mItems
An "All ramps item", which contains all items in a flat hierarchy.
static QgsCptCityArchive * defaultArchive()
QVector< QgsCptCityDataItem * > children() const
static QMap< QString, QgsCptCityArchive *> mArchiveRegistry
QgsCptCityColorRamp mRamp
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)
const QgsCptCityColorRamp & ramp() const
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
A Collection: logical collection of subcollections and color ramps.
QVector< QgsCptCityDataItem * > mChildren
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
QVector< QgsCptCityDataItem * > mRootItems
QString info() const
virtual QWidget * paramWidget()
virtual QIcon icon(QSize size)
virtual int leafCount() const override
void setBaseDir(const QString &dirName)
QVector< QgsCptCityDataItem *> rootItems() const
A selection: contains subdirectories and color ramps.
QgsCptCityDataItem * parent() const
QVector< QgsCptCityDataItem *> mRootItems