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