add shortcut Ctrl+O to insert backquotes

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

View File

@ -249,6 +249,14 @@ bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event)
break;
}
case Qt::Key_O:
{
if (handleKeyO(p_event)) {
return true;
}
break;
}
case Qt::Key_U:
{
if (handleKeyU(p_event)) {
@ -515,6 +523,50 @@ bool VMdEditOperations::handleKeyI(QKeyEvent *p_event)
return false;
}
bool VMdEditOperations::handleKeyO(QKeyEvent *p_event)
{
if (p_event->modifiers() == Qt::ControlModifier) {
// Ctrl+O, inline codeblock.
QTextCursor cursor = m_editor->textCursor();
if (cursor.hasSelection()) {
// Insert ` around the selected text.
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
cursor.beginEditBlock();
cursor.clearSelection();
cursor.setPosition(start, QTextCursor::MoveAnchor);
cursor.insertText("`");
cursor.setPosition(end + 1, QTextCursor::MoveAnchor);
cursor.insertText("`");
cursor.endEditBlock();
m_editor->setTextCursor(cursor);
} else {
// Insert `` and place cursor in the middle.
// Or if there are one ` after current cursor, just skip it.
cursor.beginEditBlock();
int pos = cursor.positionInBlock();
bool hasBackquote = false;
QString text = cursor.block().text();
if (pos <= text.size() - 1) {
if (text[pos] == '`') {
hasBackquote = true;
}
}
if (hasBackquote) {
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);
} else {
cursor.insertText("``");
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1);
}
cursor.endEditBlock();
m_editor->setTextCursor(cursor);
}
p_event->accept();
return true;
}
return false;
}
bool VMdEditOperations::handleKeyU(QKeyEvent *p_event)
{
if (p_event->modifiers() == Qt::ControlModifier) {

View File

@ -36,6 +36,7 @@ private:
bool handleKeyD(QKeyEvent *p_event);
bool handleKeyH(QKeyEvent *p_event);
bool handleKeyI(QKeyEvent *p_event);
bool handleKeyO(QKeyEvent *p_event);
bool handleKeyU(QKeyEvent *p_event);
bool handleKeyW(QKeyEvent *p_event);
bool handleKeyEsc(QKeyEvent *p_event);