QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
17#include "qgsdataitem.h"
20#include "qgsgui.h"
21#include "qgslogger.h"
22#include "qgsmessagebar.h"
23
24#include "moc_qgsbrowserguimodel.cpp"
25
30
31QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
32{
34 context.setMessageBar( mMessageBar );
35 context.setMapCanvas( mMapCanvas );
36 return context;
37}
38
45
46Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
47{
48 if ( !index.isValid() )
49 return Qt::ItemFlags();
50
51 Qt::ItemFlags flags = QgsBrowserModel::flags( index );
52 QgsDataItem *ptr = dataItem( index );
53
54 if ( !ptr )
55 {
56 QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
57 return Qt::ItemFlags();
58 }
59
61 const bool legacyAcceptDrop = ptr->acceptDrop();
63
64 if ( legacyAcceptDrop )
65 // legacy support for data items
66 flags |= Qt::ItemIsDropEnabled;
67 else
68 {
69 // Cache the value of acceptDrop(), as it can be slow to evaluate.
70 // e.g. for a OGR datasource, this requires to open it. And this method
71 // is called each time the browser is redrawn.
72 // We cache the number of providers too, to be able to invalidate the
73 // cached value if new providers are installed.
74 QVariant cachedProperty = ptr->property( "_qgs_accept_drop_cached" );
75 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
76 bool refreshAcceptDrop = true;
77 if ( cachedProperty.isValid() )
78 {
80 if ( cached.numberOfProviders == providers.size() )
81 {
82 refreshAcceptDrop = false;
83 if ( cached.acceptDrop )
84 flags |= Qt::ItemIsDropEnabled;
85 }
86 }
87
88 if ( refreshAcceptDrop )
89 {
90 // new support
91 for ( QgsDataItemGuiProvider *provider : providers )
92 {
93 if ( provider->acceptDrop( ptr, createDataItemContext() ) )
94 {
95 flags |= Qt::ItemIsDropEnabled;
96 break;
97 }
98 }
99
101 cached.acceptDrop = ( flags & Qt::ItemIsDropEnabled ) != 0;
102 cached.numberOfProviders = providers.size();
103 QVariant var;
104 var.setValue( cached );
105 ptr->setProperty( "_qgs_accept_drop_cached", var );
106 }
107 }
108 return flags;
109}
110
111bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
112{
113 QgsDataItem *destItem = dataItem( parent );
114 if ( !destItem )
115 {
116 QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
117 return false;
118 }
119
121 const bool legacyAcceptDrop = destItem->acceptDrop();
123
124 // legacy support for data items
125 if ( legacyAcceptDrop )
126 {
128 return destItem->handleDrop( data, action );
130 }
131 else
132 {
133 // new support
134 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
135 for ( QgsDataItemGuiProvider *provider : providers )
136 {
137 if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
138 {
139 return true;
140 }
141 }
142 }
143 return false;
144}
145
146bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
147{
148 QgsDataItem *item = dataItem( index );
149 if ( !item )
150 {
151 QgsDebugMsgLevel( QStringLiteral( "RENAME PROBLEM!" ), 4 );
152 return false;
153 }
154
157 return false;
158
159 switch ( role )
160 {
161 case Qt::EditRole:
162 {
163 // new support
164 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
165 for ( QgsDataItemGuiProvider *provider : providers )
166 {
167 if ( provider->rename( item, value.toString(), createDataItemContext() ) )
168 {
169 return true;
170 }
171 }
172
174 return item->rename( value.toString() );
176 }
177 }
178 return false;
179}
180
182{
183 mMessageBar = bar;
184}
185
187{
188 mMapCanvas = canvas;
189}
@ Rename
Item can be renamed.
Definition qgis.h:957
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
Definition qgis.h:959
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.
Qt::ItemFlags flags(const QModelIndex &index) const override
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
QgsBrowserModel(QObject *parent=nullptr)
Constructor for QgsBrowserModel, with the specified parent object.
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:47
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:191
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:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61