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