42 lines
1.4 KiB
C++
42 lines
1.4 KiB
C++
#include "tagcompleter.h"
|
|||
|
|
|
||
|
|
#include <QLineEdit>
|
||
|
|
#include <QStringListModel>
|
||
|
|
|
||
|
|
TagCompleter::TagCompleter(QObject *parent)
|
||
|
|
: QCompleter(parent)
|
||
|
|
, m_model(new QStringListModel(this))
|
||
|
|
{
|
||
|
|
setModel(m_model);
|
||
|
|
setCaseSensitivity(Qt::CaseInsensitive);
|
||
|
|
setFilterMode(Qt::MatchContains); // match anywhere in the tag, like Android
|
||
|
|
setCompletionMode(QCompleter::PopupCompletion);
|
||
|
|
}
|
||
|
|
|
||
|
|
void TagCompleter::setSuggestions(const QStringList &tags)
|
||
|
|
{
|
||
|
|
m_model->setStringList(tags);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Complete against the token after the last comma, ignoring the tags already
|
||
|
|
// entered before it.
|
||
|
|
QStringList TagCompleter::splitPath(const QString &path) const
|
||
|
|
{
|
||
|
|
return QStringList(path.section(QLatin1Char(','), -1).trimmed());
|
||
|
|
}
|
||
|
|
|
||
|
|
// Rebuild the full field value: everything up to and including the last comma,
|
||
|
|
// then the chosen tag and a trailing ", " ready for the next one.
|
||
|
|
QString TagCompleter::pathFromIndex(const QModelIndex &index) const
|
||
|
|
{
|
||
|
|
const QString completion = QCompleter::pathFromIndex(index);
|
||
|
|
const auto *edit = qobject_cast<const QLineEdit *>(widget());
|
||
|
|
if (!edit)
|
||
|
|
return completion;
|
||
|
|
|
||
|
|
const QString text = edit->text();
|
||
|
|
const int lastComma = text.lastIndexOf(QLatin1Char(','));
|
||
|
|
const QString head =
|
||
|
|
lastComma >= 0 ? text.left(lastComma + 1) + QLatin1Char(' ') : QString();
|
||
|
|
return head + completion + QStringLiteral(", ");
|
||
|
|
}
|