KTextEditor

document.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "document.h"
8#include "katedocument.h"
9
10using namespace KTextEditor;
11
12Document::Document(DocumentPrivate *impl, const KPluginMetaData &data, QObject *parent)
13 : KParts::ReadWritePart(parent, data)
14 , d(impl)
15{
16}
17
18Document::~Document() = default;
19
20namespace KTextEditor
21{
22/**
23 * Private d-pointer type for EditingTransaction
24 */
25class EditingTransactionPrivate
26{
27public:
28 /**
29 * real document implementation
30 */
31 DocumentPrivate *document;
32
33 /**
34 * Indicator for running editing transaction
35 */
36 bool transactionRunning;
37};
38
39}
40
42 : d(new EditingTransactionPrivate())
43{
44 // Although it works in release-mode, we usually want a valid document
45 Q_ASSERT(document != nullptr);
46
47 // initialize d-pointer
49 d->transactionRunning = false;
50
51 // start the editing transaction
52 start();
53}
54
56{
57 if (d->document && !d->transactionRunning) {
58 d->document->editStart();
59 d->transactionRunning = true;
60 }
61}
62
64{
65 if (d->document && d->transactionRunning) {
66 d->document->editEnd();
67 d->transactionRunning = false;
68 }
69}
70
72{
73 // finish the editing transaction
74 finish();
75
76 // delete our d-pointer
77 delete d;
78}
79
81{
82 return d->m_openingError;
83}
84
86{
87 bool success = true;
88 EditingTransaction transaction(this);
89 success &= removeText(range, block);
90 success &= insertText(range.start(), text, block);
91 return success;
92}
93
94bool Document::replaceText(Range range, const QStringList &text, bool block)
95{
96 bool success = true;
97 EditingTransaction transaction(this);
98 success &= removeText(range, block);
99 success &= insertText(range.start(), text, block);
100 return success;
101}
102
104{
105 return documentEnd() == Cursor::start();
106}
107
109{
110 return d->searchText(range, pattern, options);
111}
static constexpr Cursor start() noexcept
Returns a cursor representing the start of any document - i.e., line 0, column 0.
Definition cursor.h:120
Backend of KTextEditor::Document related public KTextEditor interfaces.
bool editStart()
Enclose editor actions with editStart() and editEnd() to group them.
bool editEnd()
End a editor operation.
Editing transaction support.
Definition document.h:574
~EditingTransaction()
Destructs the object and, if needed, finishes a running editing transaction by calling finish().
Definition document.cpp:71
void start()
By calling start(), the editing transaction can be started again.
Definition document.cpp:55
void finish()
By calling finish(), the editing transaction can be finished already before destruction of this insta...
Definition document.cpp:63
EditingTransaction(Document *document)
Constructs the object and starts an editing transaction by calling start().
Definition document.cpp:41
A KParts derived class representing a text document.
Definition document.h:284
bool openingError() const
True, eg if the file for opening could not be read This doesn't have to handle the KPart job canceled...
Definition document.cpp:80
virtual bool isEmpty() const
Returns if the document is empty.
Definition document.cpp:103
virtual QString text() const =0
Get the document content.
virtual bool removeText(Range range, bool block=false)=0
Remove the text specified in range.
QList< KTextEditor::Range > searchText(KTextEditor::Range range, const QString &pattern, const SearchOptions options=Default) const
Searches the given input range for a text pattern.
Definition document.cpp:108
virtual bool insertText(KTextEditor::Cursor position, const QString &text, bool block=false)=0
Insert text at position.
~Document() override
Virtual destructor.
virtual Cursor documentEnd() const =0
End position of the document.
virtual bool replaceText(Range range, const QString &text, bool block=false)
Replace text from range with specified text.
Definition document.cpp:85
An object representing a section of text, from one Cursor to another.
constexpr Cursor start() const noexcept
Get the start position of this range.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.