Qt 4.8
Public Functions | Private Functions | Properties | List of all members
QTextCopyHelper Class Reference

#include <qtextdocumentfragment_p.h>

Public Functions

void copy ()
 
 QTextCopyHelper (const QTextCursor &_source, const QTextCursor &_destination, bool forceCharFormat=false, const QTextCharFormat &fmt=QTextCharFormat())
 

Private Functions

int appendFragment (int pos, int endPos, int objectIndex=-1)
 
void appendFragments (int pos, int endPos)
 
QTextFormat convertFormat (const QTextFormat &fmt)
 
int convertFormatIndex (const QTextFormat &oldFormat, int objectIndexToSet=-1)
 
int convertFormatIndex (int oldFormatIndex, int objectIndexToSet=-1)
 

Properties

QTextCursor cursor
 
QTextDocumentPrivatedst
 
bool forceCharFormat
 
QTextFormatCollectionformatCollection
 
int insertPos
 
QMap< int, int > objectIndexMap
 
const QString originalText
 
int primaryCharFormatIndex
 
QTextDocumentPrivatesrc
 

Detailed Description

Definition at line 71 of file qtextdocumentfragment_p.h.

Constructors and Destructors

◆ QTextCopyHelper()

QTextCopyHelper::QTextCopyHelper ( const QTextCursor _source,
const QTextCursor _destination,
bool  forceCharFormat = false,
const QTextCharFormat fmt = QTextCharFormat() 
)

Definition at line 55 of file qtextdocumentfragment.cpp.

57  : formatCollection(*_destination.d->priv->formatCollection()), originalText((const QString)_source.d->priv->buffer())
58 #else
59  : formatCollection(*_destination.d->priv->formatCollection()), originalText(_source.d->priv->buffer())
60 #endif
61 {
62  src = _source.d->priv;
63  dst = _destination.d->priv;
64  insertPos = _destination.position();
67  cursor = _source;
68 }
QTextFormatCollection & formatCollection
QTextDocumentPrivate * dst
The QString class provides a Unicode character string.
Definition: qstring.h:83
QSharedDataPointer< QTextCursorPrivate > d
Definition: qtextcursor.h:230
const QString originalText
QTextFormatCollection * formatCollection()
int convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet=-1)
int position() const
Returns the absolute position of the cursor within the document.
QString buffer() const
QTextDocumentPrivate * priv
QTextDocumentPrivate * src

Functions

◆ appendFragment()

int QTextCopyHelper::appendFragment ( int  pos,
int  endPos,
int  objectIndex = -1 
)
private

Definition at line 90 of file qtextdocumentfragment.cpp.

Referenced by appendFragments(), and copy().

