Skip to content

Commit

Permalink
feat: 新增Ctrl + 鼠标点击移动光标功能
Browse files Browse the repository at this point in the history
ESC [ pn C        cursor right pn times - stop at far right
ESC [ pn D        cursor left pn times - stop at far left
使用每次移动一格的方式进行光标移动

Log: 新增Ctrl + 鼠标点击移动光标功能
  • Loading branch information
DaiZW007 authored and ArchieMeng committed May 7, 2024
1 parent 10c8111 commit 01f2122
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 3rdparty/terminalwidget/lib/Emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ public slots:
*/
virtual void sendString(const char *string, int length = -1) = 0;

/**
* Send Cursor Change
*
* @param count count < 0
* cursor left count times
* count > 0
* cursor right count times
*/
virtual void sendCursor(int count) = 0;

/**
* Processes an incoming stream of characters. receiveData() decodes the incoming
* character buffer using the current codec(), and then calls receiveChar() for
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/terminalwidget/lib/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ void Session::addView(TerminalDisplay * widget)
SLOT(sendKeyEvent(QKeyEvent *)) );
connect( widget , SIGNAL(mouseSignal(int,int,int,int)) , _emulation ,
SLOT(sendMouseEvent(int,int,int,int)) );
connect(widget, SIGNAL(changedCursonPosition(int)), _emulation,
SLOT(sendCursor(int)));
// 先判断是否有远程连接,若有,则连接远程
// connect(widget, SIGNAL(sendStringToEmu(const char *)), _emulation, SLOT(sendString(const char *)));

Expand Down
40 changes: 40 additions & 0 deletions 3rdparty/terminalwidget/lib/TerminalDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,17 @@ void TerminalDisplay::mousePressEvent(QMouseEvent* ev)

selected = _screenWindow->isSelected(pos.x(),pos.y());

if (ev->modifiers() & Qt::ControlModifier) {
int line;
int column;
getExactCharacterPosition(ev->pos(), line, column);
line -= cursorPosition().y();
column -= cursorPosition().x();
int count = 0;
count += line * _columns + column;
emit changedCursonPosition(count);
}

if ((!_ctrlDrag || ev->modifiers() & Qt::ControlModifier) && selected ) {
// The user clicked inside selected text
dragInfo.state = diPending;
Expand Down Expand Up @@ -2544,6 +2555,35 @@ void TerminalDisplay::getCharacterPosition(const QPoint& widgetPoint,int& line,i
column = _usedColumns;
}

void TerminalDisplay::getExactCharacterPosition(const QPoint &widgetPoint, int &line, int &column) const
{
line = (widgetPoint.y() - contentsRect().top() - _topMargin) / _fontHeight;
if (line < 0)
line = 0;
if (line >= _usedLines)
line = _usedLines - 1;

int x = widgetPoint.x() - contentsRect().left() - _leftMargin;
if (_fixedFont)
column = x / _fontWidth;
else {
column = 0;
while (column + 1 < _usedColumns && x > textWidth(0, column + 1, line))
column++;
}

if (column < 0)
column = 0;

// the column value returned can be equal to _usedColumns, which
// is the position just after the last character displayed in a line.
//
// this is required so that the user can select characters in the right-most
// column (or left-most for right-to-left input)
if (column > _usedColumns)
column = _usedColumns;
}

void TerminalDisplay::setHideCursor(bool hideCursor)
{
_hideCursor = hideCursor;
Expand Down
2 changes: 2 additions & 0 deletions 3rdparty/terminalwidget/lib/TerminalDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ class KONSOLEPRIVATE_EXPORT TerminalDisplay : public QWidget
// maps a point on the widget to the position ( ie. line and column )
// of the character at that point.
void getCharacterPosition(const QPoint& widgetPoint,int& line,int& column) const;
void getExactCharacterPosition(const QPoint& widgetPoint,int& line,int& column) const;

void setHideCursor(bool hideCursor);

Expand Down Expand Up @@ -573,6 +574,7 @@ public slots:
void mouseSignal(int button, int column, int line, int eventType);
void changedFontMetricSignal(int height, int width);
void changedContentSizeSignal(int height, int width);
void changedCursonPosition(int count);

/**
* Emitted when the user right clicks on the display, or right-clicks with the Shift
Expand Down
10 changes: 10 additions & 0 deletions 3rdparty/terminalwidget/lib/Vt102Emulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,16 @@ void Vt102Emulation::sendMouseEvent( int cb, int cx, int cy , int eventType )
sendString(command);
}

void Vt102Emulation::sendCursor(int count)
{
char command[32];
command[0] = '\0';
snprintf(command, sizeof(command), "\033[%c", count < 0 ? 'D' : 'C');
for (int i = 0; i < qAbs(count); ++i) {
sendString(command);
}
}

/**
* The focus lost event can be used by Vim (or other terminal applications)
* to recognize that the konsole window has lost focus.
Expand Down
1 change: 1 addition & 0 deletions 3rdparty/terminalwidget/lib/Vt102Emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public slots:
void sendText(const QString& text) override;
void sendKeyEvent(QKeyEvent*) override;
void sendMouseEvent(int buttons, int column, int line, int eventType) override;
void sendCursor(int count) override;
virtual void focusLost();
virtual void focusGained();

Expand Down

0 comments on commit 01f2122

Please sign in to comment.