QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 "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 return context;
34}
35
42
43Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
44{
45 if ( !index.isValid() )
46 return Qt::ItemFlags();
47
48 Qt::ItemFlags flags = QgsBrowserModel::flags( index );
49 QgsDataItem *ptr = dataItem( index );
50
51 if ( !ptr )
52 {
53 QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
54 return Qt::ItemFlags();
55 }
56
58 const bool legacyAcceptDrop = ptr->acceptDrop();
60
61 if ( legacyAcceptDrop )
62 // legacy support for data items
63 flags |= Qt::ItemIsDropEnabled;
64 else
65 {
66 // Cache the value of acceptDrop(), as it can be slow to evaluate.
67 // e.g. for a OGR datasource, this requires to open it. And this method
68 // is called each time the browser is redrawn.
69 // We cache the number of providers too, to be able to invalidate the
70 // cached value if new providers are installed.
71 QVariant cachedProperty = ptr->property( "_qgs_accept_drop_cached" );
72 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
73 bool refreshAcceptDrop = true;
74 if ( cachedProperty.isValid() )
75 {
77 if ( cached.numberOfProviders == providers.size() )
78 {
79 refreshAcceptDrop = false;
80 if ( cached.acceptDrop )
81 flags |= Qt::ItemIsDropEnabled;
82 }
83 }
84
85 if ( refreshAcceptDrop )
86 {
87 // new support
88 for ( QgsDataItemGuiProvider *provider : providers )
89 {
90 if ( provider->acceptDrop( ptr, createDataItemContext() ) )
91 {
92 flags |= Qt::ItemIsDropEnabled;
93 break;
94 }
95 }
96
98 cached.acceptDrop = ( flags & Qt::ItemIsDropEnabled ) != 0;
99 cached.numberOfProviders = providers.size();
100 QVariant var;
101 var.setValue( cached );
102 ptr->setProperty( "_qgs_accept_drop_cached", var );
103 }
104 }
105 return flags;
106}
107
108bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
109{
110 QgsDataItem *destItem = dataItem( parent );
111 if ( !destItem )
112 {
113 QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
114 return false;
115 }
116
118 const bool legacyAcceptDrop = destItem->acceptDrop();
120
121 // legacy support for data items
122 if ( legacyAcceptDrop )
123 {
125 return destItem->handleDrop( data, action );
127 }
128 else
129 {
130 // new support
131 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
132 for ( QgsDataItemGuiProvider *provider : providers )
133 {
134 if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
135 {
136 return true;
137 }
138 }
139 }
140 return false;
141}
142
143bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
144{
145 QgsDataItem *item = dataItem( index );
146 if ( !item )
147 {
148 QgsDebugMsgLevel( QStringLiteral( "RENAME PROBLEM!" ), 4 );
149 return false;
150 }
151
154 return false;
155
156 switch ( role )
157 {
158 case Qt::EditRole:
159 {
160 // new support
161 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
162 for ( QgsDataItemGuiProvider *provider : providers )
163 {
164 if ( provider->rename( item, value.toString(), createDataItemContext() ) )
165 {
166 return true;
167 }
168 }
169
171 return item->rename( value.toString() );
173 }
174 }
175 return false;
176}
177
179{
180 mMessageBar = bar;
181}
@ 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
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:179
A bar for displaying non-blocking messages to the user.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39