91 {
93  const QTextFragmentData * const frag = fragIt.value();
94 
95  Q_ASSERT(objectIndex == -1
96  || (frag->size_array[0] == 1 && src->formatCollection()->format(frag->format).objectIndex() != -1));
97 
98  int charFormatIndex;
99  if (forceCharFormat)
100  charFormatIndex = primaryCharFormatIndex;
101  else
102  charFormatIndex = convertFormatIndex(frag->format, objectIndex);
103 
104  const int inFragmentOffset = qMax(0, pos - fragIt.position());
105  int charsToCopy = qMin(int(frag->size_array[0] - inFragmentOffset), endPos - pos);
106 
107  QTextBlock nextBlock = src->blocksFind(pos + 1);
108 
109  int blockIdx = -2;
110  if (nextBlock.position() == pos + 1) {
111  blockIdx = convertFormatIndex(nextBlock.blockFormat());
112  } else if (pos == 0 && insertPos == 0) {
114  dst->setCharFormat(-1, 1, convertFormat(src->blocksBegin().charFormat()).toCharFormat());
115  }
116 
117  QString txtToInsert(originalText.constData() + frag->stringPosition + inFragmentOffset, charsToCopy);
118  if (txtToInsert.length() == 1
119  && (txtToInsert.at(0) == QChar::ParagraphSeparator
120  || txtToInsert.at(0) == QTextBeginningOfFrame
121  || txtToInsert.at(0) == QTextEndOfFrame
122  )
123  ) {
124  dst->insertBlock(txtToInsert.at(0), insertPos, blockIdx, charFormatIndex);
125  ++insertPos;
126  } else {
127  if (nextBlock.textList()) {
128  QTextBlock dstBlock = dst->blocksFind(insertPos);
129  if (!dstBlock.textList()) {
130  // insert a new text block with the block and char format from the
131  // source block to make sure that the following text fragments
132  // end up in a list as they should
133  int listBlockFormatIndex = convertFormatIndex(nextBlock.blockFormat());
134  int listCharFormatIndex = convertFormatIndex(nextBlock.charFormat());
135  dst->insertBlock(insertPos, listBlockFormatIndex, listCharFormatIndex);
136  ++insertPos;
137  }
138  }
139  dst->insert(insertPos, txtToInsert, charFormatIndex);
140  const int userState = nextBlock.userState();
141  if (userState != -1)
142  dst->blocksFind(insertPos).setUserState(userState);
143  insertPos += txtToInsert.length();
144  }
145 
146  return charsToCopy;
147 }
int objectIndex() const
Returns the index of the format object, or -1 if the format object is invalid.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QTextList * textList() const
If the block represents a list item, returns the list that the item belongs to; otherwise returns 0...
void setUserState(int state)
Stores the specified state integer value in the text block.
QTextDocumentPrivate * dst
#define QTextBeginningOfFrame
void setBlockFormat(const QTextBlock &from, const QTextBlock &to, const QTextBlockFormat &newFormat, FormatChangeMode mode=SetFormat)
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTextFormat format(int idx) const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
void insert(int pos, const QString &text, int format)
FragmentMap::ConstIterator FragmentIterator
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
const QString originalText
int position() const
Returns the index of the block&#39;s first character within the document.
FragmentIterator find(int pos) const
QTextBlock blocksBegin() const
QTextFormatCollection * formatCollection()
The QTextBlock class provides a container for text fragments in a QTextDocument.
Definition: qtextobject.h:199
int convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet=-1)
QTextCharFormat charFormat() const
Returns the QTextCharFormat that describes the block&#39;s character format.
int userState() const
Returns the integer value previously set with setUserState() or -1.
quint32 size_array[N]
QTextBlock blocksFind(int pos) const
QTextFormat convertFormat(const QTextFormat &fmt)
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
QTextDocumentPrivate * src
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
#define QTextEndOfFrame

◆ appendFragments()

void QTextCopyHelper::appendFragments ( int  pos,
int  endPos 
)
private

Definition at line 149 of file qtextdocumentfragment.cpp.

Referenced by copy().

150 {
151  Q_ASSERT(pos < endPos);
152 
153  while (pos < endPos)
154  pos += appendFragment(pos, endPos);
155 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int appendFragment(int pos, int endPos, int objectIndex=-1)

◆ convertFormat()

QTextFormat QTextCopyHelper::convertFormat ( const QTextFormat fmt)
inlineprivate

Definition at line 84 of file qtextdocumentfragment_p.h.

Referenced by appendFragment().

85  { return dst->formatCollection()->format(convertFormatIndex(fmt)); }
QTextDocumentPrivate * dst
QTextFormat format(int idx) const
QTextFormatCollection * formatCollection()
int convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet=-1)

◆ convertFormatIndex() [1/2]

int QTextCopyHelper::convertFormatIndex ( const QTextFormat oldFormat,
int  objectIndexToSet = -1 
)
private

Definition at line 70 of file qtextdocumentfragment.cpp.

Referenced by appendFragment(), convertFormat(), convertFormatIndex(), and copy().

