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