refine editing shortcuts

1. Ctrl+[ behaves exactly like ESC;
2. Ctrl+W should delete the selected text if there has one.

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2017-01-19 23:48:00 +08:00
parent 336d9ad109
commit 46988f5bef
2 changed files with 13 additions and 6 deletions

View File

@ -874,7 +874,9 @@ void VMainWindow::resizeEvent(QResizeEvent *event)
void VMainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
if (event->key() == Qt::Key_Escape
|| (event->key() == Qt::Key_BracketLeft
&& event->modifiers() == Qt::ControlModifier)) {
m_findReplaceDialog->closeDialog();
event->accept();
return;

View File

@ -297,25 +297,27 @@ bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event)
return false;
}
// Let Ctrl+[ behave exactly like ESC.
bool VMdEditOperations::handleKeyBracketLeft(QKeyEvent *p_event)
{
// 1. If it is not in Normal state, just go back to Normal state;
// 2. If it is already Normal state, try to clear selection;
// 3. Anyway, we accept this event.
// 3. Otherwise, ignore this event and let parent handles it.
bool accept = false;
if (p_event->modifiers() == Qt::ControlModifier) {
if (m_keyState != KeyState::Normal) {
m_pendingTimer->stop();
setKeyState(KeyState::Normal);
m_pendingKey.clear();
accept = true;
} else {
QTextCursor cursor = m_editor->textCursor();
if (cursor.hasSelection()) {
cursor.clearSelection();
m_editor->setTextCursor(cursor);
accept = true;
}
}
accept = true;
}
if (accept) {
p_event->accept();
@ -593,11 +595,14 @@ bool VMdEditOperations::handleKeyW(QKeyEvent *p_event)
if (p_event->modifiers() == Qt::ControlModifier) {
// Ctrl+W, delete till the start of previous word.
QTextCursor cursor = m_editor->textCursor();
bool ret = cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor);
if (ret) {
if (cursor.hasSelection()) {
cursor.removeSelectedText();
} else {
bool ret = cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::KeepAnchor);
if (ret) {
cursor.removeSelectedText();
}
}
p_event->accept();
return true;
}