QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
23  : QgsBrowserModel( parent )
24 {
25 }
26 
27 QgsDataItemGuiContext QgsBrowserGuiModel::createDataItemContext() const
28 {
29  QgsDataItemGuiContext context;
30  context.setMessageBar( mMessageBar );
31  return context;
32 }
33 
34 Qt::ItemFlags QgsBrowserGuiModel::flags( const QModelIndex &index ) const
35 {
36  if ( !index.isValid() )
37  return Qt::ItemFlags();
38 
39  Qt::ItemFlags flags = QgsBrowserModel::flags( index );
40  QgsDataItem *ptr = dataItem( index );
41 
42  if ( !ptr )
43  {
44  QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
45  return Qt::ItemFlags();
46  }
47 
49  bool legacyAcceptDrop = ptr->acceptDrop();
51 
52  if ( legacyAcceptDrop )
53  // legacy support for data items
54  flags |= Qt::ItemIsDropEnabled;
55  else
56  {
57  // new support
58  const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
59  for ( QgsDataItemGuiProvider *provider : providers )
60  {
61  if ( provider->acceptDrop( ptr, createDataItemContext() ) )
62  {
63  flags |= Qt::ItemIsDropEnabled;
64  break;
65  }
66  }
67  }
68  return flags;
69 }
70 
71 bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
72 {
73  QgsDataItem *destItem = dataItem( parent );
74  if ( !destItem )
75  {
76  QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
77  return false;
78  }
79 
81  bool legacyAcceptDrop = destItem->acceptDrop();
83 
84  // legacy support for data items
85  if ( legacyAcceptDrop )
86  {
88  return destItem->handleDrop( data, action );
90  }
91  else
92  {
93  // new support
94  const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
95  for ( QgsDataItemGuiProvider *provider : providers )
96  {
97  if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
98  {
99  return true;
100  }
101  }
102  }
103  return false;
104 }
105 
106 bool QgsBrowserGuiModel::setData( const QModelIndex &index, const QVariant &value, int role )
107 {
108  QgsDataItem *item = dataItem( index );
109  if ( !item )
110  {
111  QgsDebugMsgLevel( QStringLiteral( "RENAME PROBLEM!" ), 4 );
112  return false;
113  }
114 
115  if ( !( item->capabilities2() & QgsDataItem::Rename ) )
116  return false;
117 
118  switch ( role )
119  {
120  case Qt::EditRole:
121  {
122  // new support
123  const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
124  for ( QgsDataItemGuiProvider *provider : providers )
125  {
126  if ( provider->rename( item, value.toString(), createDataItemContext() ) )
127  {
128  return true;
129  }
130  }
131 
133  return item->rename( value.toString() );
135  }
136  }
137  return false;
138 }
139 
141 {
142  mMessageBar = bar;
143 }
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:112
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:649
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
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.
QList< QgsDataItemGuiProvider * > providers() const
Returns the list of available providers.
QgsBrowserGuiModel(QObject *parent=nullptr)
Constructor for QgsBrowserGuiModel, with the specified parent object.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QModelIndex parent(const QModelIndex &index) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:183
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index...
Abstract base class for providers which affect how QgsDataItem items behave within the application GU...
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Base class for all items in the model.
Definition: qgsdataitem.h:49
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:650
Item can be renamed.
Definition: qgsdataitem.h:227
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void setMessageBar(QgsMessageBar *bar)
Sets the associated message bar.
A model for showing available data sources and other items in a structured tree.
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:193
Qt::ItemFlags flags(const QModelIndex &index) const 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.
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:265