QGIS API Documentation 3.37.0-Master (c2d15952569)
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#include "qgslogger.h"
19#include "qgsgui.h"
20#include "qgsmessagebar.h"
21#include "qgsdataitem.h"
22
24 : QgsBrowserModel( parent )
25{
26}
27
28QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
29{
31 context.setMessageBar( mMessageBar );
32 return context;
33}
34
41
42Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
43{
44 if ( !index.isValid() )
45 return Qt::ItemFlags();
46
47 Qt::ItemFlags flags = QgsBrowserModel::flags( index );
48 QgsDataItem *ptr = dataItem( index );
49
50 if ( !ptr )
51 {
52 QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
53 return Qt::ItemFlags();
54 }
55
57 const bool legacyAcceptDrop = ptr->acceptDrop();
59
60 if ( legacyAcceptDrop )
61 // legacy support for data items
62 flags |= Qt::ItemIsDropEnabled;
63 else
64 {
65 // Cache the value of acceptDrop(), as it can be slow to evaluate.
66 // e.g. for a OGR datasource, this requires to open it. And this method
67 // is called each time the browser is redrawn.
68 // We cache the number of providers too, to be able to invalidate the
69 // cached value if new providers are installed.
70 QVariant cachedProperty = ptr->property( "_qgs_accept_drop_cached" );
71 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
72 bool refreshAcceptDrop = true;
73 if ( cachedProperty.isValid() )
74 {
76 if ( cached.numberOfProviders == providers.size() )
77 {
78 refreshAcceptDrop = false;
79 if ( cached.acceptDrop )
80 flags |= Qt::ItemIsDropEnabled;
81 }
82 }
83
84 if ( refreshAcceptDrop )
85 {
86 // new support
87 for ( QgsDataItemGuiProvider *provider : providers )
88 {
89 if ( provider->acceptDrop( ptr, createDataItemContext() ) )
90 {
91 flags |= Qt::ItemIsDropEnabled;
92 break;
93 }
94 }
95
97 cached.acceptDrop = ( flags & Qt::ItemIsDropEnabled ) != 0;
98 cached.numberOfProviders = providers.size();
99 QVariant var;
100 var.setValue( cached );
101 ptr->setProperty( "_qgs_accept_drop_cached", var );
102 }
103 }
104 return flags;
105}
106
107bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
108{
109 QgsDataItem *destItem = dataItem( parent );
110 if ( !destItem )
111 {
112 QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
113 return false;
114 }
115
117 const bool legacyAcceptDrop = destItem->acceptDrop();
119
120 // legacy support for data items
121 if ( legacyAcceptDrop )
122 {
124 return destItem->handleDrop( data, action );
126 }
127 else
128 {
129 // new support
130 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
131 for ( QgsDataItemGuiProvider *provider : providers )
132 {
133 if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
134 {
135 return true;
136 }
137 }
138 }
139 return false;
140}
141
142bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
143{
144 QgsDataItem *item = dataItem( index );
145 if ( !item )
146 {
147 QgsDebugMsgLevel( QStringLiteral( "RENAME PROBLEM!" ), 4 );
148 return false;
149 }
150
153 return false;
154
155 switch ( role )
156 {
157 case Qt::EditRole:
158 {
159 // new support
160 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
161 for ( QgsDataItemGuiProvider *provider : providers )
162 {
163 if ( provider->rename( item, value.toString(), createDataItemContext() ) )
164 {
165 return true;
166 }
167 }
168
170 return item->rename( value.toString() );
172 }
173 }
174 return false;
175}
176
178{
179 mMessageBar = bar;
180}
@ Rename
Item can be renamed.
@ ItemRepresentsFile
Item's path() directly represents a file on disk (since QGIS 3.22)
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
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 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:164
A bar for displaying non-blocking messages to the user.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:5713
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:5712
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39