QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
35Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
36{
37 if ( !index.isValid() )
38 return Qt::ItemFlags();
39
40 Qt::ItemFlags flags = QgsBrowserModel::flags( index );
41 QgsDataItem *ptr = dataItem( index );
42
43 if ( !ptr )
44 {
45 QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
46 return Qt::ItemFlags();
47 }
48
50 const bool legacyAcceptDrop = ptr->acceptDrop();
52
53 if ( legacyAcceptDrop )
54 // legacy support for data items
55 flags |= Qt::ItemIsDropEnabled;
56 else
57 {
58 // new support
59 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
60 for ( QgsDataItemGuiProvider *provider : providers )
61 {
62 if ( provider->acceptDrop( ptr, createDataItemContext() ) )
63 {
64 flags |= Qt::ItemIsDropEnabled;
65 break;
66 }
67 }
68 }
69 return flags;
70}
71
72bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
73{
74 QgsDataItem *destItem = dataItem( parent );
75 if ( !destItem )
76 {
77 QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
78 return false;
79 }
80
82 const bool legacyAcceptDrop = destItem->acceptDrop();
84
85 // legacy support for data items
86 if ( legacyAcceptDrop )
87 {
89 return destItem->handleDrop( data, action );
91 }
92 else
93 {
94 // new support
95 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
96 for ( QgsDataItemGuiProvider *provider : providers )
97 {
98 if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
99 {
100 return true;
101 }
102 }
103 }
104 return false;
105}
106
107bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
108{
109 QgsDataItem *item = dataItem( index );
110 if ( !item )
111 {
112 QgsDebugMsgLevel( QStringLiteral( "RENAME PROBLEM!" ), 4 );
113 return false;
114 }
115
118 return false;
119
120 switch ( role )
121 {
122 case Qt::EditRole:
123 {
124 // new support
125 const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
126 for ( QgsDataItemGuiProvider *provider : providers )
127 {
128 if ( provider->rename( item, value.toString(), createDataItemContext() ) )
129 {
130 return true;
131 }
132 }
133
135 return item->rename( value.toString() );
137 }
138 }
139 return false;
140}
141
143{
144 mMessageBar = bar;
145}
@ Rename
Item can be renamed.
@ ItemRepresentsFile
Item's path() directly represents a file on disk (since QGIS 3.22)
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.
Qt::ItemFlags flags(const QModelIndex &index) const override
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.
Definition: qgsdataitem.h:233
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:218
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.
Definition: qgsdataitem.h:303
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:158
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:3061
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:3060
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39