Commit 56629b43 authored by Alice Salsé's avatar Alice Salsé
Browse files

🔨 export fields selection

parent 31c8bd25
...@@ -50,6 +50,37 @@ def executeQuery(provider, query): ...@@ -50,6 +50,37 @@ def executeQuery(provider, query):
print(err.driverText()) print(err.driverText())
return return
class MulipleDialog(QDialog):
def __init__(self, title, message, items, parent=None):
super(MulipleDialog, self).__init__(parent=None)
form = QFormLayout(self)
form.addRow(QLabel(message))
self.listView = QListView(self)
form.addRow(self.listView)
model = QStandardItemModel(self.listView)
self.setWindowTitle(title)
for item in items:
# create an item with a caption
standardItem = QStandardItem(item)
standardItem.setCheckable(True)
model.appendRow(standardItem)
self.listView.setModel(model)
buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
form.addRow(buttonBox)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
def itemsSelected(self):
selected = []
model = self.listView.model()
i = 0
while model.item(i):
if model.item(i).checkState():
selected.append(model.item(i).text())
i += 1
return selected
class NetworkTools: class NetworkTools:
def __init__(self, iface): def __init__(self, iface):
...@@ -412,17 +443,19 @@ class NetworkTools: ...@@ -412,17 +443,19 @@ class NetworkTools:
or "start_node" not in layer.fields().names() : or "start_node" not in layer.fields().names() :
dir = QFileDialog.getSaveFileName(QFileDialog(), "Définir le fichier de destination", filter="CSV (*.csv)") dir = QFileDialog.getSaveFileName(QFileDialog(), "Définir le fichier de destination", filter="CSV (*.csv)")
if dir : if dir :
l_qtablename=layer.dataProvider().uri().quotedTablename () dial = MulipleDialog("Selectionner les attributs à exporter", "Liste des attributs disponibles", layer.fields().names(), self)
column_names, records = executeQuery(layer.dataProvider() if dial.exec_() == QDialog.Accepted:
, "select p.id, p.start_node, p.end_node \ print(dial.itemsSelected())
,code_insee, diametre, length \ l_qtablename=layer.dataProvider().uri().quotedTablename ()
from utils.fnc_order_pipe('"+l_qtablename+"'::regclass, ARRAY["+','.join([str(i) for i in l_ids])+"]) p \ column_names, records = executeQuery(layer.dataProvider()
left outer join "+l_qtablename+" x on p.id = x.id;") , "select p.id, p.start_node, p.end_node {2} \
with open(dir[0], 'w', newline='') as csvfile: from utils.fnc_order_pipe('{0}'::regclass, ARRAY[{1}]) p \
csvwriter = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL) left outer join {0} x on p.id = x.id;".format(l_qtablename, ','.join([str(i) for i in l_ids]), [','+','.join([str(i) for i in dial.itemsSelected()]), ''][len(dial.itemsSelected())==0]))
csvwriter.writerow(column_names) with open(dir[0], 'w', newline='') as csvfile:
for r in records : csvwriter = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(r) csvwriter.writerow(column_names)
for r in records :
csvwriter.writerow(r)
else : else :
self.iface.messageBar().pushMessage("La couche sélectionnée n'est pas une conduite", Qgis.Warning) self.iface.messageBar().pushMessage("La couche sélectionnée n'est pas une conduite", Qgis.Warning)
return return
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment