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