|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to edit a list of strings. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import ( |
|
11 pyqtSlot, pyqtSignal, Qt, QSortFilterProxyModel, QStringListModel |
|
12 ) |
|
13 from PyQt6.QtWidgets import QWidget, QInputDialog, QLineEdit |
|
14 |
|
15 from .Ui_EricStringListEditWidget import Ui_EricStringListEditWidget |
|
16 |
|
17 |
|
18 class EricStringListEditWidget(QWidget, Ui_EricStringListEditWidget): |
|
19 """ |
|
20 Class implementing a dialog to edit a list of strings. |
|
21 |
|
22 @signal setToDefault() emitted to request the default list of values |
|
23 """ |
|
24 setToDefault = pyqtSignal() |
|
25 |
|
26 def __init__(self, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param parent reference to the parent widget (QWidget) |
|
31 """ |
|
32 super().__init__(parent) |
|
33 self.setupUi(self) |
|
34 |
|
35 self.__model = QStringListModel(self) |
|
36 self.__proxyModel = QSortFilterProxyModel(self) |
|
37 self.__proxyModel.setFilterCaseSensitivity( |
|
38 Qt.CaseSensitivity.CaseInsensitive) |
|
39 self.__proxyModel.setSourceModel(self.__model) |
|
40 self.stringList.setModel(self.__proxyModel) |
|
41 |
|
42 self.defaultButton.hide() |
|
43 |
|
44 self.searchEdit.textChanged.connect( |
|
45 self.__proxyModel.setFilterFixedString) |
|
46 |
|
47 self.removeButton.clicked.connect(self.stringList.removeSelected) |
|
48 self.removeAllButton.clicked.connect(self.stringList.removeAll) |
|
49 self.defaultButton.clicked.connect(self.setToDefault) |
|
50 |
|
51 def setList(self, stringList): |
|
52 """ |
|
53 Public method to set the list of strings to be edited. |
|
54 |
|
55 @param stringList list of strings to be edited (list of string) |
|
56 """ |
|
57 self.__model.setStringList(stringList) |
|
58 self.__model.sort(0) |
|
59 |
|
60 def getList(self): |
|
61 """ |
|
62 Public method to get the edited list of strings. |
|
63 |
|
64 @return edited list of string (list of string) |
|
65 """ |
|
66 return self.__model.stringList()[:] |
|
67 |
|
68 def setListWhatsThis(self, txt): |
|
69 """ |
|
70 Public method to set a what's that help text for the string list. |
|
71 |
|
72 @param txt help text to be set (string) |
|
73 """ |
|
74 self.stringList.setWhatsThis(txt) |
|
75 |
|
76 def setDefaultVisible(self, visible): |
|
77 """ |
|
78 Public method to show or hide the default button. |
|
79 |
|
80 @param visible flag indicating the visibility of the default button |
|
81 @type bool |
|
82 """ |
|
83 self.defaultButton.setVisible(visible) |
|
84 |
|
85 def setAddVisible(self, visible): |
|
86 """ |
|
87 Public method to show or hide the add button. |
|
88 |
|
89 @param visible flag indicating the visibility of the add button |
|
90 @type bool |
|
91 """ |
|
92 self.addButton.setVisible(visible) |
|
93 self.addLine.setVisible(visible) |
|
94 |
|
95 @pyqtSlot() |
|
96 def on_addButton_clicked(self): |
|
97 """ |
|
98 Private slot to add an entry to the list. |
|
99 """ |
|
100 entry, ok = QInputDialog.getText( |
|
101 self, |
|
102 self.tr("Add Entry"), |
|
103 self.tr("Enter the entry to add to the list:"), |
|
104 QLineEdit.EchoMode.Normal) |
|
105 if ok and entry != "" and entry not in self.__model.stringList(): |
|
106 self.__model.insertRow(self.__model.rowCount()) |
|
107 self.__model.setData( |
|
108 self.__model.index(self.__model.rowCount() - 1), entry) |
|
109 self.__model.sort(0) |