|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show the available modules of all bundles. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt, pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QListWidgetItem |
|
12 |
|
13 from eric7.EricGui import EricPixmapCache |
|
14 |
|
15 from .Ui_ShowModulesDialog import Ui_ShowModulesDialog |
|
16 |
|
17 |
|
18 class ShowModulesDialog(QDialog, Ui_ShowModulesDialog): |
|
19 """ |
|
20 Class implementing a dialog to show the available modules of all bundles. |
|
21 """ |
|
22 |
|
23 def __init__(self, modulesList, selectionMode=False, info="", parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param modulesList list of module names to be shown |
|
28 @type list of str |
|
29 @param selectionMode flag indicating the activation of the selection mode |
|
30 (defaults to False) |
|
31 @type bool (optional) |
|
32 @param info string containing some informational data (defaults to "") |
|
33 @type str (optional) |
|
34 @param parent reference to the parent widget (defaults to None) |
|
35 @type QWidget (optional) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.setupUi(self) |
|
39 |
|
40 self.filterButton.setIcon(EricPixmapCache.getIcon("check")) |
|
41 self.filterButton.clicked.connect(self.__applyFilter) |
|
42 |
|
43 self.__checkCount = 0 |
|
44 self.__selectionMode = selectionMode |
|
45 if self.__selectionMode: |
|
46 self.buttonBox.setStandardButtons( |
|
47 QDialogButtonBox.StandardButton.Ok |
|
48 | QDialogButtonBox.StandardButton.Cancel |
|
49 ) |
|
50 else: |
|
51 self.buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Close) |
|
52 |
|
53 if self.__selectionMode: |
|
54 for moduleName in modulesList: |
|
55 itm = QListWidgetItem(moduleName) |
|
56 itm.setFlags(itm.flags() | Qt.ItemFlag.ItemIsUserCheckable) |
|
57 itm.setCheckState(Qt.CheckState.Unchecked) |
|
58 self.modulesList.addItem(itm) |
|
59 else: |
|
60 self.modulesList.addItems(modulesList) |
|
61 self.modulesList.sortItems(Qt.SortOrder.AscendingOrder) |
|
62 |
|
63 if info: |
|
64 self.infoLabel.setText(info) |
|
65 else: |
|
66 self.infoLabel.hide() |
|
67 |
|
68 self.__applyFilter() |
|
69 |
|
70 self.__checkCountUpdated() |
|
71 |
|
72 @pyqtSlot() |
|
73 def __applyFilter(self): |
|
74 """ |
|
75 Private slot to apply the filter to the list of available modules. |
|
76 """ |
|
77 filterStr = self.filterEdit.text() |
|
78 counter = 0 |
|
79 for row in range(self.modulesList.count()): |
|
80 itm = self.modulesList.item(row) |
|
81 visible = filterStr in itm.text() if filterStr else True |
|
82 itm.setHidden(not visible) |
|
83 if visible: |
|
84 counter += 1 |
|
85 |
|
86 self.statusLabel.setText( |
|
87 self.tr("Showing {0} of {1} modules/packages").format( |
|
88 counter, self.modulesList.count() |
|
89 ) |
|
90 ) |
|
91 self.filterEdit.selectAll() |
|
92 self.filterEdit.setFocus(Qt.FocusReason.OtherFocusReason) |
|
93 |
|
94 @pyqtSlot(QListWidgetItem) |
|
95 def on_modulesList_itemChanged(self, item): |
|
96 """ |
|
97 Private slot to handle a change of the check state of an item. |
|
98 |
|
99 @param item reference to the changed item |
|
100 @type QTreeWidgetItem |
|
101 """ |
|
102 if self.__selectionMode: |
|
103 if item.checkState() == Qt.CheckState.Checked: |
|
104 self.__checkCount += 1 |
|
105 else: |
|
106 self.__checkCount -= 1 |
|
107 |
|
108 self.__checkCountUpdated() |
|
109 |
|
110 def __checkCountUpdated(self): |
|
111 """ |
|
112 Private method to handle an update of the check count. |
|
113 """ |
|
114 if self.__selectionMode: |
|
115 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
116 self.__checkCount > 0 |
|
117 ) |
|
118 |
|
119 def getSelection(self): |
|
120 """ |
|
121 Public method to get the list of selected modules. |
|
122 |
|
123 @return list of selected modules |
|
124 @rtype circup.Module |
|
125 """ |
|
126 results = [] |
|
127 if self.__selectionMode: |
|
128 for row in range(self.modulesList.count()): |
|
129 itm = self.modulesList.item(row) |
|
130 if itm.checkState() == Qt.CheckState.Checked: |
|
131 results.append(itm.text()) |
|
132 |
|
133 return results |