QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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#include <QIcon>
19
20QgsProviderConnectionModel::QgsProviderConnectionModel( const QString &provider, QObject *parent )
21 : QAbstractItemModel( parent )
22 , mProvider( provider )
23 , mMetadata( QgsProviderRegistry::instance()->providerMetadata( provider ) )
24{
25 Q_ASSERT( mMetadata );
26
27 connect( mMetadata, &QgsProviderMetadata::connectionCreated, this, &QgsProviderConnectionModel::addConnection );
28 connect( mMetadata, &QgsProviderMetadata::connectionDeleted, this, &QgsProviderConnectionModel::removeConnection );
29
30 mConnections = mMetadata->connections().keys();
31}
32
34{
35 if ( allowEmpty == mAllowEmpty )
36 return;
37
38 if ( allowEmpty )
39 {
40 beginInsertRows( QModelIndex(), 0, 0 );
41 mAllowEmpty = true;
42 endInsertRows();
43 }
44 else
45 {
46 beginRemoveRows( QModelIndex(), 0, 0 );
47 mAllowEmpty = false;
48 endRemoveRows();
49 }
50}
51
52void QgsProviderConnectionModel::removeConnection( const QString &connection )
53{
54 const int index = mConnections.indexOf( connection );
55 if ( index < 0 )
56 return;
57
58 beginRemoveRows( QModelIndex(), index + ( mAllowEmpty ? 1 : 0 ), index + ( mAllowEmpty ? 1 : 0 ) );
59 mConnections.removeAt( index );
60 endRemoveRows();
61}
62
63void QgsProviderConnectionModel::addConnection( const QString &connection )
64{
65 beginInsertRows( QModelIndex(), mConnections.count() + ( mAllowEmpty ? 1 : 0 ), mConnections.count() + ( mAllowEmpty ? 1 : 0 ) );
66 mConnections.append( connection );
67 endInsertRows();
68}
69
70QModelIndex QgsProviderConnectionModel::parent( const QModelIndex &child ) const
71{
72 Q_UNUSED( child )
73 return QModelIndex();
74}
75
76
77int QgsProviderConnectionModel::rowCount( const QModelIndex &parent ) const
78{
79 if ( parent.isValid() )
80 return 0;
81
82 return mConnections.count() + ( mAllowEmpty ? 1 : 0 );
83}
84
85int QgsProviderConnectionModel::columnCount( const QModelIndex &parent ) const
86{
87 Q_UNUSED( parent )
88 return 1;
89}
90
91
92QVariant QgsProviderConnectionModel::data( const QModelIndex &index, int role ) const
93{
94 if ( !index.isValid() )
95 return QVariant();
96
97 if ( index.row() == 0 && mAllowEmpty )
98 {
99 if ( role == RoleEmpty )
100 return true;
101
102 return QVariant();
103 }
104
105 const QString connectionName = mConnections.value( index.row() - ( mAllowEmpty ? 1 : 0 ) );
106 switch ( role )
107 {
108 case RoleEmpty:
109 return false;
110
111 case Qt::DisplayRole:
112 case Qt::EditRole:
114 {
115 return connectionName;
116 }
117
118 case Qt::DecorationRole:
119 if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
120 {
121 return connection->icon();
122 }
123 else
124 {
125 return QIcon();
126 }
127
128 case Qt::ToolTipRole:
129 case RoleUri:
130 {
131 if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
132 {
133 return connection->uri();
134 }
135 else
136 {
137 return QString();
138 }
139 }
140
142 {
143 if ( const QgsAbstractProviderConnection *connection = mMetadata->findConnection( connectionName ) )
144 {
145 return connection->configuration();
146 }
147 else
148 {
149 return QVariant();
150 }
151 }
152
153 }
154
155 return QVariant();
156}
157
158QModelIndex QgsProviderConnectionModel::index( int row, int column, const QModelIndex &parent ) const
159{
160 if ( hasIndex( row, column, parent ) )
161 {
162 return createIndex( row, column, row );
163 }
164
165 return QModelIndex();
166}
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
@ RoleEmpty
Entry is an empty entry.
@ RoleConfiguration
Connection configuration variant map.
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) SIP_THROW(QgsProviderConnectionException)
Searches and returns a (possibly NULL) 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) SIP_THROW(QgsProviderConnectionException)
Returns a dictionary of stored provider connections, the dictionary key is the connection identifier.
A registry / canonical manager of data providers.