QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
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
***************************************************************************/
15
#include "
qgsproviderconnectionmodel.h
"
16
17
#include "
qgsprovidermetadata.h
"
18
#include "
qgsproviderregistry.h
"
19
20
#include <QIcon>
21
22
#include "moc_qgsproviderconnectionmodel.cpp"
23
24
QgsProviderConnectionModel::QgsProviderConnectionModel
(
const
QString &provider, QObject *
parent
)
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
37
void
QgsProviderConnectionModel::setAllowEmptyConnection
(
bool
allowEmpty )
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
56
void
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
67
void
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
74
QModelIndex
QgsProviderConnectionModel::parent
(
const
QModelIndex &child )
const
75
{
76
Q_UNUSED( child )
77
return
QModelIndex();
78
}
79
80
81
int
QgsProviderConnectionModel::rowCount
(
const
QModelIndex &
parent
)
const
82
{
83
if
(
parent
.isValid() )
84
return
0;
85
86
return
mConnections.count() + ( mAllowEmpty ? 1 : 0 );
87
}
88
89
int
QgsProviderConnectionModel::columnCount
(
const
QModelIndex &
parent
)
const
90
{
91
Q_UNUSED(
parent
)
92
return
1;
93
}
94
95
96
QVariant
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
164
QModelIndex
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
}
QgsAbstractProviderConnection
An interface for data provider connections.
Definition
qgsabstractproviderconnection.h:43
QgsProviderConnectionModel::QgsProviderConnectionModel
QgsProviderConnectionModel(const QString &provider, QObject *parent=nullptr)
Constructor for QgsProviderConnectionModel, for the specified provider.
Definition
qgsproviderconnectionmodel.cpp:24
QgsProviderConnectionModel::setAllowEmptyConnection
void setAllowEmptyConnection(bool allowEmpty)
Sets whether an optional empty connection ("not set") option is present in the model.
Definition
qgsproviderconnectionmodel.cpp:37
QgsProviderConnectionModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition
qgsproviderconnectionmodel.cpp:81
QgsProviderConnectionModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition
qgsproviderconnectionmodel.cpp:96
QgsProviderConnectionModel::CustomRole::Configuration
@ Configuration
Connection configuration variant map.
Definition
qgsproviderconnectionmodel.h:55
QgsProviderConnectionModel::CustomRole::ConnectionName
@ ConnectionName
Connection name.
Definition
qgsproviderconnectionmodel.h:53
QgsProviderConnectionModel::CustomRole::Uri
@ Uri
Connection URI string.
Definition
qgsproviderconnectionmodel.h:54
QgsProviderConnectionModel::CustomRole::Empty
@ Empty
Entry is an empty entry.
Definition
qgsproviderconnectionmodel.h:56
QgsProviderConnectionModel::parent
QModelIndex parent(const QModelIndex &child) const override
Definition
qgsproviderconnectionmodel.cpp:74
QgsProviderConnectionModel::index
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition
qgsproviderconnectionmodel.cpp:164
QgsProviderConnectionModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition
qgsproviderconnectionmodel.cpp:89
QgsProviderMetadata::connectionDeleted
void connectionDeleted(const QString &name)
Emitted when the connection with the specified name was deleted.
QgsProviderMetadata::connectionCreated
void connectionCreated(const QString &name)
Emitted when a connection with the specified name is created.
QgsProviderRegistry
A registry / canonical manager of data providers.
Definition
qgsproviderregistry.h:61
qgsproviderconnectionmodel.h
qgsprovidermetadata.h
qgsproviderregistry.h
Generated on
for QGIS API Documentation by
1.17.0