eric6/Helpviewer/QtHelpDocumentationSelectionDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
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 @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.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.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 list of str
174 """
175 documents = []
176 for item in self.documentationList.selectedItems():
177 fileName = item.data(0, Qt.UserRole)
178 if fileName:
179 documents.append(fileName)
180 return documents

eric ide

mercurial