1
1
from importlib import resources
2
2
3
- import psycopg2
3
+ from qgis . core import QgsProviderRegistry
4
4
from qgis .PyQt import uic
5
5
from qgis .PyQt .QtCore import QRegularExpression , QSortFilterProxyModel , Qt
6
6
from qgis .PyQt .QtGui import QStandardItem , QStandardItemModel
7
- from qgis .PyQt .QtWidgets import QComboBox , QDialog , QLineEdit , QMessageBox , QPushButton , QTableView
7
+ from qgis .PyQt .QtWidgets import QComboBox , QDialog , QDialogButtonBox , QLineEdit , QMessageBox , QPushButton , QTableView
8
8
9
- from arho_feature_template .gui .ask_credentials import DbAskCredentialsDialog
10
- from arho_feature_template .utils .db_utils import get_db_connection_params
9
+ from arho_feature_template .core .exceptions import UnexpectedNoneError
11
10
12
11
ui_path = resources .files (__package__ ) / "load_plan_dialog.ui"
13
12
@@ -34,66 +33,67 @@ def filterAcceptsRow(self, source_row, source_parent): # noqa: N802
34
33
35
34
36
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
+
37
42
def __init__ (self , parent , connections ):
38
43
super ().__init__ (parent )
44
+ self .setupUi (self )
39
45
40
- uic . loadUi ( ui_path , self )
46
+ self . _selected_plan_id = None
41
47
42
- self .connectionComboBox : QComboBox = self . findChild ( QComboBox , "connectionComboBox" )
43
- self .planTableView : QTableView = self . findChild ( QTableView , "planTableView" )
44
- self .okButton : QPushButton = self . findChild ( QPushButton , "okButton" )
48
+ self .buttonBox . rejected . connect ( self . reject )
49
+ self .buttonBox . accepted . connect ( self . accept )
50
+ self .buttonBox . button ( QDialogButtonBox . Ok ). setEnabled ( False )
45
51
46
- self .searchLineEdit : QLineEdit = self . findChild ( QLineEdit , "searchLineEdit" )
47
- self .searchLineEdit .setPlaceholderText ( "Etsi kaavoja..." )
52
+ self .push_button_load . clicked . connect ( self . load_plans )
53
+ self .searchLineEdit .textChanged . connect ( self . filter_plans )
48
54
49
55
self .connectionComboBox .addItems (connections )
50
56
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 .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 )
57
75
self .filterProxyModel .setFilterCaseSensitivity (Qt .CaseInsensitive )
58
76
59
77
self .planTableView .setModel (self .filterProxyModel )
60
- self .searchLineEdit .textChanged .connect (self .filter_plans )
61
-
62
- self .selected_plan_id = None
63
78
64
79
def load_plans (self ):
80
+ self .model .removeRows (0 , self .model .rowCount ())
81
+
65
82
selected_connection = self .connectionComboBox .currentText ()
66
83
if not selected_connection :
67
84
self .planTableView .setModel (QStandardItemModel ())
68
85
return
69
86
70
- cursor = None
71
- conn = None
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
72
93
73
94
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 ("""
95
+ connection = postgres_provider_metadata .createConnection (selected_connection )
96
+ plans = connection .executeSql ("""
97
97
SELECT
98
98
p.id,
99
99
p.producers_plan_identifier,
@@ -111,46 +111,12 @@ def load_plans(self):
111
111
ON
112
112
p.plan_type_id = pt.id;
113
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 ())
114
+ for plan in plans :
115
+ self .model .appendRow ([QStandardItem (column ) for column in plan ])
144
116
145
117
except Exception as e : # noqa: BLE001
146
118
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 ()
119
+ self .model .removeRows (0 , self .model .rowCount ())
154
120
155
121
def filter_plans (self ):
156
122
search_text = self .searchLineEdit .text ()
@@ -163,16 +129,17 @@ def filter_plans(self):
163
129
def on_selection_changed (self ):
164
130
# Enable the OK button only if a row is selected
165
131
selection = self .planTableView .selectionModel ().selectedRows ()
132
+ ok_button = self .buttonBox .button (QDialogButtonBox .Ok )
166
133
if selection :
167
134
selected_row = selection [0 ].row ()
168
- self .selected_plan_id = self .planTableView .model ().index (selected_row , 0 ).data ()
169
- self . okButton .setEnabled (True )
135
+ self ._selected_plan_id = self .planTableView .model ().index (selected_row , 0 ).data ()
136
+ ok_button .setEnabled (True )
170
137
else :
171
- self .selected_plan_id = None
172
- self . okButton .setEnabled (False )
138
+ self ._selected_plan_id = None
139
+ ok_button .setEnabled (False )
173
140
174
141
def get_selected_connection (self ):
175
142
return self .connectionComboBox .currentText ()
176
143
177
144
def get_selected_plan_id (self ):
178
- return self .selected_plan_id
145
+ return self ._selected_plan_id
0 commit comments