Skip to content

Commit

Permalink
[ux] Disable add/remove settings buttons when there are no more setti…
Browse files Browse the repository at this point in the history
…ngs to add and when there is no selection, respectively (#44)
  • Loading branch information
gacarrillor authored Jul 11, 2024
1 parent 99f1059 commit 22f1e3a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from qgis.core import QgsApplication
from qgis.gui import QgsMessageBar
from qgis.PyQt.QtCore import Qt, pyqtSlot
from qgis.PyQt.QtCore import QItemSelection, Qt, pyqtSlot
from qgis.PyQt.QtWidgets import QDialog, QMessageBox, QSizePolicy

from pg_service_parser.conf.service_settings import SERVICE_SETTINGS, SETTINGS_TEMPLATE
Expand Down Expand Up @@ -62,6 +62,7 @@ def __initialize_dialog(self):
self.txtConfFile.setVisible(True)
self.tabWidget.setEnabled(True)
self.btnCreateServiceFile.setVisible(False)
self.btnRemoveSetting.setEnabled(False)

self.radOverwrite.toggled.connect(self.__update_target_controls)
self.btnCopyService.clicked.connect(self.__copy_service)
Expand All @@ -75,6 +76,7 @@ def __initialize_dialog(self):
self.__initialize_edit_services()
self.__initialize_copy_services()
self.__update_target_controls(True)
self.__update_add_settings_button()

self.bar = QgsMessageBar()
self.bar.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
Expand All @@ -97,6 +99,11 @@ def __update_target_controls(self, checked):
self.cboTargetService.setEnabled(self.radOverwrite.isChecked())
self.txtNewService.setEnabled(not self.radOverwrite.isChecked())

def __update_add_settings_button(self):
# Make sure to call this method whenever the settings are added/removed
enable = self.__edit_model and self.__edit_model.rowCount() < len(SERVICE_SETTINGS)
self.btnAddSettings.setEnabled(enable)

@pyqtSlot(int)
def __source_service_changed(self, index):
# Remember latest currentText only if current item in source
Expand Down Expand Up @@ -197,6 +204,9 @@ def __edit_service_changed(self, index):
target_service, service_config(target_service, self.__conf_file_path)
)
self.tblServiceConfig.setModel(self.__edit_model)
self.tblServiceConfig.selectionModel().selectionChanged.connect(
self.__update_settings_buttons
)
self.__edit_model.is_dirty_changed.connect(self.btnUpdateService.setEnabled)
self.btnUpdateService.setDisabled(True)

Expand All @@ -205,6 +215,13 @@ def __edit_service_changed(self, index):
self.__edit_model.add_settings(SETTINGS_TEMPLATE)
self.__new_empty_file = False

self.__update_add_settings_button() # Model just created
self.__update_settings_buttons(QItemSelection(), QItemSelection())

@pyqtSlot(QItemSelection, QItemSelection)
def __update_settings_buttons(self, selected, deselected):
self.btnRemoveSetting.setEnabled(bool(selected.indexes()))

@pyqtSlot()
def __add_settings_clicked(self):
dlg = ServiceSettingsDialog(self, self.__edit_model.current_setting_keys())
Expand All @@ -215,6 +232,7 @@ def __add_settings_clicked(self):
k: v["default"] for k, v in SERVICE_SETTINGS.items() if k in dlg.settings_to_add
}
self.__edit_model.add_settings(settings)
self.__update_add_settings_button() # Settings added

@pyqtSlot()
def __remove_setting_clicked(self):
Expand All @@ -232,6 +250,7 @@ def __remove_setting_clicked(self):
== QMessageBox.StandardButton.Yes
):
self.__edit_model.remove_setting(selected_indexes[0])
self.__update_add_settings_button() # Settings removed

@pyqtSlot()
def __update_service_clicked(self):
Expand Down

0 comments on commit 22f1e3a

Please sign in to comment.