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/configinterface.h> 14 #include <ktexteditor/document.h> 15 #include <ktexteditor/range.h> 16 #include <ktexteditor/view.h> 17 18 class AbstractExporter 19 { 20 public: 21 /// If \p m_encapsulate is set, you should add some kind of header in the ctor 22 /// to \p m_output. 23 AbstractExporter(KTextEditor::View *view, QTextStream &output, const bool encapsulate = false) m_view(view)24 : m_view(view) 25 , m_output(output) 26 , m_encapsulate(encapsulate) 27 , m_defaultAttribute(nullptr) 28 { 29 QColor defaultBackground; 30 if (KTextEditor::ConfigInterface *ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_view)) { 31 QVariant variant = ciface->configValue(QStringLiteral("background-color")); 32 if (variant.canConvert<QColor>()) { 33 defaultBackground = variant.value<QColor>(); 34 } 35 } 36 37 m_defaultAttribute = view->defaultStyleAttribute(KTextEditor::dsNormal); 38 m_defaultAttribute->setBackground(QBrush(defaultBackground)); 39 } 40 41 /// Gets called after everything got exported. 42 /// Hence, if \p m_encapsulate is set, you should probably add some kind of footer here. ~AbstractExporter()43 virtual ~AbstractExporter() 44 { 45 } 46 47 /// Begin a new line. 48 virtual void openLine() = 0; 49 50 /// Finish the current line. 51 virtual void closeLine(const bool lastLine) = 0; 52 53 /// Export \p text with given text attribute \p attrib. 54 virtual void exportText(const QString &text, const KTextEditor::Attribute::Ptr &attrib) = 0; 55 56 protected: 57 KTextEditor::View *m_view; 58 QTextStream &m_output; 59 bool m_encapsulate; 60 KTextEditor::Attribute::Ptr m_defaultAttribute; 61 }; 62 63 #endif 64