Skip to content

Commit

Permalink
Make shortcuts for autocompletion configurable (fixes #293, #654)
Browse files Browse the repository at this point in the history
Use the following entries in shortcuts.ini:
- actionNext_Completion
- actionPrevious_Completion
- actionNext_Completion_Placeholder
- actionPrevious_Completion_Placeholder
  • Loading branch information
stloeffler committed Dec 22, 2019
1 parent 9d38f29 commit f632880
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ else ()
endif ()

set(TEXWORKS_UIS CitationSelectDialog.ui
CompletingEdit.ui
ConfirmDelete.ui
Find.ui
HardWrapDialog.ui
Expand Down
87 changes: 61 additions & 26 deletions src/CompletingEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,27 @@ CompletingEdit::CompletingEdit(QWidget *parent /* = nullptr */)
connect(this, SIGNAL(textChanged()), lineNumberArea, SLOT(update()));

connect(TWApp::instance(), SIGNAL(highlightLineOptionChanged()), this, SLOT(resetExtraSelections()));


setupUi(this);
// As these actions are not used in menus/toolbars, we need to manually add
// them to the widget for TWUtils::installCustomShortcuts to work
insertActions(nullptr, {actionNext_Completion, actionPrevious_Completion, actionNext_Completion_Placeholder, actionPrevious_Completion_Placeholder});

#ifdef Q_OS_DARWIN
// Backwards compatibility
// Ctrl+Tab is mapped to Command+Tab on the Mac, which is the standard key
// sequence for switching applications. Hence that combination is changed to
// Alt+Tab on the Mac
if (actionNext_Completion_Placeholder->shortcut() == QKeySequence(Qt::CTRL + Qt::Key_Tab))
actionNext_Completion_Placeholder->setShortcut(QKeySequence(Qt::ALT + Qt::Key_Tab));
if (actionPrevious_Completion_Placeholder->shortcut() == QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Tab))
actionPrevious_Completion_Placeholder->setShortcut(QKeySequence(Qt::ALT + Qt::SHIFT + Qt::Key_Tab));
#endif

cursorPositionChangedSlot();
updateLineNumberAreaWidth(0);
updateColors();
TWUtils::installCustomShortcuts(this);
}

void CompletingEdit::prefixLines(const QString &prefix)
Expand Down Expand Up @@ -555,11 +572,12 @@ void CompletingEdit::resetExtraSelections()

void CompletingEdit::keyPressEvent(QKeyEvent *e)
{
// Shortcut key for command completion
bool isShortcut = (e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab);
if (isShortcut && autocompleteEnabled) {
handleCompletionShortcut(e);
return;
if (autocompleteEnabled) {
QKeySequence seq(static_cast<int>(e->modifiers()) | e->key());
if (seq == actionNext_Completion->shortcut() || seq == actionPrevious_Completion->shortcut() || seq == actionNext_Completion_Placeholder->shortcut() || seq == actionPrevious_Completion_Placeholder->shortcut()) {
if (handleCompletionShortcut(e))
return;
}
}

if (!e->text().isEmpty())
Expand All @@ -570,6 +588,11 @@ void CompletingEdit::keyPressEvent(QKeyEvent *e)
handleReturn(e);
break;

case Qt::Key_Tab:
case Qt::Key_Backtab:
handleTab(e);
break;

case Qt::Key_Backspace:
handleBackspace(e);
break;
Expand Down Expand Up @@ -813,24 +836,16 @@ void CompletingEdit::smartenQuotes()
}
}

void CompletingEdit::handleCompletionShortcut(QKeyEvent *e)
// \returns true if shortcut was handled, false otherwise
bool CompletingEdit::handleCompletionShortcut(QKeyEvent *e)
{
// usage:
// unmodified: next completion
// shift : previous completion
// ctl/alt : skip to next placeholder (alt on Mac, ctl elsewhere)
// ctl/alt-shift : skip to previous placeholder

#if defined(Q_OS_DARWIN)
if ((e->modifiers() & ~Qt::ShiftModifier) == Qt::AltModifier)
#else
if ((e->modifiers() & ~Qt::ShiftModifier) == Qt::ControlModifier)
#endif
QKeySequence seq(static_cast<int>(e->modifiers()) | e->key());
if (seq == actionNext_Completion_Placeholder->shortcut() || seq == actionPrevious_Completion_Placeholder->shortcut())
{
if (!find(QString(0x2022), (e->modifiers() & Qt::ShiftModifier)
if (!find(QString(0x2022), (seq == actionPrevious_Completion_Placeholder->shortcut())
? QTextDocument::FindBackward : QTextDocument::FindFlags()))
QApplication::beep();
return;
return true;
}

// if we are at the beginning of the line (i.e., only whitespaces before a
Expand Down Expand Up @@ -895,18 +910,18 @@ void CompletingEdit::handleCompletionShortcut(QKeyEvent *e)
setCompleter(nullptr);
}
else {
if (e->modifiers() == Qt::ShiftModifier)
if (seq == actionPrevious_Completion->shortcut())
c->setCurrentRow(c->completionCount() - 1);
showCurrentCompletion();
return;
return true;
}
}
break;
}
}

