|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2013 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select multiple entries from a list. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtWidgets import QDialog, QListWidgetItem, QAbstractItemView |
|
11 |
|
12 from .Ui_DotDesktopListSelectionDialog import Ui_DotDesktopListSelectionDialog |
|
13 |
|
14 |
|
15 class DotDesktopListSelectionDialog(QDialog, Ui_DotDesktopListSelectionDialog): |
|
16 """ |
|
17 Class implementing a dialog to select multiple entries from a list. |
|
18 """ |
|
19 def __init__(self, entries, selectedEntries, separator, subEntries=None, |
|
20 allowMultiMain=True, allowMultiSub=True, parent=None): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param entries list of entries to be shown (list of string) |
|
25 @param selectedEntries list of entries to be selected (list of string |
|
26 or string of entries separated by separator) |
|
27 @param separator separator string (string) |
|
28 @param subEntries secondary list of entries (list of string) |
|
29 @param allowMultiMain flag indicating to allow multiple selections for |
|
30 the main entry (bool) |
|
31 @param allowMultiSub flag indicating to allow multiple selections for |
|
32 the sub entry (bool) |
|
33 @param parent reference to the parent widget (QWidget) |
|
34 """ |
|
35 super().__init__(parent) |
|
36 self.setupUi(self) |
|
37 |
|
38 if isinstance(selectedEntries, str): |
|
39 selectedEntries = selectedEntries.split(separator) |
|
40 |
|
41 if not allowMultiMain: |
|
42 self.entriesList.setSelectionMode( |
|
43 QAbstractItemView.SelectionMode.SingleSelection) |
|
44 if not allowMultiSub: |
|
45 self.subList.setSelectionMode( |
|
46 QAbstractItemView.SelectionMode.SingleSelection) |
|
47 |
|
48 for entry in entries: |
|
49 itm = QListWidgetItem(entry, self.entriesList) |
|
50 if entry in selectedEntries: |
|
51 itm.setSelected(True) |
|
52 |
|
53 if subEntries: |
|
54 for entry in subEntries: |
|
55 itm = QListWidgetItem(entry, self.subList) |
|
56 if entry in selectedEntries: |
|
57 itm.setSelected(True) |
|
58 else: |
|
59 self.subList.setVisible(False) |
|
60 |
|
61 def getData(self, separator=None, separatorAtEnd=False): |
|
62 """ |
|
63 Public method to extract the selected entries as a list |
|
64 or a string. |
|
65 |
|
66 @param separator separator string (string) |
|
67 @param separatorAtEnd flag indicating to append the separator (boolean) |
|
68 @return list of selected entries (list of string) if the separator is |
|
69 None or a string with entries delimited by separator (string) |
|
70 """ |
|
71 entries = [] |
|
72 for itm in self.entriesList.selectedItems(): |
|
73 entries.append(itm.text()) |
|
74 for itm in self.subList.selectedItems(): |
|
75 entries.append(itm.text()) |
|
76 |
|
77 if separator is None: |
|
78 return entries |
|
79 else: |
|
80 entriesStr = separator.join(entries) |
|
81 if separatorAtEnd: |
|
82 entriesStr += separator |
|
83 return entriesStr |