forked from PnX-SI/GeoNature-GIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone_filter_dialog.py
143 lines (101 loc) · 4.74 KB
/
zone_filter_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import *
from PyQt5.uic import *
from PyQt5.QtSql import *
from qgis.core import *
from qgis.gui import *
import sys, os
import os
ui_path = os.path.dirname(os.path.abspath(__file__))
ui_path = os.path.join(ui_path, "ui")
form_filter_zonage, _ = uic.loadUiType(os.path.join(ui_path, "filter_zonage.ui"))
class ZoneFilterWidget(QDialog, form_filter_zonage):
def __init__(self, interfaceFenetres, whost, wport, wbdd, wusername, wpsw, typeZone, parent=None):
self.interfaceZoneFilter = interfaceFenetres
QDialog.__init__(self)
self.setupUi(self) # méthode de Ui_action1_form pour construire les widgets
self.host = whost
self.port = wport
self.bdd = wbdd
self.username = wusername
self.psw = wpsw
self.typeZone = "".join(typeZone)
self.filterText = ""
self.pb_rechercher.clicked.connect(self.filtreRechercher)
self.resultZone = []
self.getZone(self.typeZone, self.filterText)
def getZone(self, typeZone, filterText):
db = QSqlDatabase.addDatabase("QPSQL", "geonature")
db.setHostName(self.host)
db.setPort(self.port)
db.setDatabaseName(self.bdd)
db.setUserName(self.username)
db.setPassword(self.psw)
if (not db.open()):
QMessageBox.critical(self, "Erreur", "Impossible de se connecter à la base de données ...", QMessageBox.Ok)
else:
if filterText != "":
print("FILTRE")
wsql = "SELECT DISTINCT nom FROM "
wsql += "(SELECT DISTINCT id_type, area_name as nom FROM ref_geo.l_areas "
wsql += "UNION SELECT DISTINCT id_type, linear_name as nom FROM ref_geo.l_linears "
wsql += "UNION SELECT DISTINCT id_type, point_name as nom FROM ref_geo.l_points) as rq0 "
wsql += "WHERE (id_type = " + typeZone + ") AND (nom ILIKE '"+ filterText + "%') "
wsql += "ORDER BY nom;"
print(wsql)
wquery = QSqlQuery(db)
wquery.prepare(wsql)
if not wquery.exec_():
QMessageBox.critical(self, u"Impossible de récupérer les nom des zonages.", wquery.lastError().text(), QMessageBox.Ok)
else:
self.lw_list_zone.clear()
while wquery.next():
# on crée un item qui contient à la fois le texte présenté à l'utilisateur
item = QListWidgetItem(f"{wquery.value(0)}")
# et les données associées
data = (wquery.value(0))
item.setData(256, data)
self.lw_list_zone.addItem(item)
db.close()
else:
print ("PAS FILTRE")
wsql = "SELECT DISTINCT nom FROM "
wsql += "(SELECT DISTINCT id_type, area_name as nom FROM ref_geo.l_areas "
wsql += "UNION SELECT DISTINCT id_type, linear_name as nom FROM ref_geo.l_linears "
wsql += "UNION SELECT DISTINCT id_type, point_name as nom FROM ref_geo.l_points) as rq0 "
wsql += "WHERE (id_type = " + typeZone + ") "
wsql += "ORDER BY nom;"
wquery = QSqlQuery(db)
print(wsql)
# print(typeZone)
wquery.prepare(wsql)
if not wquery.exec_():
QMessageBox.critical(self, u"Impossible de récupérer les nom des zonages.", wquery.lastError().text(), QMessageBox.Ok)
else:
self.lw_list_zone.clear()
while wquery.next():
# on crée un item qui contient à la fois le texte présenté à l'utilisateur
item = QListWidgetItem(f"{wquery.value(0)}")
# et les données associées
data = (wquery.value(0))
item.setData(256, data)
self.lw_list_zone.addItem(item)
db.close()
def filtreRechercher(self):
filterText = self.le_select.text()
print(self.typeZone)
print(filterText)
self.getZone(self.typeZone, filterText)
def reject(self):
QDialog.reject(self)
def accept(self):
for uneSelection in self.lw_list_zone.selectedItems():
data = uneSelection.data(256)
result = data
result = result.replace("'","''")
self.resultZone.append(str(result))
print(self.resultZone)
QDialog.accept(self)