KTextEditor

document.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Bernhard Beschow <[email protected]>
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, QObject *parent)
13  : KParts::ReadWritePart(parent)
14  , d(impl)
15 {
16 }
17 
19 {
20 }
21 
22 namespace KTextEditor
23 {
27 class EditingTransactionPrivate
28 {
29 public:
33  DocumentPrivate *document;
34 
38  bool transactionRunning;
39 };
40 
41 }
42 
44  : d(new EditingTransactionPrivate())
45 {
46  // Although it works in release-mode, we usually want a valid document
47  Q_ASSERT(document != nullptr);
48 
49  // initialize d-pointer
50  d->document = qobject_cast<KTextEditor::DocumentPrivate *>(document);
51  d->transactionRunning = false;
52 
53  // start the editing transaction
54  start();
55 }
56 
58 {
59  if (d->document && !d->transactionRunning) {
60  d->document->startEditing();
61  d->transactionRunning = true;
62  }
63 }
64 
66 {
67  if (d->document && d->transactionRunning) {
68  d->document->finishEditing();
69  d->transactionRunning = false;
70  }
71 }
72 
74 {
75  // finish the editing transaction
76  finish();
77 
78  // delete our d-pointer
79  delete d;
80 }
81 
83 {
84  return d->m_openingError;
85 }
86 
88 {
89  return d->m_openingErrorMessage;
90 }
91 
92 bool KTextEditor::Document::replaceText(const Range &range, const QString &text, bool block)
93 {
94  bool success = true;
95  EditingTransaction transaction(this);
96  success &= removeText(range, block);
97  success &= insertText(range.start(), text, block);
98  return success;
99 }
100 
101 bool Document::replaceText(const Range &range, const QStringList &text, bool block)
102 {
103  bool success = true;
104  EditingTransaction transaction(this);
105  success &= removeText(range, block);
106  success &= insertText(range.start(), text, block);
107  return success;
108 }
109 
110 bool Document::isEmpty() const
111 {
112  return documentEnd() == Cursor::start();
113 }
114 
115 QVector<KTextEditor::Range> Document::searchText(const KTextEditor::Range &range, const QString &pattern, const SearchOptions options) const
116 {
117  return d->searchText(range, pattern, options);
118 }
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:73
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:82
void finish()
By calling finish(), the editing transaction can be finished already before destruction of this insta...
Definition: document.cpp:65
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:43
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:117
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:185
virtual bool isEmpty() const
Returns if the document is empty.
Definition: document.cpp:110
Editing transaction support.
Definition: document.h:472
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:57
virtual ~Document()
Virtual destructor.
Definition: document.cpp:18
Document(DocumentPrivate *impl, QObject *parent)
Constructor.
Definition: document.cpp:12
T qobject_cast(QObject *object)
QString openingErrorMessage() const
Error message if any problem occurred on last load.
Definition: document.cpp:87
virtual bool replaceText(const Range &range, const QString &text, bool block=false)
Replace text from range with specified text.
Definition: document.cpp:92
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Dec 3 2020 22:58:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.