edbee - Qt Editor Library
textbuffer.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <QObject>
9 #include <QVector>
10 #include <QSharedData>
11 #include <QExplicitlySharedDataPointer>
12 
13 namespace edbee {
14 
15 class TextBuffer;
16 class TextBufferChange;
17 class TextRange;
18 class TextLineData;
19 class LineOffsetVector;
20 
21 
22 class TextBufferChangeData : public QSharedData
23 {
24 public:
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_;
36  int lineCount_;
37  QVector<int> newLineOffsets_;
38 
39 };
40 
41 
46 {
47 public:
49  TextBufferChange( TextBuffer* buffer, int off, int len, const QChar* text, int textlen );
50  TextBufferChange( LineOffsetVector* lineOffsets, int off, int len, const QChar* text, int textlen );
51  TextBufferChange( const TextBufferChange& other );
52 
53  int offset() const { return d_->offset_; }
54  int length() const { return d_->length_; }
55  const QChar* newText() const { return d_->newText_; }
56  int newTextLength() const { return d_->newTextLength_; }
57  int line() const { return d_->line_; }
58  int lineCount() const { return d_->lineCount_; }
59  inline int newLineCount() const { return d_->newLineOffsets_.size(); }
60  const QVector<int>& newLineOffsets() const { return d_->newLineOffsets_; }
61 
62 
63 private:
64  QExplicitlySharedDataPointer<TextBufferChangeData> d_;
65 };
66 
68 class TextBuffer : public QObject
69 {
70 Q_OBJECT
71 
72 public:
73  TextBuffer( QObject* parent = 0);
74 
75 // Minimal abstract interface to implement
76 
78  virtual int length() const = 0;
79 
81  virtual QChar charAt( int offset ) const = 0;
82 
84  virtual QString textPart( int offset, int length ) const = 0;
85 
88  virtual void replaceText( int offset, int length, const QChar* buffer, int bufferLength ) = 0;
89 
92  virtual int lineCount() = 0; // { return lineOffsets().length(); }
93  virtual int lineFromOffset(int offset ) = 0;
94  virtual int offsetFromLine( int line ) = 0;
95 
96 // raw loading methods
97 
99  virtual void rawAppendBegin() = 0;
100 
102  virtual void rawAppend( QChar c ) = 0;
103 
105  virtual void rawAppend( const QChar* data, int dataLength ) = 0;
106 
112  virtual void rawAppendEnd() = 0;
113 
117  virtual QChar* rawDataPointer() = 0;
118 
119 
120 // easy functions
121 
123  virtual void replaceText( int offset, int length, const QString& text );
124 
125  QString text();
126  void setText( const QString& text );
127  virtual int columnFromOffsetAndLine( int offset, int line=-1 );
128  virtual void appendText( const QString& text );
129  virtual int offsetFromLineAndColumn( int line, int col );
130  virtual QString line( int line);
131  virtual QString lineWithoutNewline( int line );
132 
133  virtual int lineLength(int line);
134  virtual int lineLengthWithoutNewline(int line);
135  virtual void replaceText( const TextRange& range, const QString& text );
136 
137  virtual int findCharPos( int offset, int direction, const QString& chars, bool equals );
138  virtual int findCharPosWithinRange( int offset, int direction, const QString& chars, bool equals, int beginRange, int endRange );
139  virtual int findCharPosOrClamp( int offset, int direction, const QString& chars, bool equals );
140  virtual int findCharPosWithinRangeOrClamp( int offset, int direction, const QString& chars, bool equals, int beginRange, int endRange );
141 
142  virtual QString lineOffsetsAsString();
143 
144  signals:
145 
146  void textAboutToBeChanged( edbee::TextBufferChange change );
147  void textChanged( edbee::TextBufferChange change );
148 
149 };
150 
151 } // edbee
152 
153 // needs to be OUTSIDE the namespace!!
154 Q_DECLARE_METATYPE(edbee::TextBufferChange)
155 
156 
This clas represents a text buffer change and is used to pass around between events This is a sharedd...
Definition: textbuffer.h:45
const QChar * newText_
The reference to a new text.
Definition: textbuffer.h:31
TextBufferChangeData(TextBuffer *buffer, int off, int len, const QChar *text, int textlen)
Initializes the textbuffer change.
Definition: textbuffer.cpp:18
int line_
The line number were the change occured.
Definition: textbuffer.h:35
int line() const
Definition: textbuffer.h:57
This class implements the vector for storing the line numbers at certain offsets/ The class allows th...
Definition: lineoffsetvector.h:26
int offset_
The offset in the buffer.
Definition: textbuffer.h:29
int offset() const
Definition: textbuffer.h:53
Copyright 2011-2013 - Reliable Bits Software by Blommers IT.
Definition: commentcommand.cpp:22
const QChar * newText() const
Definition: textbuffer.h:55
const QVector< int > & newLineOffsets() const
Definition: textbuffer.h:60
Definition: textbuffer.h:22
int length() const
Definition: textbuffer.h:54
int lineCount() const
Definition: textbuffer.h:58
int length_
The number of chars to replaced.
Definition: textbuffer.h:30
This class represents the textbuffer of the editor.
Definition: textbuffer.h:68
int lineCount_
the number of lines that are involved.
Definition: textbuffer.h:36
int newTextLength_
The length of this text.
Definition: textbuffer.h:32
A single text region A region constists of an anchor and a caret: The anchor defines the &#39;start&#39; of t...
Definition: textrange.h:29
int newLineCount() const
Definition: textbuffer.h:59
QVector< int > newLineOffsets_
A list of new line offset.
Definition: textbuffer.h:37
int newTextLength() const
Definition: textbuffer.h:56