QGIS API Documentation  3.9.0-Master (224899f119)
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 = reinterpret_cast< QgsDataItem * >( index.internalPointer() );
41 
43  bool legacyAcceptDrop = ptr->acceptDrop();
45 
46  if ( legacyAcceptDrop )
47  // legacy support for data items
48  flags |= Qt::ItemIsDropEnabled;
49  else
50  {
51  // new support
52  const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
53  for ( QgsDataItemGuiProvider *provider : providers )
54  {
55  if ( provider->acceptDrop( ptr, createDataItemContext() ) )
56  {
57  flags |= Qt::ItemIsDropEnabled;
58  break;
59  }
60  }
61  }
62  return flags;
63 }
64 
65 bool QgsBrowserGuiModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
66 {
67  QgsDataItem *destItem = dataItem( parent );
68  if ( !destItem )
69  {
70  QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
71  return false;
72  }
73 
75  bool legacyAcceptDrop = destItem->acceptDrop();
77 
78  // legacy support for data items
79  if ( legacyAcceptDrop )
80  {
82  return destItem->handleDrop( data, action );
84  }
85  else
86  {
87  // new support
88  const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
89  for ( QgsDataItemGuiProvider *provider : providers )
90  {
91  if ( provider->handleDrop( destItem, createDataItemContext(), data, action ) )
92  {
93  return true;
94  }
95  }
96  }
97  return false;
98 }
99 
101 {
102  mMessageBar = bar;
103 }
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:104
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:634
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
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:635
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
void setMessageBar(QgsMessageBar *bar)
Sets message bar that will be passed in QgsDataItemGuiContext to data items.