| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qtextcursor.h"
|
|---|
| 43 | #include "qtextcursor_p.h"
|
|---|
| 44 | #include "qglobal.h"
|
|---|
| 45 | #include "qtextdocumentfragment.h"
|
|---|
| 46 | #include "qtextdocumentfragment_p.h"
|
|---|
| 47 | #include "qtextlist.h"
|
|---|
| 48 | #include "qtexttable.h"
|
|---|
| 49 | #include "qtexttable_p.h"
|
|---|
| 50 | #include "qtextengine_p.h"
|
|---|
| 51 | #include "qabstracttextdocumentlayout.h"
|
|---|
| 52 |
|
|---|
| 53 | #include <qtextlayout.h>
|
|---|
| 54 | #include <qdebug.h>
|
|---|
| 55 |
|
|---|
| 56 | QT_BEGIN_NAMESPACE
|
|---|
| 57 |
|
|---|
| 58 | enum {
|
|---|
| 59 | AdjustPrev = 0x1,
|
|---|
| 60 | AdjustUp = 0x3,
|
|---|
| 61 | AdjustNext = 0x4,
|
|---|
| 62 | AdjustDown = 0x12
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | QTextCursorPrivate::QTextCursorPrivate(QTextDocumentPrivate *p)
|
|---|
| 66 | : priv(p), x(0), position(0), anchor(0), adjusted_anchor(0),
|
|---|
| 67 | currentCharFormat(-1), visualNavigation(false)
|
|---|
| 68 | {
|
|---|
| 69 | priv->addCursor(this);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | QTextCursorPrivate::QTextCursorPrivate(const QTextCursorPrivate &rhs)
|
|---|
| 73 | : QSharedData(rhs)
|
|---|
| 74 | {
|
|---|
| 75 | position = rhs.position;
|
|---|
| 76 | anchor = rhs.anchor;
|
|---|
| 77 | adjusted_anchor = rhs.adjusted_anchor;
|
|---|
| 78 | priv = rhs.priv;
|
|---|
| 79 | x = rhs.x;
|
|---|
| 80 | currentCharFormat = rhs.currentCharFormat;
|
|---|
| 81 | visualNavigation = rhs.visualNavigation;
|
|---|
| 82 | priv->addCursor(this);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | QTextCursorPrivate::~QTextCursorPrivate()
|
|---|
| 86 | {
|
|---|
| 87 | if (priv)
|
|---|
| 88 | priv->removeCursor(this);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | QTextCursorPrivate::AdjustResult QTextCursorPrivate::adjustPosition(int positionOfChange, int charsAddedOrRemoved, QTextUndoCommand::Operation op)
|
|---|
| 92 | {
|
|---|
| 93 | QTextCursorPrivate::AdjustResult result = QTextCursorPrivate::CursorMoved;
|
|---|
| 94 | // not(!) <= , so that inserting text adjusts the cursor correctly
|
|---|
| 95 | if (position < positionOfChange ||
|
|---|
| 96 | (position == positionOfChange && op == QTextUndoCommand::KeepCursor)) {
|
|---|
| 97 | result = CursorUnchanged;
|
|---|
| 98 | } else {
|
|---|
| 99 | if (charsAddedOrRemoved < 0 && position < positionOfChange - charsAddedOrRemoved)
|
|---|
| 100 | position = positionOfChange;
|
|---|
| 101 | else
|
|---|
| 102 | position += charsAddedOrRemoved;
|
|---|
| 103 |
|
|---|
| 104 | currentCharFormat = -1;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (anchor >= positionOfChange
|
|---|
| 108 | && (anchor != positionOfChange || op != QTextUndoCommand::KeepCursor)) {
|
|---|
| 109 | if (charsAddedOrRemoved < 0 && anchor < positionOfChange - charsAddedOrRemoved)
|
|---|
| 110 | anchor = positionOfChange;
|
|---|
| 111 | else
|
|---|
| 112 | anchor += charsAddedOrRemoved;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (adjusted_anchor >= positionOfChange
|
|---|
| 116 | && (adjusted_anchor != positionOfChange || op != QTextUndoCommand::KeepCursor)) {
|
|---|
| 117 | if (charsAddedOrRemoved < 0 && adjusted_anchor < positionOfChange - charsAddedOrRemoved)
|
|---|
| 118 | adjusted_anchor = positionOfChange;
|
|---|
| 119 | else
|
|---|
| 120 | adjusted_anchor += charsAddedOrRemoved;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | return result;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void QTextCursorPrivate::setX()
|
|---|
| 127 | {
|
|---|
| 128 | if (priv && priv->isInEditBlock()) {
|
|---|
| 129 | x = -1; // mark dirty
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | QTextBlock block = this->block();
|
|---|
| 134 | const QTextLayout *layout = blockLayout(block);
|
|---|
| 135 | int pos = position - block.position();
|
|---|
| 136 |
|
|---|
| 137 | QTextLine line = layout->lineForTextPosition(pos);
|
|---|
| 138 | if (line.isValid())
|
|---|
| 139 | x = line.cursorToX(pos);
|
|---|
| 140 | else
|
|---|
| 141 | x = -1; // delayed init. Makes movePosition() call setX later on again.
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | void QTextCursorPrivate::remove()
|
|---|
| 145 | {
|
|---|
| 146 | if (anchor == position)
|
|---|
| 147 | return;
|
|---|
| 148 | priv->beginEditBlock();
|
|---|
| 149 | currentCharFormat = -1;
|
|---|
| 150 | int pos1 = position;
|
|---|
| 151 | int pos2 = adjusted_anchor;
|
|---|
| 152 | QTextUndoCommand::Operation op = QTextUndoCommand::KeepCursor;
|
|---|
| 153 | if (pos1 > pos2) {
|
|---|
| 154 | pos1 = adjusted_anchor;
|
|---|
| 155 | pos2 = position;
|
|---|
| 156 | op = QTextUndoCommand::MoveCursor;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | // deleting inside table? -> delete only content
|
|---|
| 160 | QTextTable *table = complexSelectionTable();
|
|---|
| 161 | if (table) {
|
|---|
| 162 | int startRow, startCol, numRows, numCols;
|
|---|
| 163 | selectedTableCells(&startRow, &numRows, &startCol, &numCols);
|
|---|
| 164 | clearCells(table, startRow, startCol, numRows, numCols, op);
|
|---|
| 165 | } else {
|
|---|
| 166 | priv->remove(pos1, pos2-pos1, op);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | adjusted_anchor = anchor = position;
|
|---|
| 170 | priv->endEditBlock();
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | void QTextCursorPrivate::clearCells(QTextTable *table, int startRow, int startCol, int numRows, int numCols, QTextUndoCommand::Operation op)
|
|---|
| 174 | {
|
|---|
| 175 | priv->beginEditBlock();
|
|---|
| 176 |
|
|---|
| 177 | for (int row = startRow; row < startRow + numRows; ++row)
|
|---|
| 178 | for (int col = startCol; col < startCol + numCols; ++col) {
|
|---|
| 179 | QTextTableCell cell = table->cellAt(row, col);
|
|---|
| 180 | const int startPos = cell.firstPosition();
|
|---|
| 181 | const int endPos = cell.lastPosition();
|
|---|
| 182 | Q_ASSERT(startPos <= endPos);
|
|---|
| 183 | priv->remove(startPos, endPos - startPos, op);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | priv->endEditBlock();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | bool QTextCursorPrivate::canDelete(int pos) const
|
|---|
| 190 | {
|
|---|
| 191 | QTextDocumentPrivate::FragmentIterator fit = priv->find(pos);
|
|---|
| 192 | QTextCharFormat fmt = priv->formatCollection()->charFormat((*fit)->format);
|
|---|
| 193 | return (fmt.objectIndex() == -1 || fmt.objectType() == QTextFormat::ImageObject);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | void QTextCursorPrivate::insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat)
|
|---|
| 197 | {
|
|---|
| 198 | QTextFormatCollection *formats = priv->formatCollection();
|
|---|
| 199 | int idx = formats->indexForFormat(format);
|
|---|
| 200 | Q_ASSERT(formats->format(idx).isBlockFormat());
|
|---|
| 201 |
|
|---|
| 202 | priv->insertBlock(position, idx, formats->indexForFormat(charFormat));
|
|---|
| 203 | currentCharFormat = -1;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | void QTextCursorPrivate::adjustCursor(QTextCursor::MoveOperation m)
|
|---|
| 207 | {
|
|---|
| 208 | adjusted_anchor = anchor;
|
|---|
| 209 | if (position == anchor)
|
|---|
| 210 | return;
|
|---|
| 211 |
|
|---|
| 212 | QTextFrame *f_position = priv->frameAt(position);
|
|---|
| 213 | QTextFrame *f_anchor = priv->frameAt(adjusted_anchor);
|
|---|
| 214 |
|
|---|
| 215 | if (f_position != f_anchor) {
|
|---|
| 216 | // find common parent frame
|
|---|
| 217 | QList<QTextFrame *> positionChain;
|
|---|
| 218 | QList<QTextFrame *> anchorChain;
|
|---|
| 219 | QTextFrame *f = f_position;
|
|---|
| 220 | while (f) {
|
|---|
| 221 | positionChain.prepend(f);
|
|---|
| 222 | f = f->parentFrame();
|
|---|
| 223 | }
|
|---|
| 224 | f = f_anchor;
|
|---|
| 225 | while (f) {
|
|---|
| 226 | anchorChain.prepend(f);
|
|---|
| 227 | f = f->parentFrame();
|
|---|
| 228 | }
|
|---|
| 229 | Q_ASSERT(positionChain.at(0) == anchorChain.at(0));
|
|---|
| 230 | int i = 1;
|
|---|
| 231 | int l = qMin(positionChain.size(), anchorChain.size());
|
|---|
| 232 | for (; i < l; ++i) {
|
|---|
| 233 | if (positionChain.at(i) != anchorChain.at(i))
|
|---|
| 234 | break;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | if (m <= QTextCursor::WordLeft) {
|
|---|
| 238 | if (i < positionChain.size())
|
|---|
| 239 | position = positionChain.at(i)->firstPosition() - 1;
|
|---|
| 240 | } else {
|
|---|
| 241 | if (i < positionChain.size())
|
|---|
| 242 | position = positionChain.at(i)->lastPosition() + 1;
|
|---|
| 243 | }
|
|---|
| 244 | if (position < adjusted_anchor) {
|
|---|
| 245 | if (i < anchorChain.size())
|
|---|
| 246 | adjusted_anchor = anchorChain.at(i)->lastPosition() + 1;
|
|---|
| 247 | } else {
|
|---|
| 248 | if (i < anchorChain.size())
|
|---|
| 249 | adjusted_anchor = anchorChain.at(i)->firstPosition() - 1;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | f_position = positionChain.at(i-1);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | // same frame, either need to adjust to cell boundaries or return
|
|---|
| 256 | QTextTable *table = qobject_cast<QTextTable *>(f_position);
|
|---|
| 257 | if (!table)
|
|---|
| 258 | return;
|
|---|
| 259 |
|
|---|
| 260 | QTextTableCell c_position = table->cellAt(position);
|
|---|
| 261 | QTextTableCell c_anchor = table->cellAt(adjusted_anchor);
|
|---|
| 262 | if (c_position != c_anchor) {
|
|---|
| 263 | bool before;
|
|---|
| 264 | int col_position = c_position.column();
|
|---|
| 265 | int col_anchor = c_anchor.column();
|
|---|
| 266 | if (col_position == col_anchor) {
|
|---|
| 267 | before = c_position.row() < c_anchor.row();
|
|---|
| 268 | } else {
|
|---|
| 269 | before = col_position < col_anchor;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | // adjust to cell boundaries
|
|---|
| 273 | if (m <= QTextCursor::WordLeft) {
|
|---|
| 274 | position = c_position.firstPosition();
|
|---|
| 275 | if (!before)
|
|---|
| 276 | --position;
|
|---|
| 277 | } else {
|
|---|
| 278 | position = c_position.lastPosition();
|
|---|
| 279 | if (before)
|
|---|
| 280 | ++position;
|
|---|
| 281 | }
|
|---|
| 282 | if (position < adjusted_anchor)
|
|---|
| 283 | adjusted_anchor = c_anchor.lastPosition();
|
|---|
| 284 | else
|
|---|
| 285 | adjusted_anchor = c_anchor.firstPosition();
|
|---|
| 286 | }
|
|---|
| 287 | currentCharFormat = -1;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | void QTextCursorPrivate::aboutToRemoveCell(int from, int to)
|
|---|
| 291 | {
|
|---|
| 292 | Q_ASSERT(from <= to);
|
|---|
| 293 | if (position == anchor)
|
|---|
| 294 | return;
|
|---|
| 295 |
|
|---|
| 296 | QTextTable *t = qobject_cast<QTextTable *>(priv->frameAt(position));
|
|---|
| 297 | if (!t)
|
|---|
| 298 | return;
|
|---|
| 299 | QTextTableCell removedCellFrom = t->cellAt(from);
|
|---|
| 300 | QTextTableCell removedCellEnd = t->cellAt(to);
|
|---|
| 301 | if (! removedCellFrom.isValid() || !removedCellEnd.isValid())
|
|---|
| 302 | return;
|
|---|
| 303 |
|
|---|
| 304 | int curFrom = position;
|
|---|
| 305 | int curTo = adjusted_anchor;
|
|---|
| 306 | if (curTo < curFrom)
|
|---|
| 307 | qSwap(curFrom, curTo);
|
|---|
| 308 |
|
|---|
| 309 | QTextTableCell cellStart = t->cellAt(curFrom);
|
|---|
| 310 | QTextTableCell cellEnd = t->cellAt(curTo);
|
|---|
| 311 |
|
|---|
| 312 | if (cellStart.row() >= removedCellFrom.row() && cellEnd.row() <= removedCellEnd.row()
|
|---|
| 313 | && cellStart.column() >= removedCellFrom.column()
|
|---|
| 314 | && cellEnd.column() <= removedCellEnd.column()) { // selection is completely removed
|
|---|
| 315 | // find a new position, as close as possible to where we were.
|
|---|
| 316 | QTextTableCell cell;
|
|---|
| 317 | if (removedCellFrom.row() == 0 && removedCellEnd.row() == t->rows()-1) // removed n columns
|
|---|
| 318 | cell = t->cellAt(cellStart.row(), removedCellEnd.column()+1);
|
|---|
| 319 | else if (removedCellFrom.column() == 0 && removedCellEnd.column() == t->columns()-1) // removed n rows
|
|---|
| 320 | cell = t->cellAt(removedCellEnd.row() + 1, cellStart.column());
|
|---|
| 321 |
|
|---|
| 322 | int newPosition;
|
|---|
| 323 | if (cell.isValid())
|
|---|
| 324 | newPosition = cell.firstPosition();
|
|---|
| 325 | else
|
|---|
| 326 | newPosition = t->lastPosition()+1;
|
|---|
| 327 |
|
|---|
| 328 | setPosition(newPosition);
|
|---|
| 329 | anchor = newPosition;
|
|---|
| 330 | adjusted_anchor = newPosition;
|
|---|
| 331 | x = 0;
|
|---|
| 332 | }
|
|---|
| 333 | else if (cellStart.row() >= removedCellFrom.row() && cellStart.row() <= removedCellEnd.row()
|
|---|
| 334 | && cellEnd.row() > removedCellEnd.row()) {
|
|---|
| 335 | int newPosition = t->cellAt(removedCellEnd.row() + 1, cellStart.column()).firstPosition();
|
|---|
| 336 | if (position < anchor)
|
|---|
| 337 | position = newPosition;
|
|---|
| 338 | else
|
|---|
| 339 | anchor = adjusted_anchor = newPosition;
|
|---|
| 340 | }
|
|---|
| 341 | else if (cellStart.column() >= removedCellFrom.column() && cellStart.column() <= removedCellEnd.column()
|
|---|
| 342 | && cellEnd.column() > removedCellEnd.column()) {
|
|---|
| 343 | int newPosition = t->cellAt(cellStart.row(), removedCellEnd.column()+1).firstPosition();
|
|---|
| 344 | if (position < anchor)
|
|---|
| 345 | position = newPosition;
|
|---|
| 346 | else
|
|---|
| 347 | anchor = adjusted_anchor = newPosition;
|
|---|
| 348 | }
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | bool QTextCursorPrivate::movePosition(QTextCursor::MoveOperation op, QTextCursor::MoveMode mode)
|
|---|
| 352 | {
|
|---|
| 353 | currentCharFormat = -1;
|
|---|
| 354 | bool adjustX = true;
|
|---|
| 355 | QTextBlock blockIt = block();
|
|---|
| 356 |
|
|---|
| 357 | if (op >= QTextCursor::Left && op <= QTextCursor::WordRight
|
|---|
| 358 | && blockIt.blockFormat().layoutDirection() == Qt::RightToLeft) {
|
|---|
| 359 | if (op == QTextCursor::Left)
|
|---|
| 360 | op = QTextCursor::NextCharacter;
|
|---|
| 361 | else if (op == QTextCursor::Right)
|
|---|
| 362 | op = QTextCursor::PreviousCharacter;
|
|---|
| 363 | else if (op == QTextCursor::WordLeft)
|
|---|
| 364 | op = QTextCursor::NextWord;
|
|---|
| 365 | else if (op == QTextCursor::WordRight)
|
|---|
| 366 | op = QTextCursor::PreviousWord;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | const QTextLayout *layout = blockLayout(blockIt);
|
|---|
| 370 | int relativePos = position - blockIt.position();
|
|---|
| 371 | QTextLine line;
|
|---|
| 372 | if (!priv->isInEditBlock())
|
|---|
| 373 | line = layout->lineForTextPosition(relativePos);
|
|---|
| 374 |
|
|---|
| 375 | Q_ASSERT(priv->frameAt(position) == priv->frameAt(adjusted_anchor));
|
|---|
| 376 |
|
|---|
| 377 | int newPosition = position;
|
|---|
| 378 |
|
|---|
| 379 | if (x == -1 && !priv->isInEditBlock() && (op == QTextCursor::Up || op == QTextCursor::Down))
|
|---|
| 380 | setX();
|
|---|
| 381 |
|
|---|
| 382 | switch(op) {
|
|---|
| 383 | case QTextCursor::NoMove:
|
|---|
| 384 | return true;
|
|---|
| 385 |
|
|---|
| 386 | case QTextCursor::Start:
|
|---|
| 387 | newPosition = 0;
|
|---|
| 388 | break;
|
|---|
| 389 | case QTextCursor::StartOfLine: {
|
|---|
| 390 | newPosition = blockIt.position();
|
|---|
| 391 | if (line.isValid())
|
|---|
| 392 | newPosition += line.textStart();
|
|---|
| 393 |
|
|---|
| 394 | break;
|
|---|
| 395 | }
|
|---|
| 396 | case QTextCursor::StartOfBlock: {
|
|---|
| 397 | newPosition = blockIt.position();
|
|---|
| 398 | break;
|
|---|
| 399 | }
|
|---|
| 400 | case QTextCursor::PreviousBlock: {
|
|---|
| 401 | if (blockIt == priv->blocksBegin())
|
|---|
| 402 | return false;
|
|---|
| 403 | blockIt = blockIt.previous();
|
|---|
| 404 |
|
|---|
| 405 | newPosition = blockIt.position();
|
|---|
| 406 | break;
|
|---|
| 407 | }
|
|---|
| 408 | case QTextCursor::PreviousCharacter:
|
|---|
| 409 | case QTextCursor::Left:
|
|---|
| 410 | newPosition = priv->previousCursorPosition(position, QTextLayout::SkipCharacters);
|
|---|
| 411 | break;
|
|---|
| 412 | case QTextCursor::StartOfWord: {
|
|---|
| 413 | if (relativePos == 0)
|
|---|
| 414 | break;
|
|---|
| 415 |
|
|---|
| 416 | // skip if already at word start
|
|---|
| 417 | QTextEngine *engine = layout->engine();
|
|---|
| 418 | engine->attributes();
|
|---|
| 419 | if ((relativePos == blockIt.length() - 1)
|
|---|
| 420 | && (engine->atSpace(relativePos - 1) || engine->atWordSeparator(relativePos - 1)))
|
|---|
| 421 | return false;
|
|---|
| 422 |
|
|---|
| 423 | if (relativePos < blockIt.length()-1)
|
|---|
| 424 | ++position;
|
|---|
| 425 |
|
|---|
| 426 | // FALL THROUGH!
|
|---|
| 427 | }
|
|---|
| 428 | case QTextCursor::PreviousWord:
|
|---|
| 429 | case QTextCursor::WordLeft:
|
|---|
| 430 | newPosition = priv->previousCursorPosition(position, QTextLayout::SkipWords);
|
|---|
| 431 | break;
|
|---|
| 432 | case QTextCursor::Up: {
|
|---|
| 433 | int i = line.lineNumber() - 1;
|
|---|
| 434 | if (i == -1) {
|
|---|
| 435 | if (blockIt == priv->blocksBegin())
|
|---|
| 436 | return false;
|
|---|
| 437 | int blockPosition = blockIt.position();
|
|---|
| 438 | QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(blockPosition));
|
|---|
| 439 | if (table) {
|
|---|
| 440 | QTextTableCell cell = table->cellAt(blockPosition);
|
|---|
| 441 | if (cell.firstPosition() == blockPosition) {
|
|---|
| 442 | int row = cell.row() - 1;
|
|---|
| 443 | if (row >= 0) {
|
|---|
| 444 | blockPosition = table->cellAt(row, cell.column()).lastPosition();
|
|---|
| 445 | } else {
|
|---|
| 446 | // move to line above the table
|
|---|
| 447 | blockPosition = table->firstPosition() - 1;
|
|---|
| 448 | }
|
|---|
| 449 | blockIt = priv->blocksFind(blockPosition);
|
|---|
| 450 | } else {
|
|---|
| 451 | blockIt = blockIt.previous();
|
|---|
| 452 | }
|
|---|
| 453 | } else {
|
|---|
| 454 | blockIt = blockIt.previous();
|
|---|
| 455 | }
|
|---|
| 456 | layout = blockLayout(blockIt);
|
|---|
| 457 | i = layout->lineCount()-1;
|
|---|
| 458 | }
|
|---|
| 459 | if (layout->lineCount()) {
|
|---|
| 460 | QTextLine line = layout->lineAt(i);
|
|---|
| 461 | newPosition = line.xToCursor(x) + blockIt.position();
|
|---|
| 462 | } else {
|
|---|
| 463 | newPosition = blockIt.position();
|
|---|
| 464 | }
|
|---|
| 465 | adjustX = false;
|
|---|
| 466 | break;
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | case QTextCursor::End:
|
|---|
| 470 | newPosition = priv->length() - 1;
|
|---|
| 471 | break;
|
|---|
| 472 | case QTextCursor::EndOfLine: {
|
|---|
| 473 | if (!line.isValid() || line.textLength() == 0) {
|
|---|
| 474 | if (blockIt.length() >= 1)
|
|---|
| 475 | // position right before the block separator
|
|---|
| 476 | newPosition = blockIt.position() + blockIt.length() - 1;
|
|---|
| 477 | break;
|
|---|
| 478 | }
|
|---|
| 479 | newPosition = blockIt.position() + line.textStart() + line.textLength();
|
|---|
| 480 | if (line.lineNumber() < layout->lineCount() - 1) {
|
|---|
| 481 | const QString text = blockIt.text();
|
|---|
| 482 | // ###### this relies on spaces being the cause for linebreaks.
|
|---|
| 483 | // this doesn't work with japanese
|
|---|
| 484 | if (text.at(line.textStart() + line.textLength() - 1).isSpace())
|
|---|
| 485 | --newPosition;
|
|---|
| 486 | }
|
|---|
| 487 | break;
|
|---|
| 488 | }
|
|---|
| 489 | case QTextCursor::EndOfWord: {
|
|---|
| 490 | QTextEngine *engine = layout->engine();
|
|---|
| 491 | engine->attributes();
|
|---|
| 492 | const int len = blockIt.length() - 1;
|
|---|
| 493 | if (relativePos >= len)
|
|---|
| 494 | return false;
|
|---|
| 495 | if (engine->atWordSeparator(relativePos)) {
|
|---|
| 496 | ++relativePos;
|
|---|
| 497 | while (relativePos < len && engine->atWordSeparator(relativePos))
|
|---|
| 498 | ++relativePos;
|
|---|
| 499 | } else {
|
|---|
| 500 | while (relativePos < len && !engine->atSpace(relativePos) && !engine->atWordSeparator(relativePos))
|
|---|
| 501 | ++relativePos;
|
|---|
| 502 | }
|
|---|
| 503 | newPosition = blockIt.position() + relativePos;
|
|---|
| 504 | break;
|
|---|
| 505 | }
|
|---|
| 506 | case QTextCursor::EndOfBlock:
|
|---|
| 507 | if (blockIt.length() >= 1)
|
|---|
| 508 | // position right before the block separator
|
|---|
| 509 | newPosition = blockIt.position() + blockIt.length() - 1;
|
|---|
| 510 | break;
|
|---|
| 511 | case QTextCursor::NextBlock: {
|
|---|
| 512 | blockIt = blockIt.next();
|
|---|
| 513 | if (!blockIt.isValid())
|
|---|
| 514 | return false;
|
|---|
| 515 |
|
|---|
| 516 | newPosition = blockIt.position();
|
|---|
| 517 | break;
|
|---|
| 518 | }
|
|---|
| 519 | case QTextCursor::NextCharacter:
|
|---|
| 520 | case QTextCursor::Right:
|
|---|
| 521 | newPosition = priv->nextCursorPosition(position, QTextLayout::SkipCharacters);
|
|---|
| 522 | break;
|
|---|
| 523 | case QTextCursor::NextWord:
|
|---|
| 524 | case QTextCursor::WordRight:
|
|---|
| 525 | newPosition = priv->nextCursorPosition(position, QTextLayout::SkipWords);
|
|---|
| 526 | break;
|
|---|
| 527 |
|
|---|
| 528 | case QTextCursor::Down: {
|
|---|
| 529 | int i = line.lineNumber() + 1;
|
|---|
| 530 |
|
|---|
| 531 | if (i >= layout->lineCount()) {
|
|---|
| 532 | int blockPosition = blockIt.position() + blockIt.length() - 1;
|
|---|
| 533 | QTextTable *table = qobject_cast<QTextTable *>(priv->frameAt(blockPosition));
|
|---|
| 534 | if (table) {
|
|---|
| 535 | QTextTableCell cell = table->cellAt(blockPosition);
|
|---|
| 536 | if (cell.lastPosition() == blockPosition) {
|
|---|
| 537 | int row = cell.row() + cell.rowSpan();
|
|---|
| 538 | if (row < table->rows()) {
|
|---|
| 539 | blockPosition = table->cellAt(row, cell.column()).firstPosition();
|
|---|
| 540 | } else {
|
|---|
| 541 | // move to line below the table
|
|---|
| 542 | blockPosition = table->lastPosition() + 1;
|
|---|
| 543 | }
|
|---|
| 544 | blockIt = priv->blocksFind(blockPosition);
|
|---|
| 545 | } else {
|
|---|
| 546 | blockIt = blockIt.next();
|
|---|
| 547 | }
|
|---|
| 548 | } else {
|
|---|
| 549 | blockIt = blockIt.next();
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | if (blockIt == priv->blocksEnd())
|
|---|
| 553 | return false;
|
|---|
| 554 | layout = blockLayout(blockIt);
|
|---|
| 555 | i = 0;
|
|---|
| 556 | }
|
|---|
| 557 | if (layout->lineCount()) {
|
|---|
| 558 | QTextLine line = layout->lineAt(i);
|
|---|
| 559 | newPosition = line.xToCursor(x) + blockIt.position();
|
|---|
|
|---|