QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
18 #include "qgsdataitemguiprovider.h"
19 #include "qgsgui.h"
20 #include "qgsmessagebar.h"
21 #include "qgsdataitem.h"
22 
24  : QgsBrowserModel( parent )
25 {
26 }
27 
28 QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
29 {
30  QgsDataItemGuiContext context;
31  context.setMessageBar( mMessageBar );
32  return context;
33 }
34 
35 Qt::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 
72 bool 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 
107 bool 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 }
qgsdataitemguiproviderregistry.h
QgsDataItemGuiProvider
Abstract base class for providers which affect how QgsDataItem items behave within the application GU...
Definition: qgsdataitemguiprovider.h:85
QgsBrowserModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:556
QgsBrowserGuiModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: qgsbrowserguimodel.cpp:107
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
qgsgui.h
qgsdataitemguiprovider.h
qgsdataitem.h
QgsDataItemGuiProviderRegistry::providers
QList< QgsDataItemGuiProvider * > providers() const
Returns the list of available providers.
Definition: qgsdataitemguiproviderregistry.h:51
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
Qgis::BrowserItemCapability::ItemRepresentsFile
@ ItemRepresentsFile
Item's path() directly represents a file on disk (since QGIS 3.22)
QgsDataItem::rename
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.
Definition: qgsdataitem.cpp:542
QgsBrowserModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgsbrowsermodel.cpp:289
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:60
QgsDataItem::acceptDrop
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:218
qgsmessagebar.h
QgsBrowserGuiModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: qgsbrowserguimodel.cpp:72
QgsDataItemGuiContext
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
Definition: qgsdataitemguiprovider.h:40
QgsGui::dataItemGuiProviderRegistry
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:160
QgsDataItemGuiContext::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets the associated message bar.
Definition: qgsdataitemguiprovider.cpp:32
QgsBrowserModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:545
qgsbrowserguimodel.h
QgsDataItem::handleDrop
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:233
Qgis::BrowserItemCapability::Rename
@ Rename
Item can be renamed.
QgsDataItem::capabilities2
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:303
QgsBrowserGuiModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsbrowserguimodel.cpp:35
qgslogger.h
QgsBrowserGuiModel::QgsBrowserGuiModel
QgsBrowserGuiModel(QObject *parent=nullptr)
Constructor for QgsBrowserGuiModel, with the specified parent object.
Definition: qgsbrowserguimodel.cpp:23
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsBrowserModel::dataItem
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
Definition: qgsbrowsermodel.cpp:695
QgsBrowserModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:259
QgsBrowserGuiModel::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets message bar that will be passed in QgsDataItemGuiContext to data items.
Definition: qgsbrowserguimodel.cpp:142
QgsBrowserModel
A model for showing available data sources and other items in a structured tree.
Definition: qgsbrowsermodel.h:52