|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to edit a list of strings. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 from PyQt5.QtCore import pyqtSlot, Qt, QSortFilterProxyModel, QStringListModel |
|
12 from PyQt5.QtWidgets import QWidget, QInputDialog, QLineEdit |
|
13 |
|
14 from .Ui_E5StringListEditWidget import Ui_E5StringListEditWidget |
|
15 |
|
16 |
|
17 class E5StringListEditWidget(QWidget, Ui_E5StringListEditWidget): |
|
18 """ |
|
19 Class implementing a dialog to edit a list of strings. |
|
20 """ |
|
21 def __init__(self, parent=None): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param parent reference to the parent widget (QWidget) |
|
26 """ |
|
27 super(E5StringListEditWidget, self).__init__(parent) |
|
28 self.setupUi(self) |
|
29 |
|
30 self.__model = QStringListModel(self) |
|
31 self.__proxyModel = QSortFilterProxyModel(self) |
|
32 self.__proxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive) |
|
33 self.__proxyModel.setSourceModel(self.__model) |
|
34 self.stringList.setModel(self.__proxyModel) |
|
35 |
|
36 self.searchEdit.textChanged.connect( |
|
37 self.__proxyModel.setFilterFixedString) |
|
38 |
|
39 self.removeButton.clicked.connect(self.stringList.removeSelected) |
|
40 self.removeAllButton.clicked.connect(self.stringList.removeAll) |
|
41 |
|
42 def setList(self, stringList): |
|
43 """ |
|
44 Public method to set the list of strings to be edited. |
|
45 |
|
46 @param stringList list of strings to be edited (list of string) |
|
47 """ |
|
48 self.__model.setStringList(stringList) |
|
49 self.__model.sort(0) |
|
50 |
|
51 def getList(self): |
|
52 """ |
|
53 Public method to get the edited list of strings. |
|
54 |
|
55 @return edited list of string (list of string) |
|
56 """ |
|
57 return self.__model.stringList()[:] |
|
58 |
|
59 def setListWhatsThis(self, txt): |
|
60 """ |
|
61 Public method to set a what's that help text for the string list. |
|
62 |
|
63 @param txt help text to be set (string) |
|
64 """ |
|
65 self.stringList.setWhatsThis(txt) |
|
66 |
|
67 @pyqtSlot() |
|
68 def on_addButton_clicked(self): |
|
69 """ |
|
70 Private slot to add an entry to the list. |
|
71 """ |
|
72 entry, ok = QInputDialog.getText( |
|
73 self, |
|
74 self.tr("Add Entry"), |
|
75 self.tr("Enter the entry to add to the list:"), |
|
76 QLineEdit.Normal) |
|
77 if ok and entry != "" and entry not in self.__model.stringList(): |
|
78 self.__model.insertRow(self.__model.rowCount()) |
|
79 self.__model.setData( |
|
80 self.__model.index(self.__model.rowCount() - 1), entry) |
|
81 self.__model.sort(0) |