Skip to content

Instantly share code, notes, and snippets.

@Guts
Created January 13, 2021 10:16
Show Gist options
  • Save Guts/88fbb3f87b2260fd67a19536677cd73b to your computer and use it in GitHub Desktop.
Save Guts/88fbb3f87b2260fd67a19536677cd73b to your computer and use it in GitHub Desktop.
QGIS API data provider
import pprint
from functools import partial
from qgis.core import QgsDataSourceUri, QgsProviderRegistry
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QDialog, QComboBox
# variables
db_types = {
# point de vigilance, les noms du type de base de données ne sont pas exactement les mêmes...
"ogr": QIcon(":/images/themes/default/mGeoPackage.svg"),
"postgres": QIcon(":/images/themes/default/mIconPostgis.svg"),
"spatialite": QIcon(":/images/themes/default/mIconSpatialite.svg"),
}
dico_connections_for_combobox = {}
for db_type in db_types:
print(db_type)
# retrouver les connections du type de base de données
connections = QgsProviderRegistry.instance().providerMetadata(db_type).connections()
for connection_name in connections:
print(type(connection_name), connection_name)
dico_connections_for_combobox[connection_name] = db_type, connections.get(connection_name)
def popo():
print("hey")
print(cbb_db_connections.currentText())
print(dico_connections_for_combobox[cbb_db_connections.currentText()][1].schemas())
# la fenêtre de dialogue pour accueillir notre liste déroulante
dd = QDialog(iface.mainWindow())
dd.setWindowTitle("Connexions {}".format(" / ".join(db_types)))
# on remplit la liste déroulante
cbb_db_connections = QComboBox(dd)
for k, v in dico_connections_for_combobox.items():
cbb_db_connections.addItem(db_types.get(v[0]), k, v[1])
cbb_db_connections.activated.connect(partial(popo))
# un peu de tunning des dimensions
dd.resize(300, 30)
cbb_db_connections.resize(300, 30)
# on affiche
dd.show()
import pprint
from qgis.core import QgsDataSourceUri
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QDialog, QComboBox
# variables
db_types = {
"GeoPackage": QIcon(":/images/themes/default/mGeoPackage.svg"),
"PostgreSQL": QIcon(":/images/themes/default/mIconPostgis.svg"),
"SpatiaLite": QIcon(":/images/themes/default/mIconSpatialite.svg"),
}
dico_connections_for_combobox = {}
settings = QSettings()
for db_type in db_types:
print(db_type)
# retrouver les connections du type de base de données
settings.beginGroup(f"/{db_type}/connections/")
connections = settings.childGroups()
settings.endGroup()
selected_conn = settings.value(f"/{db_type}/connections/selected", "", type=str)
if selected_conn not in connections:
connections.append(selected_conn)
for connection_name in connections:
print(connection_name)
uri = QgsDataSourceUri()
uri.setConnection(
aHost=settings.value(f"{db_type}/connections/{connection_name}/host"),
aPort=settings.value(f"{db_type}/connections/{connection_name}/port"),
aDatabase=settings.value(
f"{db_type}/connections/{connection_name}/database"
),
aUsername="",
aPassword="",
)
# selon le type d'authentification configuré, on s'adapte
if (
settings.value(f"{db_type}/connections/{connection_name}/saveUsername")
== "true"
):
uri.setUsername(
settings.value(f"{db_type}/connections/{connection_name}/username"),
)
if (
settings.value(f"{db_type}/connections/{connection_name}/savePassword")
== "true"
):
uri.setPassword(
settings.value(f"{db_type}/connections/{connection_name}/password"),
)
dico_connections_for_combobox[connection_name] = db_type, uri
# la fenêtre de dialogue pour accueillir notre liste déroulante
dd = QDialog(iface.mainWindow())
dd.setWindowTitle("Connexions {}".format(" / ".join(db_types)))
# on remplit la liste déroulante
cbb_db_connections = QComboBox(dd)
for k, v in dico_connections_for_combobox.items():
cbb_db_connections.addItem(db_types.get(v[0]), k, v[1])
# un peu de tunning des dimensions
dd.resize(300, 30)
cbb_db_connections.resize(300, 30)
# on affiche
dd.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment