mirror of
https://github.com/zealdocs/zeal.git
synced 2024-11-30 03:13:56 +03:00
Adds autocompletion hint
When the user starts typing a prefix a hint will show in the search field.
This commit is contained in:
parent
cfe87ad58c
commit
a8d81b568c
@ -2,13 +2,18 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QKeyEvent>
|
||||
#include <QDebug>
|
||||
#include <QCompleter>
|
||||
|
||||
#include "../zealsearchquery.h"
|
||||
|
||||
ZealSearchEdit::ZealSearchEdit(QWidget *parent) :
|
||||
LineEdit(parent)
|
||||
{
|
||||
completionLabel = new QLabel(this);
|
||||
completionLabel->setObjectName("completer");
|
||||
completionLabel->setStyleSheet("QLabel#completer { color: gray; }");
|
||||
completionLabel->setFont(this->font());
|
||||
|
||||
connect(this, &ZealSearchEdit::textChanged, this, &ZealSearchEdit::showCompletions);
|
||||
}
|
||||
|
||||
void ZealSearchEdit::setTreeView(QTreeView *view)
|
||||
@ -21,10 +26,10 @@ void ZealSearchEdit::setTreeView(QTreeView *view)
|
||||
// Makes the line edit use autocompletions.
|
||||
void ZealSearchEdit::setCompletions(QStringList completions)
|
||||
{
|
||||
QCompleter *completer = new QCompleter(completions, this);
|
||||
completer->setCompletionMode(QCompleter::InlineCompletion);
|
||||
completer->setCaseSensitivity(Qt::CaseInsensitive);
|
||||
this->setCompleter(completer);
|
||||
prefixCompleter = new QCompleter(completions, this);
|
||||
prefixCompleter->setCompletionMode(QCompleter::InlineCompletion);
|
||||
prefixCompleter->setCaseSensitivity(Qt::CaseInsensitive);
|
||||
prefixCompleter->setWidget(this);
|
||||
}
|
||||
|
||||
// Clear input with consideration to docset filters
|
||||
@ -61,9 +66,10 @@ bool ZealSearchEdit::eventFilter(QObject *obj, QEvent *ev)
|
||||
|
||||
// Autocompletes the prefixes.
|
||||
if (keyEvent->key() == Qt::Key_Tab) {
|
||||
QString currentCompletion = this->completer()->currentCompletion();
|
||||
if (!currentCompletion.isEmpty()) {
|
||||
this->setText(currentCompletion);
|
||||
QString currentText = text();
|
||||
QString completed = this->currentCompletion(currentText);
|
||||
if (!completed.isEmpty()) {
|
||||
this->setText(completed);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -71,6 +77,13 @@ bool ZealSearchEdit::eventFilter(QObject *obj, QEvent *ev)
|
||||
return LineEdit::eventFilter(obj, ev);
|
||||
}
|
||||
|
||||
void ZealSearchEdit::resizeEvent(QResizeEvent *ev)
|
||||
{
|
||||
QString text = this->text();
|
||||
showCompletions(text);
|
||||
LineEdit::resizeEvent(ev);
|
||||
}
|
||||
|
||||
void ZealSearchEdit::focusInEvent(QFocusEvent * evt)
|
||||
{
|
||||
// Focus on the widget.
|
||||
@ -94,3 +107,27 @@ void ZealSearchEdit::mousePressEvent(QMouseEvent *ev)
|
||||
}
|
||||
focusing = false;
|
||||
}
|
||||
|
||||
QString ZealSearchEdit::currentCompletion(const QString &text)
|
||||
{
|
||||
if (text.isEmpty()) {
|
||||
return QString();
|
||||
} else {
|
||||
return this->prefixCompleter->currentCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
void ZealSearchEdit::showCompletions(const QString &newValue)
|
||||
{
|
||||
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
||||
int textWidth = fontMetrics().width(newValue);
|
||||
|
||||
this->prefixCompleter->setCompletionPrefix(this->text());
|
||||
|
||||
QString completed = this->currentCompletion(newValue).mid(newValue.size());
|
||||
QSize labelSize(fontMetrics().width(completed), size().height());
|
||||
|
||||
completionLabel->setMinimumSize(labelSize);
|
||||
completionLabel->move(frameWidth + 2 + textWidth, frameWidth);
|
||||
completionLabel->setText(completed);
|
||||
}
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
#include <QEvent>
|
||||
#include <QTreeView>
|
||||
#include <QCompleter>
|
||||
#include <QLabel>
|
||||
#include "../lineedit.h"
|
||||
|
||||
class ZealSearchEdit : public LineEdit
|
||||
@ -18,14 +20,19 @@ protected:
|
||||
bool eventFilter(QObject *obj, QEvent *ev);
|
||||
void focusInEvent(QFocusEvent *);
|
||||
void mousePressEvent(QMouseEvent *ev);
|
||||
void resizeEvent(QResizeEvent *ev);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
void clear();
|
||||
QString currentCompletion(const QString &text);
|
||||
void showCompletions(const QString &text);
|
||||
|
||||
private:
|
||||
QCompleter *prefixCompleter;
|
||||
QTreeView *treeView;
|
||||
QLabel *completionLabel;
|
||||
bool focusing;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user