KTextEditor

exporter.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de>
3 SPDX-FileCopyrightText: 2002 John Firebaugh <jfirebaugh@kde.org>
4 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
5 SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org>
6 SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "exporter.h"
12#include "abstractexporter.h"
13#include "htmlexporter.h"
14
15#include <ktexteditor/document.h>
16#include <ktexteditor/view.h>
17
18#include <KLocalizedString>
19
20#include <QApplication>
21#include <QClipboard>
22#include <QFileDialog>
23#include <QMimeData>
24
25void KateExporter::exportToClipboard()
26{
27 if (!m_view->selection()) {
28 return;
29 }
30
31 QMimeData *data = new QMimeData();
32
33 QString s;
34 QTextStream output(&s, QIODevice::WriteOnly);
35 exportData(true, output);
36
37 data->setHtml(s);
38 data->setText(s);
39
41}
42
43void KateExporter::exportToFile(const QString &file)
44{
45 QFile savefile(file);
46 if (!savefile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
47 return;
48 }
49
50 QTextStream outputStream(&savefile);
51 exportData(false, outputStream);
52}
53
54void KateExporter::exportData(const bool useSelection, QTextStream &output)
55{
56 const KTextEditor::Range range = useSelection ? m_view->selectionRange() : m_view->document()->documentRange();
57 const bool blockwise = useSelection ? m_view->blockSelection() : false;
58
59 if ((blockwise || range.onSingleLine()) && (range.start().column() > range.end().column())) {
60 return;
61 }
62
63 /// TODO: add more exporters
64 std::unique_ptr<AbstractExporter> exporter = std::make_unique<HTMLExporter>(m_view, output, !useSelection);
65
66 const KTextEditor::Attribute::Ptr noAttrib(nullptr);
67
68 for (int i = range.start().line(); (i <= range.end().line()) && (i < m_view->document()->lines()); ++i) {
69 const QString &line = m_view->document()->line(i);
70
71 const QList<KTextEditor::AttributeBlock> attribs = m_view->lineAttributes(i);
72
73 int lineStart = 0;
74 int remainingChars = line.length();
75 if (blockwise || range.onSingleLine()) {
76 lineStart = range.start().column();
77 remainingChars = range.columnWidth();
78 } else if (i == range.start().line()) {
79 lineStart = range.start().column();
80 } else if (i == range.end().line()) {
81 remainingChars = range.end().column();
82 }
83
84 int handledUntil = lineStart;
85
86 for (const KTextEditor::AttributeBlock &block : attribs) {
87 // honor (block-) selections
88 if (block.start + block.length <= lineStart) {
89 continue;
90 } else if (block.start >= lineStart + remainingChars) {
91 break;
92 }
93 int start = qMax(block.start, lineStart);
94 if (start > handledUntil) {
95 exporter->exportText(line.mid(handledUntil, start - handledUntil), noAttrib);
96 }
97 int length = qMin(block.length, remainingChars);
98 exporter->exportText(line.mid(start, length), block.attribute);
99 handledUntil = start + length;
100 }
101
102 if (handledUntil < lineStart + remainingChars) {
103 exporter->exportText(line.mid(handledUntil, remainingChars), noAttrib);
104 }
105
106 exporter->closeLine(i == range.end().line());
107 }
108
109 output.flush();
110}
QExplicitlySharedDataPointer< Attribute > Ptr
Shared data pointer for Attribute.
Definition attribute.h:56
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
Definition cursor.h:192
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition cursor.h:174
constexpr Cursor end() const noexcept
Get the end position of this range.
constexpr Cursor start() const noexcept
Get the start position of this range.
constexpr int columnWidth() const noexcept
Returns the number of columns separating the start() and end() positions.
constexpr bool onSingleLine() const noexcept
Check whether this range is wholly contained within one line, ie.
Q_SCRIPTABLE Q_NOREPLY void start()
void setMimeData(QMimeData *src, Mode mode)
QClipboard * clipboard()
void setHtml(const QString &html)
void setText(const QString &text)
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
void flush()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:55:24 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.