QGIS API Documentation 4.1.0-Master (467af3bbe65)
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:
93 QgsDataItem( Qgis::BrowserItemType type, QgsDataItem *parent SIP_TRANSFERTHIS, const QString &name, const QString &path, const QString &providerKey = QString() );
94
95 ~QgsDataItem() override;
96
97#ifdef SIP_RUN
98 // clang-format off
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// clang-format on
105#endif
106
110 bool hasChildren() const;
111
117 virtual bool layerCollection() const;
118
122 int rowCount() const;
123
128 virtual QVector<QgsDataItem *> createChildren() SIP_TRANSFERBACK;
129#ifdef SIP_RUN
131 PyObject *sipResObj = sipCallMethod( 0, sipMethod, "" );
132 // H = Convert a Python object to a mapped type instance.
133 // 5 = 1 (disallows the conversion of Py_None to NULL) + 4 (returns a copy of the C/C++ instance)
134 sipIsErr = !sipResObj || sipParseResult( 0, sipMethod, sipResObj, "H5", sipType_QVector_0101QgsDataItem, &sipRes ) < 0;
135 if ( !sipIsErr )
136 {
137 for ( QgsDataItem *item : sipRes )
138 {
139 PyObject *pyItem = sipGetPyObject( item, sipType_QgsDataItem );
140 if ( pyItem != NULL )
141 {
142 // pyItem is given an extra reference which is removed when the C++ instance’s destructor is called.
143 sipTransferTo( pyItem, Py_None );
144 }
145 }
146 }
147 if ( sipResObj != NULL )
148 {
149 Py_DECREF( sipResObj );
150 }
151 SIP_END
152#endif
153
171 int creatorAncestorDepth() const;
172
174
179 virtual void setState( Qgis::BrowserItemState state );
180
187 virtual void addChildItem( QgsDataItem *child SIP_TRANSFER, bool refresh = false );
188
194 virtual void deleteChildItem( QgsDataItem *child );
195
202
206 virtual bool equal( const QgsDataItem *other );
207
215 Q_DECL_DEPRECATED virtual QWidget *paramWidget() SIP_FACTORY SIP_DEPRECATED { return nullptr; }
216
224 virtual QList<QAction *> actions( QWidget *parent );
225
233 virtual QList<QMenu *> menus( QWidget *parent );
234
243 Q_DECL_DEPRECATED virtual bool acceptDrop() SIP_DEPRECATED { return false; }
244
245#ifdef __clang__
246#pragma clang diagnostic push
247#pragma clang diagnostic ignored "-Woverloaded-virtual"
248#endif
249
258 Q_DECL_DEPRECATED virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) SIP_DEPRECATED { return false; }
259#ifdef __clang__
260#pragma clang diagnostic pop
261#endif
262
268 virtual bool handleDoubleClick();
269
276 virtual bool hasDragEnabled() const { return false; }
277
284 Q_DECL_DEPRECATED virtual QgsMimeDataUtils::Uri mimeUri() const SIP_DEPRECATED;
285
294 virtual QgsMimeDataUtils::UriList mimeUris() const;
295
302 Q_DECL_DEPRECATED virtual bool setCrs( const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;
303
316 Q_DECL_DEPRECATED virtual bool rename( const QString &name ) SIP_DEPRECATED;
317
318 // ### QGIS 5 - rename to capabilities()
319
325 virtual Qgis::BrowserItemCapabilities capabilities2() const { return mCapabilities; }
326
332 virtual void setCapabilities( Qgis::BrowserItemCapabilities capabilities ) SIP_PYNAME( setCapabilitiesV2 ) { mCapabilities = capabilities; }
333
337 Q_DECL_DEPRECATED void setCapabilities( int capabilities ) SIP_DEPRECATED;
338
344 virtual Qgis::BrowserItemFilterFlags filterFlags() const;
345
346 // static methods
347
348 // Find child index in vector of items using '==' operator
349 static int findItem( QVector<QgsDataItem *> items, QgsDataItem *item );
350
351#ifndef SIP_RUN
352
358 template<class T> static QList< T * > filteredItems( const QList< QgsDataItem * > &items )
359 {
360 QList< T * > result;
361 result.reserve( items.size() );
362 for ( QgsDataItem *item : items )
363 {
364 if ( T *matchedItem = qobject_cast< T * >( item ) )
365 result << matchedItem;
366 }
367 return result;
368 }
369#endif
370
371 // members
372
373 Qgis::BrowserItemType type() const { return mType; }
374
379 QgsDataItem *parent() const { return mParent; }
380
391 QgsDataItem *ancestorAtDepth( int depth ) const;
392
397 void setParent( QgsDataItem *parent );
398 QVector<QgsDataItem *> children() const { return mChildren; }
399 virtual QIcon icon();
400
406 QString name() const { return mName; }
407
413 void setName( const QString &name );
414
415 QString path() const { return mPath; }
416 void setPath( const QString &path ) { mPath = path; }
417
426 QString providerKey() const;
427
436 void setProviderKey( const QString &value );
437
439 static QString pathComponent( const QString &component );
440
450 virtual QVariant sortKey() const;
451
456 void setSortKey( const QVariant &key );
457
458
459 // Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
460 // not possible to set mIcon in constructor. Either use mIconName/setIconName()
461 // or implement icon().
462 void setIcon( const QIcon &icon ) { mIcon = icon; }
463 void setIconName( const QString &iconName ) { mIconName = iconName; }
464
465 void setToolTip( const QString &msg ) { mToolTip = msg; }
466 QString toolTip() const { return mToolTip; }
467
468 // deleteLater() items and clear the vector
469 static void deleteLater( QVector<QgsDataItem *> &items );
470
472 void moveToThread( QThread *targetThread );
473
483 virtual QgsAbstractDatabaseProviderConnection *databaseConnection() const SIP_FACTORY;
484
485 // TODO should be private, but MSSQL data item provider is badly behaved and needs to
486 // manually manipulate this!
491
492 protected:
493 virtual void populate( const QVector<QgsDataItem *> &children );
494
498 virtual void refresh( const QVector<QgsDataItem *> &children );
499
510 bool deferredDelete() const { return mDeferredDelete; }
511
515 QVector<QgsDataItem *> mChildren; // easier to have it always
517 QString mName;
519 // Path is slash ('/') separated chain of item identifiers which are usually item names, but may be different if it is
520 // necessary to distinguish paths of two providers to the same source (e.g GRASS location and standard directory have the same
521 // name but different paths). Identifiers in path must not contain '/' characters.
522 // The path is used to identify item in tree.
523 QString mPath;
524 QString mToolTip;
525 QString mIconName;
526 QIcon mIcon;
527 QMap<QString, QIcon> mIconMap;
528
530 QVariant mSortKey;
531
532 public slots:
533
543 virtual void deleteLater();
544
545 // Populate children using children vector created by createChildren()
546 // \param foreground run createChildren in foreground
547 virtual void populate( bool foreground = false );
548
550 virtual void depopulate();
551
552 virtual void refresh();
553
560 virtual void refreshConnections( const QString &providerKey = QString() );
561
562 virtual void childrenCreated();
563
564 signals:
565
577 void beginInsertItems( QgsDataItem *parent, int first, int last );
578
587
599 void beginRemoveItems( QgsDataItem *parent, int first, int last );
600
609
614
619
627 void connectionsChanged( const QString &providerKey = QString() );
628
629 protected slots:
630
635 void updateIcon();
636
637 private:
638 static QVector<QgsDataItem *> runCreateChildren( QgsDataItem *item );
639
640 // Set to true if object has to be deleted when possible (nothing running in threads)
641 bool mDeferredDelete = false;
642 std::unique_ptr<QFutureWatcher<QVector<QgsDataItem *> >> mFutureWatcher;
643
644 // number of items currently in loading (populating) state
645 static QgsAnimatedIcon *sPopulatingIcon;
646};
647
652class CORE_EXPORT QgsErrorItem : public QgsDataItem
653{
654 Q_OBJECT
655 public:
656 QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path );
657
658#ifdef SIP_RUN
659 // clang-format off
660 SIP_PYOBJECT __repr__();
661 % MethodCode
662 QString str = u"<QgsErrorItem: \"%1\" %2>"_s.arg( sipCpp->name(), sipCpp->path() );
663 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
664 % End
665// clang-format on
666#endif
667};
668
669#endif // QGSDATAITEM_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
BrowserItemState
Browser item states.
Definition qgis.h:985
@ NotPopulated
Children not yet created.
Definition qgis.h:986
@ NoCapabilities
Item has no capabilities.
Definition qgis.h:999
BrowserItemType
Browser item types.
Definition qgis.h:966
QFlags< BrowserItemCapability > BrowserItemCapabilities
Browser item capabilities.
Definition qgis.h:1012
QFlags< BrowserItemFilterFlag > BrowserItemFilterFlags
Browser item filter flags.
Definition qgis.h:1031
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:198
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_TRANSFERBACK
Definition qgis_sip.h:47
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_VIRTUAL_CATCHER_CODE(code)
Definition qgis_sip.h:225
#define SIP_END
Definition qgis_sip.h:215
QgsDataItem * dataItem_t(QString, QgsDataItem *)
Definition qgsdataitem.h:41