From 6d8d8b9ab9c651c765b4026c6d845202017e9ea3 Mon Sep 17 00:00:00 2001 From: asc Date: Fri, 3 Mar 2023 23:48:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E4=BA=8C=E8=BF=9B=E5=88=B6=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=92=8C=E6=96=87=E6=9C=AC=E9=83=A8=E5=88=86=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scintillahexeditview.cpp | 128 +++++++++++++++++++++++++++++++++++ src/scintillahexeditview.h | 14 ++++ 2 files changed, 142 insertions(+) diff --git a/src/scintillahexeditview.cpp b/src/scintillahexeditview.cpp index c8da9b2..18f0ea1 100755 --- a/src/scintillahexeditview.cpp +++ b/src/scintillahexeditview.cpp @@ -122,6 +122,134 @@ void ScintillaHexEditView::dropEvent(QDropEvent* e) //qDebug() << ui.leftSrc->geometry() << ui.rightSrc->geometry() << QCursor::pos() << this->mapFromGlobal(QCursor::pos()); } +void ScintillaHexEditView::mouseMoveEvent(QMouseEvent* e) +{ + if (property("type").toInt() == HEX_TYPE && selecting) + { + long from, to; + int r1, c1, r2, c2; + int begin, end; + const int col_hex_start = 9; + const int col_hex_finish = 56; + const int col_text_start = 57; + const int col_text_finish = 72; + const int col_num = 75; + + m_SelTo = Scintilla::Point::FromInts(e->x(), e->y()); + + from = execute(SCI_POSITIONFROMPOINT, m_SelFrom.x, m_SelFrom.y); + to = execute(SCI_POSITIONFROMPOINT, m_SelTo.x, m_SelTo.y); + + lineIndexFromPosition(from, &r1, &c1); + lineIndexFromPosition(to, &r2, &c2); + + execute(SCI_CLEARSELECTIONS); + + if (r1 < 0 || c1 < 0 || r2 < 0 || c2 < 0) + return; + + + if (c1 < col_text_start || c1 > col_text_finish) + { + //选择二进制区域 + for (int i = r1; i <= r2; ++i) + { + //每行hex显示区域 + if (i == r1) + { + if (c1 < col_hex_start) + begin = col_hex_start; + else if (c1 > col_hex_finish) + begin = col_hex_finish; + else begin = c1 / 3 * 3; + } + else + begin = col_hex_start; + + if (i == r2) + { + if (c2 < col_hex_start) + end = col_hex_start; + else if (c2 > col_hex_finish) + end = col_hex_finish; + else end = c2 / 3 * 3 + 2; + } + else + end = col_hex_finish; + + + for (int j = begin; j < end; j += 3) + { + execute(SCI_ADDSELECTION, i * col_num + j, i * col_num + j + 2); + execute(SCI_ADDSELECTION, i * col_num + col_text_start + (j - col_hex_start) / 3, + i * col_num + col_text_start + (j - col_hex_start) / 3 + 1); + } + } + } + else + { + //选择字符区域 + for (int i = r1; i <= r2; ++i) + { + //每行字符显示区域 + if (i == r1) + { + if (c1 < col_text_start) + begin = col_text_start; + else if (c1 > col_text_finish) + begin = col_text_finish; + else begin = c1; + } + else + begin = col_text_start; + + if (i == r2) + { + if (c2 < col_text_start) + end = col_text_start; + else if (c2 > col_text_finish) + end = col_text_finish; + else end = c2; + } + else + end = col_text_finish; + + + for (int j = begin; j <= end; ++j) + { + execute(SCI_ADDSELECTION, i * col_num + col_hex_start + (j - col_text_start) * 3, + i * col_num + col_hex_start + (j - col_text_start) * 3 + 2); + execute(SCI_ADDSELECTION, i * col_num + j, i * col_num + j + 1); + } + } + } + return; + } + QsciScintillaBase::mouseMoveEvent(e); +} + +void ScintillaHexEditView::mousePressEvent(QMouseEvent* e) +{ + if (property("type").toInt() == HEX_TYPE && !selecting) + { + execute(SCI_CLEARSELECTIONS); + selecting = true; + m_SelFrom = Scintilla::Point::FromInts(e->x(), e->y()); + return; + } + QsciScintillaBase::mousePressEvent(e); +} + +void ScintillaHexEditView::mouseReleaseEvent(QMouseEvent* e) +{ + if (property("type").toInt() == HEX_TYPE && selecting) + { + selecting = false; + return; + } + QsciScintillaBase::mouseReleaseEvent(e); +} + void ScintillaHexEditView::updateThemes() { diff --git a/src/scintillahexeditview.h b/src/scintillahexeditview.h index 26b6c3f..5154416 100755 --- a/src/scintillahexeditview.h +++ b/src/scintillahexeditview.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -33,6 +34,14 @@ protected: void dragEnterEvent(QDragEnterEvent * event); void dropEvent(QDropEvent * e); + //! Re-implemented to handle mouse moves. + virtual void mouseMoveEvent(QMouseEvent* e); + + //! Re-implemented to handle mouse presses. + virtual void mousePressEvent(QMouseEvent* e); + + //! Re-implemented to handle mouse releases. + virtual void mouseReleaseEvent(QMouseEvent* e); private: static bool _SciInit; @@ -41,4 +50,9 @@ private: SCINTILLA_PTR m_pScintillaPtr = 0; CCNotePad* m_NoteWin; + + bool selecting = false; + + Scintilla::Point m_SelFrom; + Scintilla::Point m_SelTo; };