Added support for settings

print
Josef Rokos 9 years ago
parent 2bb07716b9
commit e569dfbc12

@ -10,7 +10,7 @@ TARGET = accommodation
TEMPLATE = lib
DEFINES += ACCOMMODATION_LIBRARY\
_GLIBCXX_USE_CXX11_ABI=0
_GLIBCXX_USE_CXX11_ABI=1
SOURCES += accommodation.cpp \
data/person.cpp \

@ -2,6 +2,9 @@
#include "ui_acform.h"
#include <QList>
#include <settingsservice.h>
#include <settings/globalsettings.h>
#include "accommodation-odb.hxx"
AcForm::AcForm(QWidget *parent) :

@ -11,7 +11,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = prodejna
TEMPLATE = app
DEFINES += _GLIBCXX_USE_CXX11_ABI=0
DEFINES += _GLIBCXX_USE_CXX11_ABI=1
win32 {
INCLUDEPATH += d:/prac/odb/libodb-2.4.0

@ -121,3 +121,9 @@ void MainWindow::showEvent(QShowEvent *evt)
m_loginDialog->show();
}
}
void MainWindow::on_actionSettings_triggered()
{
SettingsForm *settings = new SettingsForm(this);
settings->show();
}

@ -30,6 +30,8 @@ private slots:
void on_actionLogin_triggered();
void on_actionSettings_triggered();
private:
Ui::MainWindow *ui;
LoginDialog *m_loginDialog;

@ -56,7 +56,7 @@
<x>0</x>
<y>0</y>
<width>1000</width>
<height>21</height>
<height>19</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -78,6 +78,7 @@
<bool>false</bool>
</attribute>
<addaction name="actionLogin"/>
<addaction name="actionSettings"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionExit">
@ -99,6 +100,14 @@
<string>Login...</string>
</property>
</action>
<action name="actionSettings">
<property name="text">
<string>Settings</string>
</property>
<property name="toolTip">
<string>Settings</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>

