Skip to content
Permalink
Browse files

Formatted with clang Google style

  • Loading branch information
zehnm committed Dec 30, 2019
1 parent 71c863e commit fde7f151a4af639a7efd1224932bd0983857ba63
Showing with 252 additions and 350 deletions.
  1. +11 βˆ’0 .clang-format
  2. +34 βˆ’55 sources/config.cpp
  3. +51 βˆ’81 sources/config.h
  4. +9 βˆ’17 sources/hardware/hardwarefactory.cpp
  5. +8 βˆ’7 sources/hardware/hardwarefactory.h
  6. +28 βˆ’49 sources/jsonfile.cpp
  7. +25 βˆ’23 sources/jsonfile.h
  8. +22 βˆ’25 sources/main.cpp
  9. +38 βˆ’66 sources/yioapi.cpp
  10. +26 βˆ’27 sources/yioapi.h
@@ -0,0 +1,11 @@
# YIO project style configuration
#
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html

BasedOnStyle: Google

IndentWidth: 4
ColumnLimit: 120

AccessModifierOffset: -3
AlignConsecutiveDeclarations: true
@@ -24,14 +24,11 @@

#include "config.h"

ConfigInterface::~ConfigInterface()
{}
Config* Config::s_instance = nullptr;

Config::Config(QQmlApplicationEngine *engine, QString path, QString schemaPath)
: m_engine(engine)
, m_error("")
{
Config *Config::s_instance = nullptr;

ConfigInterface::~ConfigInterface() {}

Config::Config(QQmlApplicationEngine *engine, QString path, QString schemaPath) : m_engine(engine), m_error("") {
m_jsf = new JsonFile();
m_jsf->setSchemaPath(schemaPath);
s_instance = this;
@@ -40,28 +37,25 @@ Config::Config(QQmlApplicationEngine *engine, QString path, QString schemaPath)
readConfig(path);
}

Config::~Config()
{
s_instance = nullptr;
}
Config::~Config() { s_instance = nullptr; }

void Config::setFavorite(const QString& entityId, bool value)
{
void Config::setFavorite(const QString &entityId, bool value) {
QStringList fav = profileFavorites();

if (value && !fav.contains(entityId))
if (value && !fav.contains(entityId)) {
fav.append(entityId);
}

if (!value && fav.contains(entityId))
if (!value && fav.contains(entityId)) {
fav.removeOne(entityId);
}

m_cacheUIProfile.insert("favorites", fav);
writeConfig();
emit profileFavoritesChanged();
}

void Config::setConfig(const QVariantMap &config)
{
void Config::setConfig(const QVariantMap &config) {
m_error.clear();
if (!m_jsf->validate(QJsonDocument::fromVariant(config), m_error)) {
return;
@@ -72,14 +66,9 @@ void Config::setConfig(const QVariantMap &config)
emit configChanged();
}

QVariant Config::getContextProperty(const QString &name)
{
return m_engine->rootContext()->contextProperty(name);
}

QVariant Config::getContextProperty(const QString &name) { return m_engine->rootContext()->contextProperty(name); }

bool Config::readConfig(const QString &path)
{
bool Config::readConfig(const QString &path) {
// load the config.json file from the filesystem
m_jsf->setName(path + "/config.json");
m_config = m_jsf->read().toMap();
@@ -90,8 +79,7 @@ bool Config::readConfig(const QString &path)
return m_jsf->isValid();
}

bool Config::writeConfig()
{
bool Config::writeConfig() {
syncCacheToConfig();
bool result = m_jsf->write(m_config);
m_error = m_jsf->error();
@@ -104,35 +92,29 @@ void Config::setSettings(const QVariantMap &config) {
writeConfig();
}

void Config::setUIConfig(const QVariantMap &config)
{
void Config::setUIConfig(const QVariantMap &config) {
m_cacheUIConfig = config;
emit uiConfigChanged();
writeConfig();
}

QObject *Config::getQMLObject(QList<QObject*> nodes, const QString &name)
{
for (int i=0; i < nodes.size(); i++)
{
QObject *Config::getQMLObject(QList<QObject *> nodes, const QString &name) {
for (int i = 0; i < nodes.size(); i++) {
if (nodes.at(i) && nodes.at(i)->objectName() == name) {
return dynamic_cast<QObject*>(nodes.at(i));
return dynamic_cast<QObject *>(nodes.at(i));
} else if (nodes.at(i) && nodes.at(i)->children().size() > 0) {
QObject* item = getQMLObject(nodes.at(i)->children(), name);
if (item)
QObject *item = getQMLObject(nodes.at(i)->children(), name);
if (item) {
return item;
}
}
}
return nullptr;
return nullptr;
}

QObject *Config::getQMLObject(const QString &name)
{
return getQMLObject(m_engine->rootObjects(), name);
}
QObject *Config::getQMLObject(const QString &name) { return getQMLObject(m_engine->rootObjects(), name); }

void Config::setProfile(QString id)
{
void Config::setProfile(QString id) {
QVariantMap p = getUIConfig();
p.insert("selected_profile", id);
m_config.insert("ui_config", p);
@@ -141,22 +123,19 @@ void Config::setProfile(QString id)
emit profileChanged();
}

void Config::syncConfigToCache() {
m_cacheSettings = m_config["settings"].toMap();
m_cacheUIConfig = m_config["ui_config"].toMap();

void Config::syncConfigToCache()
{
m_cacheSettings = m_config["settings"].toMap();
m_cacheUIConfig = m_config["ui_config"].toMap();

m_cacheProfile = m_cacheUIConfig["selected_profile"].toString();
m_cacheUIProfiles = m_cacheUIConfig["profiles"].toMap();
m_cacheUIPages = m_cacheUIConfig["pages"].toMap();
m_cacheUIGroups = m_cacheUIConfig["groups"].toMap();
m_cacheProfile = m_cacheUIConfig["selected_profile"].toString();
m_cacheUIProfiles = m_cacheUIConfig["profiles"].toMap();
m_cacheUIPages = m_cacheUIConfig["pages"].toMap();
m_cacheUIGroups = m_cacheUIConfig["groups"].toMap();

m_cacheUIProfile = m_cacheUIProfiles[m_cacheProfile].toMap();
m_cacheUIProfile = m_cacheUIProfiles[m_cacheProfile].toMap();
}

void Config::syncCacheToConfig()
{
void Config::syncCacheToConfig() {
m_cacheUIProfiles.insert(m_cacheProfile, m_cacheUIProfile);

m_cacheUIConfig.insert("selected_profile", m_cacheProfile);
@@ -25,113 +25,83 @@

#include <QtDebug>

#include <QJsonArray>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QJsonArray>
#include "configinterface.h"
#include "jsonfile.h"

class Config : public QObject, ConfigInterface
{
class Config : public QObject, ConfigInterface {
Q_OBJECT
Q_INTERFACES(ConfigInterface)

public:
Q_PROPERTY(bool valid READ isValid CONSTANT)
Q_PROPERTY(QString error READ getError CONSTANT)
Q_PROPERTY(QVariantMap config READ config WRITE setConfig NOTIFY configChanged)
Q_PROPERTY(QString profile READ profile WRITE setProfile NOTIFY profileChanged)
Q_PROPERTY(QStringList profileFavorites READ profileFavorites NOTIFY profileFavoritesChanged)
Q_PROPERTY(QVariantMap settings READ getSettings WRITE setSettings NOTIFY settingsChanged)
Q_PROPERTY(QVariantMap integrations READ getIntegrations CONSTANT)
Q_PROPERTY(QVariantMap entities READ getAllEntities CONSTANT)
Q_PROPERTY(QVariantMap profiles READ getProfiles NOTIFY profilesChanged)
Q_PROPERTY(QVariantMap ui_config READ getUIConfig WRITE setUIConfig NOTIFY uiConfigChanged)
Q_PROPERTY(QVariantMap pages READ getPages NOTIFY pagesChanged)
Q_PROPERTY(QVariantMap groups READ getGroups NOTIFY groupsChanged)

Q_INVOKABLE bool readConfig(const QString &path);
Q_PROPERTY(bool valid READ isValid CONSTANT)
Q_PROPERTY(QString error READ getError CONSTANT)
Q_PROPERTY(QVariantMap config READ config WRITE setConfig NOTIFY configChanged)
Q_PROPERTY(QString profile READ profile WRITE setProfile NOTIFY profileChanged)
Q_PROPERTY(QStringList profileFavorites READ profileFavorites NOTIFY profileFavoritesChanged)
Q_PROPERTY(QVariantMap settings READ getSettings WRITE setSettings NOTIFY settingsChanged)
Q_PROPERTY(QVariantMap integrations READ getIntegrations CONSTANT)
Q_PROPERTY(QVariantMap entities READ getAllEntities CONSTANT)
Q_PROPERTY(QVariantMap profiles READ getProfiles NOTIFY profilesChanged)
Q_PROPERTY(QVariantMap ui_config READ getUIConfig WRITE setUIConfig NOTIFY uiConfigChanged)
Q_PROPERTY(QVariantMap pages READ getPages NOTIFY pagesChanged)
Q_PROPERTY(QVariantMap groups READ getGroups NOTIFY groupsChanged)

Q_INVOKABLE bool readConfig(const QString& path);
Q_INVOKABLE bool writeConfig();

// Shortcuts to get the config items, and to decouple a bit from Json structure
// Please avoid old access via read property
QVariantMap getIntegrations() {
return m_config["integrations"].toMap();
}
Q_INVOKABLE QVariantMap getIntegration(const QString& type) {
return getIntegrations().value(type).toMap();
}
QVariantMap getAllEntities() {
return m_config["entities"].toMap();
}
Q_INVOKABLE QVariantList getEntities(const QString& type) {
return getAllEntities().value(type).toList();
}
QVariantMap getIntegrations() { return m_config["integrations"].toMap(); }
QVariantMap getAllEntities() { return m_config["entities"].toMap(); }

Q_INVOKABLE QVariantMap getIntegration(const QString& type) { return getIntegrations().value(type).toMap(); }
Q_INVOKABLE QVariantList getEntities(const QString& type) { return getAllEntities().value(type).toList(); }

// Assuming that the following config items are accessed quite often they are cached
QVariantMap getSettings() { return m_cacheSettings; }

void setSettings(const QVariantMap& config);

QVariantMap getUIConfig() {
return m_cacheUIConfig;
}
void setUIConfig(const QVariantMap& config);

QVariantMap getProfiles() {
return m_cacheUIProfiles;
}
Q_INVOKABLE QVariantMap getProfile(const QString& profile) {
return m_cacheUIProfiles.value(profile).toMap();
}
QVariantMap getPages() {
return m_cacheUIPages;
}
Q_INVOKABLE QVariantMap getPage(const QString& pageId) {
return m_cacheUIPages.value(pageId).toMap();
}
QVariantMap getGroups() {
return m_cacheUIGroups;
}
Q_INVOKABLE QVariantMap getGroup(const QString& groupId) {
return m_cacheUIGroups.value(groupId).toMap();
}
QVariantMap getUIConfig() { return m_cacheUIConfig; }
void setUIConfig(const QVariantMap& config);
QVariantMap getProfiles() { return m_cacheUIProfiles; }
QVariantMap getPages() { return m_cacheUIPages; }
QVariantMap getGroups() { return m_cacheUIGroups; }

Q_INVOKABLE QVariantMap getProfile(const QString& profile) { return m_cacheUIProfiles.value(profile).toMap(); }
Q_INVOKABLE QVariantMap getPage(const QString& pageId) { return m_cacheUIPages.value(pageId).toMap(); }
Q_INVOKABLE QVariantMap getGroup(const QString& groupId) { return m_cacheUIGroups.value(groupId).toMap(); }

// The selected, cached profile
Q_INVOKABLE QVariantMap getProfile() {
return m_cacheUIProfile;
}
Q_INVOKABLE QStringList profileFavorites() {
return m_cacheUIProfile.value("favorites").toStringList();
}
Q_INVOKABLE QStringList getProfilePages() {
return m_cacheUIProfile.value("pages").toStringList();
}
Q_INVOKABLE QVariantMap getProfile() { return m_cacheUIProfile; }
Q_INVOKABLE QStringList profileFavorites() { return m_cacheUIProfile.value("favorites").toStringList(); }
Q_INVOKABLE QStringList getProfilePages() { return m_cacheUIProfile.value("pages").toStringList(); }

// Removed from entities
Q_INVOKABLE void setFavorite(const QString& entityId, bool value);


bool isValid() const { return m_error.isEmpty(); }
bool isValid() const { return m_error.isEmpty(); }
QString getError() const { return m_error; }


QVariantMap config() { return m_config; }
void setConfig(const QVariantMap& config);
void setConfig(const QVariantMap& config);

QVariant getContextProperty(const QString& name);

// get a QML object, you need to have objectName property of the QML object set to be able to use this
QObject *getQMLObject(QList<QObject*> nodes, const QString &name);
QObject *getQMLObject(const QString &name);
QObject* getQMLObject(QList<QObject*> nodes, const QString& name);
QObject* getQMLObject(const QString& name);

// profile
QString profile() { return m_cacheProfile; }
void setProfile(QString id);
void setProfile(QString id);

public:
explicit Config(QQmlApplicationEngine *engine = nullptr, QString path = "", QString schemaPath = "");
explicit Config(QQmlApplicationEngine* engine = nullptr, QString path = "", QString schemaPath = "");
virtual ~Config();

static Config* getInstance() { return s_instance; }
@@ -150,22 +120,22 @@ class Config : public QObject, ConfigInterface
void syncConfigToCache();
void syncCacheToConfig();

static Config* s_instance;
QQmlApplicationEngine* m_engine;
static Config* s_instance;
QQmlApplicationEngine* m_engine;

QVariantMap m_config;
QVariantMap m_config;

JsonFile* m_jsf;
QString m_error;
JsonFile* m_jsf;
QString m_error;

// Caches to improve performance
QString m_cacheProfile;
QVariantMap m_cacheSettings;
QVariantMap m_cacheUIConfig;
QVariantMap m_cacheUIProfile; // of selected Profile
QVariantMap m_cacheUIProfiles;
QVariantMap m_cacheUIPages;
QVariantMap m_cacheUIGroups;
QString m_cacheProfile;
QVariantMap m_cacheSettings;
QVariantMap m_cacheUIConfig;
QVariantMap m_cacheUIProfile; // of selected Profile
QVariantMap m_cacheUIProfiles;
QVariantMap m_cacheUIPages;
QVariantMap m_cacheUIGroups;
};

#endif // CONFIG_H_

0 comments on commit fde7f15

Please sign in to comment.
You can’t perform that action at this time.