QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsproviderconnectionmodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsproviderconnectionmodel.cpp
3  --------------------------------------
4  Date : March 2020
5  Copyright : (C) 2020 Nyall Dawson
6  Email : nyall dot dawson 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 ***************************************************************************/
16 #include "qgsproviderregistry.h"
17 #include "qgsprovidermetadata.h"
18 
19 QgsProviderConnectionModel::QgsProviderConnectionModel( const QString &provider, QObject *parent )
20  : QAbstractItemModel( parent )
21  , mProvider( provider )
22  , mMetadata( QgsProviderRegistry::instance()->providerMetadata( provider ) )
23 {
24  Q_ASSERT( mMetadata );
25 
26  connect( mMetadata, &QgsProviderMetadata::connectionCreated, this, &QgsProviderConnectionModel::addConnection );
27  connect( mMetadata, &QgsProviderMetadata::connectionDeleted, this, &QgsProviderConnectionModel::removeConnection );
28 
29  mConnections = mMetadata->connections().keys();
30 }
31 
33 {
34  if ( allowEmpty == mAllowEmpty )
35  return;
36 
37  if ( allowEmpty )
38  {
39  beginInsertRows( QModelIndex(), 0, 0 );
40  mAllowEmpty = true;
41  endInsertRows();
42  }
43  else
44  {
45  beginRemoveRows( QModelIndex(), 0, 0 );
46  mAllowEmpty = false;
47  endRemoveRows();
48  }
49 }
50 
51 void QgsProviderConnectionModel::removeConnection( const QString &connection )
52 {
53  int index = mConnections.indexOf( connection );
54  if ( index < 0 )
55  return;
56 
57  beginRemoveRows( QModelIndex(), index + ( mAllowEmpty ? 1 : 0 ), index + ( mAllowEmpty ? 1 : 0 ) );
58  mConnections.removeAt( index );
59  endRemoveRows();
60 }
61 
62 void QgsProviderConnectionModel::addConnection( const QString &connection )
63 {
64  beginInsertRows( QModelIndex(), mConnections.count() + ( mAllowEmpty ? 1 : 0 ), mConnections.count() + ( mAllowEmpty ? 1 : 0 ) );
65  mConnections.append( connection );
66  endInsertRows();
67 }
68 
69 QModelIndex QgsProviderConnectionModel::parent( const QModelIndex &child ) const
70 {
71  Q_UNUSED( child )
72  return QModelIndex();
73 }
74 
75 
76 int QgsProviderConnectionModel::rowCount( const QModelIndex &parent ) const
77 {
78  if ( parent.isValid() )
79  return 0;
80 
81  return mConnections.count() + ( mAllowEmpty ? 1 : 0 );
82 }
83 
84 int QgsProviderConnectionModel::columnCount( const QModelIndex &parent ) const
85 {
86  Q_UNUSED( parent )
87  return 1;
88 }
89 
90 
91 QVariant QgsProviderConnectionModel::data( const QModelIndex &index, int role ) const
92 {
93  if ( !index.isValid() )
94  return QVariant();
95 
96  if ( index.row() == 0 && mAllowEmpty )
97  {
98  if ( role == RoleEmpty )
99  return true;
100 
101  return QVariant();
102  }
103 
104  const QString connectionName = mConnections.value( index.row() - ( mAllowEmpty ? 1 : 0 ) );
105  switch ( role )
106  {
107  case RoleEmpty:
108  return false;
109 
110  case Qt::DisplayRole:
111  case Qt::EditRole:
112  case RoleConnectionName:
113  {
114  return connectionName;
115  }
116 
117  case Qt::DecorationRole:
118  if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
119  {
120  return connection->icon();
121  }
122  else
123  {
124  return QIcon();
125  }
126 
127  case Qt::ToolTipRole:
128  case RoleUri:
129  {
130  if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
131  {
132  return connection->uri();
133  }
134  else
135  {
136  return QString();
137  }
138  }
139 
140  case RoleConfiguration:
141  {
142  if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
143  {
144  return connection->configuration();
145  }
146  else
147  {
148  return QVariant();
149  }
150  }
151 
152  }
153 
154  return QVariant();
155 }
156 
157 QModelIndex QgsProviderConnectionModel::index( int row, int column, const QModelIndex &parent ) const
158 {
159  if ( hasIndex( row, column, parent ) )
160  {
161  return createIndex( row, column, row );
162  }
163 
164  return QModelIndex();
165 }
QgsProviderRegistry
Definition: qgsproviderregistry.h:54
QgsProviderConnectionModel::parent
QModelIndex parent(const QModelIndex &child) const override
Definition: qgsproviderconnectionmodel.cpp:69
QgsProviderConnectionModel::RoleConnectionName
@ RoleConnectionName
Connection name.
Definition: qgsproviderconnectionmodel.h:47
qgsproviderconnectionmodel.h
QgsProviderConnectionModel::RoleEmpty
@ RoleEmpty
Entry is an empty entry.
Definition: qgsproviderconnectionmodel.h:50
QgsProviderConnectionModel::RoleUri
@ RoleUri
Connection URI string.
Definition: qgsproviderconnectionmodel.h:48
qgsprovidermetadata.h
QgsProviderMetadata::findConnection
QgsAbstractProviderConnection * findConnection(const QString &name, bool cached=true) SIP_THROW(QgsProviderConnectionException)
Searches and returns a (possibly NULL) connection from the stored provider connections.
Definition: qgsprovidermetadata.cpp:220
QgsProviderConnectionModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsproviderconnectionmodel.cpp:76
qgsproviderregistry.h
QgsProviderMetadata::connectionCreated
void connectionCreated(const QString &name)
Emitted when a connection with the specified name is created.
QgsProviderMetadata::connectionDeleted
void connectionDeleted(const QString &name)
Emitted when the connection with the specified name was deleted.
QgsProviderConnectionModel::index
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition: qgsproviderconnectionmodel.cpp:157
QgsProviderConnectionModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgsproviderconnectionmodel.cpp:91
QgsProviderConnectionModel::setAllowEmptyConnection
void setAllowEmptyConnection(bool allowEmpty)
Sets whether an optional empty connection ("not set") option is present in the model.
Definition: qgsproviderconnectionmodel.cpp:32
QgsProviderConnectionModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsproviderconnectionmodel.cpp:84
QgsProviderConnectionModel::QgsProviderConnectionModel
QgsProviderConnectionModel(const QString &provider, QObject *parent=nullptr)
Constructor for QgsProviderConnectionModel, for the specified provider.
Definition: qgsproviderconnectionmodel.cpp:19
QgsProviderConnectionModel::RoleConfiguration
@ RoleConfiguration
Connection configuration variant map.
Definition: qgsproviderconnectionmodel.h:49
QgsAbstractProviderConnection
The QgsAbstractProviderConnection provides an interface for data provider connections.
Definition: qgsabstractproviderconnection.h:44
QgsProviderMetadata::connections
virtual QMap< QString, QgsAbstractProviderConnection * > connections(bool cached=true) SIP_THROW(QgsProviderConnectionException)
Returns a dictionary of stored provider connections, the dictionary key is the connection identifier.
Definition: qgsprovidermetadata.cpp:209