|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select QtHelp documentation sets to be |
|
8 installed. |
|
9 """ |
|
10 |
|
11 import os |
|
12 import shutil |
|
13 |
|
14 from PyQt6.QtCore import Qt, pyqtSlot |
|
15 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
|
16 |
|
17 from eric7.EricWidgets import EricMessageBox |
|
18 |
|
19 from .Ui_QtHelpDocumentationSelectionDialog import Ui_QtHelpDocumentationSelectionDialog |
|
20 |
|
21 |
|
22 class QtHelpDocumentationSelectionDialog( |
|
23 QDialog, Ui_QtHelpDocumentationSelectionDialog |
|
24 ): |
|
25 """ |
|
26 Class implementing a dialog to select QtHelp documentation sets to be |
|
27 installed. |
|
28 """ |
|
29 |
|
30 AddMode = "Add" |
|
31 ManageMode = "Manage" |
|
32 |
|
33 def __init__(self, helpDocuments, mode, parent=None): |
|
34 """ |
|
35 Constructor |
|
36 |
|
37 @param helpDocuments dictionary containing the lists of help documents |
|
38 to be shown |
|
39 @type dict of lists of str |
|
40 @param mode mode of the dialog |
|
41 @type str |
|
42 @param parent reference to the parent widget |
|
43 @type QWidget |
|
44 """ |
|
45 super().__init__(parent) |
|
46 self.setupUi(self) |
|
47 |
|
48 if mode == QtHelpDocumentationSelectionDialog.AddMode: |
|
49 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).hide() |
|
50 else: |
|
51 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).hide() |
|
52 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).hide() |
|
53 |
|
54 for category in helpDocuments: |
|
55 parentItem = QTreeWidgetItem(self.documentationList, [category]) |
|
56 for document in helpDocuments[category]: |
|
57 item = QTreeWidgetItem(parentItem, [os.path.basename(document)]) |
|
58 item.setData(0, Qt.ItemDataRole.UserRole, document) |
|
59 parentItem.setData( |
|
60 0, Qt.ItemDataRole.UserRole, os.path.dirname(document) |
|
61 ) |
|
62 self.documentationList.sortItems(0, Qt.SortOrder.AscendingOrder) |
|
63 |
|
64 self.on_documentationList_itemSelectionChanged() |
|
65 |
|
66 @pyqtSlot() |
|
67 def on_documentationList_itemSelectionChanged(self): |
|
68 """ |
|
69 Private slot handling the selection of items. |
|
70 """ |
|
71 selectedCategoriesCount = 0 |
|
72 selectedDocumentSetCount = 0 |
|
73 for itm in self.documentationList.selectedItems(): |
|
74 if itm.parent() is None: |
|
75 selectedCategoriesCount += 1 |
|
76 else: |
|
77 selectedDocumentSetCount += 1 |
|
78 |
|
79 self.deleteButton.setEnabled(selectedDocumentSetCount > 0) |
|
80 self.deleteCategoryButton.setEnabled(selectedCategoriesCount > 0) |
|
81 |
|
82 @pyqtSlot() |
|
83 def on_deleteButton_clicked(self): |
|
84 """ |
|
85 Private slot to delete the selected documentation sets. |
|
86 """ |
|
87 yes = EricMessageBox.yesNo( |
|
88 self, |
|
89 self.tr("Delete Documentation Sets"), |
|
90 self.tr("""Shall the selected documentation sets really be deleted?"""), |
|
91 ) |
|
92 if yes: |
|
93 for itm in self.documentationList.selectedItems(): |
|
94 if itm.parent is None: |
|
95 # it is a category item, skip it |
|
96 continue |
|
97 |
|
98 category = itm.parent() |
|
99 fileName = itm.data(0, Qt.ItemDataRole.UserRole) |
|
100 try: |
|
101 os.remove(fileName) |
|
102 except OSError as err: |
|
103 EricMessageBox.warning( |
|
104 self, |
|
105 self.tr("Delete Documentation Sets"), |
|
106 self.tr( |
|
107 """<p>The documentation set <b>{0}</b> could""" |
|
108 """ not be deleted.</p><p>Reason: {1}</p>""" |
|
109 ).format(fileName, str(err)), |
|
110 ) |
|
111 continue |
|
112 |
|
113 category.removeChild(itm) |
|
114 del itm |
|
115 |
|
116 if category.childCount() == 0: |
|
117 self.__deleteCategory(category) |
|
118 |
|
119 @pyqtSlot() |
|
120 def on_deleteCategoryButton_clicked(self): |
|
121 """ |
|
122 Private slot to delete the selected documentation set categories. |
|
123 """ |
|
124 yes = EricMessageBox.yesNo( |
|
125 self, |
|
126 self.tr("Delete Documentation Sets"), |
|
127 self.tr( |
|
128 """Shall the selected documentation set categories""" |
|
129 """ really be deleted?""" |
|
130 ), |
|
131 ) |
|
132 if yes: |
|
133 categories = [] |
|
134 for itm in self.documentationList.selectedItems(): |
|
135 if itm.parent() is None: |
|
136 categories.append(itm) |
|
137 for category in categories: |
|
138 self.__deleteCategory(category) |
|
139 |
|
140 @pyqtSlot() |
|
141 def on_deleteAllButton_clicked(self): |
|
142 """ |
|
143 Private slot to delete all documentation sets. |
|
144 """ |
|
145 yes = EricMessageBox.yesNo( |
|
146 self, |
|
147 self.tr("Delete Documentation Sets"), |
|
148 self.tr("""Shall all documentation sets really be deleted?"""), |
|
149 ) |
|
150 if yes: |
|
151 categories = [] |
|
152 for index in range(self.documentationList.topLevelItemCount()): |
|
153 categories.append(self.documentationList.topLevelItem(index)) |
|
154 for category in categories: |
|
155 self.__deleteCategory(category) |
|
156 |
|
157 def __deleteCategory(self, category): |
|
158 """ |
|
159 Private method to delete a category. |
|
160 |
|
161 @param category reference to the category item |
|
162 @type QTreeWidgetItem |
|
163 """ |
|
164 categoryDir = category.data(0, Qt.ItemDataRole.UserRole) |
|
165 shutil.rmtree(categoryDir, True) |
|
166 |
|
167 self.documentationList.takeTopLevelItem( |
|
168 self.documentationList.indexOfTopLevelItem(category) |
|
169 ) |
|
170 del category |
|
171 |
|
172 def getData(self): |
|
173 """ |
|
174 Public method to retrieve the selected help documents. |
|
175 |
|
176 @return list of QtHelp documentation sets to be installed |
|
177 @rtype set of str |
|
178 """ |
|
179 documents = set() |
|
180 for item in self.documentationList.selectedItems(): |
|
181 if item.parent() is None: |
|
182 # it is a category item; add all files of that category |
|
183 for childIndex in range(item.childCount()): |
|
184 child = item.child(childIndex) |
|
185 fileName = child.data(0, Qt.ItemDataRole.UserRole) |
|
186 if fileName: |
|
187 documents.add(fileName) |
|
188 else: |
|
189 fileName = item.data(0, Qt.ItemDataRole.UserRole) |
|
190 if fileName: |
|
191 documents.add(fileName) |
|
192 return documents |