179void QTextCursorPrivate::adjustCursor(QTextCursor::MoveOperation m)
181 adjusted_anchor = anchor;
182 if (position == anchor)
185 QTextFrame *f_position = priv->frameAt(position);
186 QTextFrame *f_anchor = priv->frameAt(adjusted_anchor);
188 if (f_position != f_anchor) {
190 QList<QTextFrame *> positionChain;
191 QList<QTextFrame *> anchorChain;
192 QTextFrame *f = f_position;
194 positionChain.prepend(f);
195 f = f->parentFrame();
199 anchorChain.prepend(f);
200 f = f->parentFrame();
202 Q_ASSERT(positionChain.at(0) == anchorChain.at(0));
204 int l = qMin(positionChain.size(), anchorChain.size());
206 if (positionChain.at(i) != anchorChain.at(i))
210 if (m <= QTextCursor::WordLeft) {
211 if (i < positionChain.size())
212 position = positionChain.at(i)->firstPosition() - 1;
214 if (i < positionChain.size())
215 position = positionChain.at(i)->lastPosition() + 1;
217 if (position < adjusted_anchor) {
218 if (i < anchorChain.size())
219 adjusted_anchor = anchorChain.at(i)->lastPosition() + 1;
221 if (i < anchorChain.size())
222 adjusted_anchor = anchorChain.at(i)->firstPosition() - 1;
225 f_position = positionChain.at(i-1);
229 QTextTable *table = qobject_cast<QTextTable *>(f_position);
233 QTextTableCell c_position = table->cellAt(position);
234 QTextTableCell c_anchor = table->cellAt(adjusted_anchor);
235 if (c_position != c_anchor) {
236 position = c_position.firstPosition();
237 if (position < adjusted_anchor)
238 adjusted_anchor = c_anchor.lastPosition();
240 adjusted_anchor = c_anchor.firstPosition();
242 currentCharFormat = -1;
245void QTextCursorPrivate::aboutToRemoveCell(
int from,
int to)
247 Q_ASSERT(from <= to);
248 if (position == anchor)
251 QTextTable *t = qobject_cast<QTextTable *>(priv->frameAt(position));
254 QTextTableCell removedCellFrom = t->cellAt(from);
255 QTextTableCell removedCellEnd = t->cellAt(to);
256 if (! removedCellFrom.isValid() || !removedCellEnd.isValid())
259 int curFrom = position;
260 int curTo = adjusted_anchor;
262 qSwap(curFrom, curTo);
264 QTextTableCell cellStart = t->cellAt(curFrom);
265 QTextTableCell cellEnd = t->cellAt(curTo);
267 if (cellStart.row() >= removedCellFrom.row() && cellEnd.row() <= removedCellEnd.row()
268 && cellStart.column() >= removedCellFrom.column()
269 && cellEnd.column() <= removedCellEnd.column()) {
272 if (removedCellFrom.row() == 0 && removedCellEnd.row() == t->rows()-1)
273 cell = t->cellAt(cellStart.row(), removedCellEnd.column()+1);
274 else if (removedCellFrom.column() == 0 && removedCellEnd.column() == t->columns()-1)
275 cell = t->cellAt(removedCellEnd.row() + 1, cellStart.column());
279 newPosition = cell.firstPosition();
281 newPosition = t->lastPosition()+1;
283 setPosition(newPosition);
284 anchor = newPosition;
285 adjusted_anchor = newPosition;
288 else if (cellStart.row() >= removedCellFrom.row() && cellStart.row() <= removedCellEnd.row()
289 && cellEnd.row() > removedCellEnd.row()) {
290 int newPosition = t->cellAt(removedCellEnd.row() + 1, cellStart.column()).firstPosition();
291 if (position < anchor)
292 position = newPosition;
294 anchor = adjusted_anchor = newPosition;
296 else if (cellStart.column() >= removedCellFrom.column() && cellStart.column() <= removedCellEnd.column()
297 && cellEnd.column() > removedCellEnd.column()) {
298 int newPosition = t->cellAt(cellStart.row(), removedCellEnd.column()+1).firstPosition();
299 if (position < anchor)
300 position = newPosition;
302 anchor = adjusted_anchor = newPosition;
306bool QTextCursorPrivate::movePosition(QTextCursor::MoveOperation op, QTextCursor::MoveMode mode)
308 currentCharFormat = -1;
310 QTextBlock blockIt = block();
311 bool visualMovement = priv->defaultCursorMoveStyle == Qt::VisualMoveStyle;
313 if (!blockIt.isValid())
316 const QTextLayout *layout = blockLayout(blockIt);
318 Qt::LayoutDirection blockDirection = layout->textOption().textDirection();
319 if (blockDirection == Qt::LayoutDirectionAuto)
320 blockDirection = blockIt.textDirection();
322 if (blockDirection == Qt::RightToLeft) {
323 if (op == QTextCursor::WordLeft)
324 op = QTextCursor::NextWord;
325 else if (op == QTextCursor::WordRight)
326 op = QTextCursor::PreviousWord;
328 if (!visualMovement) {
329 if (op == QTextCursor::Left)
330 op = QTextCursor::NextCharacter;
331 else if (op == QTextCursor::Right)
332 op = QTextCursor::PreviousCharacter;
336 int relativePos = position - blockIt.position();
338 if (!priv->isInEditBlock())
339 line = layout->lineForTextPosition(relativePos);
341 Q_ASSERT(priv->frameAt(position) == priv->frameAt(adjusted_anchor));
343 int newPosition = position;
345 if (mode == QTextCursor::KeepAnchor && complexSelectionTable() !=
nullptr) {
346 if ((op >= QTextCursor::EndOfLine && op <= QTextCursor::NextWord)
347 || (op >= QTextCursor::Right && op <= QTextCursor::WordRight)) {
348 QTextTable *t = qobject_cast<QTextTable *>(priv->frameAt(position));
350 QTextTableCell cell_pos = t->cellAt(position);
351 if (cell_pos.column() + cell_pos.columnSpan() != t->columns())
352 op = QTextCursor::NextCell;
356 if (x == -1 && !priv->isInEditBlock() && (op == QTextCursor::Up || op == QTextCursor::Down))
360 case QTextCursor::NoMove:
363 case QTextCursor::Start:
366 case QTextCursor::StartOfLine: {
367 newPosition = blockIt.position();
369 newPosition += line.textStart();
373 case QTextCursor::StartOfBlock: {
374 newPosition = blockIt.position();
377 case QTextCursor::PreviousBlock: {
378 if (blockIt == priv->blocksBegin())
380 blockIt = blockIt.previous();
382 newPosition = blockIt.position();
385 case QTextCursor::PreviousCharacter:
386 if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor)
387 newPosition = qMin(position, adjusted_anchor);
389 newPosition = priv->previousCursorPosition(position, QTextLayout::SkipCharacters);
391 case QTextCursor::Left:
392 if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor)
393 newPosition = visualMovement ? qMax(position, adjusted_anchor)
394 : qMin(position, adjusted_anchor);
396 newPosition = visualMovement ? priv->leftCursorPosition(position)
397 : priv->previousCursorPosition(position, QTextLayout::SkipCharacters);
399 case QTextCursor::StartOfWord: {
400 if (relativePos == 0)
404 QTextEngine *engine = layout->engine();
405 const QCharAttributes *attributes = engine->attributes();
406 if ((relativePos == blockIt.length() - 1)
407 && (attributes[relativePos - 1].whiteSpace || engine->atWordSeparator(relativePos - 1)))
410 if (relativePos < blockIt.length()-1)
415 case QTextCursor::PreviousWord:
416 case QTextCursor::WordLeft:
417 newPosition = priv->previousCursorPosition(position, QTextLayout::SkipWords);
419 case QTextCursor::Up: {
420 int i = line.lineNumber() - 1;
422 if (blockIt == priv->blocksBegin())
424 int blockPosition = blockIt.position();
425 QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(blockPosition));
427 QTextTableCell cell = table->cellAt(blockPosition);
428 if (cell.firstPosition() == blockPosition) {
429 int row = cell.row() - 1;
431 blockPosition = table->cellAt(row, cell.column()).lastPosition();
434 blockPosition = table->firstPosition() - 1;
436 blockIt = priv->blocksFind(blockPosition);
438 blockIt = blockIt.previous();
441 blockIt = blockIt.previous();
443 layout = blockLayout(blockIt);
444 i = layout->lineCount()-1;
446 if (layout->lineCount()) {
447 QTextLine line = layout->lineAt(i);
448 newPosition = line.xToCursor(x) + blockIt.position();
450 newPosition = blockIt.position();
456 case QTextCursor::End:
457 newPosition = priv->length() - 1;
459 case QTextCursor::EndOfLine: {
460 if (!line.isValid() || line.textLength() == 0) {
461 if (blockIt.length() >= 1)
463 newPosition = blockIt.position() + blockIt.length() - 1;
466 newPosition = blockIt.position() + line.textStart() + line.textLength();
467 if (newPosition >= priv->length())
468 newPosition = priv->length() - 1;
469 if (line.lineNumber() < layout->lineCount() - 1) {
470 const QString text = blockIt.text();
473 if (text.at(line.textStart() + line.textLength() - 1).isSpace())
478 case QTextCursor::EndOfWord: {
479 QTextEngine *engine = layout->engine();
480 const QCharAttributes *attributes = engine->attributes();
481 const int len = blockIt.length() - 1;
482 if (relativePos >= len)
484 if (engine->atWordSeparator(relativePos)) {
486 while (relativePos < len && engine->atWordSeparator(relativePos))
489 while (relativePos < len && !attributes[relativePos].whiteSpace && !engine->atWordSeparator(relativePos))
492 newPosition = blockIt.position() + relativePos;
495 case QTextCursor::EndOfBlock:
496 if (blockIt.length() >= 1)
498 newPosition = blockIt.position() + blockIt.length() - 1;
500 case QTextCursor::NextBlock: {
501 blockIt = blockIt.next();
502 if (!blockIt.isValid())
505 newPosition = blockIt.position();
508 case QTextCursor::NextCharacter:
509 if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor)
510 newPosition = qMax(position, adjusted_anchor);
512 newPosition = priv->nextCursorPosition(position, QTextLayout::SkipCharacters);
514 case QTextCursor::Right:
515 if (mode == QTextCursor::MoveAnchor && position != adjusted_anchor)
516 newPosition = visualMovement ? qMin(position, adjusted_anchor)
517 : qMax(position, adjusted_anchor);
519 newPosition = visualMovement ? priv->rightCursorPosition(position)
520 : priv->nextCursorPosition(position, QTextLayout::SkipCharacters);
522 case QTextCursor::NextWord:
523 case QTextCursor::WordRight:
524 newPosition = priv->nextCursorPosition(position, QTextLayout::SkipWords);
527 case QTextCursor::Down: {
528 int i = line.lineNumber() + 1;
530 if (i >= layout->lineCount()) {
531 int blockPosition = blockIt.position() + blockIt.length() - 1;
532 QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(blockPosition));
534 QTextTableCell cell = table->cellAt(blockPosition);
535 if (cell.lastPosition() == blockPosition) {
536 int row = cell.row() + cell.rowSpan();
537 if (row < table->rows()) {
538 blockPosition = table->cellAt(row, cell.column()).firstPosition();
541 blockPosition = table->lastPosition() + 1;
543 blockIt = priv->blocksFind(blockPosition);
545 blockIt = blockIt.next();
548 blockIt = blockIt.next();
551 if (blockIt == priv->blocksEnd())
553 layout = blockLayout(blockIt);
556 if (layout->lineCount()) {
557 QTextLine line = layout->lineAt(i);
558 newPosition = line.xToCursor(x) + blockIt.position();
560 newPosition = blockIt.position();
565 case QTextCursor::NextCell:
566 case QTextCursor::PreviousCell:
567 case QTextCursor::NextRow:
568 case QTextCursor::PreviousRow: {
569 QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(position));
573 QTextTableCell cell = table->cellAt(position);
574 Q_ASSERT(cell.isValid());
575 int column = cell.column();
576 int row = cell.row();
577 const int currentRow = row;
578 if (op == QTextCursor::NextCell || op == QTextCursor::NextRow) {
580 column += cell.columnSpan();
581 if (column >= table->columns()) {
585 cell = table->cellAt(row, column);
587 }
while (cell.isValid()
588 && ((op == QTextCursor::NextRow && currentRow == cell.row())
589 || cell.row() < row));
591 else if (op == QTextCursor::PreviousCell || op == QTextCursor::PreviousRow) {
595 column = table->columns()-1;
598 cell = table->cellAt(row, column);
600 }
while (cell.isValid()
601 && ((op == QTextCursor::PreviousRow && currentRow == cell.row())
602 || cell.row() < row));
605 newPosition = cell.firstPosition();
610 if (mode == QTextCursor::KeepAnchor) {
611 QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(position));
612 if (table && ((op >= QTextCursor::PreviousBlock && op <= QTextCursor::WordLeft)
613 || (op >= QTextCursor::NextBlock && op <= QTextCursor::WordRight))) {
614 int oldColumn = table->cellAt(position).column();
616 const QTextTableCell otherCell = table->cellAt(newPosition);
617 if (!otherCell.isValid())
620 int newColumn = otherCell.column();
621 if ((oldColumn > newColumn && op >= QTextCursor::End)
622 || (oldColumn < newColumn && op <= QTextCursor::WordLeft))
627 const bool moved = setPosition(newPosition);
629 if (mode == QTextCursor::MoveAnchor) {
631 adjusted_anchor = position;