32
32
QgsLayerTreeGroup ,
33
33
QgsMessageLog ,
34
34
QgsProject ,
35
+ QgsSettings ,
35
36
QgsVectorLayer ,
36
37
)
37
38
from qgis .gui import QgsFileWidget
38
- from qgis .PyQt .QtCore import QCoreApplication , QSettings , QTimer , QTranslator , qVersion
39
+ from qgis .PyQt .QtCore import QCoreApplication , QTimer , QTranslator , qVersion
39
40
from qgis .PyQt .QtGui import QIcon
40
41
from qgis .PyQt .QtWidgets import QAction , QListWidgetItem , QMessageBox
41
42
from qgis .utils import iface
55
56
DissolveFeaturesTask ,
56
57
)
57
58
from nlsgpkgloader .qgis_plugin_tools .tools .resources import resources_path
59
+ from nlsgpkgloader .qgis_plugin_tools .tools .settings import get_setting , set_setting
58
60
from nlsgpkgloader .ui import (
59
61
NLSGeoPackageLoaderMunicipalitySelectionDialog ,
60
62
NLSGeoPackageLoaderProgressDialog ,
61
63
NLSGeoPackageLoaderUserKeyDialog ,
62
64
)
63
65
66
+ OLD_SETTINGS_GROUP_NAME = "NLSgpkgloader"
67
+
64
68
65
69
class NLSGeoPackageLoader :
66
70
"""QGIS Plugin Implementation."""
67
71
68
72
def __init__ (self ):
69
- """Constructor.
73
+ """Constructor"""
70
74
71
- :param iface: An interface instance that will be passed to this class
72
- which provides the hook by which you can manipulate the QGIS
73
- application at run time.
74
- :type iface: QgsInterface
75
- """
76
75
# Save reference to the QGIS interface
77
76
self .iface = iface
78
77
# initialize plugin directory
79
78
self .plugin_dir = os .path .dirname (__file__ )
80
79
# initialize locale
81
- locale = QSettings (). value ( "locale/userLocale" )[0 :2 ]
80
+ locale = get_setting ( "locale/userLocale" , internal = False )[0 :2 ]
82
81
locale_path = resources_path ("i18n" , "NLSGeoPackageLoader_{}.qm" .format (locale ))
83
82
84
83
if os .path .exists (locale_path ):
@@ -101,9 +100,12 @@ def __init__(self):
101
100
102
101
self .nls_user_key_dialog = NLSGeoPackageLoaderUserKeyDialog ()
103
102
104
- self .first_run = QSettings ().value ("/NLSgpkgloader/first_run" , True , type = bool )
103
+ if OLD_SETTINGS_GROUP_NAME in QgsSettings ().childGroups ():
104
+ self ._migrate_settings ()
105
+
106
+ self .first_run = get_setting ("first_run" , True , bool )
105
107
if self .first_run :
106
- QSettings (). setValue ( "/NLSgpkgloader/ first_run" , False )
108
+ set_setting ( " first_run" , False )
107
109
108
110
# noinspection PyMethodMayBeStatic
109
111
def tr (self , message ):
@@ -206,6 +208,15 @@ def initGui(self): # noqa N802
206
208
# will be set False in run()
207
209
self .first_start = True
208
210
211
+ @staticmethod
212
+ def _migrate_settings ():
213
+ settings = QgsSettings ()
214
+ settings .beginGroup (OLD_SETTINGS_GROUP_NAME )
215
+ for key in settings .allKeys ():
216
+ set_setting (key , settings .value (key ))
217
+ settings .endGroup ()
218
+ settings .remove (OLD_SETTINGS_GROUP_NAME )
219
+
209
220
def unload (self ):
210
221
"""Removes the plugin menu item and icon from QGIS GUI."""
211
222
for action in self .actions :
@@ -214,24 +225,21 @@ def unload(self):
214
225
215
226
def run (self ):
216
227
"""Run method that performs all the real work"""
217
- self .nls_user_key = QSettings ().value ("/NLSgpkgloader/userKey" , "" , type = str )
218
- self .data_download_dir = QSettings ().value (
219
- "/NLSgpkgloader/dataDownloadDir" , "" , type = str
220
- )
221
- self .fileName = QSettings ().value (
222
- "/NLSgpkgloader/defaultFileName" , "mtk.gpkg" , type = str
223
- )
224
- self .addDownloadedDataAsLayer = QSettings ().value (
225
- "/NLSgpkgloader/addDownloadedDataAsLayer" , True , type = bool
228
+ self .nls_user_key = get_setting ("userKey" , "" , typehint = str )
229
+
230
+ self .data_download_dir = get_setting ("dataDownloadDir" , "" , typehint = str )
231
+ self .fileName = get_setting ("defaultFileName" , "mtk.gpkg" , typehint = str )
232
+ self .addDownloadedDataAsLayer = get_setting (
233
+ "addDownloadedDataAsLayer" , True , typehint = bool
226
234
)
227
- self .showMunicipalitiesAsLayer = QSettings (). value (
228
- "/NLSgpkgloader/ showMunicipalitiesAsLayer" , True , type = bool
235
+ self .showMunicipalitiesAsLayer = get_setting (
236
+ "showMunicipalitiesAsLayer" , True , typehint = bool
229
237
)
230
- self .showUTMGridsAsLayer = QSettings (). value (
231
- "/NLSgpkgloader/ showUTMGridsAsLayer" , False , type = bool
238
+ self .showUTMGridsAsLayer = get_setting (
239
+ "showUTMGridsAsLayer" , False , typehint = bool
232
240
)
233
- self .showSeatilesAsLayer = QSettings (). value (
234
- "/NLSgpkgloader/ showSeatilesAsLayer" , False , type = bool
241
+ self .showSeatilesAsLayer = get_setting (
242
+ "showSeatilesAsLayer" , False , typehint = bool
235
243
)
236
244
237
245
if self .nls_user_key == "" :
@@ -290,7 +298,7 @@ def run(self):
290
298
return
291
299
if self .fileName .split ("." )[- 1 ].lower () != "gpkg" :
292
300
self .fileName += ".gpkg"
293
- QSettings (). setValue ( "/NLSgpkgloader/ defaultFileName" , self .fileName )
301
+ set_setting ( " defaultFileName" , self .fileName )
294
302
self .gpkg_path = os .path .join (self .data_download_dir , self .fileName )
295
303
if os .path .isfile (self .gpkg_path ):
296
304
reply = QMessageBox .question (
@@ -398,18 +406,10 @@ def toggle_layers(self):
398
406
)
399
407
self .showUTMGridsAsLayer = self .municipalities_dialog .loadUtmGrids .isChecked ()
400
408
self .showSeatilesAsLayer = self .municipalities_dialog .loadSeaGrids .isChecked ()
401
- QSettings ().setValue (
402
- "/NLSgpkgloader/addDownloadedDataAsLayer" , self .addDownloadedDataAsLayer
403
- )
404
- QSettings ().setValue (
405
- "/NLSgpkgloader/showMunicipalitiesAsLayer" , self .showMunicipalitiesAsLayer
406
- )
407
- QSettings ().setValue (
408
- "/NLSgpkgloader/showUTMGridsAsLayer" , self .showUTMGridsAsLayer
409
- )
410
- QSettings ().setValue (
411
- "/NLSgpkgloader/showSeatilesAsLayer" , self .showSeatilesAsLayer
412
- )
409
+ set_setting ("addDownloadedDataAsLayer" , self .addDownloadedDataAsLayer )
410
+ set_setting ("showMunicipalitiesAsLayer" , self .showMunicipalitiesAsLayer )
411
+ set_setting ("showUTMGridsAsLayer" , self .showUTMGridsAsLayer )
412
+ set_setting ("showSeatilesAsLayer" , self .showSeatilesAsLayer )
413
413
414
414
found_utm5_layer = (
415
415
found_utm10_layer
@@ -872,10 +872,10 @@ def show_settings_dialog(self):
872
872
)
873
873
self .nls_user_key_dialog .userKeyLineEdit .setText (self .nls_user_key )
874
874
self .nls_user_key_dialog .dataLocationQgsFileWidget .setFilePath (
875
- QSettings (). value (
876
- "/NLSgpkgloader/ dataDownloadDir" ,
875
+ get_setting (
876
+ "dataDownloadDir" ,
877
877
os .path .join (self .path , "data" ),
878
- type = str ,
878
+ typehint = str ,
879
879
)
880
880
)
881
881
@@ -895,10 +895,8 @@ def show_settings_dialog(self):
895
895
self .nls_user_key_dialog .dataLocationQgsFileWidget .filePath ()
896
896
)
897
897
898
- QSettings ().setValue ("/NLSgpkgloader/userKey" , self .nls_user_key )
899
- QSettings ().setValue (
900
- "/NLSgpkgloader/dataDownloadDir" , self .data_download_dir
901
- )
898
+ set_setting ("userKey" , self .nls_user_key )
899
+ set_setting ("dataDownloadDir" , self .data_download_dir )
902
900
return True
903
901
904
902
else :
0 commit comments