|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 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 |
|
18 from .Ui_QtHelpDocumentationDialog import Ui_QtHelpDocumentationDialog |
|
19 |
|
20 |
|
21 class QtHelpDocumentationDialog(QDialog, Ui_QtHelpDocumentationDialog): |
|
22 """ |
|
23 Class implementing a dialog to manage the QtHelp documentation database. |
|
24 """ |
|
25 def __init__(self, engine, parent): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param engine reference to the help engine (QHelpEngine) |
|
30 @param parent reference to the parent widget (QWidget) |
|
31 """ |
|
32 super(QtHelpDocumentationDialog, self).__init__(parent) |
|
33 self.setupUi(self) |
|
34 |
|
35 self.removeButton.setEnabled(False) |
|
36 |
|
37 self.__engine = engine |
|
38 self.__mw = parent |
|
39 |
|
40 docs = self.__engine.registeredDocumentations() |
|
41 self.documentsList.addItems(docs) |
|
42 |
|
43 self.__registeredDocs = [] |
|
44 self.__unregisteredDocs = [] |
|
45 self.__tabsToClose = [] |
|
46 |
|
47 @pyqtSlot() |
|
48 def on_documentsList_itemSelectionChanged(self): |
|
49 """ |
|
50 Private slot handling a change of the documents selection. |
|
51 """ |
|
52 self.removeButton.setEnabled( |
|
53 len(self.documentsList.selectedItems()) != 0) |
|
54 |
|
55 @pyqtSlot() |
|
56 def on_addButton_clicked(self): |
|
57 """ |
|
58 Private slot to add documents to the help database. |
|
59 """ |
|
60 fileNames = E5FileDialog.getOpenFileNames( |
|
61 self, |
|
62 self.tr("Add Documentation"), |
|
63 "", |
|
64 self.tr("Qt Compressed Help Files (*.qch)")) |
|
65 if not fileNames: |
|
66 return |
|
67 |
|
68 for fileName in fileNames: |
|
69 ns = QHelpEngineCore.namespaceName(fileName) |
|
70 if not ns: |
|
71 E5MessageBox.warning( |
|
72 self, |
|
73 self.tr("Add Documentation"), |
|
74 self.tr( |
|
75 """The file <b>{0}</b> is not a valid""" |
|
76 """ Qt Help File.""").format(fileName) |
|
77 ) |
|
78 continue |
|
79 |
|
80 if len(self.documentsList.findItems(ns, Qt.MatchFixedString)): |
|
81 E5MessageBox.warning( |
|
82 self, |
|
83 self.tr("Add Documentation"), |
|
84 self.tr( |
|
85 """The namespace <b>{0}</b> is already registered.""") |
|
86 .format(ns) |
|
87 ) |
|
88 continue |
|
89 |
|
90 self.__engine.registerDocumentation(fileName) |
|
91 self.documentsList.addItem(ns) |
|
92 self.__registeredDocs.append(ns) |
|
93 if ns in self.__unregisteredDocs: |
|
94 self.__unregisteredDocs.remove(ns) |
|
95 |
|
96 @pyqtSlot() |
|
97 def on_removeButton_clicked(self): |
|
98 """ |
|
99 Private slot to remove a document from the help database. |
|
100 """ |
|
101 res = E5MessageBox.yesNo( |
|
102 self, |
|
103 self.tr("Remove Documentation"), |
|
104 self.tr( |
|
105 """Do you really want to remove the selected documentation """ |
|
106 """sets from the database?""")) |
|
107 if not res: |
|
108 return |
|
109 |
|
110 openedDocs = self.__mw.getSourceFileList() |
|
111 |
|
112 items = self.documentsList.selectedItems() |
|
113 for item in items: |
|
114 ns = item.text() |
|
115 if ns in list(openedDocs.values()): |
|
116 res = E5MessageBox.yesNo( |
|
117 self, |
|
118 self.tr("Remove Documentation"), |
|
119 self.tr( |
|
120 """Some documents currently opened reference the """ |
|
121 """documentation you are attempting to remove. """ |
|
122 """Removing the documentation will close those """ |
|
123 """documents. Remove anyway?"""), |
|
124 icon=E5MessageBox.Warning) |
|
125 if not res: |
|
126 return |
|
127 self.__unregisteredDocs.append(ns) |
|
128 for id in openedDocs: |
|
129 if openedDocs[id] == ns and id not in self.__tabsToClose: |
|
130 self.__tabsToClose.append(id) |
|
131 itm = self.documentsList.takeItem(self.documentsList.row(item)) |
|
132 del itm |
|
133 |
|
134 self.__engine.unregisterDocumentation(ns) |
|
135 |
|
136 if self.documentsList.count(): |
|
137 self.documentsList.setCurrentRow( |
|
138 0, QItemSelectionModel.ClearAndSelect) |
|
139 |
|
140 def hasChanges(self): |
|
141 """ |
|
142 Public slot to test the dialog for changes. |
|
143 |
|
144 @return flag indicating presence of changes |
|
145 """ |
|
146 return len(self.__registeredDocs) > 0 or \ |
|
147 len(self.__unregisteredDocs) > 0 |
|
148 |
|
149 def getTabsToClose(self): |
|
150 """ |
|
151 Public method to get the list of tabs to close. |
|
152 |
|
153 @return list of tab ids to be closed (list of integers) |
|
154 """ |
|
155 return self.__tabsToClose |