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 | |
10 | using namespace KTextEditor; |
11 | |
12 | Document::Document(DocumentPrivate *impl, const KPluginMetaData &data, QObject *parent) |
13 | : KParts::ReadWritePart(parent, data) |
14 | , d(impl) |
15 | { |
16 | } |
17 | |
18 | Document::~Document() = default; |
19 | |
20 | namespace KTextEditor |
21 | { |
22 | /** |
23 | * Private d-pointer type for EditingTransaction |
24 | */ |
25 | class EditingTransactionPrivate |
26 | { |
27 | public: |
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 | |
41 | Document::EditingTransaction::EditingTransaction(Document *document) |
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 |
48 | d->document = qobject_cast<KTextEditor::DocumentPrivate *>(object: document); |
49 | d->transactionRunning = false; |
50 | |
51 | // start the editing transaction |
52 | start(); |
53 | } |
54 | |
55 | void Document::EditingTransaction::start() |
56 | { |
57 | if (d->document && !d->transactionRunning) { |
58 | d->document->editStart(); |
59 | d->transactionRunning = true; |
60 | } |
61 | } |
62 | |
63 | void Document::EditingTransaction::finish() |
64 | { |
65 | if (d->document && d->transactionRunning) { |
66 | d->document->editEnd(); |
67 | d->transactionRunning = false; |
68 | } |
69 | } |
70 | |
71 | Document::EditingTransaction::~EditingTransaction() |
72 | { |
73 | // finish the editing transaction |
74 | finish(); |
75 | |
76 | // delete our d-pointer |
77 | delete d; |
78 | } |
79 | |
80 | bool Document::openingError() const |
81 | { |
82 | return d->m_openingError; |
83 | } |
84 | |
85 | bool KTextEditor::Document::replaceText(Range range, const QString &text, bool block) |
86 | { |
87 | bool success = true; |
88 | EditingTransaction transaction(this); |
89 | success &= removeText(range, block); |
90 | success &= insertText(position: range.start(), text, block); |
91 | return success; |
92 | } |
93 | |
94 | bool 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(position: range.start(), text, block); |
100 | return success; |
101 | } |
102 | |
103 | bool Document::isEmpty() const |
104 | { |
105 | return documentEnd() == Cursor::start(); |
106 | } |
107 | |
108 | QList<KTextEditor::Range> Document::searchText(KTextEditor::Range range, const QString &pattern, const SearchOptions options) const |
109 | { |
110 | return d->searchText(range, pattern, options); |
111 | } |
112 | |