QGIS API Documentation 3.38.0-Grenoble (exported)
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#ifdef SIP_RUN
48#include "qgslayeritem.h"
49#include "qgsdirectoryitem.h"
50#include "qgsfavoritesitem.h"
51#include "qgszipitem.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;
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
145 Qgis::BrowserItemState state() const;
146
151 virtual void setState( Qgis::BrowserItemState state );
152
159 virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
160
166 virtual void deleteChildItem( QgsDataItem *child );
167
173 virtual QgsDataItem *removeChildItem( QgsDataItem *child ) SIP_TRANSFERBACK;
174
178 virtual bool equal( const QgsDataItem *other );
179
187 Q_DECL_DEPRECATED virtual QWidget *paramWidget() SIP_FACTORY SIP_DEPRECATED { return nullptr; }
188
196 virtual QList<QAction *> actions( QWidget *parent );
197
205 virtual QList<QMenu *> menus( QWidget *parent );
206
215 Q_DECL_DEPRECATED virtual bool acceptDrop() SIP_DEPRECATED { return false; }
216
217#ifdef __clang__
218#pragma clang diagnostic push
219#pragma clang diagnostic ignored "-Woverloaded-virtual"
220#endif
221
230 Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
231#ifdef __clang__
232#pragma clang diagnostic pop
233#endif
234
240 virtual bool handleDoubleClick();
241
248 virtual bool hasDragEnabled() const { return false; }
249
256 Q_DECL_DEPRECATED virtual QgsMimeDataUtils::Uri mimeUri() const SIP_DEPRECATED;
257
266 virtual QgsMimeDataUtils::UriList mimeUris() const;
267
274 Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
275
288 Q_DECL_DEPRECATED virtual bool rename( const QString &name ) SIP_DEPRECATED;
289
290 // ### QGIS 4 - rename to capabilities()
291
297 virtual Qgis::BrowserItemCapabilities capabilities2() const { return mCapabilities; }
298
304 virtual void setCapabilities( Qgis::BrowserItemCapabilities capabilities ) SIP_PYNAME( setCapabilitiesV2 ) { mCapabilities = capabilities; }
305
309 Q_DECL_DEPRECATED void setCapabilities( int capabilities ) SIP_DEPRECATED;
310
311 // static methods
312
313 // Find child index in vector of items using '==' operator
314 static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
315
316#ifndef SIP_RUN
317
323 template<class T>
324 static QList< T * > filteredItems( const QList< QgsDataItem * > &items )
325 {
326 QList< T * > result;
327 result.reserve( items.size() );
328 for ( QgsDataItem *item : items )
329 {
330 if ( T *matchedItem = qobject_cast< T * >( item ) )
331 result << matchedItem;
332 }
333 return result;
334 }
335#endif
336
337 // members
338
339 Qgis::BrowserItemType type() const { return mType; }
340
345 QgsDataItem *parent() const { return mParent; }
346
351 void setParent( QgsDataItem *parent );
352 QVector<QgsDataItem *> children() const { return mChildren; }
353 virtual QIcon icon();
354
360 QString name() const { return mName; }
361
367 void setName( const QString &name );
368
369 QString path() const { return mPath; }
370 void setPath( const QString &path ) { mPath = path; }
371
380 QString providerKey() const;
381
390 void setProviderKey( const QString &value );
391
393 static QString pathComponent( const QString &component );
394
404 virtual QVariant sortKey() const;
405
410 void setSortKey( const QVariant &key );
411
412
413 // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
414 // not possible to set mIcon in constructor. Either use mIconName/setIconName()
415 // or implement icon().
416 void setIcon( const QIcon &icon ) { mIcon = icon; }
417 void setIconName( const QString &iconName ) { mIconName = iconName; }
418
419 void setToolTip( const QString &msg ) { mToolTip = msg; }
420 QString toolTip() const { return mToolTip; }
421
422 // deleteLater() items and clear the vector
423 static void deleteLater( QVector<QgsDataItem *> &items );
424
426 void moveToThread( QThread *targetThread );
427
437 virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
438
439 protected:
440 virtual void populate( const QVector<QgsDataItem *> &children );
441
445 virtual void refresh( const QVector<QgsDataItem *> &children );
446
455 bool deferredDelete() { return mDeferredDelete; }
456
459 QgsDataItem *mParent = nullptr;
460 QVector<QgsDataItem *> mChildren; // easier to have it always
462 QString mName;
464 // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
465 // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
466 // name but different paths). Identifiers in path must not contain '/' characters.
467 // The path is used to identify item in tree.
468 QString mPath;
469 QString mToolTip;
470 QString mIconName;
471 QIcon mIcon;
472 QMap<QString, QIcon> mIconMap;
473
475 QVariant mSortKey;
476
477 public slots:
478
488 virtual void deleteLater();
489
490 // Populate children using children vector created by createChildren()
491 // \param foreground run createChildren in foreground
492 virtual void populate( bool foreground = false );
493
495 virtual void depopulate();
496
497 virtual void refresh();
498
505 virtual void refreshConnections( const QString &providerKey = QString() );
506
507 virtual void childrenCreated();
508
509 signals:
510 void beginInsertItems( QgsDataItem *parent, int first, int last );
512 void beginRemoveItems( QgsDataItem *parent, int first, int last );
515
520
528 void connectionsChanged( const QString &providerKey = QString() );
529
530 protected slots:
531
536 void updateIcon();
537
538 private:
539 static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
540
541 // Set to true if object has to be deleted when possible (nothing running in threads)
542 bool mDeferredDelete = false;
543 QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher = nullptr;
544 // number of items currently in loading (populating) state
545 static QgsAnimatedIcon *sPopulatingIcon;
546};
547
552class CORE_EXPORT QgsErrorItem : public QgsDataItem
553{
554 Q_OBJECT
555 public:
556
557 QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
558
559#ifdef SIP_RUN
560 SIP_PYOBJECT __repr__();
561 % MethodCode
562 QString str = QStringLiteral( "<QgsErrorItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
563 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
564 % End
565#endif
566
567};
568
569#endif // QGSDATAITEM_H
570
571
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
BrowserItemState
Browser item states.
Definition qgis.h:693
@ NotPopulated
Children not yet created.
@ NoCapabilities
Item has no capabilities.
BrowserItemType
Browser item types.
Definition qgis.h:674
QFlags< BrowserItemCapability > BrowserItemCapabilities
Browser item capabilities.
Definition qgis.h:719
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)
void endRemoveItems()
QString mPath
QVector< QgsDataItem * > children() const
void beginRemoveItems(QgsDataItem *parent, int first, int last)
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)
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()
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.
#define str(x)
Definition qgis.cpp:38
#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