|
| 1 | +from importlib import resources |
| 2 | + |
| 3 | +from qgis.core import QgsProviderRegistry |
| 4 | +from qgis.PyQt import uic |
| 5 | +from qgis.PyQt.QtCore import QRegularExpression, QSortFilterProxyModel, Qt |
| 6 | +from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel |
| 7 | +from qgis.PyQt.QtWidgets import QComboBox, QDialog, QDialogButtonBox, QLineEdit, QMessageBox, QPushButton, QTableView |
| 8 | + |
| 9 | +from arho_feature_template.core.exceptions import UnexpectedNoneError |
| 10 | + |
| 11 | +ui_path = resources.files(__package__) / "load_plan_dialog.ui" |
| 12 | + |
| 13 | +LoadPlanDialogBase, _ = uic.loadUiType(ui_path) |
| 14 | + |
| 15 | + |
| 16 | +class PlanFilterProxyModel(QSortFilterProxyModel): |
| 17 | + def filterAcceptsRow(self, source_row, source_parent): # noqa: N802 |
| 18 | + model = self.sourceModel() |
| 19 | + if not model: |
| 20 | + return False |
| 21 | + |
| 22 | + filter_text = self.filterRegularExpression().pattern() |
| 23 | + if not filter_text: |
| 24 | + return True |
| 25 | + |
| 26 | + for column in range(5): |
| 27 | + index = model.index(source_row, column, source_parent) |
| 28 | + data = model.data(index) |
| 29 | + if data and filter_text.lower() in data.lower(): |
| 30 | + return True |
| 31 | + |
| 32 | + return False |
| 33 | + |
| 34 | + |
| 35 | +class LoadPlanDialog(QDialog, LoadPlanDialogBase): # type: ignore |
| 36 | + connectionComboBox: QComboBox # noqa: N815 |
| 37 | + push_button_load: QPushButton |
| 38 | + planTableView: QTableView # noqa: N815 |
| 39 | + searchLineEdit: QLineEdit # noqa: N815 |
| 40 | + buttonBox: QDialogButtonBox # noqa: N815 |
| 41 | + |
| 42 | + def __init__(self, parent, connections): |
| 43 | + super().__init__(parent) |
| 44 | + self.setupUi(self) |
| 45 | + |
| 46 | + self._selected_plan_id = None |
| 47 | + |
| 48 | + self.buttonBox.rejected.connect(self.reject) |
| 49 | + self.buttonBox.accepted.connect(self.accept) |
| 50 | + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
| 51 | + |
| 52 | + self.push_button_load.clicked.connect(self.load_plans) |
| 53 | + self.searchLineEdit.textChanged.connect(self.filter_plans) |
| 54 | + |
| 55 | + self.connectionComboBox.addItems(connections) |
| 56 | + |
| 57 | + self.planTableView.setSelectionMode(QTableView.SingleSelection) |
| 58 | + self.planTableView.setSelectionBehavior(QTableView.SelectRows) |
| 59 | + self.planTableView.selectionModel().selectionChanged.connect(self.on_selection_changed) |
| 60 | + |
| 61 | + self.model = QStandardItemModel() |
| 62 | + self.model.setColumnCount(5) |
| 63 | + self.model.setHorizontalHeaderLabels( |
| 64 | + [ |
| 65 | + "ID", |
| 66 | + "Tuottajan kaavatunnus", |
| 67 | + "Nimi", |
| 68 | + "Kaavan elinkaaren tila", |
| 69 | + "Kaavalaji", |
| 70 | + ] |
| 71 | + ) |
| 72 | + |
| 73 | + self.filterProxyModel = PlanFilterProxyModel() |
| 74 | + self.filterProxyModel.setSourceModel(self.model) |
| 75 | + self.filterProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive) |
| 76 | + |
| 77 | + self.planTableView.setModel(self.filterProxyModel) |
| 78 | + |
| 79 | + def load_plans(self): |
| 80 | + self.model.removeRows(0, self.model.rowCount()) |
| 81 | + |
| 82 | + selected_connection = self.connectionComboBox.currentText() |
| 83 | + if not selected_connection: |
| 84 | + self.planTableView.setModel(QStandardItemModel()) |
| 85 | + return |
| 86 | + |
| 87 | + provider_registry = QgsProviderRegistry.instance() |
| 88 | + if provider_registry is None: |
| 89 | + raise UnexpectedNoneError |
| 90 | + postgres_provider_metadata = provider_registry.providerMetadata("postgres") |
| 91 | + if postgres_provider_metadata is None: |
| 92 | + raise UnexpectedNoneError |
| 93 | + |
| 94 | + try: |
| 95 | + connection = postgres_provider_metadata.createConnection(selected_connection) |
| 96 | + plans = connection.executeSql(""" |
| 97 | + SELECT |
| 98 | + p.id, |
| 99 | + p.producers_plan_identifier, |
| 100 | + p.name ->> 'fin' AS name_fin, |
| 101 | + l.name ->> 'fin' AS lifecycle_status_fin, |
| 102 | + pt.name ->> 'fin' AS plan_type_fin |
| 103 | + FROM |
| 104 | + hame.plan p |
| 105 | + LEFT JOIN |
| 106 | + codes.lifecycle_status l |
| 107 | + ON |
| 108 | + p.lifecycle_status_id = l.id |
| 109 | + LEFT JOIN |
| 110 | + codes.plan_type pt |
| 111 | + ON |
| 112 | + p.plan_type_id = pt.id; |
| 113 | + """) |
| 114 | + for plan in plans: |
| 115 | + self.model.appendRow([QStandardItem(column) for column in plan]) |
| 116 | + |
| 117 | + except Exception as e: # noqa: BLE001 |
| 118 | + QMessageBox.critical(self, "Error", f"Failed to load plans: {e}") |
| 119 | + self.model.removeRows(0, self.model.rowCount()) |
| 120 | + |
| 121 | + def filter_plans(self): |
| 122 | + search_text = self.searchLineEdit.text() |
| 123 | + if search_text: |
| 124 | + search_regex = QRegularExpression(search_text) |
| 125 | + self.filterProxyModel.setFilterRegularExpression(search_regex) |
| 126 | + else: |
| 127 | + self.filterProxyModel.setFilterRegularExpression("") |
| 128 | + |
| 129 | + def on_selection_changed(self): |
| 130 | + # Enable the OK button only if a row is selected |
| 131 | + selection = self.planTableView.selectionModel().selectedRows() |
| 132 | + ok_button = self.buttonBox.button(QDialogButtonBox.Ok) |
| 133 | + if selection: |
| 134 | + selected_row = selection[0].row() |
| 135 | + self._selected_plan_id = self.planTableView.model().index(selected_row, 0).data() |
| 136 | + ok_button.setEnabled(True) |
| 137 | + else: |
| 138 | + self._selected_plan_id = None |
| 139 | + ok_button.setEnabled(False) |
| 140 | + |
| 141 | + def get_selected_connection(self): |
| 142 | + return self.connectionComboBox.currentText() |
| 143 | + |
| 144 | + def get_selected_plan_id(self): |
| 145 | + return self._selected_plan_id |
0 commit comments