Qt 4.8
Classes | Public Functions | Private Functions | Properties | List of all members
QDeclarativeJS::TextWriter Class Reference

#include <textwriter_p.h>

Classes

struct  Move
 
struct  Replace
 

Public Functions

void move (int pos, int length, int to)
 
void replace (int pos, int length, const QString &replacement)
 
 TextWriter ()
 
void write (QString *s)
 
void write (QTextCursor *textCursor)
 

Private Functions

void doMove (const Move &move)
 
void doReplace (const Replace &replace)
 
bool hasMoveInto (int pos, int length)
 
bool hasOverlap (int pos, int length)
 
void write_helper ()
 

Properties

QTextCursorcursor
 
QList< MovemoveList
 
QList< ReplacereplaceList
 
QStringstring
 

Detailed Description

Definition at line 56 of file textwriter_p.h.

Constructors and Destructors

◆ TextWriter()

TextWriter::TextWriter ( )

Definition at line 48 of file textwriter.cpp.

49  :string(0), cursor(0)
50 {
51 }

Functions

◆ doMove()

void TextWriter::doMove ( const Move move)
private

Definition at line 149 of file textwriter.cpp.

Referenced by write_helper().

150 {
151  QString text;
152  if (string) {
153  text = string->mid(move.pos, move.length);
154  } else if (cursor) {
155  cursor->setPosition(move.pos);
157  text = cursor->selectedText();
158  }
159 
160  Replace cut;
161  cut.pos = move.pos;
162  cut.length = move.length;
163  Replace paste;
164  paste.pos = move.to;
165  paste.length = 0;
166  paste.replacement = text;
167 
168  replaceList.append(cut);
169  replaceList.append(paste);
170 
171  Replace cmd;
172  while (!replaceList.isEmpty()) {
173  cmd = replaceList.first();
174  replaceList.removeFirst();
175  doReplace(cmd);
176  }
177 }
void doReplace(const Replace &replace)
Definition: textwriter.cpp:113
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString selectedText() const
Returns the current selection&#39;s text (which may be empty).
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QList< Replace > replaceList
Definition: textwriter_p.h:67
void move(int pos, int length, int to)
Definition: textwriter.cpp:102
void setPosition(int pos, MoveMode mode=MoveAnchor)
Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified...
#define text
Definition: qobjectdefs.h:80

◆ doReplace()

void TextWriter::doReplace ( const Replace replace)
private

Definition at line 113 of file textwriter.cpp.

Referenced by doMove(), and write_helper().

114 {
115  int diff = replace.replacement.size() - replace.length;
116  {
117  QMutableListIterator<Replace> i(replaceList);
118  while (i.hasNext()) {
119  Replace &c = i.next();
120  if (replace.pos < c.pos)
121  c.pos += diff;
122  else if (replace.pos + replace.length < c.pos + c.length)
123  c.length += diff;
124  }
125  }
126  {
127  QMutableListIterator<Move> i(moveList);
128  while (i.hasNext()) {
129  Move &c = i.next();
130  if (replace.pos < c.pos)
131  c.pos += diff;
132  else if (replace.pos + replace.length < c.pos + c.length)
133  c.length += diff;
134 
135  if (replace.pos < c.to)
136  c.to += diff;
137  }
138  }
139 
140  if (string) {
141  string->replace(replace.pos, replace.length, replace.replacement);
142  } else if (cursor) {
143  cursor->setPosition(replace.pos);
145  cursor->insertText(replace.replacement);
146  }
147 }
unsigned char c[8]
Definition: qnumeric_p.h:62
void insertText(const QString &text)
Inserts text at the current position, using the current character format.
void replace(int pos, int length, const QString &replacement)
Definition: textwriter.cpp:90
static Bigint * diff(Bigint *a, Bigint *b)
QList< Replace > replaceList
Definition: textwriter_p.h:67
void setPosition(int pos, MoveMode mode=MoveAnchor)
Moves the cursor to the absolute position in the document specified by pos using a MoveMode specified...

◆ hasMoveInto()

bool TextWriter::hasMoveInto ( int  pos,
int  length 
)
private

Definition at line 79 of file textwriter.cpp.

Referenced by replace().

80 {
81  QListIterator<Move> i(moveList);
82  while (i.hasNext()) {
83  const Move &cmd = i.next();
84  if (cmd.to >= pos && cmd.to < pos + length)
85  return true;
86  }
87  return false;
88 }

◆ hasOverlap()

