edbee - Qt Editor Library v0.11.1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
textbuffer.h
Go to the documentation of this file.
1// edbee - Copyright (c) 2012-2025 by Rick Blommers and contributors
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include "edbee/exports.h"
7
8#include <QObject>
9#include <QVector>
10#include <QSharedData>
11#include <QExplicitlySharedDataPointer>
12
13namespace edbee {
14
15class TextBuffer;
17class TextRange;
18class TextLineData;
20
21
22class EDBEE_EXPORT TextBufferChangeData : public QSharedData
23{
24public:
25 TextBufferChangeData( TextBuffer* buffer, int off, int len, const QChar* text, int textlen );
26 TextBufferChangeData( LineOffsetVector* lineOffsets, int off, int len, const QChar* text, int textlen );
27
28 // text information
29 int offset_;
30 int length_;
31 const QChar* newText_;
33
34 // line informationm
35 int line_;
37 QVector<int> newLineOffsets_;
38
39};
40
41
46public:
48 TextBufferChange( TextBuffer* buffer, int off, int len, const QChar* text, int textlen );
49 TextBufferChange( LineOffsetVector* lineOffsets, int off, int len, const QChar* text, int textlen );
50 TextBufferChange( const TextBufferChange& other );
51
52 int offset() const { return d_->offset_; }
53 int length() const { return d_->length_; }
54 const QChar* newText() const { return d_->newText_; }
55 int newTextLength() const { return d_->newTextLength_; }
56 int line() const { return d_->line_; }
57 int lineCount() const { return d_->lineCount_; }
58 inline int newLineCount() const { return d_->newLineOffsets_.size(); }
59 const QVector<int>& newLineOffsets() const { return d_->newLineOffsets_; }
60
61
62private:
63 QExplicitlySharedDataPointer<TextBufferChangeData> d_;
64};
65
67class EDBEE_EXPORT TextBuffer : public QObject
68{
69Q_OBJECT
70
71public:
72 TextBuffer( QObject* parent = 0);
73
74// Minimal abstract interface to implement
75
77 virtual int length() const = 0;
78
80 virtual QChar charAt( int offset ) const = 0;
81
83 virtual QString textPart( int offset, int length ) const = 0;
84
87 virtual void replaceText( int offset, int length, const QChar* buffer, int bufferLength ) = 0;
88
91 virtual int lineCount() = 0; // { return lineOffsets().length(); }
92 virtual int lineFromOffset(int offset ) = 0;
93 virtual int offsetFromLine( int line ) = 0;
94
95// raw loading methods
96
98 virtual void rawAppendBegin() = 0;
99
101 virtual void rawAppend( QChar c ) = 0;
102
104 virtual void rawAppend( const QChar* data, int dataLength ) = 0;
105
111 virtual void rawAppendEnd() = 0;
112
116 virtual QChar* rawDataPointer() = 0;
117
118
119// easy functions
120
122 virtual void replaceText( int offset, int length, const QString& text );
123
124 QString text();
125 void setText( const QString& text );
126 virtual int columnFromOffsetAndLine( int offset, int line=-1 );
127 virtual void appendText( const QString& text );
128 virtual int offsetFromLineAndColumn( int line, int col );
129 virtual QString line( int line);
130 virtual QString lineWithoutNewline( int line );
131
132 virtual int lineLength(int line);
133 virtual int lineLengthWithoutNewline(int line);
134 virtual void replaceText( const TextRange& range, const QString& text );
135
136 virtual int findCharPos( int offset, int direction, const QString& chars, bool equals );
137 virtual int findCharPosWithinRange( int offset, int direction, const QString& chars, bool equals, int beginRange, int endRange );
138 virtual int findCharPosOrClamp( int offset, int direction, const QString& chars, bool equals );
139 virtual int findCharPosWithinRangeOrClamp( int offset, int direction, const QString& chars, bool equals, int beginRange, int endRange );
140
141 virtual QString lineOffsetsAsString();
142
143 signals:
144
146 void textChanged( edbee::TextBufferChange change, QString oldText = QString() );
147
148};
149
150} // edbee
151
152// needs to be OUTSIDE the namespace!!
153Q_DECLARE_METATYPE(edbee::TextBufferChange)
154
155
This class implements the vector for storing the line numbers at certain offsets/ The class allows th...
Definition lineoffsetvector.h:26
int lineCount_
the number of lines that are involved.
Definition textbuffer.h:36
int length_
The number of chars to replaced.
Definition textbuffer.h:30
int offset_
The offset in the buffer.
Definition textbuffer.h:29
int line_
The line number were the change occured.
Definition textbuffer.h:35
int newTextLength_
The length of this text.
Definition textbuffer.h:32
QVector< int > newLineOffsets_
A list of new line offset.
Definition textbuffer.h:37
TextBufferChangeData(TextBuffer *buffer, int off, int len, const QChar *text, int textlen)
Initializes the textbuffer change.
Definition textbuffer.cpp:16
const QChar * newText_
The reference to a new text.
Definition textbuffer.h:31
This clas represents a text buffer change and is used to pass around between events This is a sharedd...
Definition textbuffer.h:45
TextBufferChange()
Definition textbuffer.cpp:65
const QChar * newText() const
Definition textbuffer.h:54
int lineCount() const
Definition textbuffer.h:57
const QVector< int > & newLineOffsets() const
Definition textbuffer.h:59
int length() const
Definition textbuffer.h:53
int line() const
Definition textbuffer.h:56
int offset() const
Definition textbuffer.h:52
int newLineCount() const
Definition textbuffer.h:58
int newTextLength() const
Definition textbuffer.h:55
This class represents the textbuffer of the editor.
Definition textbuffer.h:68
virtual int length() const =0
should return the number of 'characters'.
virtual int lineCount()=0
this method should return an array with all line offsets. A line offset pointsto the START of a line ...
virtual int findCharPosOrClamp(int offset, int direction, const QString &chars, bool equals)
See documentation at findCharPosWithinRange. This method searches a char position within the given ra...
Definition textbuffer.cpp:245
virtual void replaceText(int offset, int length, const QChar *buffer, int bufferLength)=0
this method should replace the given text And fire a 'text-replaced' signal
virtual int findCharPosWithinRangeOrClamp(int offset, int direction, const QString &chars, bool equals, int beginRange, int endRange)
See documentation at findCharPosWithinRange. This method searches a char position within the given ra...
Definition textbuffer.cpp:253
virtual QString textPart(int offset, int length) const =0
return the given text.
virtual QString line(int line)
Returns the line at the given line position. This line INCLUDES the newline character (if it's there)
Definition textbuffer.cpp:159
virtual void rawAppend(const QChar *data, int dataLength)=0
This method should raw append the given character string.
virtual void rawAppend(QChar c)=0
this method should append the given character to the buffer
virtual void rawAppendEnd()=0
the end raw append method should bring the document in a consistent state and emit the correct "repla...
virtual QString lineWithoutNewline(int line)
Returns the line without the newline character.
Definition textbuffer.cpp:168
virtual int lineLength(int line)
Returns the length of the given line. Also counting the trailing newline character if present.
Definition textbuffer.cpp:180
virtual int columnFromOffsetAndLine(int offset, int line=-1)
this method translates the given position to a column number.
Definition textbuffer.cpp:123
virtual void rawAppendBegin()=0
This method starts raw appending.
virtual QChar charAt(int offset) const =0
A method for returning a single char.
virtual int lineFromOffset(int offset)=0
TextBuffer(QObject *parent=0)
The textbuffer constructor.
Definition textbuffer.cpp:90
QString text()
Returns the full text as a QString.
Definition textbuffer.cpp:107
virtual void appendText(const QString &text)
Appends the given text to the textbuffer.
Definition textbuffer.cpp:139
virtual int findCharPosWithinRange(int offset, int direction, const QString &chars, bool equals, int beginRange, int endRange)
This method finds the find the first character position that equals the given char.
Definition textbuffer.cpp:228
virtual int offsetFromLine(int line)=0
void textChanged(edbee::TextBufferChange change, QString oldText=QString())
Definition moc_textbuffer.cpp:226
void textAboutToBeChanged(edbee::TextBufferChange change)
Definition moc_textbuffer.cpp:219
virtual int lineLengthWithoutNewline(int line)
Returns the length of the given line. Without counting a trailing newline character.
Definition textbuffer.cpp:189
virtual QString lineOffsetsAsString()
Definition textbuffer.cpp:266
virtual int offsetFromLineAndColumn(int line, int col)
This method returns the offset from the give line and column If the column exceed the number of colum...
Definition textbuffer.cpp:147
void setText(const QString &text)
A convenient method for directly filling the textbuffer with the given content.
Definition textbuffer.cpp:114
virtual QChar * rawDataPointer()=0
This method returns the raw data buffer. WARNING this method CAN be slow because when using a gapvect...
virtual int findCharPos(int offset, int direction, const QString &chars, bool equals)
See documentation at findCharPosWithinRange.
Definition textbuffer.cpp:212
A text line item reference.
Definition textlinedata.h:30
A single text region A region constists of an anchor and a caret: The anchor defines the 'start' of t...
Definition textrange.h:29
#define EDBEE_EXPORT
Definition exports.h:15
QT Acessibility has an issue with reporting blank lines between elements lines. defining 'WINDOWS_EMP...
Definition commentcommand.cpp:20