71 {
72  QTextFormat fmt = oldFormat;
73  if (objectIndexToSet != -1) {
74  fmt.setObjectIndex(objectIndexToSet);
75  } else if (fmt.objectIndex() != -1) {
76  int newObjectIndex = objectIndexMap.value(fmt.objectIndex(), -1);
77  if (newObjectIndex == -1) {
79  Q_ASSERT(objFormat.objectIndex() == -1);
80  newObjectIndex = formatCollection.createObjectIndex(objFormat);
81  objectIndexMap.insert(fmt.objectIndex(), newObjectIndex);
82  }
83  fmt.setObjectIndex(newObjectIndex);
84  }
85  int idx = formatCollection.indexForFormat(fmt);
86  Q_ASSERT(formatCollection.format(idx).type() == oldFormat.type());
87  return idx;
88 }
int objectIndex() const
Returns the index of the format object, or -1 if the format object is invalid.
QMap< int, int > objectIndexMap
QTextFormatCollection & formatCollection
int createObjectIndex(const QTextFormat &f)
QTextFormat format(int idx) const
QTextFormat objectFormat(int objectIndex) const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int type() const
Returns the type of this format.
The QTextFormat class provides formatting information for a QTextDocument.
Definition: qtextformat.h:129
QTextFormatCollection * formatCollection()
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
void setObjectIndex(int object)
Sets the format object&#39;s object index.
int indexForFormat(const QTextFormat &f)
QTextDocumentPrivate * src

◆ convertFormatIndex() [2/2]

int QTextCopyHelper::convertFormatIndex ( int  oldFormatIndex,
int  objectIndexToSet = -1 
)
inlineprivate

Definition at line 82 of file qtextdocumentfragment_p.h.

83  { return convertFormatIndex(src->formatCollection()->format(oldFormatIndex), objectIndexToSet); }
QTextFormat format(int idx) const
QTextFormatCollection * formatCollection()
int convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet=-1)
QTextDocumentPrivate * src

◆ copy()

void QTextCopyHelper::copy ( )

Definition at line 157 of file qtextdocumentfragment.cpp.

Referenced by QTextDocumentFragmentPrivate::QTextDocumentFragmentPrivate().

