QGIS API Documentation  3.21.0-Master (5b68dc587e)
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 );
499  void beginRemoveItems( QgsDataItem *parent, int first, int last );
501  void dataChanged( QgsDataItem *item );
502 
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 
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:63
BrowserItemState
Browser item states.
Definition: qgis.h:267
@ NotPopulated
Children not yet created.
@ NoCapabilities
Item has no capabilities.
BrowserItemType
Symbol layer flags.
Definition: qgis.h:248
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:456
QString mName
Definition: qgsdataitem.h:449
Qgis::BrowserItemType mType
Definition: qgsdataitem.h:444
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:447
void setToolTip(const QString &msg)
Definition: qgsdataitem.h:406
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:233
void dataChanged(QgsDataItem *item)
void endRemoveItems()
QString mPath
Definition: qgsdataitem.h:455
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
void beginRemoveItems(QgsDataItem *parent, int first, int last)
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:330
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:324
QString mIconName
Definition: qgsdataitem.h:457
QMap< QString, QIcon > mIconMap
Definition: qgsdataitem.h:459
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:253
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
Definition: qgsdataitem.h:462
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:345
QString path() const
Definition: qgsdataitem.h:354
void beginInsertItems(QgsDataItem *parent, int first, int last)
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
Definition: qgsdataitem.h:310
virtual Q_DECL_DEPRECATED QWidget * paramWidget()
Returns source widget from data item for QgsBrowserPropertiesWidget.
Definition: qgsdataitem.h:189
QString toolTip() const
Definition: qgsdataitem.h:407
void setIcon(const QIcon &icon)
Definition: qgsdataitem.h:403
void setIconName(const QString &iconName)
Definition: qgsdataitem.h:404
void setPath(const QString &path)
Definition: qgsdataitem.h:355
void endInsertItems()
QString mProviderKey
Definition: qgsdataitem.h:450
Data item that can be used to report problems (e.g.
Definition: qgsdataitem.h:541
#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