forked from qt-creator/qt-creator
Utils: add property CompletingTextEdit::completionLengthThreshold
Change-Id: If68e981511e4c3df3766a2c5040bba8688f6928d Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
This commit is contained in:
committed by
Tobias Hunger
parent
e054dc546c
commit
02470b35c5
@@ -59,7 +59,10 @@ public:
|
|||||||
void insertCompletion(const QString &completion);
|
void insertCompletion(const QString &completion);
|
||||||
QString textUnderCursor() const;
|
QString textUnderCursor() const;
|
||||||
|
|
||||||
|
bool acceptsCompletionPrefix(const QString &prefix) const;
|
||||||
|
|
||||||
QCompleter *m_completer;
|
QCompleter *m_completer;
|
||||||
|
int m_completionLengthThreshold;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CompletingTextEdit *m_backPointer;
|
CompletingTextEdit *m_backPointer;
|
||||||
@@ -67,6 +70,7 @@ private:
|
|||||||
|
|
||||||
CompletingTextEditPrivate::CompletingTextEditPrivate(CompletingTextEdit *textEdit)
|
CompletingTextEditPrivate::CompletingTextEditPrivate(CompletingTextEdit *textEdit)
|
||||||
: m_completer(0),
|
: m_completer(0),
|
||||||
|
m_completionLengthThreshold(3),
|
||||||
m_backPointer(textEdit)
|
m_backPointer(textEdit)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -90,6 +94,11 @@ QString CompletingTextEditPrivate::textUnderCursor() const
|
|||||||
return tc.selectedText();
|
return tc.selectedText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CompletingTextEditPrivate::acceptsCompletionPrefix(const QString &prefix) const
|
||||||
|
{
|
||||||
|
return prefix.length() >= m_completionLengthThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
CompletingTextEdit::CompletingTextEdit(QWidget *parent)
|
CompletingTextEdit::CompletingTextEdit(QWidget *parent)
|
||||||
: QTextEdit(parent),
|
: QTextEdit(parent),
|
||||||
d(new CompletingTextEditPrivate(this))
|
d(new CompletingTextEditPrivate(this))
|
||||||
@@ -121,6 +130,16 @@ QCompleter *CompletingTextEdit::completer() const
|
|||||||
return d->m_completer;
|
return d->m_completer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CompletingTextEdit::completionLengthThreshold() const
|
||||||
|
{
|
||||||
|
return d->m_completionLengthThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompletingTextEdit::setCompletionLengthThreshold(int len)
|
||||||
|
{
|
||||||
|
d->m_completionLengthThreshold = len;
|
||||||
|
}
|
||||||
|
|
||||||
void CompletingTextEdit::keyPressEvent(QKeyEvent *e)
|
void CompletingTextEdit::keyPressEvent(QKeyEvent *e)
|
||||||
{
|
{
|
||||||
if (completer() && completer()->popup()->isVisible()) {
|
if (completer() && completer()->popup()->isVisible()) {
|
||||||
@@ -150,8 +169,8 @@ void CompletingTextEdit::keyPressEvent(QKeyEvent *e)
|
|||||||
const QString newCompletionPrefix = d->textUnderCursor();
|
const QString newCompletionPrefix = d->textUnderCursor();
|
||||||
const QChar lastChar = e->text().isEmpty() ? QChar() : e->text().right(1).at(0);
|
const QChar lastChar = e->text().isEmpty() ? QChar() : e->text().right(1).at(0);
|
||||||
|
|
||||||
if (!isShortcut && (hasModifier || e->text().isEmpty() || newCompletionPrefix.length() < 3
|
if (!isShortcut && (hasModifier || e->text().isEmpty() || isEndOfWordChar(lastChar)
|
||||||
|| isEndOfWordChar(lastChar))) {
|
|| !d->acceptsCompletionPrefix(newCompletionPrefix))) {
|
||||||
completer()->popup()->hide();
|
completer()->popup()->hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ namespace Utils {
|
|||||||
class QTCREATOR_UTILS_EXPORT CompletingTextEdit : public QTextEdit
|
class QTCREATOR_UTILS_EXPORT CompletingTextEdit : public QTextEdit
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(int completionLengthThreshold
|
||||||
|
READ completionLengthThreshold WRITE setCompletionLengthThreshold)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CompletingTextEdit(QWidget *parent = 0);
|
CompletingTextEdit(QWidget *parent = 0);
|
||||||
@@ -54,6 +56,9 @@ public:
|
|||||||
void setCompleter(QCompleter *c);
|
void setCompleter(QCompleter *c);
|
||||||
QCompleter *completer() const;
|
QCompleter *completer() const;
|
||||||
|
|
||||||
|
int completionLengthThreshold() const;
|
||||||
|
void setCompletionLengthThreshold(int len);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void keyPressEvent(QKeyEvent *e);
|
void keyPressEvent(QKeyEvent *e);
|
||||||
void focusInEvent(QFocusEvent *e);
|
void focusInEvent(QFocusEvent *e);
|
||||||
|
|||||||
Reference in New Issue
Block a user