QGIS API Documentation  3.13.0-Master (788156190c)
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 }
qgsdataitemguiproviderregistry.h
QgsDataItemGuiProvider
Definition: qgsdataitemguiprovider.h:84
QgsBrowserModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:493
QgsBrowserGuiModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: qgsbrowserguimodel.cpp:106
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsDataItem::capabilities2
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:283
qgsgui.h
qgsdataitemguiprovider.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:732
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:578
QgsBrowserModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgsbrowsermodel.cpp:230
QgsDataItem::Rename
@ Rename
Item can be renamed.
Definition: qgsdataitem.h:245
QgsMessageBar
Definition: qgsmessagebar.h:45
QgsDataItem::acceptDrop
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:201
qgsmessagebar.h
QgsBrowserGuiModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: qgsbrowserguimodel.cpp:71
QgsDataItemGuiContext
Definition: qgsdataitemguiprovider.h:39
QgsGui::dataItemGuiProviderRegistry
static QgsDataItemGuiProviderRegistry * dataItemGuiProviderRegistry()
Returns the global data item GUI provider registry, used for tracking providers which affect the brow...
Definition: qgsgui.cpp:118
QgsDataItemGuiContext::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets the associated message bar.
Definition: qgsdataitemguiprovider.cpp:27
QgsBrowserModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:482
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:211
QgsBrowserGuiModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsbrowserguimodel.cpp:34
qgslogger.h
QgsBrowserGuiModel::QgsBrowserGuiModel
QgsBrowserGuiModel(QObject *parent=nullptr)
Constructor for QgsBrowserGuiModel, with the specified parent object.
Definition: qgsbrowserguimodel.cpp:22
QgsDataItem
Definition: qgsdataitem.h:49
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:731
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:626
QgsBrowserModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:201
QgsBrowserGuiModel::setMessageBar
void setMessageBar(QgsMessageBar *bar)
Sets message bar that will be passed in QgsDataItemGuiContext to data items.
Definition: qgsbrowserguimodel.cpp:140
QgsBrowserModel
Definition: qgsbrowsermodel.h:71