QGIS API Documentation 3.99.0-Master (8e76e220402)
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 <QString>
25
26#include "moc_qgsbrowserguimodel.cpp"
27
28using namespace Qt::StringLiterals;
29
34
35QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
36{
38 context.setMessageBar( mMessageBar );
39 context.setMapCanvas( mMapCanvas );
40 return context;
41}
42
49
50Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
51{
52 if ( !index.isValid() )
53 return Qt::ItemFlags();
54
55 Qt::ItemFlags flags = QgsBrowserModel::flags( index );
56 QgsDataItem *ptr = dataItem( index );
57
58 if ( !ptr )
59 {
60 QgsDebugMsgLevel( u"FLAGS PROBLEM!"_s, 4 );
61 return Qt::ItemFlags();
62 }
63
65 const bool legacyAcceptDrop = ptr->acceptDrop();
67
68 if ( legacyAcceptDrop )
69 // legacy support for data items
70 flags |= Qt::ItemIsDropEnabled;
71 else
72 {
73 // Cache the value of acceptDrop(), as it can be slow to evaluate.
74 // e.g. for a OGR datasource, this requires to open it. And this method
75 // is called each time the browser is redrawn.
76 // We cache the number of providers too, to be able to invalidate the
77 // cached value if new providers are installed.
78 QVariant cachedProperty = ptr->property( "_qgs_accept_drop_cached" );
79 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
80 bool refreshAcceptDrop = true;
81 if ( cachedProperty.isValid() )
82 {
84 if ( cached.numberOfProviders == providers.size() )
85 {
86 refreshAcceptDrop = false;
87 if ( cached.acceptDrop )
88 flags |= Qt::ItemIsDropEnabled;
89 }
90 }
91
92 if ( refreshAcceptDrop )
93 {
94 // new support
95 for ( QgsDataItemGuiProvider *provider : providers )
96 {
97 if ( provider->acceptDrop( ptr, createDataItemContext() ) )
98 {
99 flags |= Qt::ItemIsDropEnabled;
100 break;
101 }
102 }
103
105 cached.acceptDrop = ( flags & Qt::ItemIsDropEnabled ) != 0;
106 cached.numberOfProviders = providers.size();
107 QVariant var;
108 var.setValue( cached );
109 ptr->setProperty( "_qgs_accept_drop_cached", var );
110 }
111 }
112 return flags;
113}
114
115bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
116{
117 QgsDataItem *destItem = dataItem( parent );
118 if ( !destItem )
119 {
120 QgsDebugMsgLevel( u"DROP PROBLEM!"_s, 4 );
121 return false;
122 }
123
125 const bool legacyAcceptDrop = destItem->acceptDrop();
127
128 // legacy support for data items
129 if ( legacyAcceptDrop )
130 {
132 return destItem->handleDrop( data, action );
134 }
135 else
136 {
137 // new support
138 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
139 for ( QgsDataItemGuiProvider *provider : providers )
140 {
141 if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
142 {
143 return true;
144 }
145 }
146 }
147 return false;
148}
149
150bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
151{
152 QgsDataItem *item = dataItem( index );
153 if ( !item )
154 {
155 QgsDebugMsgLevel( u"RENAME PROBLEM!"_s, 4 );
156 return false;
157 }
158
161 return false;
162
163 switch ( role )
164 {
165 case Qt::EditRole:
166 {
167 // new support
168 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
169 for ( QgsDataItemGuiProvider *provider : providers )
170 {
171 if ( provider->rename( item, value.toString(), createDataItemContext() ) )
172 {
173 return true;
174 }
175 }
176
178 return item->rename( value.toString() );
180 }
181 }
182 return false;
183}
184
186{
187 mMessageBar = bar;
188}
189
191{
192 mMapCanvas = canvas;
193}
@ Rename
Item can be renamed.
Definition qgis.h:976
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
Definition qgis.h:978
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:50
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:194
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:7486
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7485
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63