FancyLineEdit: Make completers triggerable via Ctrl+Space

In particular, this allows to trigger the completer without providing a
leading character, thus showing all history entries.

Task-number: QTCREATORBUG-17891
Change-Id: I8ae900d196de2e5083a626faa757648637b325ae
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Christian Kandeler
2019-08-27 14:16:24 +02:00
parent 0ba729e527
commit 5f845a02cd
3 changed files with 52 additions and 1 deletions

View File

@@ -36,7 +36,9 @@
#include <QAbstractItemView> #include <QAbstractItemView>
#include <QDebug> #include <QDebug>
#include <QKeyEvent> #include <QKeyEvent>
#include <QKeySequence>
#include <QMenu> #include <QMenu>
#include <QShortcut>
#include <QStylePainter> #include <QStylePainter>
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QStyle> #include <QStyle>
@@ -83,6 +85,29 @@ namespace Utils {
static bool camelCaseNavigation = false; static bool camelCaseNavigation = false;
class CompletionShortcut : public QObject
{
Q_OBJECT
public:
void setKeySequence(const QKeySequence &key)
{
if (m_key != key) {
m_key = key;
emit keyChanged(key);
}
}
QKeySequence key() const { return m_key; }
signals:
void keyChanged(const QKeySequence &key);
private:
QKeySequence m_key = Qt::Key_Space + HostOsInfo::controlModifier();
};
Q_GLOBAL_STATIC(CompletionShortcut, completionShortcut)
// --------- FancyLineEditPrivate // --------- FancyLineEditPrivate
class FancyLineEditPrivate : public QObject class FancyLineEditPrivate : public QObject
{ {
@@ -94,6 +119,7 @@ public:
FancyLineEdit *m_lineEdit; FancyLineEdit *m_lineEdit;
IconButton *m_iconbutton[2]; IconButton *m_iconbutton[2];
HistoryCompleter *m_historyCompleter = nullptr; HistoryCompleter *m_historyCompleter = nullptr;
QShortcut m_completionShortcut;
FancyLineEdit::ValidationFunction m_validationFunction = &FancyLineEdit::validateWithValidator; FancyLineEdit::ValidationFunction m_validationFunction = &FancyLineEdit::validateWithValidator;
QString m_oldText; QString m_oldText;
QMenu *m_menu[2]; QMenu *m_menu[2];
@@ -113,10 +139,15 @@ public:
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) : FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
QObject(parent), QObject(parent),
m_lineEdit(parent) m_lineEdit(parent),
m_completionShortcut(completionShortcut()->key(), parent)
{ {
m_okTextColor = parent->palette().color(QPalette::Active, QPalette::Text); m_okTextColor = parent->palette().color(QPalette::Active, QPalette::Text);
m_completionShortcut.setContext(Qt::WidgetShortcut);
connect(completionShortcut(), &CompletionShortcut::keyChanged,
&m_completionShortcut, &QShortcut::setKey);
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
m_iconbutton[i] = new IconButton(parent); m_iconbutton[i] = new IconButton(parent);
m_iconbutton[i]->installEventFilter(this); m_iconbutton[i]->installEventFilter(this);
@@ -166,6 +197,12 @@ FancyLineEdit::FancyLineEdit(QWidget *parent) :
connect(d->m_iconbutton[Left], &QAbstractButton::clicked, this, &FancyLineEdit::iconClicked); connect(d->m_iconbutton[Left], &QAbstractButton::clicked, this, &FancyLineEdit::iconClicked);
connect(d->m_iconbutton[Right], &QAbstractButton::clicked, this, &FancyLineEdit::iconClicked); connect(d->m_iconbutton[Right], &QAbstractButton::clicked, this, &FancyLineEdit::iconClicked);
connect(this, &QLineEdit::textChanged, this, &FancyLineEdit::validate); connect(this, &QLineEdit::textChanged, this, &FancyLineEdit::validate);
connect(&d->m_completionShortcut, &QShortcut::activated, this, [this] {
if (!completer())
return;
completer()->setCompletionPrefix(text().left(cursorPosition()));
completer()->complete();
});
} }
FancyLineEdit::~FancyLineEdit() FancyLineEdit::~FancyLineEdit()
@@ -349,6 +386,11 @@ void FancyLineEdit::setCamelCaseNavigationEnabled(bool enabled)
camelCaseNavigation = enabled; camelCaseNavigation = enabled;
} }
void FancyLineEdit::setCompletionShortcut(const QKeySequence &shortcut)
{
completionShortcut()->setKeySequence(shortcut);
}
void FancyLineEdit::setSpecialCompleter(QCompleter *completer) void FancyLineEdit::setSpecialCompleter(QCompleter *completer)
{ {
QTC_ASSERT(!d->m_historyCompleter, return); QTC_ASSERT(!d->m_historyCompleter, return);
@@ -586,3 +628,5 @@ void IconButton::keyReleaseEvent(QKeyEvent *ke)
} }
} // namespace Utils } // namespace Utils
#include <fancylineedit.moc>

View File

@@ -34,6 +34,7 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QEvent; class QEvent;
class QKeySequence;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { namespace Utils {
@@ -142,6 +143,7 @@ public:
void onEditingFinished(); void onEditingFinished();
static void setCamelCaseNavigationEnabled(bool enabled); static void setCamelCaseNavigationEnabled(bool enabled);
static void setCompletionShortcut(const QKeySequence &shortcut);
protected: protected:
// Custom behaviour can be added here. // Custom behaviour can be added here.

View File

@@ -48,6 +48,7 @@
#include <texteditor/icodestylepreferences.h> #include <texteditor/icodestylepreferences.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <utils/fancylineedit.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/macroexpander.h> #include <utils/macroexpander.h>
@@ -125,6 +126,10 @@ bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMe
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor()) if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor())
editor->editorWidget()->invokeAssist(Completion); editor->editorWidget()->invokeAssist(Completion);
}); });
connect(command, &Command::keySequenceChanged, [command] {
Utils::FancyLineEdit::setCompletionShortcut(command->keySequence());
});
Utils::FancyLineEdit::setCompletionShortcut(command->keySequence());
// Add shortcut for invoking quick fix options // Add shortcut for invoking quick fix options
QAction *quickFixAction = new QAction(tr("Trigger Refactoring Action"), this); QAction *quickFixAction = new QAction(tr("Trigger Refactoring Action"), this);