Add ScriptProxyModel
parent
a9c394b577
commit
a45f32a1f1
|
|
@ -111,6 +111,7 @@
|
|||
#include "types/script.h"
|
||||
#include "types/scripts.h"
|
||||
#include "types/types.h"
|
||||
#include "models/scriptsproxymodel.h"
|
||||
#include "usermanager.h"
|
||||
#include "types/tokeninfos.h"
|
||||
#include "types/tokeninfo.h"
|
||||
|
|
@ -319,6 +320,7 @@ void registerQmlTypes() {
|
|||
qmlRegisterUncreatableType<ScriptManager>(uri, 1, 0, "ScriptManager", "Get it from Engine");
|
||||
qmlRegisterUncreatableType<Scripts>(uri, 1, 0, "Scripts", "Getit from ScriptManager");
|
||||
qmlRegisterUncreatableType<Script>(uri, 1, 0, "Script", "Getit from Scripts");
|
||||
qmlRegisterType<ScriptsProxyModel>(uri, 1, 0, "ScriptsProxyModel");
|
||||
qmlRegisterType<ScriptSyntaxHighlighter>(uri, 1, 0, "ScriptSyntaxHighlighter");
|
||||
qmlRegisterType<CodeCompletion>(uri, 1, 0, "CodeCompletion");
|
||||
qmlRegisterUncreatableType<CompletionProxyModel>(uri, 1, 0, "CompletionModel", "Get it from ScriptSyntaxHighlighter");
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ INCLUDEPATH += \
|
|||
$$top_srcdir/QtZeroConf
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/models/scriptsproxymodel.cpp \
|
||||
$${PWD}/logging.cpp \
|
||||
$${PWD}/applogcontroller.cpp \
|
||||
$${PWD}/wifisetup/btwifisetup.cpp \
|
||||
|
|
@ -165,6 +166,7 @@ SOURCES += \
|
|||
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/models/scriptsproxymodel.h \
|
||||
$${PWD}/logging.h \
|
||||
$${PWD}/applogcontroller.h \
|
||||
$${PWD}/wifisetup/btwifisetup.h \
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
#include "scriptsproxymodel.h"
|
||||
|
||||
#include "types/script.h"
|
||||
|
||||
ScriptsProxyModel::ScriptsProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Scripts *ScriptsProxyModel::scripts() const
|
||||
{
|
||||
return m_scripts;
|
||||
}
|
||||
|
||||
void ScriptsProxyModel::setScripts(Scripts *scripts)
|
||||
{
|
||||
if (m_scripts != scripts) {
|
||||
if (m_scripts) {
|
||||
disconnect(m_scripts, &Scripts::countChanged, this, &ScriptsProxyModel::countChanged);
|
||||
}
|
||||
m_scripts = scripts;
|
||||
setSourceModel(scripts);
|
||||
emit scriptsChanged();
|
||||
|
||||
if (m_scripts) {
|
||||
connect(m_scripts, &Scripts::countChanged, this, &ScriptsProxyModel::countChanged);
|
||||
}
|
||||
|
||||
emit countChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ScriptsProxyModel::filterName() const
|
||||
{
|
||||
return m_filterName;
|
||||
}
|
||||
|
||||
void ScriptsProxyModel::setFilterName(const QString &filterName)
|
||||
{
|
||||
if (m_filterName != filterName) {
|
||||
m_filterName = filterName;
|
||||
emit filterNameChanged();
|
||||
invalidateFilter();
|
||||
emit countChanged();
|
||||
}
|
||||
}
|
||||
|
||||
Script *ScriptsProxyModel::get(int index) const
|
||||
{
|
||||
return m_scripts->get(mapToSource(this->index(index, 0)).row());
|
||||
}
|
||||
|
||||
bool ScriptsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
||||
{
|
||||
Q_UNUSED(sourceParent)
|
||||
Script *script = m_scripts->get(sourceRow);
|
||||
if (!m_filterName.isEmpty()) {
|
||||
if (!script->name().contains(m_filterName)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
#ifndef SCRIPTSPROXYMODEL_H
|
||||
#define SCRIPTSPROXYMODEL_H
|
||||
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "types/scripts.h"
|
||||
|
||||
class ScriptsProxyModel : public QSortFilterProxyModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
|
||||
Q_PROPERTY(Scripts* scripts READ scripts WRITE setScripts NOTIFY scriptsChanged)
|
||||
|
||||
Q_PROPERTY(QString filterName READ filterName WRITE setFilterName NOTIFY filterNameChanged)
|
||||
|
||||
public:
|
||||
explicit ScriptsProxyModel(QObject *parent = nullptr);
|
||||
|
||||
Scripts* scripts() const;
|
||||
void setScripts(Scripts *scripts);
|
||||
|
||||
QString filterName() const;
|
||||
void setFilterName(const QString &filterName);
|
||||
|
||||
Script* get(int index) const;
|
||||
|
||||
signals:
|
||||
void countChanged();
|
||||
void scriptsChanged();
|
||||
void filterNameChanged();
|
||||
|
||||
protected:
|
||||
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
|
||||
|
||||
private:
|
||||
Scripts* m_scripts = nullptr;
|
||||
|
||||
QString m_filterName;
|
||||
};
|
||||
|
||||
#endif // SCRIPTSPROXYMODEL_H
|
||||
|
|
@ -33,6 +33,9 @@
|
|||
#include "types/script.h"
|
||||
#include "types/scripts.h"
|
||||
|
||||
#include "logging.h"
|
||||
NYMEA_LOGGING_CATEGORY(dcScriptManager, "Scripts")
|
||||
|
||||
ScriptManager::ScriptManager(JsonRpcClient *jsonClient, QObject *parent):
|
||||
JsonHandler(parent),
|
||||
m_client(jsonClient)
|
||||
|
|
@ -46,6 +49,13 @@ void ScriptManager::init()
|
|||
{
|
||||
m_scripts->clear();
|
||||
m_client->sendCommand("Scripts.GetScripts", QVariantMap(), this, "onScriptsFetched");
|
||||
m_fetchingData = true;
|
||||
emit fetchingDataChanged();
|
||||
}
|
||||
|
||||
bool ScriptManager::fetchingData() const
|
||||
{
|
||||
return m_fetchingData;
|
||||
}
|
||||
|
||||
QString ScriptManager::nameSpace() const
|
||||
|
|
@ -98,12 +108,15 @@ int ScriptManager::fetchScript(const QUuid &id)
|
|||
|
||||
void ScriptManager::onScriptsFetched(int /*commandId*/, const QVariantMap ¶ms)
|
||||
{
|
||||
qCDebug(dcScriptManager()) << "Scripts fetched";
|
||||
foreach (const QVariant &variant, params.value("scripts").toList()) {
|
||||
QUuid id = variant.toMap().value("id").toUuid();
|
||||
Script *script = new Script(id);
|
||||
script->setName(variant.toMap().value("name").toString());
|
||||
m_scripts->addScript(script);
|
||||
}
|
||||
m_fetchingData = false;
|
||||
emit fetchingDataChanged();
|
||||
}
|
||||
|
||||
void ScriptManager::onScriptFetched(int commandId, const QVariantMap ¶ms)
|
||||
|
|
|
|||
|
|
@ -41,11 +41,13 @@ class ScriptManager : public JsonHandler
|
|||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(Scripts* scripts READ scripts CONSTANT)
|
||||
Q_PROPERTY(bool fetchingData READ fetchingData NOTIFY fetchingDataChanged)
|
||||
|
||||
public:
|
||||
explicit ScriptManager(JsonRpcClient* jsonClient, QObject *parent = nullptr);
|
||||
|
||||
void init();
|
||||
bool fetchingData() const;
|
||||
|
||||
QString nameSpace() const override;
|
||||
|
||||
|
|
@ -66,6 +68,7 @@ signals:
|
|||
void fetchScriptReply(int id, const QString &scriptError, const QString &content);
|
||||
|
||||
void scriptMessage(const QUuid &scriptId, const QString &type, const QString &message);
|
||||
void fetchingDataChanged();
|
||||
|
||||
private slots:
|
||||
void onScriptsFetched(int commandId, const QVariantMap ¶ms);
|
||||
|
|
@ -79,6 +82,7 @@ private slots:
|
|||
private:
|
||||
JsonRpcClient* m_client = nullptr;
|
||||
Scripts *m_scripts = nullptr;
|
||||
bool m_fetchingData = false;
|
||||
};
|
||||
|
||||
#endif // SCRIPTMANAGER_H
|
||||
|
|
|
|||
Loading…
Reference in New Issue