117 filename = self._getFileName(self.tr("RTF Files (*.rtf)")) |
117 filename = self._getFileName(self.tr("RTF Files (*.rtf)")) |
118 if not filename: |
118 if not filename: |
119 return |
119 return |
120 |
120 |
121 self.editor.recolor(0, -1) |
121 self.editor.recolor(0, -1) |
122 lex = self.editor.getLexer() |
122 tabs = Preferences.getEditorExporter("RTF/UseTabs") |
123 |
|
124 tabSize = self.editor.getEditorConfig("TabWidth") |
123 tabSize = self.editor.getEditorConfig("TabWidth") |
125 if tabSize == 0: |
124 if tabSize == 0: |
126 tabSize = 4 |
125 tabSize = 4 |
127 wysiwyg = Preferences.getEditorExporter("RTF/WYSIWYG") |
|
128 if wysiwyg: |
|
129 if lex: |
|
130 defaultFont = lex.font(QsciScintilla.STYLE_DEFAULT) |
|
131 else: |
|
132 defaultFont = Preferences.getEditorOtherFonts( |
|
133 "DefaultFont") |
|
134 else: |
|
135 defaultFont = Preferences.getEditorExporter("RTF/Font") |
|
136 fontface = defaultFont.family() |
|
137 fontsize = QFontInfo(defaultFont).pointSize() << 1 |
|
138 if fontsize == 0: |
|
139 fontsize = 10 << 1 |
|
140 characterset = QsciScintilla.SC_CHARSET_DEFAULT |
|
141 tabs = Preferences.getEditorExporter("RTF/UseTabs") |
|
142 |
|
143 if lex: |
|
144 fgColour = lex.color(QsciScintilla.STYLE_DEFAULT) |
|
145 bgColour = lex.paper(QsciScintilla.STYLE_DEFAULT) |
|
146 else: |
|
147 fgColour = self.editor.color() |
|
148 bgColour = self.editor.paper() |
|
149 |
126 |
150 try: |
127 try: |
151 with E5OverrideCursor(): |
128 with E5OverrideCursor(): |
152 with open(filename, "w", encoding="utf-8") as f: |
129 with open(filename, "w", encoding="utf-8") as f: |
|
130 styles, fontsize = self.__prepareStyles(f) |
153 |
131 |
154 styles = {} |
|
155 fonts = {} |
|
156 colors = {} |
|
157 lastStyle = "" |
|
158 |
|
159 f.write(self.RTF_HEADEROPEN + self.RTF_FONTDEFOPEN) |
|
160 fonts[0] = fontface |
|
161 fontCount = 1 |
|
162 f.write(self.RTF_FONTDEF.format(0, characterset, fontface)) |
|
163 colors[0] = fgColour |
|
164 colors[1] = bgColour |
|
165 colorCount = 2 |
|
166 |
|
167 if lex: |
|
168 istyle = 0 |
|
169 while istyle <= QsciScintilla.STYLE_MAX: |
|
170 if ( |
|
171 istyle < QsciScintilla.STYLE_DEFAULT or |
|
172 istyle > QsciScintilla.STYLE_LASTPREDEFINED |
|
173 ): |
|
174 if lex.description(istyle): |
|
175 font = lex.font(istyle) |
|
176 if wysiwyg: |
|
177 fontKey = None |
|
178 for key, value in list(fonts.items()): |
|
179 if ( |
|
180 value.lower() == |
|
181 font.family().lower() |
|
182 ): |
|
183 fontKey = key |
|
184 break |
|
185 if fontKey is None: |
|
186 fonts[fontCount] = font.family() |
|
187 f.write(self.RTF_FONTDEF.format( |
|
188 fontCount, characterset, |
|
189 font.family())) |
|
190 fontKey = fontCount |
|
191 fontCount += 1 |
|
192 lastStyle = ( |
|
193 self.RTF_SETFONTFACE + |
|
194 "{0:d}".format(fontKey) |
|
195 ) |
|
196 else: |
|
197 lastStyle = self.RTF_SETFONTFACE + "0" |
|
198 |
|
199 if wysiwyg and QFontInfo(font).pointSize(): |
|
200 lastStyle += ( |
|
201 self.RTF_SETFONTSIZE + |
|
202 "{0:d}".format( |
|
203 QFontInfo(font).pointSize() << |
|
204 1) |
|
205 ) |
|
206 else: |
|
207 lastStyle += ( |
|
208 self.RTF_SETFONTSIZE + |
|
209 "{0:d}".format(fontsize) |
|
210 ) |
|
211 |
|
212 sColour = lex.color(istyle) |
|
213 sColourKey = None |
|
214 for key, value in list(colors.items()): |
|
215 if value == sColour: |
|
216 sColourKey = key |
|
217 break |
|
218 if sColourKey is None: |
|
219 colors[colorCount] = sColour |
|
220 sColourKey = colorCount |
|
221 colorCount += 1 |
|
222 lastStyle += ( |
|
223 self.RTF_SETCOLOR + |
|
224 "{0:d}".format(sColourKey) |
|
225 ) |
|
226 |
|
227 sColour = lex.paper(istyle) |
|
228 sColourKey = None |
|
229 for key, value in list(colors.items()): |
|
230 if value == sColour: |
|
231 sColourKey = key |
|
232 break |
|
233 if sColourKey is None: |
|
234 colors[colorCount] = sColour |
|
235 sColourKey = colorCount |
|
236 colorCount += 1 |
|
237 lastStyle += ( |
|
238 self.RTF_SETBACKGROUND + |
|
239 "{0:d}".format(sColourKey) |
|
240 ) |
|
241 |
|
242 if font.bold(): |
|
243 lastStyle += self.RTF_BOLD_ON |
|
244 else: |
|
245 lastStyle += self.RTF_BOLD_OFF |
|
246 if font.italic(): |
|
247 lastStyle += self.RTF_ITALIC_ON |
|
248 else: |
|
249 lastStyle += self.RTF_ITALIC_OFF |
|
250 styles[istyle] = lastStyle |
|
251 else: |
|
252 styles[istyle] = ( |
|
253 self.RTF_SETFONTFACE + "0" + |
|
254 self.RTF_SETFONTSIZE + |
|
255 "{0:d}".format(fontsize) + |
|
256 self.RTF_SETCOLOR + "0" + |
|
257 self.RTF_SETBACKGROUND + "1" + |
|
258 self.RTF_BOLD_OFF + |
|
259 self.RTF_ITALIC_OFF |
|
260 ) |
|
261 |
|
262 istyle += 1 |
|
263 else: |
|
264 styles[0] = ( |
|
265 self.RTF_SETFONTFACE + "0" + |
|
266 self.RTF_SETFONTSIZE + |
|
267 "{0:d}".format(fontsize) + |
|
268 self.RTF_SETCOLOR + "0" + |
|
269 self.RTF_SETBACKGROUND + "1" + |
|
270 self.RTF_BOLD_OFF + |
|
271 self.RTF_ITALIC_OFF |
|
272 ) |
|
273 |
|
274 f.write(self.RTF_FONTDEFCLOSE + self.RTF_COLORDEFOPEN) |
|
275 for value in list(colors.values()): |
|
276 f.write(self.RTF_COLORDEF.format( |
|
277 value.red(), value.green(), value.blue())) |
|
278 f.write(self.RTF_COLORDEFCLOSE) |
|
279 f.write(self.RTF_INFOOPEN + self.RTF_COMMENT) |
|
280 f.write(time.strftime(self.RTF_CREATED)) |
|
281 f.write(self.RTF_INFOCLOSE) |
|
282 f.write(self.RTF_HEADERCLOSE + |
|
283 self.RTF_BODYOPEN + self.RTF_SETFONTFACE + "0" + |
|
284 self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) + |
|
285 self.RTF_SETCOLOR + "0 ") |
|
286 lastStyle = ( |
132 lastStyle = ( |
287 self.RTF_SETFONTFACE + "0" + |
133 self.RTF_SETFONTFACE + "0" + |
288 self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) + |
134 self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) + |
289 self.RTF_SETCOLOR + "0" + |
135 self.RTF_SETCOLOR + "0" + |
290 self.RTF_SETBACKGROUND + "1" + |
136 self.RTF_SETBACKGROUND + "1" + |
371 self.tr("Export source"), |
217 self.tr("Export source"), |
372 self.tr( |
218 self.tr( |
373 """<p>The source could not be exported to""" |
219 """<p>The source could not be exported to""" |
374 """ <b>{0}</b>.</p><p>Reason: {1}</p>""") |
220 """ <b>{0}</b>.</p><p>Reason: {1}</p>""") |
375 .format(filename, str(err))) |
221 .format(filename, str(err))) |
|
222 |
|
223 def __prepareStyles(self, f): |
|
224 """ |
|
225 Private method to generate and store the different styles. |
|
226 |
|
227 @param f filepointer to the open RTF |
|
228 @type object |
|
229 @return styles, fontsize |
|
230 @rtype dict, int |
|
231 """ |
|
232 styles = {} |
|
233 fonts = {} |
|
234 colors = {} |
|
235 lastStyle = "" |
|
236 |
|
237 lex = self.editor.getLexer() |
|
238 |
|
239 wysiwyg = Preferences.getEditorExporter("RTF/WYSIWYG") |
|
240 if wysiwyg: |
|
241 if lex: |
|
242 defaultFont = lex.font(QsciScintilla.STYLE_DEFAULT) |
|
243 else: |
|
244 defaultFont = Preferences.getEditorOtherFonts("DefaultFont") |
|
245 else: |
|
246 defaultFont = Preferences.getEditorExporter("RTF/Font") |
|
247 fontface = defaultFont.family() |
|
248 fontsize = QFontInfo(defaultFont).pointSize() << 1 |
|
249 if fontsize == 0: |
|
250 fontsize = 10 << 1 |
|
251 characterset = QsciScintilla.SC_CHARSET_DEFAULT |
|
252 |
|
253 if lex: |
|
254 fgColour = lex.color(QsciScintilla.STYLE_DEFAULT) |
|
255 bgColour = lex.paper(QsciScintilla.STYLE_DEFAULT) |
|
256 else: |
|
257 fgColour = self.editor.color() |
|
258 bgColour = self.editor.paper() |
|
259 |
|
260 f.write(self.RTF_HEADEROPEN + self.RTF_FONTDEFOPEN) |
|
261 fonts[0] = fontface |
|
262 fontCount = 1 |
|
263 f.write(self.RTF_FONTDEF.format(0, characterset, fontface)) |
|
264 colors[0] = fgColour |
|
265 colors[1] = bgColour |
|
266 colorCount = 2 |
|
267 |
|
268 if lex: |
|
269 istyle = 0 |
|
270 while istyle <= QsciScintilla.STYLE_MAX: |
|
271 if ( |
|
272 istyle < QsciScintilla.STYLE_DEFAULT or |
|
273 istyle > QsciScintilla.STYLE_LASTPREDEFINED |
|
274 ): |
|
275 if lex.description(istyle): |
|
276 font = lex.font(istyle) |
|
277 lastStyle = self.RTF_SETFONTFACE |
|
278 if wysiwyg: |
|
279 fontKey = None |
|
280 for key, value in fonts.items(): |
|
281 if value.lower() == font.family().lower(): |
|
282 fontKey = key |
|
283 break |
|
284 else: |
|
285 fonts[fontCount] = font.family() |
|
286 f.write(self.RTF_FONTDEF.format( |
|
287 fontCount, characterset, |
|
288 font.family())) |
|
289 fontKey = fontCount |
|
290 fontCount += 1 |
|
291 |
|
292 lastStyle += "{0:d}".format(fontKey) |
|
293 else: |
|
294 lastStyle += "0" |
|
295 |
|
296 lastStyle += self.RTF_SETFONTSIZE |
|
297 if wysiwyg and QFontInfo(font).pointSize(): |
|
298 lastStyle += ( |
|
299 "{0:d}".format( |
|
300 QFontInfo(font).pointSize() << 1) |
|
301 ) |
|
302 else: |
|
303 lastStyle += "{0:d}".format(fontsize) |
|
304 |
|
305 sColour = lex.color(istyle) |
|
306 sColourKey = None |
|
307 for key, value in colors.items(): |
|
308 if value == sColour: |
|
309 sColourKey = key |
|
310 break |
|
311 else: |
|
312 colors[colorCount] = sColour |
|
313 sColourKey = colorCount |
|
314 colorCount += 1 |
|
315 lastStyle += ( |
|
316 self.RTF_SETCOLOR + |
|
317 "{0:d}".format(sColourKey) |
|
318 ) |
|
319 |
|
320 sColour = lex.paper(istyle) |
|
321 sColourKey = None |
|
322 for key, value in colors.items(): |
|
323 if value == sColour: |
|
324 sColourKey = key |
|
325 break |
|
326 else: |
|
327 colors[colorCount] = sColour |
|
328 sColourKey = colorCount |
|
329 colorCount += 1 |
|
330 |
|
331 lastStyle += ( |
|
332 self.RTF_SETBACKGROUND + |
|
333 "{0:d}".format(sColourKey) |
|
334 ) |
|
335 |
|
336 if font.bold(): |
|
337 lastStyle += self.RTF_BOLD_ON |
|
338 else: |
|
339 lastStyle += self.RTF_BOLD_OFF |
|
340 if font.italic(): |
|
341 lastStyle += self.RTF_ITALIC_ON |
|
342 else: |
|
343 lastStyle += self.RTF_ITALIC_OFF |
|
344 styles[istyle] = lastStyle |
|
345 |
|
346 # get substyles |
|
347 subs_start, subs_count = self.editor.getSubStyleRange( |
|
348 istyle) |
|
349 for subs_idx in range(subs_count): |
|
350 font = lex.font(subs_start + subs_idx) |
|
351 lastStyle = self.RTF_SETFONTFACE |
|
352 if wysiwyg: |
|
353 fontKey = None |
|
354 for key, value in fonts.items(): |
|
355 if value.lower() == font.family().lower(): |
|
356 fontKey = key |
|
357 break |
|
358 else: |
|
359 fonts[fontCount] = font.family() |
|
360 f.write(self.RTF_FONTDEF.format( |
|
361 fontCount, characterset, |
|
362 font.family())) |
|
363 fontKey = fontCount |
|
364 fontCount += 1 |
|
365 |
|
366 lastStyle += "{0:d}".format(fontKey) |
|
367 else: |
|
368 lastStyle += "0" |
|
369 |
|
370 lastStyle += self.RTF_SETFONTSIZE |
|
371 if wysiwyg and QFontInfo(font).pointSize(): |
|
372 lastStyle += ( |
|
373 "{0:d}".format( |
|
374 QFontInfo(font).pointSize() << 1) |
|
375 ) |
|
376 else: |
|
377 lastStyle += "{0:d}".format(fontsize) |
|
378 |
|
379 sColour = lex.color(subs_start + subs_idx) |
|
380 sColourKey = None |
|
381 for key, value in colors.items(): |
|
382 if value == sColour: |
|
383 sColourKey = key |
|
384 break |
|
385 else: |
|
386 colors[colorCount] = sColour |
|
387 sColourKey = colorCount |
|
388 colorCount += 1 |
|
389 lastStyle += ( |
|
390 self.RTF_SETCOLOR + |
|
391 "{0:d}".format(sColourKey) |
|
392 ) |
|
393 |
|
394 sColour = lex.paper(subs_start + subs_idx) |
|
395 sColourKey = None |
|
396 for key, value in colors.items(): |
|
397 if value == sColour: |
|
398 sColourKey = key |
|
399 break |
|
400 else: |
|
401 colors[colorCount] = sColour |
|
402 sColourKey = colorCount |
|
403 colorCount += 1 |
|
404 |
|
405 lastStyle += ( |
|
406 self.RTF_SETBACKGROUND + |
|
407 "{0:d}".format(sColourKey) |
|
408 ) |
|
409 |
|
410 if font.bold(): |
|
411 lastStyle += self.RTF_BOLD_ON |
|
412 else: |
|
413 lastStyle += self.RTF_BOLD_OFF |
|
414 if font.italic(): |
|
415 lastStyle += self.RTF_ITALIC_ON |
|
416 else: |
|
417 lastStyle += self.RTF_ITALIC_OFF |
|
418 styles[subs_idx - subs_start] = lastStyle |
|
419 |
|
420 else: |
|
421 styles[istyle] = ( |
|
422 self.RTF_SETFONTFACE + "0" + |
|
423 self.RTF_SETFONTSIZE + |
|
424 "{0:d}".format(fontsize) + |
|
425 self.RTF_SETCOLOR + "0" + |
|
426 self.RTF_SETBACKGROUND + "1" + |
|
427 self.RTF_BOLD_OFF + |
|
428 self.RTF_ITALIC_OFF |
|
429 ) |
|
430 |
|
431 istyle += 1 |
|
432 else: |
|
433 styles[0] = ( |
|
434 self.RTF_SETFONTFACE + "0" + |
|
435 self.RTF_SETFONTSIZE + |
|
436 "{0:d}".format(fontsize) + |
|
437 self.RTF_SETCOLOR + "0" + |
|
438 self.RTF_SETBACKGROUND + "1" + |
|
439 self.RTF_BOLD_OFF + |
|
440 self.RTF_ITALIC_OFF |
|
441 ) |
|
442 |
|
443 f.write(self.RTF_FONTDEFCLOSE + self.RTF_COLORDEFOPEN) |
|
444 for value in colors.values(): |
|
445 f.write(self.RTF_COLORDEF.format( |
|
446 value.red(), value.green(), value.blue())) |
|
447 f.write(self.RTF_COLORDEFCLOSE) |
|
448 f.write(self.RTF_INFOOPEN + self.RTF_COMMENT) |
|
449 f.write(time.strftime(self.RTF_CREATED)) |
|
450 f.write(self.RTF_INFOCLOSE) |
|
451 f.write(self.RTF_HEADERCLOSE + |
|
452 self.RTF_BODYOPEN + self.RTF_SETFONTFACE + "0" + |
|
453 self.RTF_SETFONTSIZE + "{0:d}".format(fontsize) + |
|
454 self.RTF_SETCOLOR + "0 ") |
|
455 |
|
456 return styles, fontsize |