@ -2,179 +2,61 @@
#define AUTOFORM_H
#include <QWidget>
#include <QList>
#include <QLineEdit>
#include <QSharedPointer>
#include <QMetaMethod>
#include <QDebug>
#include <QVariant>
#include <QMessageBox>
#include <QComboBox>
#include <QDebug>
#include "../qdecimal/src/QDecDouble.hh"
#include "iform.h"
#include "service.h"
#include "ivalidator.h"
#include "iservice.h"
#include "iplugin.h"
#include "formbinder.h"
#include "combodata.h"
template <class T>
class AutoForm : public IForm
class AutoForm : public FormBinder<T>
{
public:
explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
explicit AutoForm(QWidget *parent = 0) : FormBinder<T>(parent) {
m_serviceConnected = false;
m_saved = false;
}
virtual ~AutoForm() {
foreach (IValidator *val, m_validators) {
delete val;
}
m_validators.clear();
}
void setEntity(QSharedPointer<T> entity) {
m_entity = entity;
bindToUi();
}
QSharedPointer<T> entity() {
return m_entity;
}
virtual ~AutoForm() { }
void setNewRec(bool isNew) {
m_newRec = isNew;
}
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);
}
protected:
virtual void bindOtherToUi() {}
virtual bool bindOtherToData() { return true; }
virtual void registerCombos() {}
private:
QSharedPointer<T> m_entity;
QList<QWidget*> m_bindWidgets;
QList<IValidator*> m_validators;
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
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();
this->m_newRec = isNew;
}
public slots:
bool saveRecord() {
if (!bindToData())
if (!this->bindToData())
{
return false;
}
if (!m_serviceConnected)
{
connect(service(), &IService::dbError, [this](QString msg) {
QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str()));
this->connect(service(), &IService::dbError, [this](QString msg) {
QMessageBox::critical(this, "Database error", msg.toStdString().c_str());
m_saved = false;
});
connect(service(), &IService::dataChanged, [this]() {
this->connect(service(), &IService::dataChanged, [this]() {
m_saved = true;
});
m_serviceConnected = true;
}
if (m_newRec) {
service()->save(entity());
if (this->m_newRec) {
service()->save(this->entity());
if (m_saved)
{
emit recordAdded();
emit this->recordAdded();
}
} else {
service()->update(entity());
service()->update(this->entity());
if (m_saved)
{
emit recordUpdated();
emit this->recordUpdated();
}
}
@ -183,7 +65,7 @@ public slots:
private:
Service<T> *service() {
IPlugin *plugin = Context::instance().plugin(pluginId());
IPlugin *plugin = Context::instance().plugin(this->pluginId());
if (plugin == NULL) {
Q_ASSERT(false);
return NULL;

@ -9,5 +9,7 @@
#include "gridform.h"
#include "permissionservice.h"
#include "combodata.h"
#include "settingsservice.h"
#include "settingsform.h"
#endif // CORE_H

@ -12,7 +12,7 @@ TARGET = core
TEMPLATE = lib
DEFINES += CORE_LIBRARY \
_GLIBCXX_USE_CXX11_ABI=0
_GLIBCXX_USE_CXX11_ABI=1
SOURCES += \
data/user.cpp \
@ -45,7 +45,12 @@ SOURCES += \
itablemodel.cpp \
iservice.cpp \
combodata.cpp \
data/comboitem.cpp
data/comboitem.cpp \
settingsservice.cpp \
data/system.cpp \
settings/globalsettings.cpp \
settingsform.cpp \
settings/globalsettingsform.cpp
HEADERS += core.h\
core_global.h \
@ -89,7 +94,13 @@ HEADERS += core.h\
iservice.h \
combodata.h \
data/comboitem.h \
enums.h
settingsservice.h \
data/system.h \
enums.h \
settings/globalsettings.h \
settingsform.h \
settings/globalsettingsform.h \
formbinder.h
unix {
target.path = /usr/lib
@ -121,7 +132,9 @@ FORMS += \
roles/rolesform.ui \
filterui.ui \
savefilterdialog.ui \
filterdialog.ui
filterdialog.ui \
settingsform.ui \
settings/globalsettingsform.ui
OTHER_FILES += \
users/metaData.json \

@ -1,6 +1,7 @@
#include "coreplugin.h"
#include <QJsonDocument>
#include <QFile>
#include "settings/globalsettingsform.h"
CorePlugin::CorePlugin()
{
@ -21,5 +22,6 @@ void CorePlugin::initServiceUi()
{
m_service = NULL;
m_ui = NULL;
m_settingsUi = new GlobalSettingsForm();
}

@ -8,6 +8,7 @@ class Role;
#include "user.h"
#include "role.h"
#include "permission.h"
#include "system.h"
#endif // COREDATA_H

@ -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

@ -3,6 +3,8 @@
#include <QWidget>
#include <QString>
#include <QSharedPointer>
#include <QObject>
#include "core_global.h"
@ -16,6 +18,7 @@ public:
QString pluginId() const;
void setPluginId(const QString &pluginId);
virtual void loadEntity() {}
signals:
void recordAdded();

@ -17,6 +17,7 @@ public:
IPlugin() {
m_ui = NULL;
m_service = NULL;
m_settingsUi = NULL;
}
virtual ~IPlugin() {
@ -29,6 +30,11 @@ public:
{
delete m_ui;
}
if (m_settingsUi != NULL && m_settingsUi->parent() == NULL)
{
delete m_settingsUi;
}
}
virtual QString pluginName() = 0;
@ -51,6 +57,14 @@ public:
return m_ui;
}
virtual QWidget *settingsUi() {
return m_settingsUi;
}
virtual QString settingsTabLabel() {
return pluginName();
}
template<class T>
Service<T> *service() {
return (Service<T>*)m_service;
@ -58,6 +72,7 @@ public:
protected:
QWidget *m_ui;
QWidget *m_settingsUi;
IService *m_service;
};

@ -26,7 +26,7 @@ public:
m_pluginId = pluginId;
}
QList<QSharedPointer<T> > all() {
QList<QSharedPointer<T> > all(const QString &where = "") {
odb::database *db = Context::instance().db();
Q_ASSERT(db);
@ -36,7 +36,16 @@ public:
try
{
odb::result<T> res = db->template query<T>();
odb::result<T> res;
if (where.isEmpty())
{
res = db->template query<T>();
}
else
{
res = db->template query<T>(where.toStdString());
}
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
ret.append(it.load());

@ -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

@ -11,7 +11,7 @@ TARGET = services
TEMPLATE = lib
DEFINES += SERVICES_LIBRARY\
_GLIBCXX_USE_CXX11_ABI=0
_GLIBCXX_USE_CXX11_ABI=1
SOURCES += services.cpp \
data/accservice.cpp \

Loading…
Cancel
Save