eric7/WebBrowser/QtHelp/QtHelpDocumentationSettingsWidget.py

branch
eric7
changeset 8422
bb5da74c1b3f
child 8565
207b47c2eed9
equal deleted inserted replaced
8421:cd4eee7f1d28 8422:bb5da74c1b3f
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 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 pyqtSlot, pyqtSignal
11 from PyQt6.QtWidgets import QWidget, QListWidgetItem, QDialog
12
13 from EricWidgets.EricApplication import ericApp
14 from EricWidgets import EricMessageBox, EricFileDialog
15
16 from .Ui_QtHelpDocumentationSettingsWidget import (
17 Ui_QtHelpDocumentationSettingsWidget
18 )
19
20 from .QtHelpDocumentationSettings import QtHelpDocumentationSettings
21
22
23 class QtHelpDocumentationSettingsWidget(QWidget,
24 Ui_QtHelpDocumentationSettingsWidget):
25 """
26 Class implementing a widget to manage the QtHelp documentation settings.
27
28 @signal documentationSettingsChanged(settings) emitted to signal a change
29 of the documentation configuration
30 """
31 documentationSettingsChanged = pyqtSignal(QtHelpDocumentationSettings)
32
33 def __init__(self, parent=None):
34 """
35 Constructor
36
37 @param parent reference to the parent widget (defaults to None)
38 @type QWidget (optional)
39 """
40 super().__init__(parent)
41 self.setupUi(self)
42
43 self.__settings = None
44
45 try:
46 self.__pluginHelpDocuments = (
47 ericApp().getObject("PluginManager").getPluginQtHelpFiles()
48 )
49 except KeyError:
50 from PluginManager.PluginManager import PluginManager
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 if not filenames:
86 return
87
88 self.__registerDocumentation(filenames)
89
90 @pyqtSlot()
91 def on_addPluginButton_clicked(self):
92 """
93 Private slot to add QtHelp documents provided by plug-ins to
94 the help database.
95 """
96 from .QtHelpDocumentationSelectionDialog import (
97 QtHelpDocumentationSelectionDialog
98 )
99 dlg = QtHelpDocumentationSelectionDialog(
100 self.__pluginHelpDocuments,
101 QtHelpDocumentationSelectionDialog.AddMode,
102 self)
103 if dlg.exec() == QDialog.DialogCode.Accepted:
104 documents = dlg.getData()
105 if not documents:
106 return
107
108 self.__registerDocumentation(documents)
109
110 def __registerDocumentation(self, filenames):
111 """
112 Private method to register a given list of documentations.
113
114 @param filenames list of documentation files to be registered
115 @type list of str
116 """
117 added = False
118
119 for filename in filenames:
120 if not self.__settings.addDocumentation(filename):
121 EricMessageBox.warning(
122 self,
123 self.tr("Add Documentation"),
124 self.tr("""The file <b>{0}</b> could not be added.""")
125 .format(filename)
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 dlg = QtHelpDocumentationSelectionDialog(
152 self.__pluginHelpDocuments,
153 QtHelpDocumentationSelectionDialog.ManageMode,
154 self)
155 dlg.exec()
156
157 @pyqtSlot()
158 def on_documentsList_itemSelectionChanged(self):
159 """
160 Private slot handling a change of the documents selection.
161 """
162 self.removeDocumentsButton.setEnabled(
163 len(self.documentsList.selectedItems()) != 0)
164
165 @pyqtSlot(str)
166 def on_filterEdit_textChanged(self, txt):
167 """
168 Private slot to react on changes of the document filter text.
169
170 @param txt current entry of the filter
171 @type str
172 """
173 self.__applyDocumentsListFilter()
174
175 @pyqtSlot()
176 def __applyDocumentsListFilter(self):
177 """
178 Private slot to apply the current documents filter.
179 """
180 filterStr = self.filterEdit.text()
181 for row in range(self.documentsList.count()):
182 itm = self.documentsList.item(row)
183 matches = filterStr == "" or filterStr in itm.text()
184
185 if not matches:
186 itm.setSelected(False)
187 itm.setHidden(not matches)
188
189 def setDocumentationSettings(self, settings):
190 """
191 Public method to set the reference to the QtHelp documentation
192 configuration object.
193
194 @param settings reference to the created QtHelpDocumentationSettings
195 object
196 @type QtHelpDocumentationSettings
197 """
198 self.__settings = settings
199
200 self.documentsList.clear()
201
202 for namespace in self.__settings.namespaces():
203 itm = QListWidgetItem(namespace)
204 self.documentsList.addItem(itm)
205 self.__applyDocumentsListFilter()
206
207 self.removeDocumentsButton.setEnabled(False)
208
209 def documentationSettings(self):
210 """
211 Public method to get the reference to the QtHelp documentation
212 configuration object.
213
214 @return reference to the created QtHelpDocumentationSettings object
215 @rtype QtHelpDocumentationSettings
216 """
217 return self.__settings

eric ide

mercurial