|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a widget to manage the QtHelp documentation settings. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSignal, pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QListWidgetItem, QWidget |
|
12 |
|
13 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
|
14 from eric7.EricWidgets.EricApplication import ericApp |
|
15 |
|
16 from .QtHelpDocumentationSettings import QtHelpDocumentationSettings |
|
17 from .Ui_QtHelpDocumentationSettingsWidget import Ui_QtHelpDocumentationSettingsWidget |
|
18 |
|
19 |
|
20 class QtHelpDocumentationSettingsWidget(QWidget, Ui_QtHelpDocumentationSettingsWidget): |
|
21 """ |
|
22 Class implementing a widget to manage the QtHelp documentation settings. |
|
23 |
|
24 @signal documentationSettingsChanged(settings) emitted to signal a change |
|
25 of the documentation configuration |
|
26 """ |
|
27 |
|
28 documentationSettingsChanged = pyqtSignal(QtHelpDocumentationSettings) |
|
29 |
|
30 def __init__(self, parent=None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param parent reference to the parent widget (defaults to None) |
|
35 @type QWidget (optional) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.setupUi(self) |
|
39 |
|
40 self.__settings = None |
|
41 |
|
42 try: |
|
43 self.__pluginHelpDocuments = ( |
|
44 ericApp().getObject("PluginManager").getPluginQtHelpFiles() |
|
45 ) |
|
46 except KeyError: |
|
47 from eric7.PluginManager.PluginManager import ( # __IGNORE_WARNING_I101__ |
|
48 PluginManager, |
|
49 ) |
|
50 |
|
51 pluginManager = PluginManager(self, doLoadPlugins=False) |
|
52 pluginManager.loadDocumentationSetPlugins() |
|
53 pluginManager.activatePlugins() |
|
54 self.__pluginHelpDocuments = pluginManager.getPluginQtHelpFiles() |
|
55 self.addPluginButton.setEnabled(bool(self.__pluginHelpDocuments)) |
|
56 |
|
57 @pyqtSlot() |
|
58 def on_removeDocumentsButton_clicked(self): |
|
59 """ |
|
60 Private slot to remove a document from the help database. |
|
61 """ |
|
62 selectedItems = self.documentsList.selectedItems()[:] |
|
63 if not selectedItems: |
|
64 return |
|
65 |
|
66 for itm in selectedItems: |
|
67 namespace = itm.text() |
|
68 self.documentsList.takeItem(self.documentsList.row(itm)) |
|
69 del itm |
|
70 |
|
71 self.__settings.removeDocumentation(namespace) |
|
72 |
|
73 self.documentationSettingsChanged.emit(self.__settings) |
|
74 |
|
75 @pyqtSlot() |
|
76 def on_addDocumentsButton_clicked(self): |
|
77 """ |
|
78 Private slot to add QtHelp documents to the help database. |
|
79 """ |
|
80 filenames = EricFileDialog.getOpenFileNames( |
|
81 self, |
|
82 self.tr("Add Documentation"), |
|
83 "", |
|
84 self.tr("Qt Compressed Help Files (*.qch)"), |
|
85 ) |
|
86 if not filenames: |
|
87 return |
|
88 |
|
89 self.__registerDocumentation(filenames) |
|
90 |
|
91 @pyqtSlot() |
|
92 def on_addPluginButton_clicked(self): |
|
93 """ |
|
94 Private slot to add QtHelp documents provided by plug-ins to |
|
95 the help database. |
|
96 """ |
|
97 from .QtHelpDocumentationSelectionDialog import ( |
|
98 QtHelpDocumentationSelectionDialog, |
|
99 ) |
|
100 |
|
101 dlg = QtHelpDocumentationSelectionDialog( |
|
102 self.__pluginHelpDocuments, QtHelpDocumentationSelectionDialog.AddMode, self |
|
103 ) |
|
104 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
105 documents = dlg.getData() |
|
106 if documents: |
|
107 self.__registerDocumentation(documents) |
|
108 |
|
109 def __registerDocumentation(self, filenames): |
|
110 """ |
|
111 Private method to register a given list of documentations. |
|
112 |
|
113 @param filenames list of documentation files to be registered |
|
114 @type list of str |
|
115 """ |
|
116 added = False |
|
117 |
|
118 for filename in filenames: |
|
119 if not self.__settings.addDocumentation(filename): |
|
120 EricMessageBox.warning( |
|
121 self, |
|
122 self.tr("Add Documentation"), |
|
123 self.tr("""The file <b>{0}</b> could not be added.""").format( |
|
124 filename |
|
125 ), |
|
126 ) |
|
127 continue |
|
128 |
|
129 if not added: |
|
130 added = True |
|
131 self.documentsList.clearSelection() |
|
132 |
|
133 namespace = self.__settings.namespace(filename) |
|
134 itm = QListWidgetItem(namespace) |
|
135 self.documentsList.addItem(itm) |
|
136 |
|
137 itm.setSelected(True) |
|
138 self.__applyDocumentsListFilter() |
|
139 |
|
140 if added: |
|
141 self.documentationSettingsChanged.emit(self.__settings) |
|
142 |
|
143 @pyqtSlot() |
|
144 def on_managePluginButton_clicked(self): |
|
145 """ |
|
146 Private slot to manage the QtHelp documents provided by plug-ins. |
|
147 """ |
|
148 from .QtHelpDocumentationSelectionDialog import ( |
|
149 QtHelpDocumentationSelectionDialog, |
|
150 ) |
|
151 |
|
152 dlg = QtHelpDocumentationSelectionDialog( |
|
153 self.__pluginHelpDocuments, |
|
154 QtHelpDocumentationSelectionDialog.ManageMode, |
|
155 self, |
|
156 ) |
|
157 dlg.exec() |
|
158 |
|
159 @pyqtSlot() |
|
160 def on_documentsList_itemSelectionChanged(self): |
|
161 """ |
|
162 Private slot handling a change of the documents selection. |
|
163 """ |
|
164 self.removeDocumentsButton.setEnabled( |
|
165 len(self.documentsList.selectedItems()) != 0 |
|
166 ) |
|
167 |
|
168 @pyqtSlot(str) |
|
169 def on_filterEdit_textChanged(self, txt): |
|
170 """ |
|
171 Private slot to react on changes of the document filter text. |
|
172 |
|
173 @param txt current entry of the filter |
|
174 @type str |
|
175 """ |
|
176 self.__applyDocumentsListFilter() |
|
177 |
|
178 @pyqtSlot() |
|
179 def __applyDocumentsListFilter(self): |
|
180 """ |
|
181 Private slot to apply the current documents filter. |
|
182 """ |
|
183 filterStr = self.filterEdit.text() |
|
184 for row in range(self.documentsList.count()): |
|
185 itm = self.documentsList.item(row) |
|
186 matches = filterStr == "" or filterStr in itm.text() |
|
187 |
|
188 if not matches: |
|
189 itm.setSelected(False) |
|
190 itm.setHidden(not matches) |
|
191 |
|
192 def setDocumentationSettings(self, settings): |
|
193 """ |
|
194 Public method to set the reference to the QtHelp documentation |
|
195 configuration object. |
|
196 |
|
197 @param settings reference to the created QtHelpDocumentationSettings |
|
198 object |
|
199 @type QtHelpDocumentationSettings |
|
200 """ |
|
201 self.__settings = settings |
|
202 |
|
203 self.documentsList.clear() |
|
204 |
|
205 for namespace in self.__settings.namespaces(): |
|
206 itm = QListWidgetItem(namespace) |
|
207 self.documentsList.addItem(itm) |
|
208 self.__applyDocumentsListFilter() |
|
209 |
|
210 self.removeDocumentsButton.setEnabled(False) |
|
211 |
|
212 def documentationSettings(self): |
|
213 """ |
|
214 Public method to get the reference to the QtHelp documentation |
|
215 configuration object. |
|
216 |
|
217 @return reference to the created QtHelpDocumentationSettings object |
|
218 @rtype QtHelpDocumentationSettings |
|
219 """ |
|
220 return self.__settings |