| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef ABSTRACTEXPORTER_H |
| 8 | #define ABSTRACTEXPORTER_H |
| 9 | |
| 10 | #include <QTextStream> |
| 11 | |
| 12 | #include <ktexteditor/attribute.h> |
| 13 | #include <ktexteditor/document.h> |
| 14 | #include <ktexteditor/range.h> |
| 15 | #include <ktexteditor/view.h> |
| 16 | |
| 17 | class AbstractExporter |
| 18 | { |
| 19 | public: |
| 20 | /// If \p m_encapsulate is set, you should add some kind of header in the ctor |
| 21 | /// to \p m_output. |
| 22 | AbstractExporter(KTextEditor::View *view, QTextStream &output, const bool encapsulate = false) |
| 23 | : m_view(view) |
| 24 | , m_output(output) |
| 25 | , m_encapsulate(encapsulate) |
| 26 | , m_defaultAttribute(nullptr) |
| 27 | { |
| 28 | QColor defaultBackground; |
| 29 | QVariant variant = m_view->configValue(QStringLiteral("background-color" )); |
| 30 | if (variant.canConvert<QColor>()) { |
| 31 | defaultBackground = variant.value<QColor>(); |
| 32 | } |
| 33 | |
| 34 | m_defaultAttribute = view->defaultStyleAttribute(defaultStyle: KSyntaxHighlighting::Theme::TextStyle::Normal); |
| 35 | m_defaultAttribute->setBackground(QBrush(defaultBackground)); |
| 36 | } |
| 37 | |
| 38 | /// Gets called after everything got exported. |
| 39 | /// Hence, if \p m_encapsulate is set, you should probably add some kind of footer here. |
| 40 | virtual ~AbstractExporter() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /// Begin a new line. |
| 45 | virtual void openLine() = 0; |
| 46 | |
| 47 | /// Finish the current line. |
| 48 | virtual void closeLine(const bool lastLine) = 0; |
| 49 | |
| 50 | /// Export \p text with given text attribute \p attrib. |
| 51 | virtual void exportText(const QString &text, const KTextEditor::Attribute::Ptr &attrib) = 0; |
| 52 | |
| 53 | protected: |
| 54 | KTextEditor::View *m_view; |
| 55 | QTextStream &m_output; |
| 56 | bool m_encapsulate; |
| 57 | KTextEditor::Attribute::Ptr m_defaultAttribute; |
| 58 | }; |
| 59 | |
| 60 | #endif |
| 61 | |