1
+ from __future__ import annotations
2
+
1
3
from importlib import resources
4
+ from typing import Sequence
2
5
3
6
from qgis .core import QgsProviderRegistry
4
7
from qgis .PyQt import uic
5
8
from qgis .PyQt .QtCore import QRegularExpression , QSortFilterProxyModel , Qt
6
9
from qgis .PyQt .QtGui import QStandardItem , QStandardItemModel
7
- from qgis .PyQt .QtWidgets import QComboBox , QDialog , QDialogButtonBox , QLineEdit , QMessageBox , QPushButton , QTableView
10
+ from qgis .PyQt .QtWidgets import (
11
+ QComboBox ,
12
+ QDialog ,
13
+ QDialogButtonBox ,
14
+ QHeaderView ,
15
+ QLineEdit ,
16
+ QMessageBox ,
17
+ QPushButton ,
18
+ QTableView ,
19
+ )
8
20
9
21
from arho_feature_template .core .exceptions import UnexpectedNoneError
22
+ from arho_feature_template .utils .misc_utils import get_active_plan_id
10
23
11
24
ui_path = resources .files (__package__ ) / "load_plan_dialog.ui"
12
25
13
26
LoadPlanDialogBase , _ = uic .loadUiType (ui_path )
14
27
15
28
16
29
class PlanFilterProxyModel (QSortFilterProxyModel ):
30
+ def __init__ (self , model : QStandardItemModel ):
31
+ super ().__init__ ()
32
+ self .setSourceModel (model )
33
+ self .setFilterCaseSensitivity (Qt .CaseInsensitive )
34
+
17
35
def filterAcceptsRow (self , source_row , source_parent ): # noqa: N802
18
36
model = self .sourceModel ()
19
37
if not model :
@@ -33,57 +51,97 @@ def filterAcceptsRow(self, source_row, source_parent): # noqa: N802
33
51
34
52
35
53
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
54
+ connections_selection : QComboBox
55
+ load_btn : QPushButton
56
+ plan_table_view : QTableView
57
+ search_line_edit : QLineEdit
58
+ button_box : QDialogButtonBox
41
59
42
- def __init__ (self , parent , connections ):
60
+ ID_COLUMN = 4
61
+
62
+ def __init__ (self , parent , connection_names : list [str ]):
43
63
super ().__init__ (parent )
44
64
self .setupUi (self )
45
65
46
66
self ._selected_plan_id = None
47
67
48
- self .buttonBox .rejected .connect (self .reject )
49
- self .buttonBox .accepted .connect (self .accept )
50
- self .buttonBox .button (QDialogButtonBox .Ok ).setEnabled (False )
68
+ self .button_box .rejected .connect (self .reject )
69
+ self .button_box .accepted .connect (self .accept )
70
+ self .button_box .button (QDialogButtonBox .Ok ).setEnabled (False )
51
71
52
- self .push_button_load .clicked .connect (self .load_plans )
53
- self .searchLineEdit .textChanged .connect (self .filter_plans )
72
+ self .load_btn .clicked .connect (self .load_plans )
73
+ self .search_line_edit .textChanged .connect (self .filter_plans )
54
74
55
- self .connectionComboBox .addItems (connections )
75
+ self .connections_selection .addItems (connection_names )
56
76
57
- self .planTableView .setSelectionMode (QTableView .SingleSelection )
58
- self .planTableView .setSelectionBehavior (QTableView .SelectRows )
77
+ self .plan_table_view : QTableView
78
+ self .plan_table_view .setSelectionMode (QTableView .SingleSelection )
79
+ self .plan_table_view .setSelectionBehavior (QTableView .SelectRows )
59
80
60
81
self .model = QStandardItemModel ()
61
82
self .model .setColumnCount (5 )
62
83
self .model .setHorizontalHeaderLabels (
63
84
[
64
- "ID" ,
65
- "Tuottajan kaavatunnus" ,
66
85
"Nimi" ,
67
- "Kaavan elinkaaren tila " ,
86
+ "Tuottajan kaavatunnus " ,
68
87
"Kaavalaji" ,
88
+ "Kaavan elinkaaren tila" ,
89
+ "ID" ,
69
90
]
70
91
)
71
92
72
- self .filterProxyModel = PlanFilterProxyModel ()
73
- self .filterProxyModel .setSourceModel (self .model )
74
- self .filterProxyModel .setFilterCaseSensitivity (Qt .CaseInsensitive )
93
+ self .filterProxyModel = PlanFilterProxyModel (self .model )
75
94
76
- self .planTableView .setModel (self .filterProxyModel )
77
- self .planTableView .selectionModel ().selectionChanged .connect (self .on_selection_changed )
95
+ self .plan_table_view .setModel (self .filterProxyModel )
96
+ self .plan_table_view .selectionModel ().selectionChanged .connect (self .on_selection_changed )
97
+ # self.plan_table_view.setSortingEnabled(True)
78
98
79
- def load_plans (self ):
99
+ header = self .plan_table_view .horizontalHeader ()
100
+ for i in range (4 ):
101
+ header .setSectionResizeMode (i , QHeaderView .ResizeToContents )
102
+ header .setSectionResizeMode (4 , QHeaderView .Stretch )
103
+
104
+ # Show plans for the first connections by default
105
+ # NOTE: Could be changed to the previously used connection if/when plugin can remember it
106
+ if len (connection_names ) > 0 :
107
+ self .load_plans ()
108
+
109
+ def clear_table (self ):
80
110
self .model .removeRows (0 , self .model .rowCount ())
81
111
82
- selected_connection = self .connectionComboBox .currentText ()
112
+ def load_plans (self ):
113
+ self .clear_table ()
114
+
115
+ selected_connection = self .connections_selection .currentText ()
83
116
if not selected_connection :
84
- self .planTableView .setModel (QStandardItemModel ())
85
117
return
86
118
119
+ active_plan_id = get_active_plan_id ()
120
+ row_to_select = None
121
+ plans = self .get_plans_from_db (selected_connection )
122
+ for i , plan in enumerate (plans ):
123
+ id_ , producers_plan_identifier , name , lifecycle_status , plan_type = plan
124
+ self .model .appendRow (
125
+ [
126
+ QStandardItem (name or "" ),
127
+ QStandardItem (producers_plan_identifier or "" ),
128
+ QStandardItem (plan_type or "" ),
129
+ QStandardItem (lifecycle_status or "" ),
130
+ QStandardItem (id_ or "" ),
131
+ ]
132
+ )
133
+ if active_plan_id == id_ :
134
+ row_to_select = i
135
+
136
+ if row_to_select is not None :
137
+ self .plan_table_view .selectRow (row_to_select )
138
+
139
+ def get_plans_from_db (self , selected_connection : str ) -> Sequence [str ]:
140
+ """
141
+ Loads plans from the selected DB connection.
142
+
143
+ Returns plan information in the format [ID, producers_plan_identifier, name, lifecycle_status, plan_type].
144
+ """
87
145
provider_registry = QgsProviderRegistry .instance ()
88
146
if provider_registry is None :
89
147
raise UnexpectedNoneError
@@ -111,35 +169,36 @@ def load_plans(self):
111
169
ON
112
170
p.plan_type_id = pt.id;
113
171
""" )
114
- for plan in plans :
115
- self .model .appendRow ([QStandardItem (column or "" ) for column in plan ])
116
-
117
172
except Exception as e : # noqa: BLE001
118
173
QMessageBox .critical (self , "Error" , f"Failed to load plans: { e } " )
119
- self .model .removeRows (0 , self .model .rowCount ())
174
+ self .clear_table ()
175
+ return plans
120
176
121
177
def filter_plans (self ):
122
- search_text = self .searchLineEdit .text ()
178
+ search_text = self .search_line_edit .text ()
123
179
if search_text :
124
180
search_regex = QRegularExpression (search_text )
125
181
self .filterProxyModel .setFilterRegularExpression (search_regex )
126
182
else :
127
183
self .filterProxyModel .setFilterRegularExpression ("" )
128
184
129
185
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 )
186
+ """
187
+ Check active selection in `plan_table_view`.
188
+
189
+ Enable the OK button only if a row is selected.
190
+ """
191
+ selection = self .plan_table_view .selectionModel ().selectedRows ()
133
192
if selection :
134
193
selected_row = selection [0 ].row ()
135
- self ._selected_plan_id = self .planTableView .model ().index (selected_row , 0 ).data ()
136
- ok_button .setEnabled (True )
194
+ self ._selected_plan_id = self .plan_table_view .model ().index (selected_row , self . ID_COLUMN ).data ()
195
+ self . button_box . button ( QDialogButtonBox . Ok ) .setEnabled (True )
137
196
else :
138
197
self ._selected_plan_id = None
139
- ok_button .setEnabled (False )
198
+ self . button_box . button ( QDialogButtonBox . Ok ) .setEnabled (False )
140
199
141
200
def get_selected_connection (self ):
142
- return self .connectionComboBox .currentText ()
201
+ return self .connections_selection .currentText ()
143
202
144
203
def get_selected_plan_id (self ):
145
204
return self ._selected_plan_id
0 commit comments