QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 // members
317
318 Qgis::BrowserItemType type() const { return mType; }
319
324 QgsDataItem *parent() const { return mParent; }
325
330 void setParent( QgsDataItem *parent );
331 QVector<QgsDataItem *> children() const { return mChildren; }
332 virtual QIcon icon();
333
339 QString name() const { return mName; }
340
346 void setName( const QString &name );
347
348 QString path() const { return mPath; }
349 void setPath( const QString &path ) { mPath = path; }
350
359 QString providerKey() const;
360
369 void setProviderKey( const QString &value );
370
372 static QString pathComponent( const QString &component );
373
383 virtual QVariant sortKey() const;
384
389 void setSortKey( const QVariant &key );
390
391
392 // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
393 // not possible to set mIcon in constructor. Either use mIconName/setIconName()
394 // or implement icon().
395 void setIcon( const QIcon &icon ) { mIcon = icon; }
396 void setIconName( const QString &iconName ) { mIconName = iconName; }
397
398 void setToolTip( const QString &msg ) { mToolTip = msg; }
399 QString toolTip() const { return mToolTip; }
400
401 // deleteLater() items and clear the vector
402 static void deleteLater( QVector<QgsDataItem *> &items );
403
405 void moveToThread( QThread *targetThread );
406
416 virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
417
418 protected:
419 virtual void populate( const QVector<QgsDataItem *> &children );
420
424 virtual void refresh( const QVector<QgsDataItem *> &children );
425
434 bool deferredDelete() { return mDeferredDelete; }
435
437 Qgis::BrowserItemCapabilities mCapabilities = Qgis::BrowserItemCapability::NoCapabilities;
438 QgsDataItem *mParent = nullptr;
439 QVector<QgsDataItem *> mChildren; // easier to have it always
441 QString mName;
443 // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
444 // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
445 // name but different paths). Identifiers in path must not contain '/' characters.
446 // The path is used to identify item in tree.
447 QString mPath;
448 QString mToolTip;
449 QString mIconName;
450 QIcon mIcon;
451 QMap<QString, QIcon> mIconMap;
452
454 QVariant mSortKey;
455
456 public slots:
457
467 virtual void deleteLater();
468
469 // Populate children using children vector created by createChildren()
470 // \param foreground run createChildren in foreground
471 virtual void populate( bool foreground = false );
472
474 virtual void depopulate();
475
476 virtual void refresh();
477
484 virtual void refreshConnections( const QString &providerKey = QString() );
485
486 virtual void childrenCreated();
487
488 signals:
489 void beginInsertItems( QgsDataItem *parent, int first, int last );
491 void beginRemoveItems( QgsDataItem *parent, int first, int last );
494
499
507 void connectionsChanged( const QString &providerKey = QString() );
508
509 protected slots:
510
515 void updateIcon();
516
517 private:
518 static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
519
520 // Set to true if object has to be deleted when possible (nothing running in threads)
521 bool mDeferredDelete = false;
522 QFutureWatcher< QVector <QgsDataItem *> > *mFutureWatcher = nullptr;
523 // number of items currently in loading (populating) state
524 static QgsAnimatedIcon *sPopulatingIcon;
525};
526
531class CORE_EXPORT QgsErrorItem : public QgsDataItem
532{
533 Q_OBJECT
534 public:
535
536 QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
537
538#ifdef SIP_RUN
539 SIP_PYOBJECT __repr__();
540 % MethodCode
541 QString str = QStringLiteral( "<QgsErrorItem: \"%1\" %2>" ).arg( sipCpp->name(), sipCpp->path() );
542 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
543 % End
544#endif
545
546};
547
548#endif // QGSDATAITEM_H
549
550
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
BrowserItemState
Browser item states.
Definition qgis.h:674
@ NotPopulated
Children not yet created.
@ NoCapabilities
Item has no capabilities.
BrowserItemType
Symbol layer user flags.
Definition qgis.h:655
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
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