|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an exporter for ODT. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtGui import QTextDocument, QTextDocumentWriter |
|
11 |
|
12 from EricWidgets import EricMessageBox |
|
13 from EricGui.EricOverrideCursor import EricOverrideCursor |
|
14 |
|
15 from .ExporterBase import ExporterBase |
|
16 from .ExporterHTML import HTMLGenerator |
|
17 |
|
18 import Preferences |
|
19 |
|
20 |
|
21 class ExporterODT(ExporterBase): |
|
22 """ |
|
23 Class implementing an exporter for ODT. |
|
24 """ |
|
25 def __init__(self, editor, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param editor reference to the editor object (QScintilla.Editor.Editor) |
|
30 @param parent parent object of the exporter (QObject) |
|
31 """ |
|
32 ExporterBase.__init__(self, editor, parent) |
|
33 |
|
34 def exportSource(self): |
|
35 """ |
|
36 Public method performing the export. |
|
37 """ |
|
38 filename = self._getFileName(self.tr("ODT Files (*.odt)")) |
|
39 if not filename: |
|
40 return |
|
41 |
|
42 tabSize = self.editor.getEditorConfig("TabWidth") |
|
43 if tabSize == 0: |
|
44 tabSize = 4 |
|
45 wysiwyg = Preferences.getEditorExporter("ODT/WYSIWYG") |
|
46 onlyStylesUsed = Preferences.getEditorExporter("ODT/OnlyStylesUsed") |
|
47 tabs = Preferences.getEditorExporter("ODT/UseTabs") |
|
48 |
|
49 with EricOverrideCursor(): |
|
50 # generate HTML of the source |
|
51 generator = HTMLGenerator(self.editor) |
|
52 html = generator.generate( |
|
53 tabSize=tabSize, |
|
54 useTabs=tabs, |
|
55 wysiwyg=wysiwyg, |
|
56 folding=False, |
|
57 onlyStylesUsed=onlyStylesUsed, |
|
58 titleFullPath=False |
|
59 ) |
|
60 |
|
61 # convert HTML to ODT |
|
62 doc = QTextDocument() |
|
63 doc.setHtml(html) |
|
64 writer = QTextDocumentWriter(filename) |
|
65 ok = writer.write(doc) |
|
66 |
|
67 if not ok: |
|
68 EricMessageBox.critical( |
|
69 self.editor, |
|
70 self.tr("Export source"), |
|
71 self.tr( |
|
72 """<p>The source could not be exported to""" |
|
73 """ <b>{0}</b>.</p>""").format(filename)) |