158 {
159  if (cursor.hasComplexSelection()) {
160  QTextTable *table = cursor.currentTable();
161  int row_start, col_start, num_rows, num_cols;
162  cursor.selectedTableCells(&row_start, &num_rows, &col_start, &num_cols);
163 
164  QTextTableFormat tableFormat = table->format();
165  tableFormat.setColumns(num_cols);
166  tableFormat.clearColumnWidthConstraints();
167  const int objectIndex = dst->formatCollection()->createObjectIndex(tableFormat);
168 
169  Q_ASSERT(row_start != -1);
170  for (int r = row_start; r < row_start + num_rows; ++r) {
171  for (int c = col_start; c < col_start + num_cols; ++c) {
172  QTextTableCell cell = table->cellAt(r, c);
173  const int rspan = cell.rowSpan();
174  const int cspan = cell.columnSpan();
175  if (rspan != 1) {
176  int cr = cell.row();
177  if (cr != r)
178  continue;
179  }
180  if (cspan != 1) {
181  int cc = cell.column();
182  if (cc != c)
183  continue;
184  }
185 
186  // add the QTextBeginningOfFrame
187  QTextCharFormat cellFormat = cell.format();
188  if (r + rspan >= row_start + num_rows) {
189  cellFormat.setTableCellRowSpan(row_start + num_rows - r);
190  }
191  if (c + cspan >= col_start + num_cols) {
192  cellFormat.setTableCellColumnSpan(col_start + num_cols - c);
193  }
194  const int charFormatIndex = convertFormatIndex(cellFormat, objectIndex);
195 
196  int blockIdx = -2;
197  const int cellPos = cell.firstPosition();
198  QTextBlock block = src->blocksFind(cellPos);
199  if (block.position() == cellPos) {
200  blockIdx = convertFormatIndex(block.blockFormat());
201  }
202 
203  dst->insertBlock(QTextBeginningOfFrame, insertPos, blockIdx, charFormatIndex);
204  ++insertPos;
205 
206  // nothing to add for empty cells
207  if (cell.lastPosition() > cellPos) {
208  // add the contents
209  appendFragments(cellPos, cell.lastPosition());
210  }
211  }
212  }
213 
214  // add end of table
215  int end = table->lastPosition();
216  appendFragment(end, end+1, objectIndex);
217  } else {
219  }
220 }
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
void appendFragments(int pos, int endPos)
int selectionEnd() const
Returns the end of the selection or position() if the cursor doesn&#39;t have a selection.
int createObjectIndex(const QTextFormat &f)
QTextDocumentPrivate * dst
#define QTextBeginningOfFrame
void setTableCellRowSpan(int tableCellRowSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellRowSpa...
Definition: qtextformat.h:529
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
int rowSpan() const
Returns the number of rows this cell spans.
Definition: qtexttable.cpp:218
int position() const
Returns the index of the block&#39;s first character within the document.
void setTableCellColumnSpan(int tableCellColumnSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellColumn...
Definition: qtextformat.h:537
void selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const
If the selection spans over table cells, firstRow is populated with the number of the first row in th...
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
QTextCharFormat format() const
Returns the cell&#39;s character format.
Definition: qtexttable.cpp:153
QTextTableFormat format() const
Returns the table&#39;s format.
Definition: qtexttable.h:133
int column() const
Returns the number of the column in the table that contains this cell.
Definition: qtexttable.cpp:201
int lastPosition() const
Returns the last document position inside the frame.
QTextTable * currentTable() const
Returns a pointer to the current table if the cursor position() is inside a block that is part of a t...
QTextFormatCollection * formatCollection()
The QTextBlock class provides a container for text fragments in a QTextDocument.
Definition: qtextobject.h:199
int convertFormatIndex(const QTextFormat &oldFormat, int objectIndexToSet=-1)
int row() const
Returns the number of the row in the table that contains this cell.
Definition: qtexttable.cpp:184
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
void setColumns(int columns)
Sets the number of columns required by the table format.
Definition: qtextformat.h:885
int firstPosition() const
Returns the first valid position in the document occupied by this cell.
Definition: qtexttable.cpp:273
The QTextTableCell class represents the properties of a cell in a QTextTable.
Definition: qtexttable.h:59
int appendFragment(int pos, int endPos, int objectIndex=-1)
bool hasComplexSelection() const
Returns true if the cursor contains a selection that is not simply a range from selectionStart() to s...
void clearColumnWidthConstraints()
Clears the column width constraints for the table.
Definition: qtextformat.h:859
int lastPosition() const
Returns the last valid position in the document occupied by this cell.
Definition: qtexttable.cpp:287
The QTextTableFormat class provides formatting information for tables in a QTextDocument.
Definition: qtextformat.h:842
QTextBlock blocksFind(int pos) const
int columnSpan() const
Returns the number of columns this cell spans.
Definition: qtexttable.cpp:228
static const KeyPair *const end
QTextDocumentPrivate * src
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
int selectionStart() const
Returns the start of the selection or position() if the cursor doesn&#39;t have a selection.

Properties

◆ cursor

QTextCursor QTextCopyHelper::cursor
private

◆ dst

QTextDocumentPrivate* QTextCopyHelper::dst
private

Definition at line 93 of file qtextdocumentfragment_p.h.

Referenced by appendFragment(), convertFormat(), and copy().

◆ forceCharFormat

bool QTextCopyHelper::forceCharFormat
private

Definition at line 89 of file qtextdocumentfragment_p.h.

◆ formatCollection

QTextFormatCollection& QTextCopyHelper::formatCollection
private

Definition at line 95 of file qtextdocumentfragment_p.h.

Referenced by convertFormatIndex().

◆ insertPos

int QTextCopyHelper::insertPos
private

Definition at line 87 of file qtextdocumentfragment_p.h.

Referenced by appendFragment(), and copy().

◆ objectIndexMap

QMap<int, int> QTextCopyHelper::objectIndexMap
private

Definition at line 97 of file qtextdocumentfragment_p.h.

Referenced by convertFormatIndex().

◆ originalText

const QString QTextCopyHelper::originalText
private

Definition at line 96 of file qtextdocumentfragment_p.h.

Referenced by appendFragment().

◆ primaryCharFormatIndex

int QTextCopyHelper::primaryCharFormatIndex
private

Definition at line 90 of file qtextdocumentfragment_p.h.

Referenced by appendFragment().

◆ src

QTextDocumentPrivate* QTextCopyHelper::src
private

Definition at line 94 of file qtextdocumentfragment_p.h.

Referenced by appendFragment(), convertFormatIndex(), and copy().


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