From f3f700f7371f7a8671935642d47f6740856d04cd Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sat, 30 Oct 2021 16:43:27 +0200 Subject: [PATCH] check if form is dirty --- highlightingdialog.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/highlightingdialog.py b/highlightingdialog.py index eb79afa..39f8fcb 100644 --- a/highlightingdialog.py +++ b/highlightingdialog.py @@ -71,7 +71,7 @@ class HighlightingDialog(QDialog): row = row + 1 self.buttons = QDialogButtonBox() - self.buttons.setStandardButtons(QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Save) + self.buttons.setStandardButtons(QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok) self.buttons.accepted.connect(self._save) self.buttons.rejected.connect(self.close) form_grid.addWidget(self.buttons, row, 0, 1, 2) @@ -126,6 +126,16 @@ class HighlightingDialog(QDialog): self.list.setCurrentIndex(index.sibling(selected_index + 1, 0)) def _save(self): + + if self._is_dirty(): + unsaved = QMessageBox(QMessageBox.Icon.Question, self.tr("unsaved changes"), + self.tr("You have unsaved changes. Continue?")) + unsaved.setStandardButtons(QMessageBox.StandardButton.Cancel) + unsaved.addButton(QPushButton(self.tr("Continue")), QMessageBox.ButtonRole.AcceptRole) + result = unsaved.exec() + if result == QMessageBox.StandardButton.Cancel: + return + highlighters = [] for index in range(0, self.list.count()): item: PayloadItem = self.list.item(index) @@ -156,6 +166,21 @@ class HighlightingDialog(QDialog): self.hit_background_color.set_color(highlighter.hit_background_color) self.line_background_color.set_color(highlighter.line_background_color) + def _is_dirty(self): + if len(self.list.selectedIndexes()) == 0: + dirty = False + if len(self.list.selectedIndexes()) == 1: + item: PayloadItem = self.list.currentItem() + highlighter: HighlightRegex = item.payload + dirty = self.query.text() != highlighter.query \ + or self.ignore_case.isChecked() != highlighter.ignore_case \ + or self.is_regex.isChecked() != highlighter.is_regex \ + or self.hit_background_color.color != highlighter.hit_background_color \ + or self.line_background_color.color != highlighter.line_background_color + else: + dirty = False + return dirty + def _load_existing_hightlighters(self): highlighters: [HighlightRegex] = Highlighting.read_config(self._settings) first_item = None