|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 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 PyQt5.QtCore import pyqtSlot |
|
11 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractItemView |
|
12 |
|
13 from .Ui_E5ListSelectionDialog import Ui_E5ListSelectionDialog |
|
14 |
|
15 |
|
16 class E5ListSelectionDialog(QDialog, Ui_E5ListSelectionDialog): |
|
17 """ |
|
18 Class implementing a dialog to select from a list of strings. |
|
19 """ |
|
20 def __init__(self, entries, |
|
21 selectionMode=QAbstractItemView.ExtendedSelection, |
|
22 title="", message="", parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param entries list of entries to select from |
|
27 @type list of str |
|
28 @param selectionMode selection mode for the list |
|
29 @type QAbstractItemView.SelectionMode |
|
30 @param title tirle of the dialog |
|
31 @type str |
|
32 @param message message to be show in the dialog |
|
33 @type str |
|
34 @param parent reference to the parent widget |
|
35 @type QWidget |
|
36 """ |
|
37 super(E5ListSelectionDialog, self).__init__(parent) |
|
38 self.setupUi(self) |
|
39 |
|
40 self.selectionList.setSelectionMode(selectionMode) |
|
41 if title: |
|
42 self.setWindowTitle(title) |
|
43 if message: |
|
44 self.messageLabel.setText(message) |
|
45 |
|
46 self.selectionList.addItems(entries) |
|
47 |
|
48 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
49 |
|
50 @pyqtSlot() |
|
51 def on_selectionList_itemSelectionChanged(self): |
|
52 """ |
|
53 Private slot handling a change of the selection. |
|
54 """ |
|
55 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
|
56 len(self.selectionList.selectedItems()) > 0) |
|
57 |
|
58 def getSelection(self): |
|
59 """ |
|
60 Public method to retrieve the selected items. |
|
61 |
|
62 @return selected entries |
|
63 @rtype list of str |
|
64 """ |
|
65 entries = [] |
|
66 for item in self.selectionList.selectedItems(): |
|
67 entries.append(item.text()) |
|
68 return entries |