diff --git a/src/editor/dtextedit.cpp b/src/editor/dtextedit.cpp index 77ad4a688..17c65c4b7 100644 --- a/src/editor/dtextedit.cpp +++ b/src/editor/dtextedit.cpp @@ -2545,12 +2545,9 @@ void TextEdit::cut(bool ignoreCheck) data += "\n"; } //删除有选择 - for (int i = 0; i < m_altModSelections.size(); i++) { - if (m_altModSelections[i].cursor.hasSelection()) { - QUndoCommand *pDeleteStack = new DeleteTextUndoCommand(m_altModSelections[i].cursor, this); + QUndoCommand *pDeleteStack = new DeleteTextUndoCommand(m_altModSelections, this); m_pUndoStack->push(pDeleteStack); - } - } + //设置到剪切板 QClipboard *clipboard = QApplication::clipboard(); //获取系统剪贴板指针 clipboard->setText(data); @@ -3138,7 +3135,6 @@ void TextEdit::onAppPaletteChanged() for (auto &selection : m_altModSelections) { selection.format.setBackground(highlightBackground); } - // 更新高亮状态 renderAllSelections(); } @@ -6533,34 +6529,42 @@ void TextEdit::inputMethodEvent(QInputMethodEvent *e) void TextEdit::mousePressEvent(QMouseEvent *e) { - if (m_bIsFindClose) { + if (m_bIsFindClose) + { m_bIsFindClose = false; removeKeywords(); } if (e->button() != Qt::RightButton) m_isSelectAll = false; - if (Qt::MouseEventSynthesizedByQt == e->source()) { + if (Qt::MouseEventSynthesizedByQt == e->source()) + { m_startY = e->y(); m_startX = e->x(); } - if (e->source() == Qt::MouseEventSynthesizedByQt) { + if (e->source() == Qt::MouseEventSynthesizedByQt) + { m_lastTouchBeginPos = e->pos(); - if (QScroller::hasScroller(this)) { + if (QScroller::hasScroller(this)) + { QScroller::scroller(this)->deleteLater(); } - if (m_updateEnableSelectionByMouseTimer) { + if (m_updateEnableSelectionByMouseTimer) + { m_updateEnableSelectionByMouseTimer->stop(); - } else { + } + else + { m_updateEnableSelectionByMouseTimer = new QTimer(this); m_updateEnableSelectionByMouseTimer->setSingleShot(true); static QObject *theme_settings = reinterpret_cast(qvariant_cast(qApp->property("_d_theme_settings_object"))); QVariant touchFlickBeginMoveDelay; - if (theme_settings) { + if (theme_settings) + { touchFlickBeginMoveDelay = theme_settings->property("touchFlickBeginMoveDelay"); } @@ -6571,34 +6575,41 @@ void TextEdit::mousePressEvent(QMouseEvent *e) m_updateEnableSelectionByMouseTimer->start(); } - //add for single refers to the sliding - if (e->type() == QEvent::MouseButtonPress && e->source() == Qt::MouseEventSynthesizedByQt) { + // add for single refers to the sliding + if (e->type() == QEvent::MouseButtonPress && e->source() == Qt::MouseEventSynthesizedByQt) + { m_lastMouseTimeX = e->timestamp(); m_lastMouseTimeY = e->timestamp(); m_lastMouseYpos = e->pos().y(); m_lastMouseXpos = e->pos().x(); - if (tweenY.activeY()) { + if (tweenY.activeY()) + { m_slideContinueY = true; tweenY.stopY(); } - if (tweenX.activeX()) { + if (tweenX.activeX()) + { m_slideContinueX = true; tweenX.stopX(); } } - if (e->modifiers() == Qt::AltModifier) { + if (e->modifiers() == Qt::AltModifier) + { m_bIsAltMod = true; - //鼠标点击位置为光标位置  获取光标行列位置 + // 鼠标点击位置为光标位置  获取光标行列位置 QMouseEvent *mouseEvent = static_cast(e); m_altStartTextCursor = this->cursorForPosition(mouseEvent->pos()); m_altStartTextCursor.clearSelection(); this->setTextCursor(m_altStartTextCursor); m_altModSelections.clear(); - } else { - if (e->button() != 2) { //右键,调用右键菜单时候不能清空 + } + else + { + if (e->button() != 2) + { // 右键,调用右键菜单时候不能清空 m_bIsAltMod = false; m_altModSelections.clear(); } @@ -6609,13 +6620,15 @@ void TextEdit::mousePressEvent(QMouseEvent *e) void TextEdit::mouseMoveEvent(QMouseEvent *e) { - if (Qt::MouseEventSynthesizedByQt == e->source()) { + if (Qt::MouseEventSynthesizedByQt == e->source()) + { m_endY = e->y(); m_endX = e->x(); } - //add for single refers to the sliding - if (e->type() == QEvent::MouseMove && e->source() == Qt::MouseEventSynthesizedByQt) { + // add for single refers to the sliding + if (e->type() == QEvent::MouseMove && e->source() == Qt::MouseEventSynthesizedByQt) + { const ulong diffTimeX = e->timestamp() - m_lastMouseTimeX; const ulong diffTimeY = e->timestamp() - m_lastMouseTimeY; const int diffYpos = e->pos().y() - m_lastMouseYpos; @@ -6625,7 +6638,8 @@ void TextEdit::mouseMoveEvent(QMouseEvent *e) m_lastMouseYpos = e->pos().y(); m_lastMouseXpos = e->pos().x(); - if (m_gestureAction == GA_slide) { + if (m_gestureAction == GA_slide) + { QFont font = this->font(); /*开根号时数值越大衰减比例越大*/ @@ -6646,11 +6660,12 @@ void TextEdit::mouseMoveEvent(QMouseEvent *e) m_stepSpeedX /= sqrt(font.pointSize() * 4.0); changeX = m_stepSpeedX * sqrt(abs(m_stepSpeedX)) * 100; - //return true; + // return true; } - if (m_gestureAction != GA_null) { - //return true; + if (m_gestureAction != GA_null) + { + // return true; } } @@ -6658,75 +6673,108 @@ void TextEdit::mouseMoveEvent(QMouseEvent *e) // so they need to be restored. QApplication::restoreOverrideCursor(); - if (viewport()->cursor().shape() != Qt::IBeamCursor) { + if (viewport()->cursor().shape() != Qt::IBeamCursor) + { viewport()->setCursor(Qt::IBeamCursor); } QPlainTextEdit::mouseMoveEvent(e); - - if (e->modifiers() == Qt::AltModifier && m_bIsAltMod) { + if (e->modifiers() == Qt::AltModifier && m_bIsAltMod) + { m_altModSelections.clear(); QMouseEvent *mouseEvent = static_cast(e); QPoint curPos = mouseEvent->pos(); m_altEndTextCursor = this->cursorForPosition(curPos); - - int column = m_altEndTextCursor.positionInBlock(); int row = m_altEndTextCursor.blockNumber(); - - int startColumn = m_altStartTextCursor.positionInBlock(); int startRow = m_altStartTextCursor.blockNumber(); - - int minColumn = startColumn < column ? startColumn : column; - int maxColumn = startColumn > column ? startColumn : column; int minRow = startRow < row ? startRow : row; int maxRow = startRow > row ? startRow : row; - QTextCharFormat format; QPalette palette; QColor highlightBackground = DGuiApplicationHelper::instance()->applicationPalette().color(QPalette::Highlight); format.setBackground(highlightBackground); format.setForeground(palette.highlightedText()); - - for (int iRow = minRow; iRow <= maxRow; iRow++) { - QTextBlock block = document()->findBlockByNumber(iRow); - QTextCursor cursor(block); - cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor); - - QPoint blockTailPos = this->cursorRect(cursor).bottomRight(); - - //位置从0开始 - int length = block.text().length(); - - //鼠标x坐标大于当前块最后字符位置 遍历获取最大块长度 - if (curPos.x() >= blockTailPos.x() && length > maxColumn) { - maxColumn = length; - } + int judgeAncherPosX; + int judgeStartLength; + int startLineIdx; + { + QTextBlock block = document()->findBlockByNumber(startRow); + int startCurPos = m_altStartTextCursor.positionInBlock(); + QTextLine startLine = block.layout()->lineForTextPosition(startCurPos); + judgeAncherPosX = startLine.cursorToX(startCurPos); + judgeStartLength = startCurPos - startLine.textStart(); + startLineIdx = startLine.lineNumber(); + } + int judgeCursorPosX; + int judgeEndLength; + int endLineIdx; + { + QTextBlock block = document()->findBlockByNumber(row); + int endCurPos = m_altEndTextCursor.positionInBlock(); + QTextLine endLine = block.layout()->lineForTextPosition(endCurPos); + judgeCursorPosX = curPos.x(); + judgeEndLength = endCurPos - endLine.textStart(); + endLineIdx = endLine.lineNumber(); + } + bool isDown = false; + if (row > startRow) + { + isDown = true; } - - - for (int iRow = minRow; iRow <= maxRow; iRow++) { + else if (row == startRow && endLineIdx >= startLineIdx) + { + isDown = true; + } + for (int iRow = minRow; iRow <= maxRow; iRow++) + { QTextBlock block = document()->findBlockByNumber(iRow); - int length = block.text().size(); - - if (length < minColumn) continue; - - QTextEdit::ExtraSelection selection; - QTextCursor cursor = this->textCursor(); - cursor.clearSelection(); - setTextCursor(cursor); - cursor.setPosition(block.position() + minColumn, QTextCursor::MoveAnchor); - if (length < maxColumn) { - cursor.setPosition(block.position() + length, QTextCursor::KeepAnchor); - } else { - cursor.setPosition(block.position() + maxColumn, QTextCursor::KeepAnchor); + int lineAt = 0; + int lineCount = block.lineCount(); + // 对开始块和结束块的行数做判断 + if (iRow == minRow) + { + lineAt = isDown ? startLineIdx : endLineIdx; + } + if (iRow == maxRow) + { + lineCount = isDown ? endLineIdx + 1 : startLineIdx + 1; + } + for (; lineAt < lineCount; lineAt++) + { + // 引入行的判断 + QTextLine lineInBlock = block.layout()->lineAt(lineAt); + int lineLength = lineInBlock.textLength(); + if (lineLength < judgeStartLength && lineLength < judgeEndLength) + { + continue; + } + QTextCursor cursor = this->textCursor(); + cursor.clearSelection(); + setTextCursor(cursor); + int properColumn; + int blockPos = block.position(); + properColumn = lineInBlock.xToCursor(judgeAncherPosX); + cursor.setPosition(blockPos + properColumn, QTextCursor::MoveAnchor); + // 由于窗口大小小于块的最大长度的外部UI问题,这里要进行对鼠标pos进行若达到最大值无法对每行最后一个字符的覆盖的问题处理 + // 扩大了judgeCursorPosX的最大限度。 + int lineEndPosInBlock = lineInBlock.textStart() + lineInBlock.textLength(); + int lineEndPosToX = lineInBlock.cursorToX(lineEndPosInBlock); + if (lineInBlock.width() <= lineEndPosToX) + { + if (judgeCursorPosX > lineInBlock.cursorToX(lineEndPosInBlock - 1)) + { + judgeCursorPosX = lineEndPosToX; + } + } + properColumn = lineInBlock.xToCursor(judgeCursorPosX); + cursor.setPosition(blockPos + properColumn, QTextCursor::KeepAnchor); + QTextEdit::ExtraSelection selection; + selection.cursor = cursor; + selection.format = format; + m_altModSelections << selection; } - - selection.cursor = cursor; - selection.format = format; - m_altModSelections << selection; } - renderAllSelections(); update(); } @@ -7334,30 +7382,22 @@ void TextEdit::paintEvent(QPaintEvent *e) } } } - QColor lineColor = palette().text().color(); if (m_bIsAltMod && !m_altModSelections.isEmpty()) { QTextCursor textCursor = this->textCursor(); int cursorWidth = this->cursorWidth(); - //int cursoColumn = textCursor.positionInBlock(); QPainter painter(viewport()); QPen pen; pen.setColor(lineColor); pen.setWidth(cursorWidth); painter.setPen(pen); - QList rowList; - for (int i = 0 ; i < m_altModSelections.size(); i++) { - //if(m_altModSelections[i].cursor.positionInBlock() == cursoColumn){ - int row = m_altModSelections[i].cursor.blockNumber(); - if (!rowList.contains(row)) { - rowList << row; - QRect textCursorRect = this->cursorRect(m_altModSelections[i].cursor); - painter.drawRect(textCursorRect); - } - // } + for (int i = 0; i < m_altModSelections.size(); i++) + { + QRect textCursorRect = this->cursorRect(m_altModSelections[i].cursor); + painter.drawRect(textCursorRect); } } }