feat: add autocompletion for commands

partially copied code from emojimodel.h
This commit is contained in:
Srevin Saju
2021-05-01 13:27:54 +03:00
parent 57684aa454
commit ee595ed374
2 changed files with 90 additions and 0 deletions

32
src/commandmodel.cpp Normal file
View File

@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2021 Srevin Saju <srevinsaju@sugarlabs.org>
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QDebug>
#include "commandmodel.h"
QVariantList CommandModel::filterModel(const QString &filter)
{
QVariantList result;
for (const QVariant &e : matrix) {
auto command = qvariant_cast<Command>(e);
if (command.command.startsWith(filter)) {
result.append(e);
if (result.length() > 10) {
return result;
}
}
}
return result;
}
// the help messages are taken from Element (web matrix client, app.element.io)
const QVariantList CommandModel::matrix = {
QVariant::fromValue(Command{"/join", "Join a given room with address"}),
QVariant::fromValue(Command{"/me", "Displays action"}),
};

58
src/commandmodel.h Normal file
View File

@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2021 Srevin Saju <srevinsaju@sugarlabs.org>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <QObject>
#include <QSettings>
#include <QVariant>
#include <QVector>
#include <utility>
struct Command {
Command(QString u, QString s)
: command(std::move(std::move(u)))
, help(std::move(std::move(s)))
{
}
Command() = default;
friend QDataStream &operator<<(QDataStream &arch, const Command &object)
{
arch << object.command;
arch << object.help;
return arch;
}
friend QDataStream &operator>>(QDataStream &arch, Command &object)
{
arch >> object.command;
arch >> object.help;
return arch;
}
QString command;
QString help;
Q_GADGET
Q_PROPERTY(QString command MEMBER command)
Q_PROPERTY(QString help MEMBER help)
};
Q_DECLARE_METATYPE(Command)
class CommandModel : public QObject
{
Q_OBJECT
public:
explicit CommandModel(QObject *parent = nullptr)
: QObject(parent)
{
}
Q_INVOKABLE static QVariantList filterModel(const QString &filter);
private:
static const QVariantList matrix;
};