bool TextWriter::hasOverlap ( int  pos,
int  length 
)
private

Definition at line 58 of file textwriter.cpp.

Referenced by move(), and replace().

59 {
60  {
61  QListIterator<Replace> i(replaceList);
62  while (i.hasNext()) {
63  const Replace &cmd = i.next();
64  if (overlaps(pos, length, cmd.pos, cmd.length))
65  return true;
66  }
67  }
68  {
69  QListIterator<Move> i(moveList);
70  while (i.hasNext()) {
71  const Move &cmd = i.next();
72  if (overlaps(pos, length, cmd.pos, cmd.length))
73  return true;
74  }
75  return false;
76  }
77 }
QList< Replace > replaceList
Definition: textwriter_p.h:67
static bool overlaps(int posA, int lengthA, int posB, int lengthB)
Definition: textwriter.cpp:53

◆ move()

void TextWriter::move ( int  pos,
int  length,
int  to 
)

Definition at line 102 of file textwriter.cpp.

103 {
104  Q_ASSERT(!hasOverlap(pos, length));
105 
106  Move cmd;
107  cmd.pos = pos;
108  cmd.length = length;
109  cmd.to = to;
110  moveList += cmd;
111 }
bool hasOverlap(int pos, int length)
Definition: textwriter.cpp:58
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ replace()

void TextWriter::replace ( int  pos,
int  length,
const QString replacement 
)

Definition at line 90 of file textwriter.cpp.

91 {
92  Q_ASSERT(!hasOverlap(pos, length));
93  Q_ASSERT(!hasMoveInto(pos, length));
94 
95  Replace cmd;
96  cmd.pos = pos;
97  cmd.length = length;
98  cmd.replacement = replacement;
99  replaceList += cmd;
100 }
bool hasMoveInto(int pos, int length)
Definition: textwriter.cpp:79
bool hasOverlap(int pos, int length)
Definition: textwriter.cpp:58
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QList< Replace > replaceList
Definition: textwriter_p.h:67

◆ write() [1/2]

void TextWriter::write ( QString s)

Definition at line 179 of file textwriter.cpp.

Referenced by QDeclarativeRewrite::RewriteBinding::operator()(), and QDeclarativeRewrite::RewriteBinding::rewrite().

180 {
181  string = s;
182  write_helper();
183  string = 0;
184 }

◆ write() [2/2]

void TextWriter::write ( QTextCursor textCursor)

Definition at line 186 of file textwriter.cpp.

187 {
188  cursor = textCursor;
189  write_helper();
190  cursor = 0;
191 }

◆ write_helper()

void TextWriter::write_helper ( )
private

Definition at line 193 of file textwriter.cpp.

Referenced by write().

194 {
195  if (cursor)
197  {
198  Replace cmd;
199  while (!replaceList.isEmpty()) {
200  cmd = replaceList.first();
201  replaceList.removeFirst();
202  doReplace(cmd);
203  }
204  }
205  {
206  Move cmd;
207  while (!moveList.isEmpty()) {
208  cmd = moveList.first();
209  moveList.removeFirst();
210  doMove(cmd);
211  }
212  }
213  if (cursor)
214  cursor->endEditBlock();
215 }
void endEditBlock()
Indicates the end of a block of editing operations on the document that should appear as a single ope...
void doMove(const Move &move)
Definition: textwriter.cpp:149
void doReplace(const Replace &replace)
Definition: textwriter.cpp:113
void beginEditBlock()
Indicates the start of a block of editing operations on the document that should appear as a single o...
QList< Replace > replaceList
Definition: textwriter_p.h:67

Properties

◆ cursor

QTextCursor* QDeclarativeJS::TextWriter::cursor
private

Definition at line 59 of file textwriter_p.h.

Referenced by doMove(), doReplace(), write(), and write_helper().

◆ moveList

QList<Move> QDeclarativeJS::TextWriter::moveList
private

Definition at line 75 of file textwriter_p.h.

Referenced by doReplace(), hasMoveInto(), hasOverlap(), move(), and write_helper().

◆ replaceList

QList<Replace> QDeclarativeJS::TextWriter::replaceList
private

Definition at line 67 of file textwriter_p.h.

Referenced by doMove(), doReplace(), hasOverlap(), replace(), and write_helper().

◆ string

QString* QDeclarativeJS::TextWriter::string
private

Definition at line 58 of file textwriter_p.h.


The documentation for this class was generated from the following files: