QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsdataitem.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdataitem.h - Items representing data
3  -------------------
4  begin : 2011-04-01
5  copyright : (C) 2011 Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSDATAITEM_H
18 #define QGSDATAITEM_H
19 
20 #include "qgis_sip.h"
21 #include "qgis_core.h"
22 #include "qgsmimedatautils.h"
23 #include "qgis.h"
24 #include <QObject>
25 #include <QFutureWatcher>
26 #include <QIcon>
27 
28 class QgsDataItem;
29 class QMenu;
31 class QgsAnimatedIcon;
33 
34 class QAction;
35 class QWidget;
36 
37 typedef QgsDataItem *dataItem_t( QString, QgsDataItem * ) SIP_SKIP;
38 
45 class CORE_EXPORT QgsDataItem : public QObject
46 {
47 #ifdef SIP_RUN
48 #include "qgslayeritem.h"
49 #include "qgsdirectoryitem.h"
50 #include "qgsfavoritesitem.h"
51 #include "qgszipitem.h"
52 #include "qgsdatacollectionitem.h"
53 #include "qgsprojectitem.h"
54 #endif
55 
56 #ifdef SIP_RUN
58  if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
59  sipType = sipType_QgsLayerItem;
60  else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
61  sipType = sipType_QgsErrorItem;
62  else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
63  sipType = sipType_QgsDirectoryItem;
64  else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
65  sipType = sipType_QgsFavoritesItem;
66  else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
67  sipType = sipType_QgsZipItem;
68  else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
69  sipType = sipType_QgsDataCollectionItem;
70  else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
71  sipType = sipType_QgsProjectItem;
72  else
73  sipType = 0;
74  SIP_END
75 #endif
76 
77  Q_OBJECT
78 
79  public:
80 
92  QgsDataItem( Qgis::BrowserItemType type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path, const QString &providerKey = QString() );
93 
94  ~QgsDataItem() override;
95 
96 #ifdef SIP_RUN
97  SIP_PYOBJECT __repr__();
98  % MethodCode
99  QString str = QStringLiteral( "<QgsDataItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
100  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
101  % End
102 #endif
103 
104  bool hasChildren();
105 
111  virtual bool layerCollection() const;
112 
113  int rowCount();
114 
119  virtual QVector<QgsDataItem *> createChildren() SIP_TRANSFERBACK;
120 #ifdef SIP_RUN
122  PyObject *sipResObj = sipCallMethod( 0, sipMethod, "" );
123  // H = Convert a Python object to a mapped type instance.
124  // 5 = 1 (disallows the conversion of Py_None to NULL) + 4 (returns a copy of the C/C++ instance)
125  sipIsErr = !sipResObj || sipParseResult( 0, sipMethod, sipResObj, "H5", sipType_QVector_0101QgsDataItem, &sipRes ) < 0;
126  if ( !sipIsErr )
127  {
128  for ( QgsDataItem *item : sipRes )
129  {
130  PyObject *pyItem = sipGetPyObject( item, sipType_QgsDataItem );
131  if ( pyItem != NULL )
132  {
133  // pyItem is given an extra reference which is removed when the C++ instance’s destructor is called.
134  sipTransferTo( pyItem, Py_None );
135  }
136  }
137  }
138  if ( sipResObj != NULL )
139  {
140  Py_DECREF( sipResObj );
141  }
142  SIP_END
143 #endif
144 
146  Qgis::BrowserItemState state() const;
147 
153  virtual void setState( Qgis::BrowserItemState state );
154 
161  virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
162 
168  virtual void deleteChildItem( QgsDataItem *child );
169 
175  virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
176 
180  virtual bool equal( const QgsDataItem *other );
181 
189  Q_DECL_DEPRECATED virtual QWidget *paramWidget() SIP_FACTORY SIP_DEPRECATED { return nullptr; }
190 
198  virtual QList<QAction *> actions( QWidget *parent );
199 
208  virtual QList<QMenu *> menus( QWidget *parent );
209 
218  Q_DECL_DEPRECATED virtual bool acceptDrop() SIP_DEPRECATED { return false; }
219 
228  Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
229 
236  virtual bool handleDoubleClick();
237 
245  virtual bool hasDragEnabled() const { return false; }
246 
254  Q_DECL_DEPRECATED virtual QgsMimeDataUtils::Uri mimeUri() const SIP_DEPRECATED;
255 
264  virtual QgsMimeDataUtils::UriList mimeUris() const { return QgsMimeDataUtils::UriList(); }
265 
272  Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
273 
286  Q_DECL_DEPRECATED virtual bool rename( const QString &name ) SIP_DEPRECATED;
287 
288  // ### QGIS 4 - rename to capabilities()
289 
295  virtual Qgis::BrowserItemCapabilities capabilities2() const { return mCapabilities; }
296 
302  virtual void setCapabilities( Qgis::BrowserItemCapabilities capabilities ) SIP_PYNAME( setCapabilitiesV2 ) { mCapabilities = capabilities; }
303 
307  Q_DECL_DEPRECATED void setCapabilities( int capabilities ) SIP_DEPRECATED;
308 
309  // static methods
310 
311  // Find child index in vector of items using '==' operator
312  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
313 
314  // members
315 
316  Qgis::BrowserItemType type() const { return mType; }
317 
322  QgsDataItem *parent() const { return mParent; }
323 
328  void setParent( QgsDataItem *parent );
329  QVector<QgsDataItem *> children() const { return mChildren; }
330  virtual QIcon icon();
331 
337  QString name() const { return mName; }
338 
344  void setName( const QString &name );
345 
346  QString path() const { return mPath; }
347  void setPath( const QString &path ) { mPath = path; }
348 
357  QString providerKey() const;
358 
367  void setProviderKey( const QString &value );
368 
370  static QString pathComponent( const QString &component );
371 
382  virtual QVariant sortKey() const;
383 
389  void setSortKey( const QVariant &key );
390 
391 
392  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
393  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
394  // or implement icon().
395  void setIcon( const QIcon &icon ) { mIcon = icon; }
396  void setIconName( const QString &iconName ) { mIconName = iconName; }
397 
398  void setToolTip( const QString &msg ) { mToolTip = msg; }
399  QString toolTip() const { return mToolTip; }
400 
401  // deleteLater() items and clear the vector
402  static void deleteLater( QVector<QgsDataItem *> &items );
403 
405  void moveToThread( QThread *targetThread );
406 
416  virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
417 
418  protected:
419  virtual void populate( const QVector<QgsDataItem *> &children );
420 
424  virtual void refresh( const QVector<QgsDataItem *> &children );
425 
434  bool deferredDelete() { return mDeferredDelete; }
435 
437  Qgis::BrowserItemCapabilities mCapabilities = Qgis::BrowserItemCapability::NoCapabilities;
438  QgsDataItem *mParent = nullptr;
439  QVector<QgsDataItem *> mChildren; // easier to have it always
441  QString mName;
442  QString mProviderKey;
443  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
444  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
445  // name but different paths). Identifiers in path must not contain '/' characters.
446  // The path is used to identify item in tree.
447  QString mPath;
448  QString mToolTip;
449  QString mIconName;
450  QIcon mIcon;
451  QMap<QString, QIcon> mIconMap;
452 
454  QVariant mSortKey;
455 
456  public slots:
457 
467  virtual void deleteLater();
468 
469  // Populate children using children vector created by createChildren()
470  // \param foreground run createChildren in foreground
471  virtual void populate( bool foreground = false );
472 
474  virtual void depopulate();
475 
476  virtual void refresh();
477 
484  virtual void refreshConnections( const QString &providerKey = QString() );
485 
486  virtual void childrenCreated();
487 
488  signals:
489  void beginInsertItems( QgsDataItem *parent, int first, int last );
491  void beginRemoveItems( QgsDataItem *parent, int first, int last );
493  void dataChanged( QgsDataItem *item );
494 
499 
507  void connectionsChanged( const QString &providerKey = QString() );
508 
509  protected slots:
510 
516  void updateIcon();
517 
518  private:
519  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
520 
521  // Set to true if object has to be deleted when possible (nothing running in threads)
522  bool mDeferredDelete = false;
523  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher = nullptr;
524  // number of items currently in loading (populating) state
525  static QgsAnimatedIcon *sPopulatingIcon;
526 };
527 
532 class CORE_EXPORT QgsErrorItem : public QgsDataItem
533 {
534  Q_OBJECT
535  public:
536 
537  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
538 
539 #ifdef SIP_RUN
540  SIP_PYOBJECT __repr__();
541  % MethodCode
542  QString str = QStringLiteral( "<QgsErrorItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
543  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
544  % End
545 #endif
546 
547 };
548 
549 #endif // QGSDATAITEM_H
550 
551 
BrowserItemState
Browser item states.
Definition: qgis.h:249
@ NotPopulated
Children not yet created.
@ NoCapabilities
Item has no capabilities.
BrowserItemType
Browser item types.
Definition: qgis.h:230
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Animated icon is keeping an animation running if there are listeners connected to frameChanged.
This class represents a coordinate reference system (CRS).
Base class for all items in the model.
Definition: qgsdataitem.h:46
void stateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
QString mToolTip
Definition: qgsdataitem.h:448
QString mName
Definition: qgsdataitem.h:441
Qgis::BrowserItemType mType
Definition: qgsdataitem.h:436
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:439
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:398
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:228
void dataChanged(QgsDataItem *item)
void endRemoveItems()
QString mPath
Definition: qgsdataitem.h:447
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:329
void beginRemoveItems(QgsDataItem *parent, int first, int last)
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:322
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:218
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:316
QString mIconName
Definition: qgsdataitem.h:449
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:451
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:245
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:454
void connectionsChanged(const QString &providerKey=QString())
Emitted when the connections of the provider with the specified providerKey have changed.
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:337
QString path() const
Definition: qgsdataitem.h:346
void beginInsertItems(QgsDataItem *parent, int first, int last)
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:302
virtual Q_DECL_DEPRECATED QWidget * paramWidget()
Returns source widget from data item for QgsBrowserPropertiesWidget.
Definition: qgsdataitem.h:189
QString toolTip() const
Definition: qgsdataitem.h:399
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:395
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:396
void setPath(const QString &path)
Definition: qgsdataitem.h:347
void endInsertItems()
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:295
QString mProviderKey
Definition: qgsdataitem.h:442
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:533
QList< QgsMimeDataUtils::Uri > UriList
#define str(x)
Definition: qgis.cpp:37
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_VIRTUAL_CATCHER_CODE(code)
Definition: qgis_sip.h:204
#define SIP_END
Definition: qgis_sip.h:194
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:37
const QgsCoordinateReferenceSystem & crs