33 @type QWidget |
33 @type QWidget |
34 """ |
34 """ |
35 super(CodeDocumentationViewer, self).__init__(parent) |
35 super(CodeDocumentationViewer, self).__init__(parent) |
36 self.setupUi(self) |
36 self.setupUi(self) |
37 |
37 |
|
38 self.searchWidget.attachTextEdit(self.contents) |
|
39 |
38 self.__ui = parent |
40 self.__ui = parent |
39 |
41 |
40 self.__providers = {} |
42 self.__providers = {} |
41 self.__selectedProvider = "" |
43 self.__selectedProvider = "" |
42 self.__disabledProvider = "disabled" |
44 self.__disabledProvider = "disabled" |
43 |
45 |
44 self.__shuttingDown = False |
46 self.__shuttingDown = False |
45 self.__startingUp = True |
47 self.__startingUp = True |
|
48 |
|
49 self.__lastDocumentation = None |
|
50 |
|
51 self.__showMarkdown = Preferences.getDocuViewer("ShowInfoAsMarkdown") |
46 |
52 |
47 self.__noDocumentationString = self.tr("No documentation available") |
53 self.__noDocumentationString = self.tr("No documentation available") |
48 self.__disabledString = self.tr( |
54 self.__disabledString = self.tr( |
49 "No source code documentation provider has been registered or" |
55 "No source code documentation provider has been registered or" |
50 " this function has been disabled.") |
56 " this function has been disabled.") |
51 |
57 |
52 self.providerComboBox.addItem(self.tr("<disabled>"), "disabled") |
58 self.providerComboBox.addItem(self.tr("<disabled>"), "disabled") |
|
59 |
|
60 font = Preferences.getEditorOtherFonts("MonospacedFont") |
|
61 self.contents.setFontFamily(font.family()) |
|
62 self.contents.setFontPointSize(font.pointSize()) |
53 |
63 |
54 def finalizeSetup(self): |
64 def finalizeSetup(self): |
55 """ |
65 """ |
56 Public method to finalize the setup of the documentation viewer. |
66 Public method to finalize the setup of the documentation viewer. |
57 """ |
67 """ |
58 self.__startingUp = False |
68 self.__startingUp = False |
59 provider = Preferences.Prefs.settings.value( |
69 provider = Preferences.getDocuViewer("Provider") |
60 "CodeDocumentationViewer/Provider", "disabled") |
|
61 if provider in self.__providers: |
70 if provider in self.__providers: |
62 index = self.providerComboBox.findData(provider) |
71 index = self.providerComboBox.findData(provider) |
63 else: |
72 else: |
64 index = 0 |
73 index = 0 |
65 self.providerComboBox.setCurrentIndex(index) |
74 self.providerComboBox.setCurrentIndex(index) |
66 |
75 |
|
76 # TODO: document this hook in the plug-in document |
67 def registerProvider(self, providerName, providerDisplay, provider): |
77 def registerProvider(self, providerName, providerDisplay, provider): |
68 """ |
78 """ |
69 Public method register a source docu provider. |
79 Public method register a source docu provider. |
70 |
80 |
71 @param providerName name of the provider (must be unique) |
81 @param providerName name of the provider (must be unique) |
82 "Provider '{0}' already registered.".format(providerName)) |
92 "Provider '{0}' already registered.".format(providerName)) |
83 |
93 |
84 self.__providers[providerName] = provider |
94 self.__providers[providerName] = provider |
85 self.providerComboBox.addItem(providerDisplay, providerName) |
95 self.providerComboBox.addItem(providerDisplay, providerName) |
86 |
96 |
|
97 # TODO: document this hook in the plug-in document |
87 def unregisterProvider(self, providerName): |
98 def unregisterProvider(self, providerName): |
88 """ |
99 """ |
89 Public method register a source docu provider. |
100 Public method register a source docu provider. |
90 |
101 |
91 @param providerName name of the provider (must be unique) |
102 @param providerName name of the provider (must be unique) |
104 Public method to request code documentation data from a provider. |
115 Public method to request code documentation data from a provider. |
105 |
116 |
106 @param editor reference to the editor to request code docu for |
117 @param editor reference to the editor to request code docu for |
107 @type Editor |
118 @type Editor |
108 """ |
119 """ |
|
120 line, index = editor.getCursorPosition() |
|
121 word = editor.getWord(line, index) |
|
122 if not word: |
|
123 # try again one index before |
|
124 word = editor.getWord(line, index - 1) |
|
125 self.objectLineEdit.setText(word) |
|
126 |
109 if self.__selectedProvider != self.__disabledProvider: |
127 if self.__selectedProvider != self.__disabledProvider: |
110 self.contents.clear() |
128 self.contents.clear() |
111 self.__providers[self.__selectedProvider](editor) |
129 self.__providers[self.__selectedProvider](editor) |
112 |
130 |
|
131 # TODO: document this hook in the plug-in document |
113 def documentationReady(self, documentationInfo): |
132 def documentationReady(self, documentationInfo): |
114 """ |
133 """ |
115 Public method to provide the documentation info to the viewer. |
134 Public method to provide the documentation info to the viewer. |
116 |
135 |
117 If documentationInfo is a dictionary, it should contains these keys |
136 If documentationInfo is a dictionary, it should contains these keys |
126 @param documentationInfo dictionary containing the source docu data |
145 @param documentationInfo dictionary containing the source docu data |
127 @type dict or str |
146 @type dict or str |
128 """ |
147 """ |
129 self.__ui.activateCodeDocumentationViewer(switchFocus=False) |
148 self.__ui.activateCodeDocumentationViewer(switchFocus=False) |
130 |
149 |
|
150 self.__lastDocumentation = documentationInfo |
|
151 |
131 if not documentationInfo: |
152 if not documentationInfo: |
132 fullText = self.__noDocumentationString |
153 fullText = self.__noDocumentationString |
133 elif isinstance(documentationInfo, str): |
154 elif isinstance(documentationInfo, str): |
134 fullText = documentationInfo |
155 fullText = documentationInfo |
135 elif isinstance(documentationInfo, dict): |
156 elif isinstance(documentationInfo, dict): |
|
157 # format the text with markdown syntax |
136 name = documentationInfo["name"] |
158 name = documentationInfo["name"] |
137 if name: |
159 if name: |
138 title = "".join(["=" * len(name), "\n", name, "\n", |
160 title = "".join([name, "\n", |
139 "=" * len(name), "\n\n"]) |
161 "=" * len(name), "\n\n"]) |
140 else: |
162 else: |
141 title = "" |
163 title = "" |
142 |
164 |
143 if documentationInfo["argspec"]: |
165 if documentationInfo["argspec"]: |
144 definition = self.tr("Definition: {0}{1}\n").format( |
166 if self.__showMarkdown: |
145 name, documentationInfo["argspec"]) |
167 definition = self.tr("**Definition**: {0}{1}\n", |
|
168 "string with markdown syntax").format( |
|
169 name, documentationInfo["argspec"]) |
|
170 else: |
|
171 definition = self.tr("Definition: {0}{1}\n", |
|
172 "string as plain text").format( |
|
173 name, documentationInfo["argspec"]) |
146 else: |
174 else: |
147 definition = '' |
175 definition = '' |
148 |
176 |
149 if documentationInfo["note"]: |
177 if documentationInfo["note"]: |
150 note = self.tr("Info: {0}\n\n----\n\n").format( |
178 if self.__showMarkdown: |
151 documentationInfo["note"]) |
179 note = self.tr("**Info**: _{0}_\n\n----\n\n", |
|
180 "string with markdown syntax").format( |
|
181 documentationInfo["note"]) |
|
182 else: |
|
183 note = self.tr("Info: {0}\n\n----\n\n", |
|
184 "string as plain text").format( |
|
185 documentationInfo["note"]) |
152 else: |
186 else: |
153 note = "" |
187 note = "" |
154 |
188 |
155 fullText = "".join([title, definition, note, |
189 fullText = "".join([title, definition, note, |
156 documentationInfo['docstring']]) |
190 documentationInfo['docstring']]) |
168 if not self.__shuttingDown and not self.__startingUp: |
202 if not self.__shuttingDown and not self.__startingUp: |
169 provider = self.providerComboBox.itemData(index) |
203 provider = self.providerComboBox.itemData(index) |
170 if provider == self.__disabledProvider: |
204 if provider == self.__disabledProvider: |
171 self.documentationReady(self.__disabledString) |
205 self.documentationReady(self.__disabledString) |
172 elif provider in self.__providers: |
206 elif provider in self.__providers: |
173 Preferences.Prefs.settings.setValue( |
207 Preferences.setDocuViewer("Provider", provider) |
174 "CodeDocumentationViewer/Provider", provider) |
|
175 self.__selectedProvider = provider |
208 self.__selectedProvider = provider |
176 |
209 |
177 def shutdown(self): |
210 def shutdown(self): |
178 """ |
211 """ |
179 Public method to perform shutdown actions. |
212 Public method to perform shutdown actions. |
180 """ |
213 """ |
181 self.__shuttingDown = True |
214 self.__shuttingDown = True |
182 Preferences.Prefs.settings.setValue( |
215 Preferences.setDocuViewer("Provider", self.__selectedProvider) |
183 "CodeDocumentationViewer/Provider", self.__selectedProvider) |
216 |
|
217 def preferencesChanged(self): |
|
218 """ |
|
219 Public slot to handle a change of preferences. |
|
220 """ |
|
221 showMarkdown = Preferences.getDocuViewer("ShowInfoAsMarkdown") |
|
222 if showMarkdown != self.__showMarkdown: |
|
223 self.__showMarkdown = showMarkdown |
|
224 self.documentationReady(self.__lastDocumentation) |
|
225 |
|
226 provider = Preferences.getDocuViewer("Provider") |
|
227 if provider != self.__selectedProvider: |
|
228 index = self.providerComboBox.findData(provider) |
|
229 if index < 0: |
|
230 index = 0 |
|
231 self.providerComboBox.setCurrentIndex(index) |
|
232 |
|
233 font = Preferences.getEditorOtherFonts("MonospacedFont") |
|
234 self.contents.setFontFamily(font.family()) |
|
235 self.contents.setFontPointSize(font.pointSize()) |