QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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.h"
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgsmimedatautils.h"
24
25#include <QFutureWatcher>
26#include <QIcon>
27#include <QObject>
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
32class QgsDataItem;
33class QMenu;
35class QgsAnimatedIcon;
37
38class QAction;
39class QWidget;
40
42
49class CORE_EXPORT QgsDataItem : public QObject
50{
51 //SIP_TYPEHEADER_INCLUDE( "qgslayeritem.h" );
52 //SIP_TYPEHEADER_INCLUDE( "qgsdirectoryitem.h" );
53 //SIP_TYPEHEADER_INCLUDE( "qgsfavoritesitem.h" );
54 //SIP_TYPEHEADER_INCLUDE( "qgszipitem.h" );
55 //SIP_TYPEHEADER_INCLUDE( "qgsdatacollectionitem.h" );
56 //SIP_TYPEHEADER_INCLUDE( "qgsprojectitem.h" );
57
58#ifdef SIP_RUN
60 if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
61 sipType = sipType_QgsLayerItem;
62 else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
63 sipType = sipType_QgsErrorItem;
64 else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
65 sipType = sipType_QgsDirectoryItem;
66 else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
67 sipType = sipType_QgsFavoritesItem;
68 else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
69 sipType = sipType_QgsZipItem;
70 else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
71 sipType = sipType_QgsDataCollectionItem;
72 else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
73 sipType = sipType_QgsProjectItem;
74 else
75 sipType = 0;
77#endif
78
79 Q_OBJECT
80
81 public:
82
94 QgsDataItem( Qgis::BrowserItemType type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path, const QString &providerKey = QString() );
95
96 ~QgsDataItem() override;
97
98#ifdef SIP_RUN
99 SIP_PYOBJECT __repr__();
100 % MethodCode
101 QString str = u"<QgsDataItem: \"%1\" %2>"_s.arg( sipCpp->name(), sipCpp->path() );
102 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
103 % End
104#endif
105
109 bool hasChildren() const;
110
116 virtual bool layerCollection() const;
117
121 int rowCount() const;
122
127 virtual QVector<QgsDataItem *> createChildren() SIP_TRANSFERBACK;
128#ifdef SIP_RUN
130 PyObject *sipResObj = sipCallMethod( 0, sipMethod, "" );
131 // H = Convert a Python object to a mapped type instance.
132 // 5 = 1 (disallows the conversion of Py_None to NULL) + 4 (returns a copy of the C/C++ instance)
133 sipIsErr = !sipResObj || sipParseResult( 0, sipMethod, sipResObj, "H5", sipType_QVector_0101QgsDataItem, &sipRes ) < 0;
134 if ( !sipIsErr )
135 {
136 for ( QgsDataItem *item : sipRes )
137 {
138 PyObject *pyItem = sipGetPyObject( item, sipType_QgsDataItem );
139 if ( pyItem != NULL )
140 {
141 // pyItem is given an extra reference which is removed when the C++ instance’s destructor is called.
142 sipTransferTo( pyItem, Py_None );
143 }
144 }
145 }
146 if ( sipResObj != NULL )
147 {
148 Py_DECREF( sipResObj );
149 }
150 SIP_END
151#endif
152
170 int creatorAncestorDepth() const;
171
173
178 virtual void setState( Qgis::BrowserItemState state );
179
186 virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
187
193 virtual void deleteChildItem( QgsDataItem *child );
194
201
205 virtual bool equal( const QgsDataItem *other );
206
214 Q_DECL_DEPRECATED virtual QWidget *paramWidget() SIP_FACTORY SIP_DEPRECATED { return nullptr; }
215
223 virtual QList<QAction *> actions( QWidget *parent );
224
232 virtual QList<QMenu *> menus( QWidget *parent );
233
242 Q_DECL_DEPRECATED virtual bool acceptDrop() SIP_DEPRECATED { return false; }
243
244#ifdef __clang__
245#pragma clang diagnostic push
246#pragma clang diagnostic ignored "-Woverloaded-virtual"
247#endif
248
257 Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
258#ifdef __clang__
259#pragma clang diagnostic pop
260#endif
261
267 virtual bool handleDoubleClick();
268
275 virtual bool hasDragEnabled() const { return false; }
276
283 Q_DECL_DEPRECATED virtual QgsMimeDataUtils::Uri mimeUri() const SIP_DEPRECATED;
284
293 virtual QgsMimeDataUtils::UriList mimeUris() const;
294
301 Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
302
315 Q_DECL_DEPRECATED virtual bool rename( const QString &name ) SIP_DEPRECATED;
316
317 // ### QGIS 5 - rename to capabilities()
318
324 virtual Qgis::BrowserItemCapabilities capabilities2() const { return mCapabilities; }
325
331 virtual void setCapabilities( Qgis::BrowserItemCapabilities capabilities ) SIP_PYNAME( setCapabilitiesV2 ) { mCapabilities = capabilities; }
332
336 Q_DECL_DEPRECATED void setCapabilities( int capabilities ) SIP_DEPRECATED;
337
338 // static methods
339
340 // Find child index in vector of items using '==' operator
341 static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
342
343#ifndef SIP_RUN
344
350 template<class T>
351 static QList< T * > filteredItems( const QList< QgsDataItem * > &items )
352 {
353 QList< T * > result;
354 result.reserve( items.size() );
355 for ( QgsDataItem *item : items )
356 {
357 if ( T *matchedItem = qobject_cast< T * >( item ) )
358 result << matchedItem;
359 }
360 return result;
361 }
362#endif
363
364 // members
365
366 Qgis::BrowserItemType type() const { return mType; }
367
372 QgsDataItem *parent() const { return mParent; }
373
384 QgsDataItem *ancestorAtDepth( int depth ) const;
385
390 void setParent( QgsDataItem *parent );
391 QVector<QgsDataItem *> children() const { return mChildren; }
392 virtual QIcon icon();
393
399 QString name() const { return mName; }
400
406 void setName( const QString &name );
407
408 QString path() const { return mPath; }
409 void setPath( const QString &path ) { mPath = path; }
410
419 QString providerKey() const;
420
429 void setProviderKey( const QString &value );
430
432 static QString pathComponent( const QString &component );
433
443 virtual QVariant sortKey() const;
444
449 void setSortKey( const QVariant &key );
450
451
452 // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
453 // not possible to set mIcon in constructor. Either use mIconName/setIconName()
454 // or implement icon().
455 void setIcon( const QIcon &icon ) { mIcon = icon; }
456 void setIconName( const QString &iconName ) { mIconName = iconName; }
457
458 void setToolTip( const QString &msg ) { mToolTip = msg; }
459 QString toolTip() const { return mToolTip; }
460
461 // deleteLater() items and clear the vector
462 static void deleteLater( QVector<QgsDataItem *> &items );
463
465 void moveToThread( QThread *targetThread );
466
476 virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
477
478 // TODO should be private, but MSSQL data item provider is badly behaved and needs to
479 // manually manipulate this!
484
485 protected:
486 virtual void populate( const QVector<QgsDataItem *> &children );
487
491 virtual void refresh( const QVector<QgsDataItem *> &children );
492
503 bool deferredDelete() const { return mDeferredDelete; }
504
508 QVector<QgsDataItem *> mChildren; // easier to have it always
510 QString mName;
512 // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
513 // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
514 // name but different paths). Identifiers in path must not contain '/' characters.
515 // The path is used to identify item in tree.
516 QString mPath;
517 QString mToolTip;
518 QString mIconName;
519 QIcon mIcon;
520 QMap<QString, QIcon> mIconMap;
521
523 QVariant mSortKey;
524
525 public slots:
526
536 virtual void deleteLater();
537
538 // Populate children using children vector created by createChildren()
539 // \param foreground run createChildren in foreground
540 virtual void populate( bool foreground = false );
541
543 virtual void depopulate();
544
545 virtual void refresh();
546
553 virtual void refreshConnections( const QString &providerKey = QString() );
554
555 virtual void childrenCreated();
556
557 signals:
558
570 void beginInsertItems( QgsDataItem *parent, int first, int last );
571
580
592 void beginRemoveItems( QgsDataItem *parent, int first, int last );
593
602
607
612
620 void connectionsChanged( const QString &providerKey = QString() );
621
622 protected slots:
623
628 void updateIcon();
629
630 private:
631 static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
632
633 // Set to true if object has to be deleted when possible (nothing running in threads)
634 bool mDeferredDelete = false;
635 std::unique_ptr<QFutureWatcher<QVector<QgsDataItem *> >> mFutureWatcher;
636
637 // number of items currently in loading (populating) state
638 static QgsAnimatedIcon *sPopulatingIcon;
639};
640
645class CORE_EXPORT QgsErrorItem : public QgsDataItem
646{
647 Q_OBJECT
648 public:
649
650 QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
651
652#ifdef SIP_RUN
653 SIP_PYOBJECT __repr__();
654 % MethodCode
655 QString str = u"<QgsErrorItem: \"%1\" %2>"_s.arg( sipCpp->name(), sipCpp->path() );
656 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
657 % End
658#endif
659
660};
661
662#endif // QGSDATAITEM_H
663
664
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
BrowserItemState
Browser item states.
Definition qgis.h:957
@ NotPopulated
Children not yet created.
Definition qgis.h:958
@ NoCapabilities
Item has no capabilities.
Definition qgis.h:971
BrowserItemType
Browser item types.
Definition qgis.h:938
QFlags< BrowserItemCapability > BrowserItemCapabilities
Browser item capabilities.
Definition qgis.h:984
Provides common functionality for database based connections.
Animated icon is keeping an animation running if there are listeners connected to frameChanged.
Represents a coordinate reference system (CRS).
Base class for all items in the model.
Definition qgsdataitem.h:50
void stateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
QString mToolTip
bool hasChildren() const
Returns whether this item has children.
QString mName
Qgis::BrowserItemType mType
int rowCount() const
Returns the number of rows of this item.
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
QVector< QgsDataItem * > mChildren
int mCreatorAncestorDepth
Creator ancestor depth.
Qgis::BrowserItemState mState
void setToolTip(const QString &msg)
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
void dataChanged(QgsDataItem *item)
Emitted when data changes for an item.
int creatorAncestorDepth() const
Returns the hierarchical depth of the item's original creator/source.
static void deleteLater(QVector< QgsDataItem * > &items)
void endRemoveItems()
Emitted after child items have been removed from this data item.
virtual bool layerCollection() const
Returns true if the data item is a collection of layers The default implementation returns false,...
QString mPath
QVector< QgsDataItem * > children() const
void beginRemoveItems(QgsDataItem *parent, int first, int last)
Emitted before child items are removed from this data item.
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Qgis::BrowserItemCapabilities mCapabilities
Qgis::BrowserItemType type() const
QgsDataItem(Qgis::BrowserItemType type, QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey=QString())
Constructor for QgsDataItem, with the specified parent item.
QString mIconName
virtual QVector< QgsDataItem * > createChildren()
Create children.
QMap< QString, QIcon > mIconMap
Qgis::BrowserItemState state() const
virtual void childrenCreated()
virtual QgsDataItem * removeChildItem(QgsDataItem *child)
Removes a child item and returns it without deleting it.
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
void updateIcon()
Will request a repaint of this icon.
void connectionsChanged(const QString &providerKey=QString())
Emitted when the connections of the provider with the specified providerKey have changed.
QgsDataItem * mParent
QString name() const
Returns the name of the item (the displayed text for the item).
QString path() const
virtual QIcon icon()
virtual void setState(Qgis::BrowserItemState state)
Set item state.
void beginInsertItems(QgsDataItem *parent, int first, int last)
Emitted before child items are added to this data item.
virtual void refreshConnections(const QString &providerKey=QString())
Causes a data item provider to refresh all registered connections.
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
virtual void addChildItem(QgsDataItem *child, bool refresh=false)
Inserts a new child item.
QString toolTip() const
QgsDataItem * parent() const
Gets item parent.
virtual Q_DECL_DEPRECATED QWidget * paramWidget()
Returns source widget from data item for QgsBrowserPropertiesWidget.
void setIcon(const QIcon &icon)
void setIconName(const QString &iconName)
void setPath(const QString &path)
QString providerKey() const
Returns the provider key that created this item (e.g.
void endInsertItems()
Emitted after child items have been added to this data item.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
QString mProviderKey
virtual void populate(const QVector< QgsDataItem * > &children)
virtual bool equal(const QgsDataItem *other)
Returns true if this item is equal to another item (by testing item type and path).
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items.
bool deferredDelete() const
Returns true if the item is scheduled to be deleted.
static QList< T * > filteredItems(const QList< QgsDataItem * > &items)
Returns a filtered list of data items which match the template type.
QgsErrorItem(QgsDataItem *parent, const QString &error, const QString &path)
Contains utility functions for working with MIME data.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:199
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_PYNAME(name)
Definition qgis_sip.h:89
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_VIRTUAL_CATCHER_CODE(code)
Definition qgis_sip.h:226
#define SIP_END
Definition qgis_sip.h:216
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition qgsdataitem.h:41