forked from qt-creator/qt-creator
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:
@@ -36,7 +36,9 @@
|
||||
#include <QAbstractItemView>
|
||||
#include <QDebug>
|
||||
#include <QKeyEvent>
|
||||
#include <QKeySequence>
|
||||
#include <QMenu>
|
||||
#include <QShortcut>
|
||||
#include <QStylePainter>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QStyle>
|
||||
@@ -83,6 +85,29 @@ namespace Utils {
|
||||
|
||||
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
|
||||
class FancyLineEditPrivate : public QObject
|
||||
{
|
||||
@@ -94,6 +119,7 @@ public:
|
||||
FancyLineEdit *m_lineEdit;
|
||||
IconButton *m_iconbutton[2];
|
||||
HistoryCompleter *m_historyCompleter = nullptr;
|
||||
QShortcut m_completionShortcut;
|
||||
FancyLineEdit::ValidationFunction m_validationFunction = &FancyLineEdit::validateWithValidator;
|
||||
QString m_oldText;
|
||||
QMenu *m_menu[2];
|
||||
@@ -113,10 +139,15 @@ public:
|
||||
|
||||
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
|
||||
QObject(parent),
|
||||
m_lineEdit(parent)
|
||||
m_lineEdit(parent),
|
||||
m_completionShortcut(completionShortcut()->key(), parent)
|
||||
{
|
||||
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) {
|
||||
m_iconbutton[i] = new IconButton(parent);
|
||||
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[Right], &QAbstractButton::clicked, this, &FancyLineEdit::iconClicked);
|
||||
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()
|
||||
@@ -349,6 +386,11 @@ void FancyLineEdit::setCamelCaseNavigationEnabled(bool enabled)
|
||||
camelCaseNavigation = enabled;
|
||||
}
|
||||
|
||||
void FancyLineEdit::setCompletionShortcut(const QKeySequence &shortcut)
|
||||
{
|
||||
completionShortcut()->setKeySequence(shortcut);
|
||||
}
|
||||
|
||||
void FancyLineEdit::setSpecialCompleter(QCompleter *completer)
|
||||
{
|
||||
QTC_ASSERT(!d->m_historyCompleter, return);
|
||||
@@ -586,3 +628,5 @@ void IconButton::keyReleaseEvent(QKeyEvent *ke)
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
|
||||
#include <fancylineedit.moc>
|
||||
|
@@ -34,6 +34,7 @@
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QEvent;
|
||||
class QKeySequence;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Utils {
|
||||
@@ -142,6 +143,7 @@ public:
|
||||
void onEditingFinished();
|
||||
|
||||
static void setCamelCaseNavigationEnabled(bool enabled);
|
||||
static void setCompletionShortcut(const QKeySequence &shortcut);
|
||||
|
||||
protected:
|
||||
// Custom behaviour can be added here.
|
||||
|
@@ -48,6 +48,7 @@
|
||||
#include <texteditor/icodestylepreferences.h>
|
||||
#include <texteditor/tabsettings.h>
|
||||
|
||||
#include <utils/fancylineedit.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/macroexpander.h>
|
||||
|
||||
@@ -125,6 +126,10 @@ bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMe
|
||||
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor())
|
||||
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
|
||||
QAction *quickFixAction = new QAction(tr("Trigger Refactoring Action"), this);
|
||||
|
Reference in New Issue
Block a user