Added support for settings
parent
2bb07716b9
commit
e569dfbc12
@ -0,0 +1,31 @@
|
||||
#include "system.h"
|
||||
|
||||
System::System()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int System::id() const
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
void System::setId(int id)
|
||||
{
|
||||
m_id = id;
|
||||
}
|
||||
|
||||
QString System::pluginId() const
|
||||
{
|
||||
return m_pluginId;
|
||||
}
|
||||
|
||||
QString System::settings() const
|
||||
{
|
||||
return m_settings;
|
||||
}
|
||||
|
||||
void System::setSettings(const QString &settings)
|
||||
{
|
||||
m_settings = settings;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
#ifndef SYSTEM_H
|
||||
#define SYSTEM_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
|
||||
#pragma db object
|
||||
class System
|
||||
{
|
||||
public:
|
||||
System();
|
||||
|
||||
int id() const;
|
||||
void setId(int id);
|
||||
|
||||
QString pluginId() const;
|
||||
|
||||
QString schemaVersion() const;
|
||||
|
||||
QString settings() const;
|
||||
void setSettings(const QString &settings);
|
||||
|
||||
private:
|
||||
friend class odb::access;
|
||||
|
||||
#pragma db id auto
|
||||
int m_id;
|
||||
QString m_pluginId;
|
||||
QString m_schemaVersion;
|
||||
QString m_settings;
|
||||
};
|
||||
|
||||
#endif // SYSTEM_H
|
@ -0,0 +1,136 @@
|
||||
#ifndef FORMBINDER_H
|
||||
#define FORMBINDER_H
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QComboBox>
|
||||
#include <QWidget>
|
||||
#include <QMetaMethod>
|
||||
#include <QMetaProperty>
|
||||
#include <QVariant>
|
||||
|
||||
#include "combodata.h"
|
||||
#include "ivalidator.h"
|
||||
#include "iform.h"
|
||||
|
||||
#include "../qdecimal/src/QDecDouble.hh"
|
||||
|
||||
template<class T>
|
||||
class FormBinder : public IForm
|
||||
{
|
||||
public:
|
||||
|
||||
explicit FormBinder(QWidget *parent = NULL) : IForm(parent) {}
|
||||
|
||||
virtual ~FormBinder() {
|
||||
foreach (IValidator *val, m_validators) {
|
||||
delete val;
|
||||
}
|
||||
m_validators.clear();
|
||||
}
|
||||
|
||||
void registerBinding(QWidget *widget) {
|
||||
if (!m_bindWidgets.contains(widget)) {
|
||||
m_bindWidgets.append(widget);
|
||||
}
|
||||
}
|
||||
|
||||
void registerBinding(QComboBox *combo, const QList<ComboData> &values) {
|
||||
m_bindCombos[combo] = values;
|
||||
}
|
||||
|
||||
void registerValidator(IValidator *validator) {
|
||||
m_validators.append(validator);
|
||||
}
|
||||
|
||||
void setEntity(QSharedPointer<T> entity) {
|
||||
m_entity = entity;
|
||||
bindToUi();
|
||||
}
|
||||
|
||||
QSharedPointer<T> entity() {
|
||||
return m_entity;
|
||||
}
|
||||
|
||||
protected:
|
||||
QSharedPointer<T> m_entity;
|
||||
|
||||
virtual void bindOtherToUi() {}
|
||||
virtual bool bindOtherToData() { return true; }
|
||||
virtual void registerCombos() {}
|
||||
|
||||
void bindToUi() {
|
||||
registerCombos();
|
||||
foreach (QWidget *widget, m_bindWidgets) {
|
||||
const char* prop = widget->metaObject()->userProperty().name();
|
||||
QVariant value = ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str());
|
||||
if (value.canConvert<QDecDouble>())
|
||||
{
|
||||
widget->setProperty(prop, value.value<QDecDouble>().toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
widget->setProperty(prop, value);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (QComboBox *combo, m_bindCombos.keys()) {
|
||||
int idx = 0;
|
||||
QVariant field = ((QObject*)m_entity.data())->property(combo->objectName().toStdString().c_str());
|
||||
|
||||
combo->clear();
|
||||
for (int i = 0; i < m_bindCombos[combo].size(); i++) {
|
||||
ComboData data = m_bindCombos[combo][i];
|
||||
combo->addItem(data.label(), data.index());
|
||||
|
||||
if (data.index().canConvert<QObject*>()) {
|
||||
ComboItem* ci = qobject_cast<ComboItem*>(data.index().value<QObject*>());
|
||||
ComboItem* ciField = qobject_cast<ComboItem*>(field.value<QObject*>());
|
||||
if (ci->eq(ciField)) {
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
else if (field == data.index()) {
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
combo->setCurrentIndex(idx);
|
||||
}
|
||||
|
||||
bindOtherToUi();
|
||||
}
|
||||
|
||||
bool bindToData() {
|
||||
foreach (IValidator *val, m_validators) {
|
||||
if (!val->validate()) {
|
||||
emit validationError(val->errMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (QWidget *widget, m_bindWidgets) {
|
||||
const char* prop = widget->metaObject()->userProperty().name();
|
||||
|
||||
QVariant val = widget->property(prop);
|
||||
if (((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str()).canConvert<QDecDouble>())
|
||||
{
|
||||
QDecDouble dec(val.toDouble());
|
||||
val = QVariant::fromValue(dec);
|
||||
}
|
||||
((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), val);
|
||||
}
|
||||
|
||||
foreach (QComboBox *combo, m_bindCombos.keys()) {
|
||||
((QObject*)m_entity.data())->setProperty(combo->objectName().toStdString().c_str(), combo->currentData());
|
||||
}
|
||||
|
||||
return bindOtherToData();
|
||||
}
|
||||
|
||||
private:
|
||||
QList<QWidget*> m_bindWidgets;
|
||||
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
|
||||
QList<IValidator*> m_validators;
|
||||
};
|
||||
|
||||
#endif // FORMBINDER_H
|
@ -0,0 +1,96 @@
|
||||
#include "globalsettings.h"
|
||||
|
||||
GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString GlobalSettings::firmName() const
|
||||
{
|
||||
return m_firmName;
|
||||
}
|
||||
|
||||
void GlobalSettings::setFirmName(const QString &firmName)
|
||||
{
|
||||
m_firmName = firmName;
|
||||
}
|
||||
|
||||
QString GlobalSettings::street() const
|
||||
{
|
||||
return m_street;
|
||||
}
|
||||
|
||||
void GlobalSettings::setStreet(const QString &street)
|
||||
{
|
||||
m_street = street;
|
||||
}
|
||||
|
||||
QString GlobalSettings::houseNumber() const
|
||||
{
|
||||
return m_houseNumber;
|
||||
}
|
||||
|
||||
void GlobalSettings::setHouseNumber(const QString &houseNumber)
|
||||
{
|
||||
m_houseNumber = houseNumber;
|
||||
}
|
||||
|
||||
QString GlobalSettings::zipCode() const
|
||||
{
|
||||
return m_zipCode;
|
||||
}
|
||||
|
||||
void GlobalSettings::setZipCode(const QString &zipCode)
|
||||
{
|
||||
m_zipCode = zipCode;
|
||||
}
|
||||
|
||||
QString GlobalSettings::city() const
|
||||
{
|
||||
return m_city;
|
||||
}
|
||||
|
||||
void GlobalSettings::setCity(const QString &city)
|
||||
{
|
||||
m_city = city;
|
||||
}
|
||||
|
||||
int GlobalSettings::ic() const
|
||||
{
|
||||
return m_ic;
|
||||
}
|
||||
|
||||
void GlobalSettings::setIc(int IC)
|
||||
{
|
||||
m_ic = IC;
|
||||
}
|
||||
|
||||
QString GlobalSettings::dic() const
|
||||
{
|
||||
return m_dic;
|
||||
}
|
||||
|
||||
void GlobalSettings::setDic(const QString &dic)
|
||||
{
|
||||
m_dic = dic;
|
||||
}
|
||||
|
||||
bool GlobalSettings::vatPayer() const
|
||||
{
|
||||
return m_vatPayer;
|
||||
}
|
||||
|
||||
void GlobalSettings::setVatPayer(bool vatPayer)
|
||||
{
|
||||
m_vatPayer = vatPayer;
|
||||
}
|
||||
|
||||
QString GlobalSettings::logoPath() const
|
||||
{
|
||||
return m_logoPath;
|
||||
}
|
||||
|
||||
void GlobalSettings::setLogoPath(const QString &logoPath)
|
||||
{
|
||||
m_logoPath = logoPath;
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
#ifndef GLOBALSETTINGS_H
|
||||
#define GLOBALSETTINGS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class GlobalSettings : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString firmName READ firmName WRITE setFirmName)
|
||||
Q_PROPERTY(QString street READ street WRITE setStreet)
|
||||
Q_PROPERTY(QString houseNumber READ houseNumber WRITE setHouseNumber)
|
||||
Q_PROPERTY(QString zipCode READ zipCode WRITE setZipCode)
|
||||
Q_PROPERTY(QString city READ city WRITE setCity)
|
||||
Q_PROPERTY(int ic READ ic WRITE setIc)
|
||||
Q_PROPERTY(QString dic READ dic WRITE setDic)
|
||||
Q_PROPERTY(bool vatPayer READ vatPayer WRITE setVatPayer)
|
||||
Q_PROPERTY(QString logoPath READ logoPath WRITE setLogoPath)
|
||||
public:
|
||||
explicit GlobalSettings(QObject *parent = 0);
|
||||
|
||||
QString firmName() const;
|
||||
void setFirmName(const QString &firmName);
|
||||
|
||||
QString street() const;
|
||||
void setStreet(const QString &street);
|
||||
|
||||
QString houseNumber() const;
|
||||
void setHouseNumber(const QString &houseNumber);
|
||||
|
||||
QString zipCode() const;
|
||||
void setZipCode(const QString &zipCode);
|
||||
|
||||
QString city() const;
|
||||
void setCity(const QString &city);
|
||||
|
||||
int ic() const;
|
||||
void setIc(int ic);
|
||||
|
||||
QString dic() const;
|
||||
void setDic(const QString &dic);
|
||||
|
||||
bool vatPayer() const;
|
||||
void setVatPayer(bool vatPayer);
|
||||
|
||||
QString logoPath() const;
|
||||
void setLogoPath(const QString &logoPath);
|
||||
|
||||
private:
|
||||
QString m_firmName;
|
||||
QString m_street;
|
||||
QString m_houseNumber;
|
||||
QString m_zipCode;
|
||||
QString m_city;
|
||||
int m_ic;
|
||||
QString m_dic;
|
||||
bool m_vatPayer;
|
||||
QString m_logoPath;
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
};
|
||||
|
||||
#endif // GLOBALSETTINGS_H
|
@ -0,0 +1,42 @@
|
||||
#include "globalsettingsform.h"
|
||||
#include "ui_globalsettingsform.h"
|
||||
|
||||
#include "globalsettings.h"
|
||||
#include "../settingsservice.h"
|
||||
|
||||
GlobalSettingsForm::GlobalSettingsForm(QWidget *parent) :
|
||||
FormBinder<GlobalSettings>(parent),
|
||||
ui(new Ui::GlobalSettingsForm)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
registerBinding(ui->firmName);
|
||||
registerBinding(ui->street);
|
||||
registerBinding(ui->houseNumber);
|
||||
registerBinding(ui->zipCode);
|
||||
registerBinding(ui->city);
|
||||
registerBinding(ui->ic);
|
||||
registerBinding(ui->vatPayer);
|
||||
registerBinding(ui->dic);
|
||||
}
|
||||
|
||||
GlobalSettingsForm::~GlobalSettingsForm()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool GlobalSettingsForm::saveRecord()
|
||||
{
|
||||
bindToData();
|
||||
SettingsService srv("CORE");
|
||||
srv.saveSettings(entity());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void GlobalSettingsForm::loadEntity()
|
||||
{
|
||||
SettingsService srv("CORE");
|
||||
QSharedPointer<GlobalSettings> settings = srv.loadSettings<GlobalSettings>();
|
||||
setEntity(settings);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
#ifndef GLOBALSETTINGSFORM_H
|
||||
#define GLOBALSETTINGSFORM_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "../formbinder.h"
|
||||
#include "globalsettings.h"
|
||||
|
||||
namespace Ui {
|
||||
class GlobalSettingsForm;
|
||||
}
|
||||
|
||||
class GlobalSettingsForm : public FormBinder<GlobalSettings>
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit GlobalSettingsForm(QWidget *parent = 0);
|
||||
~GlobalSettingsForm();
|
||||
|
||||
private:
|
||||
Ui::GlobalSettingsForm *ui;
|
||||
|
||||
// IForm interface
|
||||
public slots:
|
||||
bool saveRecord() override;
|
||||
|
||||
// IForm interface
|
||||
public:
|
||||
void loadEntity() override;
|
||||
};
|
||||
|
||||
#endif // GLOBALSETTINGSFORM_H
|
@ -0,0 +1,139 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>GlobalSettingsForm</class>
|
||||
<widget class="QWidget" name="GlobalSettingsForm">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>422</width>
|
||||
<height>323</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="1" column="0">
|
||||
<widget class="QGroupBox" name="groupBox_2">
|
||||
<property name="title">
|
||||
<string>Company info</string>
|
||||
</property>
|
||||
<layout class="QFormLayout" name="formLayout_2">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label_6">
|
||||
<property name="text">
|
||||
<string>IC</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLineEdit" name="ic"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_7">
|
||||
<property name="text">
|
||||
<string>VAT number</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="dic"/>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QCheckBox" name="vatPayer">
|
||||
<property name="text">
|
||||
<string>VAT payer</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QGroupBox" name="groupBox">
|
||||
<property name="title">
|
||||
<string>Contact</string>
|
||||
</property>
|
||||
<layout class="QFormLayout" name="formLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Firm Name</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLineEdit" name="firmName"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Street</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="street"/>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>House Number</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QLineEdit" name="houseNumber"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_4">
|
||||
<property name="text">
|
||||
<string>City</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QLineEdit" name="city"/>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="label_5">
|
||||
<property name="text">
|
||||
<string>ZIP code</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QLineEdit" name="zipCode"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QGroupBox" name="groupBox_3">
|
||||
<property name="title">
|
||||
<string>Logo</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_8">
|
||||
<property name="text">
|
||||
<string>Logo</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="text">
|
||||
<string>Select file</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
@ -0,0 +1,56 @@
|
||||
#include "settingsform.h"
|
||||
#include "ui_settingsform.h"
|
||||
|
||||
#include "settingsservice.h"
|
||||
#include "context.h"
|
||||
#include "iplugin.h"
|
||||
#include "iform.h"
|
||||
|
||||
SettingsForm::SettingsForm(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::SettingsForm)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
foreach (IPlugin *plugin, Context::instance().plugins()) {
|
||||
if (plugin->settingsUi() != NULL)
|
||||
{
|
||||
SettingsService srv(plugin->pluginId());
|
||||
IForm *tab = qobject_cast<IForm*>(plugin->settingsUi());
|
||||
|
||||
if (tab != NULL)
|
||||
{
|
||||
tab->loadEntity();
|
||||
ui->tabWidget->addTab(tab, QIcon(), plugin->settingsTabLabel());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SettingsForm::~SettingsForm()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void SettingsForm::on_buttonBox_accepted()
|
||||
{
|
||||
accept();
|
||||
}
|
||||
|
||||
void SettingsForm::accept()
|
||||
{
|
||||
for (int i = 0; i < ui->tabWidget->count(); i++)
|
||||
{
|
||||
IForm *tab = qobject_cast<IForm*>(ui->tabWidget->widget(i));
|
||||
if (tab != NULL)
|
||||
{
|
||||
tab->saveRecord();
|
||||
}
|
||||
}
|
||||
QDialog::accept();
|
||||
}
|
||||
|
||||
void SettingsForm::on_buttonBox_rejected()
|
||||
{
|
||||
reject();
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
#ifndef SETTINGSFORM_H
|
||||
#define SETTINGSFORM_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QDialog>
|
||||
|
||||
namespace Ui {
|
||||
class SettingsForm;
|
||||
}
|
||||
|
||||
class SettingsForm : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SettingsForm(QWidget *parent = 0);
|
||||
~SettingsForm();
|
||||
|
||||
private slots:
|
||||
void on_buttonBox_accepted();
|
||||
|
||||
void on_buttonBox_rejected();
|
||||
|
||||
private:
|
||||
Ui::SettingsForm *ui;
|
||||
|
||||
// QDialog interface
|
||||
public slots:
|
||||
void accept() override;
|
||||
};
|
||||
|
||||
#endif // SETTINGSFORM_H
|
@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>SettingsForm</class>
|
||||
<widget class="QDialog" name="SettingsForm">
|
||||
<property name="windowModality">
|
||||
<enum>Qt::ApplicationModal</enum>
|
||||
</property>
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Settings</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="tabPosition">
|
||||
<enum>QTabWidget::North</enum>
|
||||
</property>
|
||||
<property name="tabShape">
|
||||
<enum>QTabWidget::Rounded</enum>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>-1</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
@ -0,0 +1,47 @@
|
||||
#include "settingsservice.h"
|
||||
|
||||
#include "core-odb.hxx"
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/result.hxx>
|
||||
|
||||
SettingsService::SettingsService(QObject *parent)
|
||||
:IService(parent)
|
||||
{
|
||||
}
|
||||
|
||||
SettingsService::SettingsService(const QString &pluginId, QObject *parent)
|
||||
:IService(parent)
|
||||
{
|
||||
m_pluginId = pluginId;
|
||||
}
|
||||
|
||||
QSharedPointer<System> SettingsService::loadSystem()
|
||||
{
|
||||
Service<System> srv;
|
||||
QList<QSharedPointer<System> > sysObj = srv.all("pluginId = '" + m_pluginId + "'");
|
||||
|
||||
if (sysObj.isEmpty())
|
||||
{
|
||||
emit dbErrorUpdate("Error loading sys object");
|
||||
return QSharedPointer<System>();
|
||||
}
|
||||
|
||||
return sysObj[0];
|
||||
}
|
||||
|
||||
void SettingsService::saveSystem(const QJsonDocument &doc)
|
||||
{
|
||||
QSharedPointer<System> sysObj = loadSystem();
|
||||
|
||||
if (sysObj.isNull())
|
||||
{
|
||||
emit dbErrorUpdate("Error loading sys object");
|
||||
return;
|
||||
}
|
||||
|
||||
sysObj->setSettings(QString(doc.toJson()));
|
||||
|
||||
Service<System> srv;
|
||||
srv.update(sysObj);
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
#ifndef SETTINGSSERVICE_H
|
||||
#define SETTINGSSERVICE_H
|
||||
|
||||
#include <QString>
|
||||
#include <QSharedPointer>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QMetaProperty>
|
||||
|
||||
#include "data/system.h"
|
||||
#include "service.h"
|
||||
#include "core_global.h"
|
||||
|
||||
class CORESHARED_EXPORT SettingsService : public IService
|
||||
{
|
||||
public:
|
||||
explicit SettingsService(QObject *parent = NULL);
|
||||
SettingsService(const QString &pluginId, QObject *parent = NULL);
|
||||
|
||||
|
||||
template <class T>
|
||||
QSharedPointer<T> loadSettings() {
|
||||
QSharedPointer<System> sys = loadSystem();
|
||||
QObject *objSettings = new T();
|
||||
QSharedPointer<T> settingsObj((T*)objSettings);
|
||||
|
||||
if (sys.isNull())
|
||||
{
|
||||
return settingsObj;
|
||||
}
|
||||
|
||||
QString settingStr = sys->settings();
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(settingStr.toUtf8());
|
||||
QJsonValue val = jsonDoc.object()["Settings"];
|
||||
|
||||
if (!val.isObject())
|
||||
{
|
||||
return settingsObj;
|
||||
}
|
||||
|
||||
for (int i = 0; i < objSettings->metaObject()->propertyCount(); i++)
|
||||
{
|
||||
const char *propName = objSettings->metaObject()->property(i).name();
|
||||
QVariant varVal = val.toObject()[propName].toVariant();
|
||||
objSettings->setProperty(propName, varVal);
|
||||
}
|
||||
|
||||
return settingsObj;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void saveSettings(QSharedPointer<T> objSettings)
|
||||
{
|
||||
QObject *objSettingsQo = objSettings.data();
|
||||
QJsonDocument jsonDoc;
|
||||
QJsonObject jsonObj;
|
||||
|
||||
for (int i = 0; i < objSettingsQo->metaObject()->propertyCount(); i++)
|
||||
{
|
||||
const char *propName = objSettings->metaObject()->property(i).name();
|
||||
jsonObj[propName] = QJsonValue::fromVariant(objSettingsQo->property(propName));
|
||||
}
|
||||
|
||||
QJsonObject jsonSettings;
|
||||
jsonSettings["Settings"] = jsonObj;
|
||||
jsonDoc.setObject(jsonSettings);
|
||||
|
||||
saveSystem(jsonDoc);
|
||||
}
|
||||
|
||||
private:
|
||||
QString m_pluginId;
|
||||
QSharedPointer<QObject> m_settings;
|
||||
QSharedPointer<System> loadSystem();
|
||||
void saveSystem(const QJsonDocument &doc);
|
||||
};
|
||||
|
||||
#endif // SETTINGSSERVICE_H
|
@ -1,11 +1,11 @@
|
||||
TEMPLATE = subdirs
|
||||
|
||||
SUBDIRS += \
|
||||
qdecimal \
|
||||
core \
|
||||
application \
|
||||
accommodation \
|
||||
qdecimal \
|
||||
services \
|
||||
commodity \
|
||||
addressbook
|
||||
services
|
||||
#commodity \
|
||||
#addressbook
|
||||
|
||||
|
Loading…
Reference in New Issue