Browse Source

Ajout de la fenetre de selection multiple

olivier.massot 7 years ago
parent
commit
b1cd1d2c1d
2 changed files with 224 additions and 0 deletions
  1. 78 0
      core/select_list_dialog.py
  2. 146 0
      core/select_list_dialog.ui

+ 78 - 0
core/select_list_dialog.py

@@ -0,0 +1,78 @@
+'''
+
+    Boite de dialogue qui prend une liste de strings en entrée,
+    permet d'en selectionner une ou plusieurs,
+    et retourne une liste des strings selectionnées
+
+    > utilisée par le script qgis_sync_compactage
+
+    @author: olivier.massot, mai 2018
+'''
+import sys
+
+from PyQt5 import uic
+from PyQt5.Qt import QApplication, QMessageBox, QTableWidgetItem, \
+    Qt, QDialog
+from path import Path
+
+
+Ui_window, _ = uic.loadUiType(Path(__file__).parent / 'select_list_dialog.ui')
+
+def exec_(input_list):
+
+    app = QApplication(sys.argv)
+
+    SYS_HOOK = sys.excepthook
+    def error_handler(typ, value, trace):
+        while QApplication.overrideCursor():
+            QApplication.restoreOverrideCursor()
+        QMessageBox.critical(dlg, typ.__name__, "{}".format(value))
+        SYS_HOOK(typ, value, trace)
+    sys.excepthook = error_handler
+
+    dlg = SelectListDialog(input_list)
+    dlg.show()
+    app.exec_()
+
+    return dlg.selection()
+
+class SelectListDialog(QDialog):
+
+    def __init__(self, input_list, title="Sélectionner les lignes"):
+        super (SelectListDialog, self).__init__()
+        self.input_list = input_list
+        self.title = title
+        self.createWidgets()
+
+    def createWidgets(self):
+        self.ui = Ui_window()
+        self.ui.setupUi(self)
+
+        self.setWindowModality(Qt.ApplicationModal)
+        self.ui.btn_ok.clicked.connect(self.ok)
+        self.ui.lbl_title.setText(self.title)
+
+        index = 0
+        for nomChantier in self.input_list:
+            self.ui.tbl_selection.insertRow(index)
+            self.ui.tbl_selection.setItem(index, 0, QTableWidgetItem("  {}".format(nomChantier)))
+            index += 1
+        self.ui.tbl_selection.sortItems(0, Qt.AscendingOrder)
+
+    def selection(self):
+        output_list = []
+
+        for model_index in self.ui.tbl_selection.selectedIndexes():
+            output_list.append(self.input_list[model_index.row()])
+
+        return output_list
+
+    def ok(self):
+        self.done(1)
+
+
+
+if __name__ == "__main__":
+    selection = exec_(["1", "2", "3"])
+    print(selection)
+

+ 146 - 0
core/select_list_dialog.ui

@@ -0,0 +1,146 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>window</class>
+ <widget class="QDialog" name="window">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>237</width>
+    <height>346</height>
+   </rect>
+  </property>
+  <property name="maximumSize">
+   <size>
+    <width>433</width>
+    <height>793</height>
+   </size>
+  </property>
+  <property name="windowTitle">
+   <string>Sélection</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QLabel" name="lbl_title">
+     <property name="minimumSize">
+      <size>
+       <width>0</width>
+       <height>21</height>
+      </size>
+     </property>
+     <property name="maximumSize">
+      <size>
+       <width>16777215</width>
+       <height>21</height>
+      </size>
+     </property>
+     <property name="text">
+      <string>Sélectionnez les lignes</string>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QTableWidget" name="tbl_selection">
+     <property name="palette">
+      <palette>
+       <active>
+        <colorrole role="Base">
+         <brush brushstyle="SolidPattern">
+          <color alpha="255">
+           <red>249</red>
+           <green>249</green>
+           <blue>249</blue>
+          </color>
+         </brush>
+        </colorrole>
+       </active>
+       <inactive>
+        <colorrole role="Base">
+         <brush brushstyle="SolidPattern">
+          <color alpha="255">
+           <red>249</red>
+           <green>249</green>
+           <blue>249</blue>
+          </color>
+         </brush>
+        </colorrole>
+       </inactive>
+       <disabled>
+        <colorrole role="Base">
+         <brush brushstyle="SolidPattern">
+          <color alpha="255">
+           <red>240</red>
+           <green>240</green>
+           <blue>240</blue>
+          </color>
+         </brush>
+        </colorrole>
+       </disabled>
+      </palette>
+     </property>
+     <property name="font">
+      <font>
+       <family>Verdana</family>
+      </font>
+     </property>
+     <property name="frameShape">
+      <enum>QFrame::StyledPanel</enum>
+     </property>
+     <property name="editTriggers">
+      <set>QAbstractItemView::NoEditTriggers</set>
+     </property>
+     <property name="alternatingRowColors">
+      <bool>true</bool>
+     </property>
+     <property name="selectionMode">
+      <enum>QAbstractItemView::MultiSelection</enum>
+     </property>
+     <property name="selectionBehavior">
+      <enum>QAbstractItemView::SelectRows</enum>
+     </property>
+     <attribute name="horizontalHeaderVisible">
+      <bool>false</bool>
+     </attribute>
+     <attribute name="horizontalHeaderDefaultSectionSize">
+      <number>50</number>
+     </attribute>
+     <attribute name="horizontalHeaderStretchLastSection">
+      <bool>true</bool>
+     </attribute>
+     <attribute name="verticalHeaderVisible">
+      <bool>false</bool>
+     </attribute>
+     <column>
+      <property name="text">
+       <string>nom</string>
+      </property>
+     </column>
+    </widget>
+   </item>
+   <item>
+    <widget class="QPushButton" name="btn_ok">
+     <property name="minimumSize">
+      <size>
+       <width>0</width>
+       <height>31</height>
+      </size>
+     </property>
+     <property name="maximumSize">
+      <size>
+       <width>16777215</width>
+       <height>31</height>
+      </size>
+     </property>
+     <property name="text">
+      <string>OK</string>
+     </property>
+     <property name="default">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>