|
| 1 | +from importlib import resources |
| 2 | + |
| 3 | +import psycopg2 |
| 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, QLineEdit, QMessageBox, QPushButton, QTableView |
| 8 | + |
| 9 | +from arho_feature_template.gui.ask_credentials import DbAskCredentialsDialog |
| 10 | +from arho_feature_template.utils.db_utils import get_db_connection_params |
| 11 | + |
| 12 | +ui_path = resources.files(__package__) / "load_plan_dialog.ui" |
| 13 | + |
| 14 | +LoadPlanDialogBase, _ = uic.loadUiType(ui_path) |
| 15 | + |
| 16 | + |
| 17 | +class PlanFilterProxyModel(QSortFilterProxyModel): |
| 18 | + def filterAcceptsRow(self, source_row, source_parent): # noqa: N802 |
| 19 | + model = self.sourceModel() |
| 20 | + if not model: |
| 21 | + return False |
| 22 | + |
| 23 | + filter_text = self.filterRegularExpression().pattern() |
| 24 | + if not filter_text: |
| 25 | + return True |
| 26 | + |
| 27 | + for column in range(5): |
| 28 | + index = model.index(source_row, column, source_parent) |
| 29 | + data = model.data(index) |
| 30 | + if data and filter_text.lower() in data.lower(): |
| 31 | + return True |
| 32 | + |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +class LoadPlanDialog(QDialog, LoadPlanDialogBase): # type: ignore |
| 37 | + def __init__(self, parent, connections): |
| 38 | + super().__init__(parent) |
| 39 | + |
| 40 | + uic.loadUi(ui_path, self) |
| 41 | + |
| 42 | + self.connectionComboBox: QComboBox = self.findChild(QComboBox, "connectionComboBox") |
| 43 | + self.planTableView: QTableView = self.findChild(QTableView, "planTableView") |
| 44 | + self.okButton: QPushButton = self.findChild(QPushButton, "okButton") |
| 45 | + |
| 46 | + self.searchLineEdit: QLineEdit = self.findChild(QLineEdit, "searchLineEdit") |
| 47 | + self.searchLineEdit.setPlaceholderText("Etsi kaavoja...") |
| 48 | + |
| 49 | + self.connectionComboBox.addItems(connections) |
| 50 | + |
| 51 | + self.connectionComboBox.currentIndexChanged.connect(self.load_plans) |
| 52 | + self.okButton.clicked.connect(self.accept) |
| 53 | + |
| 54 | + self.okButton.setEnabled(False) |
| 55 | + |
| 56 | + self.filterProxyModel = PlanFilterProxyModel(self) |
| 57 | + self.filterProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive) |
| 58 | + |
| 59 | + self.planTableView.setModel(self.filterProxyModel) |
| 60 | + self.searchLineEdit.textChanged.connect(self.filter_plans) |
| 61 | + |
| 62 | + self.selected_plan_id = None |
| 63 | + |
| 64 | + def load_plans(self): |
| 65 | + selected_connection = self.connectionComboBox.currentText() |
| 66 | + if not selected_connection: |
| 67 | + self.planTableView.setModel(QStandardItemModel()) |
| 68 | + return |
| 69 | + |
| 70 | + cursor = None |
| 71 | + conn = None |
| 72 | + |
| 73 | + try: |
| 74 | + conn_params = get_db_connection_params(selected_connection) |
| 75 | + |
| 76 | + if not conn_params.get("user") or not conn_params.get("password"): |
| 77 | + # Trigger dialog to ask for missing credentials |
| 78 | + dialog = DbAskCredentialsDialog(self) |
| 79 | + dialog.rejected.connect(self.reject) |
| 80 | + if dialog.exec() == QDialog.Accepted: |
| 81 | + user, password = dialog.get_credentials() |
| 82 | + conn_params["user"] = user |
| 83 | + conn_params["password"] = password |
| 84 | + |
| 85 | + conn = psycopg2.connect( |
| 86 | + host=conn_params["host"], |
| 87 | + port=conn_params["port"], |
| 88 | + dbname=conn_params["dbname"], |
| 89 | + user=conn_params["user"], |
| 90 | + password=conn_params["password"], |
| 91 | + sslmode=conn_params["sslmode"], |
| 92 | + ) |
| 93 | + |
| 94 | + cursor = conn.cursor() |
| 95 | + |
| 96 | + cursor.execute(""" |
| 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 | + plans = cursor.fetchall() |
| 115 | + |
| 116 | + model = QStandardItemModel(len(plans), 5) |
| 117 | + model.setHorizontalHeaderLabels( |
| 118 | + [ |
| 119 | + "ID", |
| 120 | + "Tuottajan kaavatunnus", |
| 121 | + "Nimi", |
| 122 | + "Kaavan elinkaaren tila", |
| 123 | + "Kaavalaji", |
| 124 | + ] |
| 125 | + ) |
| 126 | + |
| 127 | + for row_idx, plan in enumerate(plans): |
| 128 | + model.setItem(row_idx, 0, QStandardItem(str(plan[0]))) # id |
| 129 | + model.setItem(row_idx, 1, QStandardItem(str(plan[1]))) # producer_plan_identifier |
| 130 | + model.setItem(row_idx, 2, QStandardItem(str(plan[2]))) # name_fin |
| 131 | + model.setItem(row_idx, 3, QStandardItem(str(plan[3]))) # lifecycle_status_fin |
| 132 | + model.setItem(row_idx, 4, QStandardItem(str(plan[4]))) # plan_type_fin |
| 133 | + |
| 134 | + self.filterProxyModel.setSourceModel(model) |
| 135 | + |
| 136 | + self.planTableView.setSelectionMode(QTableView.SingleSelection) |
| 137 | + self.planTableView.setSelectionBehavior(QTableView.SelectRows) |
| 138 | + |
| 139 | + self.planTableView.selectionModel().selectionChanged.connect(self.on_selection_changed) |
| 140 | + |
| 141 | + except ValueError as ve: |
| 142 | + QMessageBox.critical(self, "Connection Error", str(ve)) |
| 143 | + self.planTableView.setModel(QStandardItemModel()) |
| 144 | + |
| 145 | + except Exception as e: # noqa: BLE001 |
| 146 | + QMessageBox.critical(self, "Error", f"Failed to load plans: {e}") |
| 147 | + self.planTableView.setModel(QStandardItemModel()) |
| 148 | + |
| 149 | + finally: |
| 150 | + if cursor: |
| 151 | + cursor.close() |
| 152 | + if conn: |
| 153 | + conn.close() |
| 154 | + |
| 155 | + def filter_plans(self): |
| 156 | + search_text = self.searchLineEdit.text() |
| 157 | + if search_text: |
| 158 | + search_regex = QRegularExpression(search_text) |
| 159 | + self.filterProxyModel.setFilterRegularExpression(search_regex) |
| 160 | + else: |
| 161 | + self.filterProxyModel.setFilterRegularExpression("") |
| 162 | + |
| 163 | + def on_selection_changed(self): |
| 164 | + # Enable the OK button only if a row is selected |
| 165 | + selection = self.planTableView.selectionModel().selectedRows() |
| 166 | + if selection: |
| 167 | + selected_row = selection[0].row() |
| 168 | + self.selected_plan_id = self.planTableView.model().index(selected_row, 0).data() |
| 169 | + self.okButton.setEnabled(True) |
| 170 | + else: |
| 171 | + self.selected_plan_id = None |
| 172 | + self.okButton.setEnabled(False) |
| 173 | + |
| 174 | + def get_selected_connection(self): |
| 175 | + return self.connectionComboBox.currentText() |
| 176 | + |
| 177 | + def get_selected_plan_id(self): |
| 178 | + return self.selected_plan_id |
0 commit comments