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;
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}
Attributes of a part of a line.
Definition attribute.h:307
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
virtual QString line(int line) const =0
Get a single text line.
Range documentRange() const
A Range which encompasses the whole document.
Definition document.h:785
An object representing a section of text, from one Cursor to another.
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.
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
virtual bool blockSelection() const =0
Get the status of the selection mode.
virtual QList< KTextEditor::AttributeBlock > lineAttributes(int line)=0
Get the list of AttributeBlocks for a given line in the document.
virtual bool selection() const =0
Query the view whether it has selected text, i.e.
virtual Range selectionRange() const =0
Get the range occupied by the current selection.
Q_SCRIPTABLE Q_NOREPLY void start()
void setMimeData(QMimeData *src, QClipboard::Mode mode)
QClipboard * clipboard()
void setHtml(const QString &html)
void setText(const QString &text)
int length() const const
QString mid(int position, int n) const const
void flush()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Feb 24 2024 20:00:58 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.