Added IService QObject derived class for emiting Qt signals from service objects.
This commit is contained in:
+4
-2
@@ -42,7 +42,8 @@ SOURCES += \
|
||||
samestringvalidator.cpp \
|
||||
savefilterdialog.cpp \
|
||||
filterdialog.cpp \
|
||||
itablemodel.cpp
|
||||
itablemodel.cpp \
|
||||
iservice.cpp
|
||||
|
||||
HEADERS += core.h\
|
||||
core_global.h \
|
||||
@@ -82,7 +83,8 @@ HEADERS += core.h\
|
||||
savefilterdialog.h \
|
||||
filterdialog.h \
|
||||
itablemodel.h \
|
||||
data/core_global.h
|
||||
data/core_global.h \
|
||||
iservice.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ public:
|
||||
|
||||
protected:
|
||||
QWidget *m_ui;
|
||||
void *m_service;
|
||||
IService *m_service;
|
||||
};
|
||||
|
||||
#define PluginInterface_iid "cz.itsolved.prodejna.IPlugin"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "iservice.h"
|
||||
|
||||
IService::IService(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
IService::~IService()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef ISERVICE_H
|
||||
#define ISERVICE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
#include "core_global.h"
|
||||
|
||||
class CORESHARED_EXPORT IService : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit IService(QObject *parent = 0);
|
||||
virtual ~IService();
|
||||
|
||||
signals:
|
||||
void dbError(QString errMsg);
|
||||
void dataChanged();
|
||||
void permissionDenied();
|
||||
|
||||
public slots:
|
||||
};
|
||||
|
||||
#endif // ISERVICE_H
|
||||
+64
-15
@@ -12,14 +12,15 @@
|
||||
|
||||
#include "core_global.h"
|
||||
#include "context.h"
|
||||
#include "iservice.h"
|
||||
|
||||
#include "transaction.h"
|
||||
|
||||
template<class T>
|
||||
class Service
|
||||
class Service : public IService
|
||||
{
|
||||
public:
|
||||
Service() { }
|
||||
explicit Service(QObject *parent = NULL) :IService(parent) { }
|
||||
|
||||
explicit Service(const QString &pluginId) {
|
||||
m_pluginId = pluginId;
|
||||
@@ -31,14 +32,23 @@ public:
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
odb::result<T> res = db->template query<T>();
|
||||
|
||||
QList<QSharedPointer<T> > ret;
|
||||
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
|
||||
ret.append(it.load());
|
||||
|
||||
try
|
||||
{
|
||||
odb::result<T> res = db->template query<T>();
|
||||
|
||||
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
|
||||
ret.append(it.load());
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -48,8 +58,19 @@ public:
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
db->persist(entity);
|
||||
tx.commit();
|
||||
|
||||
try
|
||||
{
|
||||
db->persist(entity);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
return;
|
||||
}
|
||||
|
||||
emit dataChanged();
|
||||
}
|
||||
|
||||
void update(QSharedPointer<T> entity) {
|
||||
@@ -58,8 +79,18 @@ public:
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
db->update(entity);
|
||||
tx.commit();
|
||||
|
||||
try
|
||||
{
|
||||
db->update(entity);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
}
|
||||
|
||||
emit dataChanged();
|
||||
}
|
||||
|
||||
QSharedPointer<T> loadById(int id) {
|
||||
@@ -68,8 +99,18 @@ public:
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
QSharedPointer<T> entity = db->template load<T>(id);
|
||||
tx.commit();
|
||||
QSharedPointer<T> entity;
|
||||
|
||||
try
|
||||
{
|
||||
entity = db->template load<T>(id);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -79,8 +120,16 @@ public:
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
db->erase(entity);
|
||||
tx.commit();
|
||||
|
||||
try
|
||||
{
|
||||
db->erase(entity);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
void setPluginId(const QString &pluginId) {
|
||||
|
||||
Reference in New Issue
Block a user