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