File indexing completed on 2024-04-28 15:30:29
0001 /* 0002 SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef ABSTRACTEXPORTER_H 0008 #define ABSTRACTEXPORTER_H 0009 0010 #include <QTextStream> 0011 0012 #include <ktexteditor/attribute.h> 0013 #include <ktexteditor/configinterface.h> 0014 #include <ktexteditor/document.h> 0015 #include <ktexteditor/range.h> 0016 #include <ktexteditor/view.h> 0017 0018 class AbstractExporter 0019 { 0020 public: 0021 /// If \p m_encapsulate is set, you should add some kind of header in the ctor 0022 /// to \p m_output. 0023 AbstractExporter(KTextEditor::View *view, QTextStream &output, const bool encapsulate = false) 0024 : m_view(view) 0025 , m_output(output) 0026 , m_encapsulate(encapsulate) 0027 , m_defaultAttribute(nullptr) 0028 { 0029 QColor defaultBackground; 0030 if (KTextEditor::ConfigInterface *ciface = qobject_cast<KTextEditor::ConfigInterface *>(m_view)) { 0031 QVariant variant = ciface->configValue(QStringLiteral("background-color")); 0032 if (variant.canConvert<QColor>()) { 0033 defaultBackground = variant.value<QColor>(); 0034 } 0035 } 0036 0037 m_defaultAttribute = view->defaultStyleAttribute(KTextEditor::dsNormal); 0038 m_defaultAttribute->setBackground(QBrush(defaultBackground)); 0039 } 0040 0041 /// Gets called after everything got exported. 0042 /// Hence, if \p m_encapsulate is set, you should probably add some kind of footer here. 0043 virtual ~AbstractExporter() 0044 { 0045 } 0046 0047 /// Begin a new line. 0048 virtual void openLine() = 0; 0049 0050 /// Finish the current line. 0051 virtual void closeLine(const bool lastLine) = 0; 0052 0053 /// Export \p text with given text attribute \p attrib. 0054 virtual void exportText(const QString &text, const KTextEditor::Attribute::Ptr &attrib) = 0; 0055 0056 protected: 0057 KTextEditor::View *m_view; 0058 QTextStream &m_output; 0059 bool m_encapsulate; 0060 KTextEditor::Attribute::Ptr m_defaultAttribute; 0061 }; 0062 0063 #endif