4c75f9e
/*
4c75f9e
    Copyright (c) 2017, Lukas Holecek <hluk@email.cz>
4c75f9e
4c75f9e
    This file is part of CopyQ.
4c75f9e
4c75f9e
    CopyQ is free software: you can redistribute it and/or modify
4c75f9e
    it under the terms of the GNU General Public License as published by
4c75f9e
    the Free Software Foundation, either version 3 of the License, or
4c75f9e
    (at your option) any later version.
4c75f9e
4c75f9e
    CopyQ is distributed in the hope that it will be useful,
4c75f9e
    but WITHOUT ANY WARRANTY; without even the implied warranty of
4c75f9e
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4c75f9e
    GNU General Public License for more details.
4c75f9e
4c75f9e
    You should have received a copy of the GNU General Public License
4c75f9e
    along with CopyQ.  If not, see <http://www.gnu.org/licenses/>.
4c75f9e
*/
4c75f9e
4c75f9e
#include "filtercompleter.h"
4c75f9e
4c75f9e
#include <QAbstractListModel>
4c75f9e
#include <QApplication>
4c75f9e
#include <QAction>
4c75f9e
#include <QLineEdit>
4c75f9e
#include <QMoveEvent>
4c75f9e
4c75f9e
namespace {
4c75f9e
4c75f9e
const int maxCompletionItems = 100;
4c75f9e
4c75f9e
class CompletionModel : public QAbstractListModel
4c75f9e
{
4c75f9e
public:
4c75f9e
    explicit CompletionModel(QObject *parent)
4c75f9e
        : QAbstractListModel(parent)
4c75f9e
    {
4c75f9e
    }
4c75f9e
4c75f9e
    int rowCount(const QModelIndex &parent = QModelIndex()) const override
4c75f9e
    {
4c75f9e
        return parent.isValid() ? 0 : m_items.size();
4c75f9e
    }
4c75f9e
4c75f9e
    QVariant data(const QModelIndex &index, int role) const override
4c75f9e
    {
4c75f9e
        if (index.isValid() && (role == Qt::EditRole || role == Qt::DisplayRole))
4c75f9e
            return m_items[index.row()];
4c75f9e
4c75f9e
        return QVariant();
4c75f9e
    }
4c75f9e
4c75f9e
    bool setData(const QModelIndex &index, const QVariant &value, int role) override
4c75f9e
    {
4c75f9e
        if (!index.isValid() && role == Qt::EditRole) {
4c75f9e
            const QString text = value.toString();
4c75f9e
            removeAll(text);
4c75f9e
            crop(maxCompletionItems - 1);
4c75f9e
            prepend(text);
4c75f9e
        }
4c75f9e
4c75f9e
        return false;
4c75f9e
    }
4c75f9e
4c75f9e
    bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
4c75f9e
    {
4c75f9e
        const auto end = row + count;
4c75f9e
        if ( parent.isValid() || row < 0 || end > rowCount() )
4c75f9e
            return false;
4c75f9e
4c75f9e
        beginRemoveRows(QModelIndex(), row, end);
4c75f9e
        m_items.erase( m_items.begin() + row, m_items.begin() + end );
4c75f9e
        endRemoveRows();
4c75f9e
4c75f9e
        return true;
4c75f9e
    }
4c75f9e
4c75f9e
private:
4c75f9e
    void prepend(const QString &text)
4c75f9e
    {
4c75f9e
        beginInsertRows(QModelIndex(), 0, 0);
4c75f9e
        m_items.prepend(text);
4c75f9e
        endInsertRows();
4c75f9e
    }
4c75f9e
4c75f9e
    void removeAll(const QString &text)
4c75f9e
    {
4c75f9e
        for ( int row = m_items.indexOf(text);
4c75f9e
              row != -1;
4c75f9e
              row = m_items.indexOf(text, row) )
4c75f9e
        {
4c75f9e
            removeRows(row, 1);
4c75f9e
        }
4c75f9e
    }
4c75f9e
4c75f9e
    void crop(int maxItems)
4c75f9e
    {
4c75f9e
        const int itemCount = m_items.size();
4c75f9e
        if (itemCount > maxItems)
4c75f9e
            removeRows(maxItems, itemCount - maxItems);
4c75f9e
    }
4c75f9e
4c75f9e
    QStringList m_items;
4c75f9e
};
4c75f9e
4c75f9e
} // namespace
4c75f9e
4c75f9e
void FilterCompleter::installCompleter(QLineEdit *lineEdit)
4c75f9e
{
4c75f9e
    Q_ASSERT(lineEdit);
4c75f9e
    new FilterCompleter(lineEdit);
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::removeCompleter(QLineEdit *lineEdit)
4c75f9e
{
4c75f9e
    lineEdit->setCompleter(nullptr);
4c75f9e
}
4c75f9e
4c75f9e
QStringList FilterCompleter::history() const
4c75f9e
{
4c75f9e
    QStringList history;
4c75f9e
4c75f9e
    for (int i = 0; i < model()->rowCount(); ++i) {
4c75f9e
        const QModelIndex index = model()->index(i, 0);
4c75f9e
        history.append( index.data(Qt::EditRole).toString() );
4c75f9e
    }
4c75f9e
4c75f9e
    return history;
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::setHistory(const QStringList &history)
4c75f9e
{
4c75f9e
    model()->removeRows( 0, model()->rowCount() );
4c75f9e
    for (int i = history.size() - 1; i >= 0; --i)
4c75f9e
        prependItem(history[i]);
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::onTextEdited(const QString &text)
4c75f9e
{
4c75f9e
    m_lastText = text;
4c75f9e
    setUnfiltered(false);
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::onEditingFinished()
4c75f9e
{
4c75f9e
    prependItem(m_lastText);
4c75f9e
    m_lastText.clear();
4c75f9e
4c75f9e
    setUnfiltered(false);
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::onComplete()
4c75f9e
{
4c75f9e
    if (m_lineEdit->text().isEmpty()) {
4c75f9e
        setUnfiltered(true);
4c75f9e
        const QModelIndex firstIndex = model()->index(0, 0);
4c75f9e
        const QString text = model()->data(firstIndex, Qt::EditRole).toString();
4c75f9e
        m_lineEdit->setText(text);
4c75f9e
    } else {
4c75f9e
        complete();
4c75f9e
    }
4c75f9e
}
4c75f9e
4c75f9e
FilterCompleter::FilterCompleter(QLineEdit *lineEdit)
4c75f9e
    : QCompleter(lineEdit)
4c75f9e
    , m_lineEdit(lineEdit)
4c75f9e
{
4c75f9e
    setModel(new CompletionModel(this));
4c75f9e
    setWrapAround(true);
4c75f9e
    setUnfiltered(false);
4c75f9e
4c75f9e
    QWidget *window = lineEdit->window();
4c75f9e
    if (window) {
4c75f9e
        auto act = new QAction(this);
4c75f9e
        act->setShortcut(tr("Alt+Down", "Filter completion shortcut"));
4c75f9e
        connect(act, SIGNAL(triggered()), this, SLOT(onComplete()));
4c75f9e
        window->addAction(act);
4c75f9e
    }
4c75f9e
4c75f9e
    // Postpone prepending item to completion list because incorrect
4c75f9e
    // item will be completed otherwise (prepending shifts rows).
4c75f9e
    connect( lineEdit, SIGNAL(editingFinished()),
4c75f9e
             this, SLOT(onEditingFinished()), Qt::QueuedConnection );
4c75f9e
    connect( lineEdit, SIGNAL(textEdited(QString)),
4c75f9e
             this, SLOT(onTextEdited(QString)) );
4c75f9e
4c75f9e
    lineEdit->setCompleter(this);
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::setUnfiltered(bool unfiltered)
4c75f9e
{
4c75f9e
    setCompletionMode(unfiltered ? QCompleter::UnfilteredPopupCompletion
4c75f9e
                                 : QCompleter::PopupCompletion);
4c75f9e
}
4c75f9e
4c75f9e
void FilterCompleter::prependItem(const QString &item)
4c75f9e
{
4c75f9e
    if ( !item.isEmpty() )
4c75f9e
        model()->setData(QModelIndex(), item);
4c75f9e
}