From db61432d36c409072f6f23bbd446ad4ebc8c48d3 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Fri, 26 Nov 2021 19:54:05 +0800 Subject: [PATCH] - fix: dragging text from lines in the front to lines back will cause error - fix: dragging text onto itself should do nothing --- NEWS.md | 2 + RedPandaIDE/qsynedit/SynEdit.cpp | 63 +++++++++++++++++++++++++------- RedPandaIDE/qsynedit/Types.cpp | 29 +++++++++++++++ RedPandaIDE/qsynedit/Types.h | 5 +++ 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0f3c1bbc..26e49e80 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ Version 0.10.2 For Dev-C++ 7 Beta - fix: select by mouse can't correctly set mouse's column position - fix: dragging out of the editor and back will cause error + - fix: dragging text from lines in the front to lines back will cause error + - fix: dragging text onto itself should do nothing Version 0.10.1 For Dev-C++ 7 Beta - fix: can't correctly expand watch expression that has spaces in it diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 201874c0..9c733be6 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -6203,23 +6203,58 @@ void SynEdit::dragEnterEvent(QDragEnterEvent *event) void SynEdit::dropEvent(QDropEvent *event) { //mScrollTimer->stop(); - mUndoList->BeginBlock(); - auto action = finally([this] { - mUndoList->EndBlock(); - }); + BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(event->pos().x(), event->pos().y())); - int topLine = mTopLine; - int leftChar = mLeftChar; - if (event->proposedAction() == Qt::DropAction::MoveAction) { - setBlockBegin(mDragSelBeginSave); - setBlockEnd(mDragSelEndSave); - setSelText(""); - } - setTopLine(topLine); - setLeftChar(leftChar); setCaretXY(coord); - setSelText(event->mimeData()->text()); + if (coord>=mDragSelBeginSave && coord<=mDragSelEndSave) { + //do nothing if drag onto itself + } else if (event->proposedAction() == Qt::DropAction::CopyAction) { + //just copy it + setSelText(event->mimeData()->text()); + } else if (event->proposedAction() == Qt::DropAction::MoveAction) { + int topLine = mTopLine; + int leftChar = mLeftChar; + mUndoList->BeginBlock(); + if (coord < mDragSelBeginSave ) { + //delete old + setBlockBegin(mDragSelBeginSave); + setBlockEnd(mDragSelEndSave); + setSelText(""); + //paste to new position + setTopLine(topLine); + setLeftChar(leftChar); + setCaretXY(coord); + setSelText(event->mimeData()->text()); + } else { + //paste to new position + setTopLine(topLine); + setLeftChar(leftChar); + setCaretXY(coord); + setSelText(event->mimeData()->text()); + //delete old + setBlockBegin(mDragSelBeginSave); + setBlockEnd(mDragSelEndSave); + setSelText(""); + //set caret to right pos + if (mDragSelBeginSave.Line == mDragSelEndSave.Line) { + if (coord.Line == mDragSelEndSave.Line) { + coord.Char -= mDragSelEndSave.Char-mDragSelBeginSave.Char; + } + } else { + if (coord.Line == mDragSelEndSave.Line) { + coord.Char -= mDragSelEndSave.Char-1; + } else { + coord.Line -= mDragSelEndSave.Line-mDragSelBeginSave.Line; + topLine -= mDragSelEndSave.Line-mDragSelBeginSave.Line; + } + } + setTopLine(topLine); + setLeftChar(leftChar); + setCaretXY(coord); + } + mUndoList->EndBlock(); + } event->acceptProposedAction(); } diff --git a/RedPandaIDE/qsynedit/Types.cpp b/RedPandaIDE/qsynedit/Types.cpp index e4e985eb..0de4c4bf 100644 --- a/RedPandaIDE/qsynedit/Types.cpp +++ b/RedPandaIDE/qsynedit/Types.cpp @@ -239,3 +239,32 @@ bool BufferCoord::operator==(const BufferCoord &coord) { return coord.Char == Char && coord.Line == Line; } + +bool BufferCoord::operator>=(const BufferCoord &coord) +{ + return (Line > coord.Line) + || (Line == coord.Line && Char >= coord.Char); +} + +bool BufferCoord::operator>(const BufferCoord &coord) +{ + return (Line > coord.Line) + || (Line == coord.Line && Char > coord.Char); +} + +bool BufferCoord::operator<(const BufferCoord &coord) +{ + return (Line < coord.Line) + || (Line == coord.Line && Char < coord.Char); +} + +bool BufferCoord::operator<=(const BufferCoord &coord) +{ + return (Line < coord.Line) + || (Line == coord.Line && Char <= coord.Char); +} + +bool BufferCoord::operator!=(const BufferCoord &coord) +{ + return coord.Char != Char || coord.Line != Line; +} diff --git a/RedPandaIDE/qsynedit/Types.h b/RedPandaIDE/qsynedit/Types.h index ce7fe313..5df31c40 100644 --- a/RedPandaIDE/qsynedit/Types.h +++ b/RedPandaIDE/qsynedit/Types.h @@ -11,6 +11,11 @@ struct BufferCoord { int Char; int Line; bool operator==(const BufferCoord& coord); + bool operator>=(const BufferCoord& coord); + bool operator>(const BufferCoord& coord); + bool operator<(const BufferCoord& coord); + bool operator<=(const BufferCoord& coord); + bool operator!=(const BufferCoord& coord); }; class SynEdit;