|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to manage the QtHelp documentation database. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt, QItemSelectionModel |
|
13 from PyQt5.QtWidgets import QDialog |
|
14 from PyQt5.QtHelp import QHelpEngineCore |
|
15 |
|
16 from E5Gui import E5MessageBox, E5FileDialog |
|
17 from E5Gui.E5Application import e5App |
|
18 |
|
19 from .Ui_QtHelpDocumentationDialog import Ui_QtHelpDocumentationDialog |
|
20 |
|
21 |
|
22 class QtHelpDocumentationDialog(QDialog, Ui_QtHelpDocumentationDialog): |
|
23 """ |
|
24 Class implementing a dialog to manage the QtHelp documentation database. |
|
25 """ |
|
26 def __init__(self, engine, parent): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param engine reference to the help engine (QHelpEngine) |
|
31 @param parent reference to the parent widget (QWidget) |
|
32 """ |
|
33 super(QtHelpDocumentationDialog, self).__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.removeButton.setEnabled(False) |
|
37 |
|
38 self.__engine = engine |
|
39 self.__mw = parent |
|
40 |
|
41 docs = self.__engine.registeredDocumentations() |
|
42 self.documentsList.addItems(docs) |
|
43 |
|
44 self.__registeredDocs = [] |
|
45 self.__unregisteredDocs = [] |
|
46 self.__tabsToClose = [] |
|
47 |
|
48 try: |
|
49 self.__pluginHelpDocuments = \ |
|
50 e5App().getObject("PluginManager").getPluginQtHelpFiles() |
|
51 except KeyError: |
|
52 from PluginManager.PluginManager import PluginManager |
|
53 pluginManager = PluginManager(self, doLoadPlugins=False) |
|
54 pluginManager.loadDocumentationSetPlugins() |
|
55 pluginManager.activatePlugins() |
|
56 self.__pluginHelpDocuments = pluginManager.getPluginQtHelpFiles() |
|
57 self.addPluginButton.setEnabled(bool(self.__pluginHelpDocuments)) |
|
58 |
|
59 @pyqtSlot() |
|
60 def on_documentsList_itemSelectionChanged(self): |
|
61 """ |
|
62 Private slot handling a change of the documents selection. |
|
63 """ |
|
64 self.removeButton.setEnabled( |
|
65 len(self.documentsList.selectedItems()) != 0) |
|
66 |
|
67 @pyqtSlot() |
|
68 def on_addButton_clicked(self): |
|
69 """ |
|
70 Private slot to add QtHelp documents to the help database. |
|
71 """ |
|
72 fileNames = E5FileDialog.getOpenFileNames( |
|
73 self, |
|
74 self.tr("Add Documentation"), |
|
75 "", |
|
76 self.tr("Qt Compressed Help Files (*.qch)")) |
|
77 if not fileNames: |
|
78 return |
|
79 |
|
80 self.__registerDocumentations(fileNames) |
|
81 |
|
82 @pyqtSlot() |
|
83 def on_addPluginButton_clicked(self): |
|
84 """ |
|
85 Private slot to add QtHelp documents provided by plug-ins to |
|
86 the help database. |
|
87 """ |
|
88 from .QtHelpDocumentationSelectionDialog import \ |
|
89 QtHelpDocumentationSelectionDialog |
|
90 dlg = QtHelpDocumentationSelectionDialog( |
|
91 self.__pluginHelpDocuments, |
|
92 QtHelpDocumentationSelectionDialog.AddMode, |
|
93 self) |
|
94 if dlg.exec_() == QDialog.Accepted: |
|
95 documents = dlg.getData() |
|
96 if not documents: |
|
97 return |
|
98 |
|
99 self.__registerDocumentations(documents) |
|
100 |
|
101 @pyqtSlot() |
|
102 def on_managePluginButton_clicked(self): |
|
103 """ |
|
104 Private slot to manage the QtHelp documents provided by plug-ins. |
|
105 """ |
|
106 from .QtHelpDocumentationSelectionDialog import \ |
|
107 QtHelpDocumentationSelectionDialog |
|
108 dlg = QtHelpDocumentationSelectionDialog( |
|
109 self.__pluginHelpDocuments, |
|
110 QtHelpDocumentationSelectionDialog.ManageMode, |
|
111 self) |
|
112 dlg.exec_() |
|
113 |
|
114 def __registerDocumentations(self, fileNames): |
|
115 """ |
|
116 Private method to register a given list of documentations. |
|
117 |
|
118 @param fileNames list of documentation files to be registered |
|
119 @type list of str |
|
120 """ |
|
121 for fileName in fileNames: |
|
122 ns = QHelpEngineCore.namespaceName(fileName) |
|
123 if not ns: |
|
124 E5MessageBox.warning( |
|
125 self, |
|
126 self.tr("Add Documentation"), |
|
127 self.tr( |
|
128 """The file <b>{0}</b> is not a valid""" |
|
129 """ Qt Help File.""").format(fileName) |
|
130 ) |
|
131 continue |
|
132 |
|
133 if len(self.documentsList.findItems(ns, Qt.MatchFixedString)): |
|
134 E5MessageBox.warning( |
|
135 self, |
|
136 self.tr("Add Documentation"), |
|
137 self.tr( |
|
138 """The namespace <b>{0}</b> is already registered.""") |
|
139 .format(ns) |
|
140 ) |
|
141 continue |
|
142 |
|
143 self.__engine.registerDocumentation(fileName) |
|
144 self.documentsList.addItem(ns) |
|
145 self.__registeredDocs.append(ns) |
|
146 if ns in self.__unregisteredDocs: |
|
147 self.__unregisteredDocs.remove(ns) |
|
148 |
|
149 @pyqtSlot() |
|
150 def on_removeButton_clicked(self): |
|
151 """ |
|
152 Private slot to remove a document from the help database. |
|
153 """ |
|
154 res = E5MessageBox.yesNo( |
|
155 self, |
|
156 self.tr("Remove Documentation"), |
|
157 self.tr( |
|
158 """Do you really want to remove the selected documentation """ |
|
159 """sets from the database?""")) |
|
160 if not res: |
|
161 return |
|
162 |
|
163 openedDocs = self.__mw.getSourceFileList() |
|
164 |
|
165 items = self.documentsList.selectedItems() |
|
166 for item in items: |
|
167 ns = item.text() |
|
168 if ns in list(openedDocs.values()): |
|
169 res = E5MessageBox.yesNo( |
|
170 self, |
|
171 self.tr("Remove Documentation"), |
|
172 self.tr( |
|
173 """Some documents currently opened reference the """ |
|
174 """documentation you are attempting to remove. """ |
|
175 """Removing the documentation will close those """ |
|
176 """documents. Remove anyway?"""), |
|
177 icon=E5MessageBox.Warning) |
|
178 if not res: |
|
179 return |
|
180 self.__unregisteredDocs.append(ns) |
|
181 for docId in openedDocs: |
|
182 if openedDocs[docId] == ns and docId not in self.__tabsToClose: |
|
183 self.__tabsToClose.append(docId) |
|
184 itm = self.documentsList.takeItem(self.documentsList.row(item)) |
|
185 del itm |
|
186 |
|
187 self.__engine.unregisterDocumentation(ns) |
|
188 |
|
189 if self.documentsList.count(): |
|
190 self.documentsList.setCurrentRow( |
|
191 0, QItemSelectionModel.ClearAndSelect) |
|
192 |
|
193 def hasChanges(self): |
|
194 """ |
|
195 Public slot to test the dialog for changes. |
|
196 |
|
197 @return flag indicating presence of changes |
|
198 """ |
|
199 return len(self.__registeredDocs) > 0 or \ |
|
200 len(self.__unregisteredDocs) > 0 |
|
201 |
|
202 def getTabsToClose(self): |
|
203 """ |
|
204 Public method to get the list of tabs to close. |
|
205 |
|
206 @return list of tab ids to be closed (list of integers) |
|
207 """ |
|
208 return self.__tabsToClose |