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 
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 
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 *>(document);
49  d->transactionRunning = false;
50 
51  // start the editing transaction
52  start();
53 }
54 
56 {
57  if (d->document && !d->transactionRunning) {
58  d->document->startEditing();
59  d->transactionRunning = true;
60  }
61 }
62 
64 {
65  if (d->document && d->transactionRunning) {
66  d->document->finishEditing();
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  return d->m_openingErrorMessage;
88 }
89 
90 bool KTextEditor::Document::replaceText(const Range &range, const QString &text, bool block)
91 {
92  bool success = true;
93  EditingTransaction transaction(this);
94  success &= removeText(range, block);
95  success &= insertText(range.start(), text, block);
96  return success;
97 }
98 
99 bool Document::replaceText(const Range &range, const QStringList &text, bool block)
100 {
101  bool success = true;
102  EditingTransaction transaction(this);
103  success &= removeText(range, block);
104  success &= insertText(range.start(), text, block);
105  return success;
106 }
107 
108 bool Document::isEmpty() const
109 {
110  return documentEnd() == Cursor::start();
111 }
112 
113 QVector<KTextEditor::Range> Document::searchText(const KTextEditor::Range &range, const QString &pattern, const SearchOptions options) const
114 {
115  return d->searchText(range, pattern, options);
116 }
Document(DocumentPrivate *impl, QObject *parent)
Constructor.
Definition: document.cpp:12
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.
QString openingErrorMessage() const
Error message if any problem occurred on last load.
Definition: document.cpp:85
~EditingTransaction()
Destructs the object and, if needed, finishes a running editing transaction by calling finish().
Definition: document.cpp:71
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
An object representing a section of text, from one Cursor to another.
virtual bool insertText(const Cursor &position, const QString &text, bool block=false)=0
Insert text at position.
virtual Cursor documentEnd() const =0
End position of the document.
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
virtual bool isEmpty() const
Returns if the document is empty.
Definition: document.cpp:108
constexpr static 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.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
virtual bool replaceText(const Range &range, const QString &text, bool block=false)
Replace text from range with specified text.
Definition: document.cpp:90
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
virtual QString text() const =0
Get the document content.
Editing transaction support.
Definition: document.h:483
~Document() override
Virtual destructor.
void start()
By calling start(), the editing transaction can be started again.
Definition: document.cpp:55
A KParts derived class representing a text document.
Definition: document.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:48:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.