QGIS API Documentation 3.41.0-Master (cea29feecf2)
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_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
28class QgsDataItem;
29class QMenu;
31class QgsAnimatedIcon;
33
34class QAction;
35class QWidget;
36
38
45class CORE_EXPORT QgsDataItem : public QObject
46{
47 //SIP_TYPEHEADER_INCLUDE( "qgslayeritem.h" );
48 //SIP_TYPEHEADER_INCLUDE( "qgsdirectoryitem.h" );
49 //SIP_TYPEHEADER_INCLUDE( "qgsfavoritesitem.h" );
50 //SIP_TYPEHEADER_INCLUDE( "qgszipitem.h" );
51 //SIP_TYPEHEADER_INCLUDE( "qgsdatacollectionitem.h" );
52 //SIP_TYPEHEADER_INCLUDE( "qgsprojectitem.h" );
53
54#ifdef SIP_RUN
56 if ( qobject_cast<QgsLayerItem *>( sipCpp ) )
57 sipType = sipType_QgsLayerItem;
58 else if ( qobject_cast<QgsErrorItem *>( sipCpp ) )
59 sipType = sipType_QgsErrorItem;
60 else if ( qobject_cast<QgsDirectoryItem *>( sipCpp ) )
61 sipType = sipType_QgsDirectoryItem;
62 else if ( qobject_cast<QgsFavoritesItem *>( sipCpp ) )
63 sipType = sipType_QgsFavoritesItem;
64 else if ( qobject_cast<QgsZipItem *>( sipCpp ) )
65 sipType = sipType_QgsZipItem;
66 else if ( qobject_cast<QgsDataCollectionItem *>( sipCpp ) )
67 sipType = sipType_QgsDataCollectionItem;
68 else if ( qobject_cast<QgsProjectItem *>( sipCpp ) )
69 sipType = sipType_QgsProjectItem;
70 else
71 sipType = 0;
73#endif
74
75 Q_OBJECT
76
77 public:
78
90 QgsDataItem( Qgis::BrowserItemType type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path, const QString &providerKey = QString() );
91
92 ~QgsDataItem() override;
93
94#ifdef SIP_RUN
95 SIP_PYOBJECT __repr__();
96 % MethodCode
97 QString str = QStringLiteral( "<QgsDataItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
98 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
99 % End
100#endif
101
102 bool hasChildren();
103
109 virtual bool layerCollection() const;
110
111 int rowCount();
112
117 virtual QVector<QgsDataItem *> createChildren() SIP_TRANSFERBACK;
118#ifdef SIP_RUN
120 PyObject *sipResObj = sipCallMethod( 0, sipMethod, "" );
121 // H = Convert a Python object to a mapped type instance.
122 // 5 = 1 (disallows the conversion of Py_None to NULL) + 4 (returns a copy of the C/C++ instance)
123 sipIsErr = !sipResObj || sipParseResult( 0, sipMethod, sipResObj, "H5", sipType_QVector_0101QgsDataItem, &sipRes ) < 0;
124 if ( !sipIsErr )
125 {
126 for ( QgsDataItem *item : sipRes )
127 {
128 PyObject *pyItem = sipGetPyObject( item, sipType_QgsDataItem );
129 if ( pyItem != NULL )
130 {
131 // pyItem is given an extra reference which is removed when the C++ instance’s destructor is called.
132 sipTransferTo( pyItem, Py_None );
133 }
134 }
135 }
136 if ( sipResObj != NULL )
137 {
138 Py_DECREF( sipResObj );
139 }
140 SIP_END
141#endif
142
143 Qgis::BrowserItemState state() const;
144
149 virtual void setState( Qgis::BrowserItemState state );
150
157 virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
158
164 virtual void deleteChildItem( QgsDataItem *child );
165
171 virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
172
176 virtual bool equal( const QgsDataItem *other );
177
185 Q_DECL_DEPRECATED virtual QWidget *paramWidget() SIP_FACTORY SIP_DEPRECATED { return nullptr; }
186
194 virtual QList<QAction *> actions( QWidget *parent );
195
203 virtual QList<QMenu *> menus( QWidget *parent );
204
213 Q_DECL_DEPRECATED virtual bool acceptDrop() SIP_DEPRECATED { return false; }
214
215#ifdef __clang__
216#pragma clang diagnostic push
217#pragma clang diagnostic ignored "-Woverloaded-virtual"
218#endif
219
228 Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
229#ifdef __clang__
230#pragma clang diagnostic pop
231#endif
232
238 virtual bool handleDoubleClick();
239
246 virtual bool hasDragEnabled() const { return false; }
247
254 Q_DECL_DEPRECATED virtual QgsMimeDataUtils::Uri mimeUri() const SIP_DEPRECATED;
255
264 virtual QgsMimeDataUtils::UriList mimeUris() const;
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#ifndef SIP_RUN
315
321 template<class T>
322 static QList< T * > filteredItems( const QList< QgsDataItem * > &items )
323 {
324 QList< T * > result;
325 result.reserve( items.size() );
326 for ( QgsDataItem *item : items )
327 {
328 if ( T *matchedItem = qobject_cast< T * >( item ) )
329 result << matchedItem;
330 }
331 return result;
332 }
333#endif
334
335 // members
336
337 Qgis::BrowserItemType type() const { return mType; }
338
343 QgsDataItem *parent() const { return mParent; }
344
349 void setParent( QgsDataItem *parent );
350 QVector<QgsDataItem *> children() const { return mChildren; }
351 virtual QIcon icon();
352
358 QString name() const { return mName; }
359
365 void setName( const QString &name );
366
367 QString path() const { return mPath; }
368 void setPath( const QString &path ) { mPath = path; }
369
378 QString providerKey() const;
379
388 void setProviderKey( const QString &value );
389
391 static QString pathComponent( const QString &component );
392
402 virtual QVariant sortKey() const;
403
408 void setSortKey( const QVariant &key );
409
410
411 // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
412 // not possible to set mIcon in constructor. Either use mIconName/setIconName()
413 // or implement icon().
414 void setIcon( const QIcon &icon ) { mIcon = icon; }
415 void setIconName( const QString &iconName ) { mIconName = iconName; }
416
417 void setToolTip( const QString &msg ) { mToolTip = msg; }
418 QString toolTip() const { return mToolTip; }
419
420 // deleteLater() items and clear the vector
421 static void deleteLater( QVector<QgsDataItem *> &items );
422
424 void moveToThread( QThread *targetThread );
425
435 virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
436
437 protected:
438 virtual void populate( const QVector<QgsDataItem *> &children );
439
443 virtual void refresh( const QVector<QgsDataItem *> &children );
444
453 bool deferredDelete() { return mDeferredDelete; }
454
457 QgsDataItem *mParent = nullptr;
458 QVector<QgsDataItem *> mChildren; // easier to have it always
460 QString mName;
462 // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
463 // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
464 // name but different paths). Identifiers in path must not contain '/' characters.
465 // The path is used to identify item in tree.
466 QString mPath;
467 QString mToolTip;
468 QString mIconName;
469 QIcon mIcon;
470 QMap<QString, QIcon> mIconMap;
471
473 QVariant mSortKey;
474
475 public slots:
476
486 virtual void deleteLater();
487
488 // Populate children using children vector created by createChildren()
489 // \param foreground run createChildren in foreground
490 virtual void populate( bool foreground = false );
491
493 virtual void depopulate();
494
495 virtual void refresh();
496
503 virtual void refreshConnections( const QString &providerKey = QString() );
504
505 virtual void childrenCreated();
506
507 signals:
508
520 void beginInsertItems( QgsDataItem *parent, int first, int last );
521
530
542 void beginRemoveItems( QgsDataItem *parent, int first, int last );
543
552
557
562
570 void connectionsChanged( const QString &providerKey = QString() );
571
572 protected slots:
573
578 void updateIcon();
579
580 private:
581 static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
582
583 // Set to true if object has to be deleted when possible (nothing running in threads)
584 bool mDeferredDelete = false;
585 QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher = nullptr;
586 // number of items currently in loading (populating) state
587 static QgsAnimatedIcon *sPopulatingIcon;
588};
589
594class CORE_EXPORT QgsErrorItem : public QgsDataItem
595{
596 Q_OBJECT
597 public:
598
599 QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
600
601#ifdef SIP_RUN
602 SIP_PYOBJECT __repr__();
603 % MethodCode
604 QString str = QStringLiteral( "<QgsErrorItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
605 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
606 % End
607#endif
608
609};
610
611#endif // QGSDATAITEM_H
612
613
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
BrowserItemState
Browser item states.
Definition qgis.h:872
@ NotPopulated
Children not yet created.
@ NoCapabilities
Item has no capabilities.
BrowserItemType
Browser item types.
Definition qgis.h:853
QFlags< BrowserItemCapability > BrowserItemCapabilities
Browser item capabilities.
Definition qgis.h:899
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
QString mName
Qgis::BrowserItemType mType
QVector< QgsDataItem * > mChildren
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.
void dataChanged(QgsDataItem *item)
Emitted when data changes for an item.
void endRemoveItems()
Emitted after child items have been removed from this data item.
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::BrowserItemType type() const
QString mIconName
QMap< QString, QIcon > mIconMap
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 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).
QString path() const
void beginInsertItems(QgsDataItem *parent, int first, int last)
Emitted before child items are added to this data item.
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data 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)
void endInsertItems()
Emitted after child items have been added to this data item.
QString mProviderKey
static QList< T * > filteredItems(const QList< QgsDataItem * > &items)
Returns a filtered list of data items which match the template type.
Data item that can be used to report problems (e.g.
Contains utility functions for working with MIME data.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#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:218
#define SIP_END
Definition qgis_sip.h:208
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition qgsdataitem.h:37
const QgsCoordinateReferenceSystem & crs