|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2021 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 PyQt5.QtCore import pyqtSlot, Qt |
|
15 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
|
16 |
|
17 from E5Gui import E5MessageBox |
|
18 |
|
19 from .Ui_QtHelpDocumentationSelectionDialog import ( |
|
20 Ui_QtHelpDocumentationSelectionDialog |
|
21 ) |
|
22 |
|
23 |
|
24 class QtHelpDocumentationSelectionDialog( |
|
25 QDialog, Ui_QtHelpDocumentationSelectionDialog): |
|
26 """ |
|
27 Class implementing a dialog to select QtHelp documentation sets to be |
|
28 installed. |
|
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( |
|
52 QDialogButtonBox.StandardButton.Ok).hide() |
|
53 self.buttonBox.button( |
|
54 QDialogButtonBox.StandardButton.Cancel).hide() |
|
55 |
|
56 for category in helpDocuments: |
|
57 parentItem = QTreeWidgetItem(self.documentationList, [category]) |
|
58 for document in helpDocuments[category]: |
|
59 item = QTreeWidgetItem(parentItem, |
|
60 [os.path.basename(document)]) |
|
61 item.setData(0, Qt.ItemDataRole.UserRole, document) |
|
62 parentItem.setData(0, Qt.ItemDataRole.UserRole, |
|
63 os.path.dirname(document)) |
|
64 self.documentationList.sortItems(0, Qt.SortOrder.AscendingOrder) |
|
65 |
|
66 self.on_documentationList_itemSelectionChanged() |
|
67 |
|
68 @pyqtSlot() |
|
69 def on_documentationList_itemSelectionChanged(self): |
|
70 """ |
|
71 Private slot handling the selection of items. |
|
72 """ |
|
73 selectedCategoriesCount = 0 |
|
74 selectedDocumentSetCount = 0 |
|
75 for itm in self.documentationList.selectedItems(): |
|
76 if itm.parent() is None: |
|
77 selectedCategoriesCount += 1 |
|
78 else: |
|
79 selectedDocumentSetCount += 1 |
|
80 |
|
81 self.deleteButton.setEnabled(selectedDocumentSetCount > 0) |
|
82 self.deleteCategoryButton.setEnabled(selectedCategoriesCount > 0) |
|
83 |
|
84 @pyqtSlot() |
|
85 def on_deleteButton_clicked(self): |
|
86 """ |
|
87 Private slot to delete the selected documentation sets. |
|
88 """ |
|
89 yes = E5MessageBox.yesNo( |
|
90 self, |
|
91 self.tr("Delete Documentation Sets"), |
|
92 self.tr("""Shall the selected documentation sets really be""" |
|
93 """ deleted?""")) |
|
94 if yes: |
|
95 for itm in self.documentationList.selectedItems(): |
|
96 if itm.parent is None: |
|
97 # it is a category item, skip it |
|
98 continue |
|
99 |
|
100 category = itm.parent() |
|
101 fileName = itm.data(0, Qt.ItemDataRole.UserRole) |
|
102 try: |
|
103 os.remove(fileName) |
|
104 except OSError as err: |
|
105 E5MessageBox.warning( |
|
106 self, |
|
107 self.tr("Delete Documentation Sets"), |
|
108 self.tr("""<p>The documentation set <b>{0}</b> could""" |
|
109 """ not be deleted.</p><p>Reason: {1}</p>""") |
|
110 .format(fileName, str(err))) |
|
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 = E5MessageBox.yesNo( |
|
125 self, |
|
126 self.tr("Delete Documentation Sets"), |
|
127 self.tr("""Shall the selected documentation set categories""" |
|
128 """ really be deleted?""")) |
|
129 if yes: |
|
130 categories = [] |
|
131 for itm in self.documentationList.selectedItems(): |
|
132 if itm.parent() is None: |
|
133 categories.append(itm) |
|
134 for category in categories: |
|
135 self.__deleteCategory(category) |
|
136 |
|
137 @pyqtSlot() |
|
138 def on_deleteAllButton_clicked(self): |
|
139 """ |
|
140 Private slot to delete all documentation sets. |
|
141 """ |
|
142 yes = E5MessageBox.yesNo( |
|
143 self, |
|
144 self.tr("Delete Documentation Sets"), |
|
145 self.tr("""Shall all documentation sets really be deleted?""")) |
|
146 if yes: |
|
147 categories = [] |
|
148 for index in range(self.documentationList.topLevelItemCount()): |
|
149 categories.append( |
|
150 self.documentationList.topLevelItem(index)) |
|
151 for category in categories: |
|
152 self.__deleteCategory(category) |
|
153 |
|
154 def __deleteCategory(self, category): |
|
155 """ |
|
156 Private method to delete a category. |
|
157 |
|
158 @param category reference to the category item |
|
159 @type QTreeWidgetItem |
|
160 """ |
|
161 categoryDir = category.data(0, Qt.ItemDataRole.UserRole) |
|
162 shutil.rmtree(categoryDir, True) |
|
163 |
|
164 self.documentationList.takeTopLevelItem( |
|
165 self.documentationList.indexOfTopLevelItem(category)) |
|
166 del category |
|
167 |
|
168 def getData(self): |
|
169 """ |
|
170 Public method to retrieve the selected help documents. |
|
171 |
|
172 @return list of QtHelp documentation sets to be installed |
|
173 @rtype set of str |
|
174 """ |
|
175 documents = set() |
|
176 for item in self.documentationList.selectedItems(): |
|
177 if item.parent() is None: |
|
178 # it is a category item; add all files of that category |
|
179 for childIndex in range(item.childCount()): |
|
180 child = item.child(childIndex) |
|
181 fileName = child.data(0, Qt.ItemDataRole.UserRole) |
|
182 if fileName: |
|
183 documents.add(fileName) |
|
184 else: |
|
185 fileName = item.data(0, Qt.ItemDataRole.UserRole) |
|
186 if fileName: |
|
187 documents.add(fileName) |
|
188 return documents |