|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select from a list of strings. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt |
|
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractItemView, \ |
|
14 QListWidgetItem |
|
15 |
|
16 from .Ui_E5ListSelectionDialog import Ui_E5ListSelectionDialog |
|
17 |
|
18 |
|
19 class E5ListSelectionDialog(QDialog, Ui_E5ListSelectionDialog): |
|
20 """ |
|
21 Class implementing a dialog to select from a list of strings. |
|
22 """ |
|
23 def __init__(self, entries, |
|
24 selectionMode=QAbstractItemView.ExtendedSelection, |
|
25 title="", message="", checkBoxSelection=False, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param entries list of entries to select from |
|
30 @type list of str |
|
31 @param selectionMode selection mode for the list |
|
32 @type QAbstractItemView.SelectionMode |
|
33 @param title tirle of the dialog |
|
34 @type str |
|
35 @param message message to be show in the dialog |
|
36 @type str |
|
37 @param checkBoxSelection flag indicating to select items via their |
|
38 checkbox |
|
39 @type bool |
|
40 @param parent reference to the parent widget |
|
41 @type QWidget |
|
42 """ |
|
43 super(E5ListSelectionDialog, self).__init__(parent) |
|
44 self.setupUi(self) |
|
45 |
|
46 if title: |
|
47 self.setWindowTitle(title) |
|
48 if message: |
|
49 self.messageLabel.setText(message) |
|
50 |
|
51 self.__checkCount = 0 |
|
52 self.__isCheckBoxSelection = checkBoxSelection |
|
53 if self.__isCheckBoxSelection: |
|
54 self.selectionList.setSelectionMode(QAbstractItemView.NoSelection) |
|
55 for entry in entries: |
|
56 itm = QListWidgetItem(entry) |
|
57 itm.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) |
|
58 itm.setCheckState(Qt.Unchecked) |
|
59 self.selectionList.addItem(itm) |
|
60 else: |
|
61 self.selectionList.setSelectionMode(selectionMode) |
|
62 self.selectionList.addItems(entries) |
|
63 |
|
64 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
65 |
|
66 @pyqtSlot() |
|
67 def on_selectionList_itemSelectionChanged(self): |
|
68 """ |
|
69 Private slot handling a change of the selection. |
|
70 """ |
|
71 if not self.__isCheckBoxSelection: |
|
72 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
|
73 len(self.selectionList.selectedItems()) > 0) |
|
74 |
|
75 def on_selectionList_itemChanged(self, itm): |
|
76 """ |
|
77 Private slot handling a change of an item. |
|
78 |
|
79 @param itm reference to the changed item |
|
80 @type QListWidgetItem |
|
81 """ |
|
82 if self.__isCheckBoxSelection: |
|
83 if itm.checkState() == Qt.Checked: |
|
84 self.__checkCount += 1 |
|
85 else: |
|
86 self.__checkCount -= 1 |
|
87 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
|
88 self.__checkCount > 0) |
|
89 |
|
90 def getSelection(self): |
|
91 """ |
|
92 Public method to retrieve the selected items. |
|
93 |
|
94 @return selected entries |
|
95 @rtype list of str |
|
96 """ |
|
97 entries = [] |
|
98 if self.__isCheckBoxSelection: |
|
99 for row in range(self.selectionList.count()): |
|
100 item = self.selectionList.item(row) |
|
101 if item.checkState() == Qt.Checked: |
|
102 entries.append(item.text()) |
|
103 else: |
|
104 for item in self.selectionList.selectedItems(): |
|
105 entries.append(item.text()) |
|
106 return entries |