KTextEditor

document.cpp
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010 Bernhard Beschow <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "document.h"
22 #include "katedocument.h"
23 
24 using namespace KTextEditor;
25 
26 Document::Document(DocumentPrivate *impl, QObject *parent)
27  : KParts::ReadWritePart(parent)
28  , d(impl)
29 {
30 }
31 
33 {
34 }
35 
36 namespace KTextEditor
37 {
41 class EditingTransactionPrivate
42 {
43 public:
47  DocumentPrivate *document;
48 
52  bool transactionRunning;
53 };
54 
55 }
56 
58  : d(new EditingTransactionPrivate())
59 {
60  // Although it works in release-mode, we usually want a valid document
61  Q_ASSERT(document != nullptr);
62 
63  // initialize d-pointer
64  d->document = qobject_cast<KTextEditor::DocumentPrivate *>(document);
65  d->transactionRunning = false;
66 
67  // start the editing transaction
68  start();
69 }
70 
72 {
73  if (d->document && !d->transactionRunning) {
74  d->document->startEditing();
75  d->transactionRunning = true;
76  }
77 }
78 
80 {
81  if (d->document && d->transactionRunning) {
82  d->document->finishEditing();
83  d->transactionRunning = false;
84  }
85 }
86 
88 {
92  finish();
93 
97  delete d;
98 }
99 
101 {
102  return d->m_openingError;
103 }
104 
106 {
107  return d->m_openingErrorMessage;
108 }
109 
110 bool KTextEditor::Document::replaceText(const Range &range, const QString &text, bool block)
111 {
112  bool success = true;
113  EditingTransaction transaction(this);
114  success &= removeText(range, block);
115  success &= insertText(range.start(), text, block);
116  return success;
117 }
118 
119 bool Document::replaceText(const Range &range, const QStringList &text, bool block)
120 {
121  bool success = true;
122  EditingTransaction transaction(this);
123  success &= removeText(range, block);
124  success &= insertText(range.start(), text, block);
125  return success;
126 }
127 
128 bool Document::isEmpty() const
129 {
130  return documentEnd() == Cursor::start();
131 }
132 
133 QVector<KTextEditor::Range> Document::searchText(const KTextEditor::Range &range, const QString &pattern, const SearchOptions options) const
134 {
135  return d->searchText(range, pattern, options);
136 }
virtual bool insertText(const Cursor &position, const QString &text, bool block=false)=0
Insert text at position.
~EditingTransaction()
Destructs the object and, if needed, finishes a running editing transaction by calling finish()...
Definition: document.cpp:87
bool openingError() const
True, eg if the file for opening could not be read This doesn&#39;t have to handle the KPart job canceled...
Definition: document.cpp:100
void finish()
By calling finish(), the editing transaction can be finished already before destruction of this insta...
Definition: document.cpp:79
virtual QString text() const =0
Get the document content.
QVector< KTextEditor::Range > searchText(const KTextEditor::Range &range, const QString &pattern, const SearchOptions options=Default) const
Searches the given input range for a text pattern.
EditingTransaction(Document *document)
Constructs the object and starts an editing transaction by calling start().
Definition: document.cpp:57
static constexpr Cursor start() Q_DECL_NOEXCEPT
Returns a cursor representing the start of any document - i.e., line 0, column 0. ...
Definition: cursor.h:131
virtual bool removeText(const Range &range, bool block=false)=0
Remove the text specified in range.
A KParts derived class representing a text document.
Definition: document.h:199
virtual bool isEmpty() const
Returns if the document is empty.
Definition: document.cpp:128
Editing transaction support.
Definition: document.h:486
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
virtual Cursor documentEnd() const =0
End position of the document.
An object representing a section of text, from one Cursor to another.
void start()
By calling start(), the editing transaction can be started again.
Definition: document.cpp:71
virtual ~Document()
Virtual destructor.
Definition: document.cpp:32
Document(DocumentPrivate *impl, QObject *parent)
Constructor.
Definition: document.cpp:26
T qobject_cast(QObject *object)
QString openingErrorMessage() const
Error message if any problem occurred on last load.
Definition: document.cpp:105
virtual bool replaceText(const Range &range, const QString &text, bool block=false)
Replace text from range with specified text.
Definition: document.cpp:110
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Aug 8 2020 22:56:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.