|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter the file filter properties. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Ui_FindFileFilterPropertiesDialog import Ui_FindFileFilterPropertiesDialog |
|
14 |
|
15 |
|
16 class FindFileFilterPropertiesDialog(QDialog, Ui_FindFileFilterPropertiesDialog): |
|
17 """ |
|
18 Class implementing a dialog to enter the file filter properties. |
|
19 """ |
|
20 |
|
21 def __init__(self, currentFilters, properties=None, parent=None): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param currentFilters list of existing filters to check against |
|
26 @type list of str |
|
27 @param properties tuple containing the filter name and pattern |
|
28 to be edited (defaults to None) |
|
29 @type tuple of (str, str) (optional) |
|
30 @param parent reference to the parent widget (defaults to None) |
|
31 @type QWidget (optional) |
|
32 """ |
|
33 super().__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.__filters = currentFilters |
|
37 |
|
38 self.__editMode = properties is not None |
|
39 if self.__editMode: |
|
40 self.textEdit.setText(properties[0]) |
|
41 self.patternEdit.setText(properties[1]) |
|
42 |
|
43 self.__updateOKAndError() |
|
44 |
|
45 self.textEdit.textChanged.connect(self.__updateOKAndError) |
|
46 self.patternEdit.textChanged.connect(self.__updateOKAndError) |
|
47 |
|
48 msh = self.minimumSizeHint() |
|
49 self.resize(max(self.width(), msh.width()), msh.height()) |
|
50 |
|
51 @pyqtSlot() |
|
52 def __updateOKAndError(self): |
|
53 """ |
|
54 Private slot to set the enabled state of the OK button. |
|
55 """ |
|
56 filterText = self.textEdit.text() |
|
57 patternText = self.patternEdit.text() |
|
58 |
|
59 # 1. Set state of the OK button. |
|
60 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
61 bool(filterText) and filterText not in self.__filters and bool(patternText) |
|
62 ) |
|
63 |
|
64 # 2. Give the user an indication why OK is not enabled. |
|
65 if filterText in self.__filters: |
|
66 self.errorLabel.setText(self.tr("The filter name exists already.")) |
|
67 elif not bool(filterText) or not bool(patternText): |
|
68 self.errorLabel.setText( |
|
69 self.tr("The filter name and/or pattern must not be empty.") |
|
70 ) |
|
71 else: |
|
72 self.errorLabel.clear() |
|
73 |
|
74 def getProperties(self): |
|
75 """ |
|
76 Public method to retrieve the entered filter properties. |
|
77 |
|
78 @return tuple cotaining the filter name and pattern |
|
79 @rtype tuple of (str, str) |
|
80 """ |
|
81 return self.textEdit.text(), self.patternEdit.text().rstrip(";") |