QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qgsbrowserguimodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsbrowserguimodel.cpp
3 ----------------------
4 begin : June 2019
5 copyright : (C) 2019 by Peter Petrik
6 email : zilolv at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgsbrowserguimodel.h"
16#include "moc_qgsbrowserguimodel.cpp"
17#include "qgslogger.h"
20#include "qgsgui.h"
21#include "qgsmessagebar.h"
22#include "qgsdataitem.h"
23
25 : QgsBrowserModel( parent )
26{
27}
28
29QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
30{
32 context.setMessageBar( mMessageBar );
33 context.setMapCanvas( mMapCanvas );
34 return context;
35}
36
43
44Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
45{
46 if ( !index.isValid() )
47 return Qt::ItemFlags();
48
49 Qt::ItemFlags flags = QgsBrowserModel::flags( index );
50 QgsDataItem *ptr = dataItem( index );
51
52 if ( !ptr )
53 {
54 QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
55 return Qt::ItemFlags();
56 }
57
59 const bool legacyAcceptDrop = ptr->acceptDrop();
61
62 if ( legacyAcceptDrop )
63 // legacy support for data items
64 flags |= Qt::ItemIsDropEnabled;
65 else
66 {
67 // Cache the value of acceptDrop(), as it can be slow to evaluate.
68 // e.g. for a OGR datasource, this requires to open it. And this method
69 // is called each time the browser is redrawn.
70 // We cache the number of providers too, to be able to invalidate the
71 // cached value if new providers are installed.
72 QVariant cachedProperty = ptr->property( "_qgs_accept_drop_cached" );
73 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
74 bool refreshAcceptDrop = true;
75 if ( cachedProperty.isValid() )
76 {
78 if ( cached.numberOfProviders == providers.size() )
79 {
80 refreshAcceptDrop = false;
81 if ( cached.acceptDrop )
82 flags |= Qt::ItemIsDropEnabled;
83 }
84 }
85
86 if ( refreshAcceptDrop )
87 {
88 // new support
89 for ( QgsDataItemGuiProvider *provider : providers )
90 {
91 if ( provider->acceptDrop( ptr, createDataItemContext() ) )
92 {
93 flags |= Qt::ItemIsDropEnabled;
94 break;
95 }
96 }
97
99 cached.acceptDrop = ( flags & Qt::ItemIsDropEnabled ) != 0;
100 cached.numberOfProviders = providers.size();
101 QVariant var;
102 var.setValue( cached );
103 ptr->setProperty( "_qgs_accept_drop_cached", var );
104 }
105 }
106 return flags;
107}
108
109bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
110{
111 QgsDataItem *destItem = dataItem( parent );
112 if ( !destItem )
113 {
114 QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
115 return false;
116 }
117
119 const bool legacyAcceptDrop = destItem->acceptDrop();
121
122 // legacy support for data items
123 if ( legacyAcceptDrop )
124 {
126 return destItem->handleDrop( data, action );
128 }
129 else
130 {
131 // new support
132 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
133 for ( QgsDataItemGuiProvider *provider : providers )
134 {
135 if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
136 {
137 return true;
138 }
139 }
140 }
141 return false;
142}
143
144bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
145{
146 QgsDataItem *item = dataItem( index );
147 if ( !item )
148 {
149 QgsDebugMsgLevel( QStringLiteral( "RENAME PROBLEM!" ), 4 );
150 return false;
151 }
152
155 return false;
156
157 switch ( role )
158 {
159 case Qt::EditRole:
160 {
161 // new support
162 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
163 for ( QgsDataItemGuiProvider *provider : providers )
164 {
165 if ( provider->rename( item, value.toString(), createDataItemContext() ) )
166 {
167 return true;
168 }
169 }
170
172 return item->rename( value.toString() );
174 }
175 }
176 return false;
177}
178
180{
181 mMessageBar = bar;
182}
183
185{
186 mMapCanvas = canvas;
187}
@ Rename
Item can be renamed.
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
A model for showing available data sources and other items in a structured tree.
QgsBrowserGuiModel(QObject *parent=nullptr)
Constructor for QgsBrowserGuiModel, with the specified parent object.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
void setMapCanvas(QgsMapCanvas *canvas)
Sets the associated map canvas that will be passed in QgsDataItemGuiContext to data items.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void setMessageBar(QgsMessageBar *bar)
Sets message bar that will be passed in QgsDataItemGuiContext to data items.
A model for showing available data sources and other items in a structured tree.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QModelIndex parent(const QModelIndex &index) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the data item.
void setMessageBar(QgsMessageBar *bar)
Sets the associated message bar.
QList< QgsDataItemGuiProvider * > providers() const
Returns the list of available providers.
Abstract base class for providers which affect how QgsDataItem items behave within the application GU...
Base class for all items in the model.
Definition qgsdataitem.h:46
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
virtual Q_DECL_DEPRECATED bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition qgsgui.cpp:180
Map canvas is a class for displaying all GIS data types on a canvas.
A bar for displaying non-blocking messages to the user.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6796
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6795
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41