QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
220 #ifdef __clang__
221 #pragma clang diagnostic push
222 #pragma clang diagnostic ignored "-Woverloaded-virtual"
223 #endif
224 
233  Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
234 #ifdef __clang__
235 #pragma clang diagnostic pop
236 #endif
237 
244  virtual bool handleDoubleClick();
245 
253  virtual bool hasDragEnabled() const { return false; }
254 
262  Q_DECL_DEPRECATED virtual QgsMimeDataUtils::Uri mimeUri() const SIP_DEPRECATED;
263 
272  virtual QgsMimeDataUtils::UriList mimeUris() const;
273 
280  Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
281 
294  Q_DECL_DEPRECATED virtual bool rename( const QString &name ) SIP_DEPRECATED;
295 
296  // ### QGIS 4 - rename to capabilities()
297 
303  virtual Qgis::BrowserItemCapabilities capabilities2() const { return mCapabilities; }
304 
310  virtual void setCapabilities( Qgis::BrowserItemCapabilities capabilities ) SIP_PYNAME( setCapabilitiesV2 ) { mCapabilities = capabilities; }
311 
315  Q_DECL_DEPRECATED void setCapabilities( int capabilities ) SIP_DEPRECATED;
316 
317  // static methods
318 
319  // Find child index in vector of items using '==' operator
320  static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
321 
322  // members
323 
324  Qgis::BrowserItemType type() const { return mType; }
325 
330  QgsDataItem *parent() const { return mParent; }
331 
336  void setParent( QgsDataItem *parent );
337  QVector<QgsDataItem *> children() const { return mChildren; }
338  virtual QIcon icon();
339 
345  QString name() const { return mName; }
346 
352  void setName( const QString &name );
353 
354  QString path() const { return mPath; }
355  void setPath( const QString &path ) { mPath = path; }
356 
365  QString providerKey() const;
366 
375  void setProviderKey( const QString &value );
376 
378  static QString pathComponent( const QString &component );
379 
390  virtual QVariant sortKey() const;
391 
397  void setSortKey( const QVariant &key );
398 
399 
400  // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
401  // not possible to set mIcon in constructor. Either use mIconName/setIconName()
402  // or implement icon().
403  void setIcon( const QIcon &icon ) { mIcon = icon; }
404  void setIconName( const QString &iconName ) { mIconName = iconName; }
405 
406  void setToolTip( const QString &msg ) { mToolTip = msg; }
407  QString toolTip() const { return mToolTip; }
408 
409  // deleteLater() items and clear the vector
410  static void deleteLater( QVector<QgsDataItem *> &items );
411 
413  void moveToThread( QThread *targetThread );
414 
424  virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
425 
426  protected:
427  virtual void populate( const QVector<QgsDataItem *> &children );
428 
432  virtual void refresh( const QVector<QgsDataItem *> &children );
433 
442  bool deferredDelete() { return mDeferredDelete; }
443 
445  Qgis::BrowserItemCapabilities mCapabilities = Qgis::BrowserItemCapability::NoCapabilities;
446  QgsDataItem *mParent = nullptr;
447  QVector<QgsDataItem *> mChildren; // easier to have it always
449  QString mName;
450  QString mProviderKey;
451  // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
452  // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
453  // name but different paths). Identifiers in path must not contain '/' characters.
454  // The path is used to identify item in tree.
455  QString mPath;
456  QString mToolTip;
457  QString mIconName;
458  QIcon mIcon;
459  QMap<QString, QIcon> mIconMap;
460 
462  QVariant mSortKey;
463 
464  public slots:
465 
475  virtual void deleteLater();
476 
477  // Populate children using children vector created by createChildren()
478  // \param foreground run createChildren in foreground
479  virtual void populate( bool foreground = false );
480 
482  virtual void depopulate();
483 
484  virtual void refresh();
485 
492  virtual void refreshConnections( const QString &providerKey = QString() );
493 
494  virtual void childrenCreated();
495 
496  signals:
497  void beginInsertItems( QgsDataItem *parent, int first, int last );
498  void endInsertItems();
499  void beginRemoveItems( QgsDataItem *parent, int first, int last );
500  void endRemoveItems();
501  void dataChanged( QgsDataItem *item );
502 
506  void stateChanged( QgsDataItem *item, Qgis::BrowserItemState oldState );
507 
515  void connectionsChanged( const QString &providerKey = QString() );
516 
517  protected slots:
518 
524  void updateIcon();
525 
526  private:
527  static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
528 
529  // Set to true if object has to be deleted when possible (nothing running in threads)
530  bool mDeferredDelete = false;
531  QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher = nullptr;
532  // number of items currently in loading (populating) state
533  static QgsAnimatedIcon *sPopulatingIcon;
534 };
535 
540 class CORE_EXPORT QgsErrorItem : public QgsDataItem
541 {
542  Q_OBJECT
543  public:
544 
545  QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
546 
547 #ifdef SIP_RUN
548  SIP_PYOBJECT __repr__();
549  % MethodCode
550  QString str = QStringLiteral( "<QgsErrorItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
551  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
552  % End
553 #endif
554 
555 };
556 
557 #endif // QGSDATAITEM_H
558 
559 
QgsDataItem::setIconName
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:404
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsDataItem::mIconMap
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:459
QgsDataItem::mIcon
QIcon mIcon
Definition: qgsdataitem.h:458
qgsdirectoryitem.h
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:354
QgsDataItem::setIcon
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:403
Qgis::BrowserItemState::NotPopulated
@ NotPopulated
Children not yet created.
Qgis::BrowserItemState
BrowserItemState
Browser item states.
Definition: qgis.h:367
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
qgis.h
QgsDataItem::setToolTip
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
QgsDataItem::name
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:345
QgsDataItem::toolTip
QString toolTip() const
Definition: qgsdataitem.h:407
qgsmimedatautils.h
QgsDataItem::mSortKey
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:462
QgsDataItem::mIconName
QString mIconName
Definition: qgsdataitem.h:457
SIP_VIRTUAL_CATCHER_CODE
#define SIP_VIRTUAL_CATCHER_CODE(code)
Definition: qgis_sip.h:213
QgsDataItem::mToolTip
QString mToolTip
Definition: qgsdataitem.h:456
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
qgslayeritem.h
QgsDataItem::mType
Qgis::BrowserItemType mType
Definition: qgsdataitem.h:444
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
dataItem_t
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition: qgsdataitem.h:37
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
QgsDataItem::paramWidget
virtual Q_DECL_DEPRECATED QWidget * paramWidget()
Returns source widget from data item for QgsBrowserPropertiesWidget.
Definition: qgsdataitem.h:189
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsDataItem::acceptDrop
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:218
qgsfavoritesitem.h
QgsDataItem::hasDragEnabled
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:253
QgsDataItem::mName
QString mName
Definition: qgsdataitem.h:449
QgsMimeDataUtils::Uri
Definition: qgsmimedatautils.h:40
QgsAnimatedIcon
Animated icon is keeping an animation running if there are listeners connected to frameChanged.
Definition: qgsanimatedicon.h:30
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsErrorItem
Data item that can be used to report problems (e.g. network error)
Definition: qgsdataitem.h:540
QgsMimeDataUtils
Definition: qgsmimedatautils.h:36
QgsDataItem::mPath
QString mPath
Definition: qgsdataitem.h:455
QgsDataItem::mProviderKey
QString mProviderKey
Definition: qgsdataitem.h:450
QgsDataItem::setPath
void setPath(const QString &path)
Definition: qgsdataitem.h:355
str
#define str(x)
Definition: qgis.cpp:37
QgsDataItem::setCapabilities
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:310
QgsDataItem::mChildren
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:447
QgsDataItem::handleDrop
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:233
qgsdatacollectionitem.h
qgsprojectitem.h
QgsDataItem::children
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
Qgis::BrowserItemCapability::NoCapabilities
@ NoCapabilities
Item has no capabilities.
qgszipitem.h
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsDataItem::type
Qgis::BrowserItemType type() const
Definition: qgsdataitem.h:324
QgsAbstractDatabaseProviderConnection
The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connection...
Definition: qgsabstractdatabaseproviderconnection.h:44
Qgis::BrowserItemType
BrowserItemType
Symbol layer flags.
Definition: qgis.h:348