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