if (c && c->completionCount() > 0) {
if (e->modifiers() == Qt::ShiftModifier) {
if (seq == actionPrevious_Completion->shortcut()) {
if (c->currentRow() == 0) {
showCompletion(c->completionPrefix());
setCompleter(nullptr);
Expand All @@ -926,10 +941,14 @@ void CompletingEdit::handleCompletionShortcut(QKeyEvent *e)
showCurrentCompletion();
}
}
return;
return true;
}

if(!noSelection) {
return false;
}

void CompletingEdit::handleTab(QKeyEvent * e)
{
if (textCursor().hasSelection()) {
if(e->modifiers() == Qt::ShiftModifier) {
unPrefixLines(QString::fromLatin1("\t"));
} else {
Expand Down Expand Up @@ -1307,6 +1326,22 @@ bool CompletingEdit::event(QEvent *e)
// derive the colors from the application's palette
if (e->type() == QEvent::PaletteChange)
updateColors();
if (e->type() == QEvent::ShortcutOverride) {
auto ke = reinterpret_cast<QKeyEvent*>(e);
QKeySequence seq(static_cast<int>(ke->modifiers()) | ke->key());
if (seq == actionNext_Completion->shortcut() ||
seq == actionPrevious_Completion->shortcut() ||
seq == actionNext_Completion_Placeholder->shortcut() ||
seq == actionPrevious_Completion_Placeholder->shortcut())
{
// If the key press corresponds to a completion shortcut, accept the
// event to tell Qt not to treat it as a shortcut but to send it to
// the focused widget normally (thereby invoking the keyPressEvent
// handler)
e->accept();
}
}

return QTextEdit::event(e);
}

Expand Down
6 changes: 4 additions & 2 deletions src/CompletingEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "document/SpellChecker.h"
#include "ui/LineNumberWidget.h"
#include "ui_CompletingEdit.h"

#include <QTextEdit>
#include <QHash>
Expand All @@ -36,7 +37,7 @@ class QCompleter;
class QStandardItemModel;
class QTextCodec;

class CompletingEdit : public QTextEdit
class CompletingEdit : public QTextEdit, private Ui::CompletingEdit
{
Q_OBJECT

Expand Down Expand Up @@ -130,9 +131,10 @@ private slots:
void loadCompletionsFromFile(QStandardItemModel *model, const QString& filename);
void loadCompletionFiles(QCompleter *theCompleter);

void handleCompletionShortcut(QKeyEvent *e);
bool handleCompletionShortcut(QKeyEvent *e);
void handleReturn(QKeyEvent *e);
void handleBackspace(QKeyEvent *e);
void handleTab(QKeyEvent * e);
void handleOtherKey(QKeyEvent *e);
void maybeSmartenQuote(int offset);

Expand Down
48 changes: 48 additions & 0 deletions src/CompletingEdit.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CompletingEdit</class>
<widget class="QWidget" name="CompletingEdit">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<height>30</height>
</rect>
</property>
<action name="actionNext_Completion">
<property name="text">
<string>Next Completion</string>
</property>
<property name="shortcut">
<string>Tab</string>
</property>
</action>
<action name="actionPrevious_Completion">
<property name="text">
<string>Previous Completion</string>
</property>
<property name="shortcut">
<string>Shift+Backtab</string>
</property>
</action>
<action name="actionNext_Completion_Placeholder">
<property name="text">
<string>Next Completion Placeholder</string>
</property>
<property name="shortcut">
<string>Ctrl+Tab</string>
</property>
</action>
<action name="actionPrevious_Completion_Placeholder">
<property name="text">
<string>Previous Completion Placeholder</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+Backtab</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
1 change: 1 addition & 0 deletions trans/TeXworks_trans.pro
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ HEADERS = \

FORMS = \
"../src/CitationSelectDialog.ui" \
"../src/CompletingEdit.ui" \
"../src/ConfirmDelete.ui" \
"../src/Find.ui" \
"../src/HardWrapDialog.ui" \
Expand Down

0 comments on commit f632880

Please sign in to comment.