RedPanda-CPP/libs/qsynedit/qsynedit/keystrokes.cpp

254 lines
8.8 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2023-01-11 16:22:26 +08:00
#include "keystrokes.h"
#include "miscprocs.h"
2021-05-14 23:56:43 +08:00
2022-09-25 09:55:18 +08:00
namespace QSynedit {
2022-09-27 14:01:38 +08:00
EditKeyStroke::EditKeyStroke()
2021-05-14 23:56:43 +08:00
{
mKey = 0;
mKeyModifiers = Qt::NoModifier;
mKey2 = 0;
mKeyModifiers2 = Qt::NoModifier;
mCommand = EditCommand::None;
2021-05-14 23:56:43 +08:00
}
2022-09-27 14:01:38 +08:00
QKeySequence EditKeyStroke::keySequence() const
2021-05-14 23:56:43 +08:00
{
if (mKey2 == 0) {
return QKeySequence(mKey | mKeyModifiers);
2021-05-14 23:56:43 +08:00
} else {
return QKeySequence(mKey | mKeyModifiers, mKey2 | mKeyModifiers2);
2021-05-14 23:56:43 +08:00
}
}
2022-09-27 14:01:38 +08:00
void EditKeyStroke::setKeySequence(QKeySequence &keySequence)
2021-05-14 23:56:43 +08:00
{
2021-08-29 22:08:43 +08:00
if (keySequence.isEmpty())
2021-05-14 23:56:43 +08:00
return;
decodeKey(keySequence[0],mKey,mKeyModifiers);
if (keySequence.count()>1) {
decodeKey(keySequence[1],mKey2,mKeyModifiers2);
} else {
mKey2=0;
mKeyModifiers2=Qt::NoModifier;
}
}
2022-09-27 14:01:38 +08:00
int EditKeyStroke::key() const
2021-05-14 23:56:43 +08:00
{
return mKey;
}
2022-09-27 14:01:38 +08:00
void EditKeyStroke::setKey(int key)
2021-05-14 23:56:43 +08:00
{
mKey = key;
}
2022-09-27 14:01:38 +08:00
Qt::KeyboardModifiers EditKeyStroke::keyModifiers() const
2021-05-14 23:56:43 +08:00
{
return mKeyModifiers;
}
2022-09-27 14:01:38 +08:00
void EditKeyStroke::setKeyModifiers(const Qt::KeyboardModifiers &keyModifiers)
2021-05-14 23:56:43 +08:00
{
mKeyModifiers = keyModifiers;
}
2022-09-27 14:01:38 +08:00
int EditKeyStroke::key2() const
2021-05-14 23:56:43 +08:00
{
return mKey2;
}
2022-09-27 14:01:38 +08:00
void EditKeyStroke::setKey2(int key2)
2021-05-14 23:56:43 +08:00
{
mKey2 = key2;
}
2022-09-27 14:01:38 +08:00
Qt::KeyboardModifiers EditKeyStroke::keyModifiers2() const
2021-05-14 23:56:43 +08:00
{
return mKeyModifiers2;
}
2022-09-27 14:01:38 +08:00
void EditKeyStroke::setKeyModifiers2(const Qt::KeyboardModifiers &keyModifiers2)
2021-05-14 23:56:43 +08:00
{
mKeyModifiers2 = keyModifiers2;
}
2022-09-27 14:01:38 +08:00
EditCommand EditKeyStroke::command() const
2021-05-14 23:56:43 +08:00
{
return mCommand;
}
2022-09-27 14:01:38 +08:00
void EditKeyStroke::setCommand(const EditCommand &command)
2021-05-14 23:56:43 +08:00
{
mCommand = command;
}
2023-01-11 16:22:26 +08:00
EditKeyStrokes::EditKeyStrokes()
2021-05-14 23:56:43 +08:00
{
}
2022-09-27 14:01:38 +08:00
PEditKeyStroke EditKeyStrokes::add(EditCommand command, int key, Qt::KeyboardModifiers modifiers)
2021-05-14 23:56:43 +08:00
{
2022-09-27 14:01:38 +08:00
PEditKeyStroke keyStroke = std::make_shared<EditKeyStroke>();
keyStroke->setKey(key);
keyStroke->setKeyModifiers(modifiers);
keyStroke->setCommand(command);
mList.append(keyStroke);
return keyStroke;
2021-05-14 23:56:43 +08:00
}
2022-09-27 14:01:38 +08:00
PEditKeyStroke EditKeyStrokes::findCommand(EditCommand command)
2021-05-14 23:56:43 +08:00
{
2022-09-27 14:01:38 +08:00
for (PEditKeyStroke& keyStroke:mList) {
2021-05-14 23:56:43 +08:00
if (keyStroke->command() == command)
return keyStroke;
}
2022-09-27 14:01:38 +08:00
return PEditKeyStroke();
2021-05-14 23:56:43 +08:00
}
2022-09-27 14:01:38 +08:00
PEditKeyStroke EditKeyStrokes::findKeycode(int key, Qt::KeyboardModifiers modifiers)
2021-05-14 23:56:43 +08:00
{
2022-09-27 14:01:38 +08:00
for (PEditKeyStroke& keyStroke:mList) {
if (keyStroke->key() == key
&& keyStroke->keyModifiers() == (modifiers & ~ Qt::KeypadModifier)
&& keyStroke->key2()==0)
2021-05-14 23:56:43 +08:00
return keyStroke;
}
2022-09-27 14:01:38 +08:00
return PEditKeyStroke();
2021-05-14 23:56:43 +08:00
}
2022-09-27 14:01:38 +08:00
PEditKeyStroke EditKeyStrokes::findKeycode2(int key, Qt::KeyboardModifiers modifiers,
2021-05-14 23:56:43 +08:00
int key2, Qt::KeyboardModifiers modifiers2)
{
2022-09-27 14:01:38 +08:00
for (PEditKeyStroke& keyStroke:mList) {
if (keyStroke->key() == key
&& keyStroke->keyModifiers()==(modifiers & ~ Qt::KeypadModifier)
&& keyStroke->key2()==key2
&& keyStroke->keyModifiers2()== (modifiers2 & ~ Qt::KeypadModifier))
2021-05-14 23:56:43 +08:00
return keyStroke;
}
2022-09-27 14:01:38 +08:00
return PEditKeyStroke();
2021-05-14 23:56:43 +08:00
}
2022-09-27 14:01:38 +08:00
PEditKeyStroke EditKeyStrokes::findKeySequence(const QKeySequence &keySeq)
2021-05-14 23:56:43 +08:00
{
switch (keySeq.count()) {
case 1: {
int key;
Qt::KeyboardModifiers modifiers;
decodeKey(keySeq[0],key,modifiers);
return findKeycode(key,modifiers);
}
case 2:
case 3:
case 4:
{
int key;
Qt::KeyboardModifiers modifiers;
int key2;
Qt::KeyboardModifiers modifiers2;
decodeKey(keySeq[0],key,modifiers);
decodeKey(keySeq[1],key2,modifiers2);
return findKeycode2(key,modifiers,key2,modifiers2);
}
default:
2022-09-27 14:01:38 +08:00
return PEditKeyStroke();
2021-05-14 23:56:43 +08:00
}
}
2022-09-27 14:01:38 +08:00
void EditKeyStrokes::clear()
2021-05-14 23:56:43 +08:00
{
return mList.clear();
}
2022-09-27 14:01:38 +08:00
void EditKeyStrokes::resetDefaults()
2021-05-14 23:56:43 +08:00
{
clear();
add(EditCommand::Up, Qt::Key_Up, Qt::NoModifier);
add(EditCommand::SelUp, Qt::Key_Up, Qt::ShiftModifier);
2022-12-19 09:35:48 +08:00
add(EditCommand::SelUp, Qt::Key_Up, Qt::KeyboardModifiers(Qt::ShiftModifier | Qt::AltModifier));
add(EditCommand::ScrollUp, Qt::Key_Up, Qt::ControlModifier);
add(EditCommand::Down, Qt::Key_Down, Qt::NoModifier);
add(EditCommand::SelDown, Qt::Key_Down, Qt::ShiftModifier);
2022-12-19 09:35:48 +08:00
add(EditCommand::SelDown, Qt::Key_Down, Qt::KeyboardModifiers(Qt::ShiftModifier | Qt::AltModifier));
add(EditCommand::ScrollDown, Qt::Key_Down, Qt::ControlModifier);
add(EditCommand::Left, Qt::Key_Left, Qt::NoModifier);
add(EditCommand::SelLeft, Qt::Key_Left, Qt::ShiftModifier);
add(EditCommand::SelLeft, Qt::Key_Left, Qt::KeyboardModifiers(Qt::ShiftModifier | Qt::AltModifier));
add(EditCommand::WordLeft, Qt::Key_Left, Qt::ControlModifier);
2022-12-19 09:35:48 +08:00
add(EditCommand::SelWordLeft, Qt::Key_Left, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier));
add(EditCommand::Right, Qt::Key_Right, Qt::NoModifier);
add(EditCommand::SelRight, Qt::Key_Right, Qt::ShiftModifier);
add(EditCommand::SelRight, Qt::Key_Right, Qt::KeyboardModifiers(Qt::ShiftModifier | Qt::AltModifier));
add(EditCommand::WordRight, Qt::Key_Right, Qt::ControlModifier);
2022-12-19 09:35:48 +08:00
add(EditCommand::SelWordRight, Qt::Key_Right, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier));
2022-12-19 09:35:48 +08:00
add(EditCommand::BlockStart, Qt::Key_Up, Qt::KeyboardModifiers(Qt::MetaModifier|Qt::ControlModifier));
add(EditCommand::SelBlockStart, Qt::Key_Up, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier|Qt::MetaModifier));
add(EditCommand::BlockEnd, Qt::Key_Down, Qt::KeyboardModifiers(Qt::MetaModifier|Qt::ControlModifier));
add(EditCommand::SelBlockEnd, Qt::Key_Down, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier|Qt::MetaModifier));
add(EditCommand::PageDown, Qt::Key_PageDown, Qt::NoModifier);
add(EditCommand::SelPageDown, Qt::Key_PageDown, Qt::ShiftModifier);
add(EditCommand::PageBottom, Qt::Key_PageDown, Qt::ControlModifier);
2022-12-19 09:35:48 +08:00
add(EditCommand::SelPageBottom, Qt::Key_PageDown, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier));
add(EditCommand::PageUp, Qt::Key_PageUp, Qt::NoModifier);
add(EditCommand::SelPageUp, Qt::Key_PageUp, Qt::ShiftModifier);
add(EditCommand::PageTop, Qt::Key_PageUp, Qt::ControlModifier);
2022-12-19 09:35:48 +08:00
add(EditCommand::SelPageTop, Qt::Key_PageUp, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier));
add(EditCommand::LineStart, Qt::Key_Home, Qt::NoModifier);
add(EditCommand::SelLineStart, Qt::Key_Home, Qt::ShiftModifier);
add(EditCommand::FileStart, Qt::Key_Home, Qt::ControlModifier);
add(EditCommand::SelFileStart, Qt::Key_Home, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier));
add(EditCommand::LineEnd, Qt::Key_End, Qt::NoModifier);
add(EditCommand::SelLineEnd, Qt::Key_End, Qt::ShiftModifier);
add(EditCommand::FileEnd, Qt::Key_End, Qt::ControlModifier);
add(EditCommand::SelFileEnd, Qt::Key_End, Qt::KeyboardModifiers(Qt::ShiftModifier|Qt::ControlModifier));
add(EditCommand::ToggleMode, Qt::Key_Insert, Qt::NoModifier);
add(EditCommand::DeleteChar, Qt::Key_Delete, Qt::NoModifier);
add(EditCommand::DeleteLastChar, Qt::Key_Backspace, Qt::NoModifier);
add(EditCommand::LineBreak, Qt::Key_Return, Qt::NoModifier);
add(EditCommand::LineBreak, Qt::Key_Return, Qt::ShiftModifier);
add(EditCommand::LineBreakAtEnd, Qt::Key_Return, Qt::ControlModifier);
add(EditCommand::LineBreak, Qt::Key_Enter, Qt::NoModifier);
add(EditCommand::LineBreak, Qt::Key_Enter, Qt::ShiftModifier);
add(EditCommand::LineBreakAtEnd, Qt::Key_Enter, Qt::ControlModifier);
}
2022-09-27 14:01:38 +08:00
void EditKeyStrokes::setExtraKeyStrokes()
{
add(EditCommand::DeleteWordStart, Qt::Key_Backspace, Qt::ControlModifier);
add(EditCommand::DeleteWordEnd, Qt::Key_Delete, Qt::ControlModifier);
add(EditCommand::DuplicateLine, Qt::Key_D, Qt::ControlModifier);
add(EditCommand::DeleteLine, Qt::Key_E, Qt::ControlModifier);
add(EditCommand::SelectAll, Qt::Key_A, Qt::ControlModifier);
add(EditCommand::Copy, Qt::Key_C, Qt::ControlModifier);
add(EditCommand::Paste, Qt::Key_V, Qt::ControlModifier);
add(EditCommand::Cut, Qt::Key_X, Qt::ControlModifier);
add(EditCommand::Undo, Qt::Key_Z, Qt::ControlModifier);
add(EditCommand::Redo, Qt::Key_Y, Qt::ControlModifier);
2021-05-14 23:56:43 +08:00
}
2022-09-25 09:55:18 +08:00
}