edbee - Qt Editor Library
textrange.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <QObject>
9 #include <QVector>
10 
12 
13 namespace edbee {
14 
15 class TextDocument;
16 
29 class TextRange
30 {
31 public:
32  TextRange( int anchor=0, int caret=0 ) : anchor_(anchor), caret_(caret) {}
33 
34  inline int anchor() const { return anchor_; }
35  inline int caret() const { return caret_; }
36 
38  inline int min() const { return qMin( caret_, anchor_ ); }
39  inline int max() const { return qMax( caret_, anchor_ ); }
40 
42  inline int& minVar() { return caret_ < anchor_ ? caret_ : anchor_; }
43  inline int& maxVar() { return caret_ < anchor_ ? anchor_: caret_; }
44 
45  inline int length() const { return qAbs(caret_ - anchor_ ); }
46 
47  void fixCaretForUnicode(TextDocument* doc, int direction );
48 
49 
50  void setAnchor( int anchor ) { anchor_ = anchor; }
51  void setAnchorBounded( TextDocument* doc, int anchor );
52  void setCaret( int caret ) { caret_ = caret; }
53  void setCaretBounded( TextDocument* doc, int caret );
54  void setLength( int newLength );
55 
56 
57  void set( int anchor, int caret ) { anchor_ = anchor; caret_ = caret; }
58 
59  void reset() { anchor_ = caret_; }
60  bool hasSelection() const { return anchor_ != caret_; }
61  bool isEmpty() const { return anchor_==caret_; }
62  void clearSelection() { caret_ = anchor_; }
63 
64  QString toString() const;
65 
66  void moveCaret( TextDocument* doc, int amount );
67  void moveCaretOrDeselect( TextDocument* doc, int amount );
68  int moveWhileChar( TextDocument* doc, int pos, int amount, const QString& chars );
69  int moveUntilChar( TextDocument* doc, int pos, int amount, const QString& chars );
70  void moveCaretWhileChar( TextDocument* doc, int amount, const QString& chars );
71  void moveCaretUntilChar( TextDocument* doc, int amount, const QString& chars );
72  void moveAnchortWhileChar( TextDocument* doc, int amount, const QString& chars );
73  void moveAnchorUntilChar( TextDocument* doc, int amount, const QString& chars );
74  void moveCaretByCharGroup( TextDocument* doc, int amount, const QString& whitespace, const QStringList& characterGroups );
75  void moveCaretToLineBoundary( TextDocument* doc, int amount, const QString& whitespace );
76 
77  void expandToFullLine( TextDocument* doc, int amount );
79  void expandToWord( TextDocument* doc, const QString& whitespace, const QStringList& characterGroups );
80  void expandToIncludeRange( TextRange& range );
81 
82  void forceBounds( TextDocument* doc );
83 
84  bool equals(const TextRange &range );
85  bool touches( TextRange& range );
86 
87  static bool lessThan( TextRange& r1, TextRange& r2 );
88 
89 private:
90  int anchor_;
91  int caret_;
92 };
93 
94 
95 //======================================================================
96 
97 
106 {
107 public:
109  virtual ~TextRangeSetBase() {}
110  // TextRangeSet(const TextRangeSet& sel);
111 
112  // pure virtual methods
113 // virtual TextRangeSetBase* clone() const = 0;
114  // TextRangeSet & operator=( const TextRangeSet& sel );
115 
116  virtual int rangeCount() const = 0;
117  virtual TextRange& range(int idx) = 0;
118  virtual const TextRange& constRange(int idx) const = 0;
119  virtual void addRange( int anchor, int caret ) = 0;
120  virtual void addRange( const TextRange& range ) = 0;
121  virtual void removeRange( int idx ) = 0;
122  virtual void clear() = 0;
123  virtual void toSingleRange() = 0;
124  virtual void sortRanges() = 0;
125 
126  TextRange& lastRange();
127  TextRange& firstRange();
128 
129  int rangeIndexAtOffset( int offset );
130  bool rangesBetweenOffsets( int offsetBegin, int offsetEnd, int& firstIndex, int& lastIndex );
131  bool rangesBetweenOffsetsExlusiveEnd( int offsetBegin, int offsetEnd, int& firstIndex, int& lastIndex );
132  bool rangesAtLine( int line, int& firstIndex, int& lastIndex );
133  bool hasSelection();
134  bool equals( TextRangeSetBase& sel );
135  void replaceAll( const TextRangeSetBase& base );
136 
137 
138  QString getSelectedText();
139  QString getSelectedTextExpandedToFullLines();
140 
141  QString rangesAsString() const;
142 
143  // changing
144  void beginChanges();
145  void endChanges();
146  void endChangesWithoutProcessing();
147  bool changing() const;
148 
149  void resetAnchors();
150  void clearSelection();
151 
152  void addTextRanges( const TextRangeSetBase& sel);
153  void substractTextRanges( const TextRangeSetBase& sel );
154  void substractRange( int min, int max );
155 
156 
157  // selection
158  void expandToFullLines(int amount);
159  void expandToWords( const QString& whitespace, const QStringList& characterGroups);
160  void selectWordAt(int offset , const QString& whitespace, const QStringList& characterGroups);
161  void toggleWordSelectionAt( int offset, const QString& whitespace, const QStringList& characterGroups);
162 
163  // movement
164  void moveCarets( int amount );
165  void moveCaretsOrDeselect( int amount );
166  void moveCaretsByCharGroup( int amount, const QString& whitespace, const QStringList& charGroups );
167  void moveCaretsToLineBoundary(int direction, const QString& whitespace );
168  void moveCaretsByLine( int amount );
169 
170 
171  // changing
172  // void growSelectionAtBegin( int amount );
173  void changeSpatial(int pos, int length, int newLength, bool sticky=false, bool performDelete=false);
174 
175  void setRange( int anchor, int caret, int index = 0 );
176  void setRange( const TextRange& range , int index = 0 );
177 
178  virtual void processChangesIfRequired(bool joinBorders=false);
179 
180  // getters
181  TextDocument* textDocument() const;
182  //TextBuffer* textBuffer() const { return textBufferRef_; }
183 
184 
185  void mergeOverlappingRanges( bool joinBorders );
186 
187 protected:
188 
190  int changing_;
191 };
192 
193 
194 //======================================================================
195 
196 
199 {
200 public:
201  TextRangeSet( TextDocument* doc );
202  TextRangeSet( const TextRangeSet& sel );
203  TextRangeSet( const TextRangeSet* sel );
204  virtual ~TextRangeSet() {}
205 
206  TextRangeSet& operator=(const TextRangeSet& sel);
207  TextRangeSet* clone() const;
208 
209  virtual int rangeCount() const { return selectionRanges_.size(); }
210  virtual TextRange& range(int idx);
211  virtual const TextRange& constRange(int idx ) const;
212  virtual void addRange(int anchor, int caret);
213  virtual void addRange( const TextRange& range );
214  virtual void removeRange(int idx);
215  virtual void clear();
216  virtual void toSingleRange();
217  virtual void sortRanges();
218 
219 private:
220 
221  QVector<TextRange> selectionRanges_;
222 };
223 
224 
225 //======================================================================
226 
227 
237 class DynamicTextRangeSet : public QObject, public TextRangeSet
238 {
239 Q_OBJECT
240 
241 public:
242  DynamicTextRangeSet( TextDocument* doc, bool stickyMode=false, bool deleteMode=false, QObject* parent=0 );
243  DynamicTextRangeSet( const TextRangeSet& sel, bool stickyMode=false, bool deleteMode=false, QObject* parent=0 );
244  DynamicTextRangeSet( const TextRangeSet* sel, bool stickyMode=false, bool deleteMode=false, QObject* parent=0 );
245  virtual ~DynamicTextRangeSet();
246 
247  void setStickyMode( bool mode );
248  bool stickyMode() const;
249 
250  void setDeleteMode( bool mode );
251  bool deleteMode() const;
252 
253 public slots:
254  void textChanged( edbee::TextBufferChange change );
255 
256 private:
257  bool stickyMode_;
258  bool deleteMode_;
259 };
260 
261 
262 } // edbee
This is the base and abstract class of a text document A TextDocument is the model part of the editor...
Definition: textdocument.h:40
This abstract class represents a set of textranges The ranges are kept ordered and will not contain o...
Definition: textrange.h:105
void expandToFullLine(TextDocument *doc, int amount)
Expands the selection range so it only consists of full lines amount specifies the amount (and the di...
Definition: textrange.cpp:253
This clas represents a text buffer change and is used to pass around between events This is a sharedd...
Definition: textbuffer.h:45
void moveCaretOrDeselect(TextDocument *doc, int amount)
move the caret or deselect the given amount
Definition: textrange.cpp:86
void reset()
Definition: textrange.h:59
A smart QObject implemenation of a TextRangeSet which listens to changes in the document. When a change happens it&#39;s changes the spatial of the ranges.
Definition: textrange.h:237
void moveCaretByCharGroup(TextDocument *doc, int amount, const QString &whitespace, const QStringList &characterGroups)
This method moves the caret to/from the given seperator.
Definition: textrange.cpp:166
void expandToIncludeRange(TextRange &range)
Definition: textrange.cpp:352
bool isEmpty() const
Definition: textrange.h:61
void moveCaretToLineBoundary(TextDocument *doc, int amount, const QString &whitespace)
This moves the caret to a line boundary.
Definition: textrange.cpp:220
void setAnchorBounded(TextDocument *doc, int anchor)
Sets the anchor to the given location, and forces the anchor to say between the document bounds...
Definition: textrange.cpp:42
void moveCaretWhileChar(TextDocument *doc, int amount, const QString &chars)
moves the caret while a character is moving
Definition: textrange.cpp:141
int max() const
Definition: textrange.h:39
int & maxVar()
Definition: textrange.h:43
TextDocument * textDocumentRef_
The reference to the textbuffer.
Definition: textrange.h:189
int & minVar()
returns the minimal variable reference
Definition: textrange.h:42
void deselectTrailingNewLine(TextDocument *doc)
This method deselects the last character if it&#39;s a newline.
Definition: textrange.cpp:299
int changing_
A (integer) boolean for handling changes between beginChagnes and endChanges.
Definition: textrange.h:190
void forceBounds(TextDocument *doc)
Sets the bounds.
Definition: textrange.cpp:361
Copyright 2011-2013 - Reliable Bits Software by Blommers IT.
Definition: commentcommand.cpp:22
int min() const
returns the minimal value
Definition: textrange.h:38
int caret() const
Definition: textrange.h:35
void expandToWord(TextDocument *doc, const QString &whitespace, const QStringList &characterGroups)
Expands the selection to a words.
Definition: textrange.cpp:325
void setAnchor(int anchor)
Definition: textrange.h:50
void moveCaret(TextDocument *doc, int amount)
Moves the caret the given amount.
Definition: textrange.cpp:76
int length() const
Definition: textrange.h:45
bool equals(const TextRange &range)
This method checks if the two ranges are equal.
Definition: textrange.cpp:369
int anchor() const
Definition: textrange.h:34
void setCaretBounded(TextDocument *doc, int caret)
Sets the caret to the given location, and forces the caret to say between the document bounds...
Definition: textrange.cpp:51
bool hasSelection() const
Definition: textrange.h:60
static bool lessThan(TextRange &r1, TextRange &r2)
This method compares selection ranges.
Definition: textrange.cpp:15
virtual ~TextRangeSetBase()
Definition: textrange.h:109
The basic textrange class. A simple class of textrange with a simple vector implementation.
Definition: textrange.h:198
void moveAnchorUntilChar(TextDocument *doc, int amount, const QString &chars)
Definition: textrange.cpp:159
void clearSelection()
Definition: textrange.h:62
virtual int rangeCount() const
Definition: textrange.h:209
void fixCaretForUnicode(TextDocument *doc, int direction)
Makes sure the caret isn&#39;t in-between a unicode boundary refs #19 - Dirty hack to improve caret-movem...
Definition: textrange.cpp:23
int moveUntilChar(TextDocument *doc, int pos, int amount, const QString &chars)
This method charactes until the given chargroup is found When moving to the LEFT the cursor is placed...
Definition: textrange.cpp:126
void moveCaretUntilChar(TextDocument *doc, int amount, const QString &chars)
Definition: textrange.cpp:147
bool touches(TextRange &range)
Checks if two ranges touch eachother. Touching means that the start and end of two ranges are onto ea...
Definition: textrange.cpp:377
void moveAnchortWhileChar(TextDocument *doc, int amount, const QString &chars)
Definition: textrange.cpp:153
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
QString toString() const
This method converts a text-selection range to a string helpfull with debuggin.
Definition: textrange.cpp:67
virtual ~TextRangeSet()
Definition: textrange.h:204
void setLength(int newLength)
Changes the length by modifying the max-variable.
Definition: textrange.cpp:58
TextRange(int anchor=0, int caret=0)
Definition: textrange.h:32
int moveWhileChar(TextDocument *doc, int pos, int amount, const QString &chars)
This method charactes after the given char group When moving to the left the cursor is placed AFTER t...
Definition: textrange.cpp:110
void setCaret(int caret)
Definition: textrange.h:52