Josef Rokos 9 years ago
commit 1bb87dcb20

@ -17,7 +17,8 @@ SOURCES += accommodation.cpp \
accommodationservice.cpp \ accommodationservice.cpp \
tablemodel.cpp \ tablemodel.cpp \
acform.cpp \ acform.cpp \
accgrid.cpp accgrid.cpp \
data/address.cpp
HEADERS += accommodation.h\ HEADERS += accommodation.h\
accommodation_global.h \ accommodation_global.h \
@ -25,7 +26,9 @@ HEADERS += accommodation.h\
accommodationservice.h \ accommodationservice.h \
tablemodel.h \ tablemodel.h \
acform.h \ acform.h \
accgrid.h accgrid.h \
data/address.h \
data/accommodation-data.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib
@ -53,7 +56,7 @@ OTHER_FILES += \
FORMS += \ FORMS += \
acform.ui acform.ui
ODB_FILES = accommodation/data/person.h ODB_FILES = accommodation/data/accommodation-data.h
H_DIR = $$PWD/data/*.h H_DIR = $$PWD/data/*.h
include(../odb.pri) include(../odb.pri)

@ -1,5 +1,8 @@
#include "acform.h" #include "acform.h"
#include "ui_acform.h" #include "ui_acform.h"
#include <QList>
#include "accommodation-odb.hxx"
AcForm::AcForm(QWidget *parent) : AcForm::AcForm(QWidget *parent) :
AutoForm<Person>(parent), AutoForm<Person>(parent),
@ -15,3 +18,14 @@ AcForm::~AcForm()
{ {
delete ui; delete ui;
} }
void AcForm::registerCombos()
{
QList<ComboData> cbData;
Service<Address> srv;
foreach (QSharedPointer<Address> adr, srv.all()) {
cbData.append(ComboData(adr));
}
registerBinding(ui->address, cbData);
}

@ -21,6 +21,10 @@ public:
private: private:
Ui::AcForm *ui; Ui::AcForm *ui;
// AutoForm interface
protected:
virtual void registerCombos();
}; };
#endif // ACFORM_H #endif // ACFORM_H

@ -46,6 +46,16 @@
<string>PushButton</string> <string>PushButton</string>
</property> </property>
</widget> </widget>
<widget class="QComboBox" name="address">
<property name="geometry">
<rect>
<x>110</x>
<y>170</y>
<width>191</width>
<height>22</height>
</rect>
</property>
</widget>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

@ -0,0 +1,8 @@
#ifndef ACCOMMODATIONDATA_H
#define ACCOMMODATIONDATA_H
#include "address.h"
#include "person.h"
#endif // ACCOMMODATIONDATA_H

@ -0,0 +1,58 @@
#include "address.h"
Address::Address(QObject *parent) : ComboItem(parent)
{
}
Address::~Address()
{
}
QString Address::city() const
{
return m_city;
}
void Address::setCity(const QString &city)
{
m_city = city;
}
QString Address::street() const
{
return m_street;
}
void Address::setStreet(const QString &street)
{
m_street = street;
}
QString Address::houseNumber() const
{
return m_houseNumber;
}
void Address::setHouseNumber(const QString &houseNumber)
{
m_houseNumber = houseNumber;
}
int Address::id() const
{
return m_id;
}
void Address::setId(int id)
{
m_id = id;
}
bool Address::eq(ComboItem *other)
{
Address *addr = qobject_cast<Address*>(other);
return addr != NULL && m_id == addr->id();
}
QString Address::toString()
{
return m_street + ", " + m_houseNumber + ", " + m_city;
}

@ -0,0 +1,55 @@
#ifndef ADDRESS_H
#define ADDRESS_H
#include <QObject>
#include <QString>
#include <data/comboitem.h>
#include <odb/core.hxx>
#pragma db object
class Address : public ComboItem
{
Q_OBJECT
Q_PROPERTY(QString city READ city WRITE setCity)
Q_PROPERTY(QString street READ street WRITE setStreet)
Q_PROPERTY(QString houseNumber READ houseNumber WRITE setHouseNumber)
public:
explicit Address(QObject *parent = 0);
~Address();
QString city() const;
void setCity(const QString &city);
QString street() const;
void setStreet(const QString &street);
QString houseNumber() const;
void setHouseNumber(const QString &houseNumber);
int id() const;
void setId(int id);
private:
friend class odb::access;
#pragma db id auto
int m_id;
QString m_city;
QString m_street;
QString m_houseNumber;
signals:
public slots:
// ComboItem interface
public:
virtual bool eq(ComboItem *other);
virtual QString toString();
};
#endif // ADDRESS_H

@ -1,3 +1,4 @@
#include <data/comboitem.h>
#include "person.h" #include "person.h"
Person::Person() Person::Person()
@ -30,6 +31,19 @@ void Person::setLastName(const QString &value)
{ {
lastName = value; lastName = value;
} }
QSharedPointer<QObject> Person::address() const
{
return m_address;
}
void Person::setAddress(const QSharedPointer<QObject> &address)
{
if (qobject_cast<Address*>(address.data()) != NULL)
{
m_address = qSharedPointerDynamicCast<Address, QObject>(address);
}
}

@ -4,6 +4,8 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include "address.h"
#include <odb/core.hxx> #include <odb/core.hxx>
#pragma db object #pragma db object
@ -13,6 +15,7 @@ class Person : public QObject
Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName) Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName)
Q_PROPERTY(QString lastName READ getLastName WRITE setLastName) Q_PROPERTY(QString lastName READ getLastName WRITE setLastName)
Q_PROPERTY(QSharedPointer<QObject> address READ address WRITE setAddress)
public: public:
Person(); Person();
@ -25,12 +28,16 @@ public:
QString getLastName() const; QString getLastName() const;
void setLastName(const QString &value); void setLastName(const QString &value);
QSharedPointer<QObject> address() const;
void setAddress(const QSharedPointer<QObject> &address);
private: private:
friend class odb::access; friend class odb::access;
#pragma db id auto #pragma db id auto
int m_id; int m_id;
QString firstName; QString firstName;
QString lastName; QString lastName;
QSharedPointer<Address> m_address;
}; };

@ -9,6 +9,7 @@
#include <QDebug> #include <QDebug>
#include <QVariant> #include <QVariant>
#include <QMessageBox> #include <QMessageBox>
#include <QComboBox>
#include "iform.h" #include "iform.h"
#include "service.h" #include "service.h"
@ -16,6 +17,7 @@
#include "iservice.h" #include "iservice.h"
#include "iplugin.h" #include "iplugin.h"
#include "combodata.h"
template <class T> template <class T>
class AutoForm : public IForm class AutoForm : public IForm
@ -53,6 +55,10 @@ public:
} }
} }
void registerBinding(QComboBox *combo, const QList<ComboData> &values) {
m_bindCombos[combo] = values;
}
void registerValidator(IValidator *validator) { void registerValidator(IValidator *validator) {
m_validators.append(validator); m_validators.append(validator);
} }
@ -60,18 +66,46 @@ public:
protected: protected:
virtual void bindOtherToUi() {} virtual void bindOtherToUi() {}
virtual bool bindOtherToData() { return true; } virtual bool bindOtherToData() { return true; }
virtual void registerCombos() {}
private: private:
QSharedPointer<T> m_entity; QSharedPointer<T> m_entity;
QList<QWidget*> m_bindWidgets; QList<QWidget*> m_bindWidgets;
QList<IValidator*> m_validators; QList<IValidator*> m_validators;
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
bool m_newRec; bool m_newRec;
void bindToUi() { void bindToUi() {
registerCombos();
foreach (QWidget *widget, m_bindWidgets) { foreach (QWidget *widget, m_bindWidgets) {
const char* prop = widget->metaObject()->userProperty().name(); const char* prop = widget->metaObject()->userProperty().name();
widget->setProperty(prop, ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str())); widget->setProperty(prop, ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str()));
} }
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(); bindOtherToUi();
} }
@ -88,6 +122,10 @@ private:
((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop)); ((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop));
} }
foreach (QComboBox *combo, m_bindCombos.keys()) {
((QObject*)m_entity.data())->setProperty(combo->objectName().toStdString().c_str(), combo->currentData());
}
return bindOtherToData(); return bindOtherToData();
} }

@ -10,6 +10,7 @@
#include "core_global.h" #include "core_global.h"
#include "exprevaluator.h" #include "exprevaluator.h"
#include "itablemodel.h" #include "itablemodel.h"
#include "data/comboitem.h"
template<class T> template<class T>
class AutoTableModel : public ITableModel class AutoTableModel : public ITableModel
@ -51,7 +52,13 @@ public:
QSharedPointer<T> entity = m_list.at(index.row()); QSharedPointer<T> entity = m_list.at(index.row());
QObject *rawEntity = (QObject*)entity.data(); QObject *rawEntity = (QObject*)entity.data();
return rawEntity->property(rawEntity->metaObject()->property(index.column() + 1).name()); QVariant dispData = rawEntity->property(rawEntity->metaObject()->property(index.column() + 1).name());
if (dispData.canConvert<QObject*>() && qobject_cast<ComboItem*>(dispData.value<QObject*>()))
{
return qobject_cast<ComboItem*>(dispData.value<QObject*>())->toString();
}
return dispData;
} }
return QVariant::Invalid; return QVariant::Invalid;

@ -0,0 +1,42 @@
#include "combodata.h"
ComboData::ComboData(const QVariant &index, const QString &label)
{
m_index = index;
m_label = label;
}
ComboData::ComboData(const QSharedPointer<QObject> &index)
{
m_index = QVariant::fromValue(index);
ComboItem *ci = qobject_cast<ComboItem*>(index.data());
if (ci != NULL)
{
m_label = ci->toString();
}
}
ComboData::~ComboData()
{
}
QVariant ComboData::index() const
{
return m_index;
}
void ComboData::setIndex(const QVariant &index)
{
m_index = index;
}
QString ComboData::label() const
{
return m_label;
}
void ComboData::setLabel(const QString &label)
{
m_label = label;
}

@ -0,0 +1,28 @@
#ifndef COMBODATA_H
#define COMBODATA_H
#include <QVariant>
#include <QSharedDataPointer>
#include "core_global.h"
#include "data/comboitem.h"
class CORESHARED_EXPORT ComboData
{
public:
ComboData(const QVariant &index, const QString &label);
ComboData(const QSharedPointer<QObject> &index);
~ComboData();
QVariant index() const;
void setIndex(const QVariant &index);
QString label() const;
void setLabel(const QString &label);
private:
QVariant m_index;
QString m_label;
};
#endif // COMBODATA_H

@ -49,7 +49,7 @@ void Context::loadPlugins()
m_plugins.append(new Users); m_plugins.append(new Users);
m_plugins.append(new Roles); m_plugins.append(new Roles);
QDir pluginsDir(qApp->applicationDirPath() + "/../plugins"); QDir pluginsDir(qApp->applicationDirPath() + "/../../plugins");
foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) { foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
@ -96,10 +96,10 @@ void Context::destroy()
m_dbOpened = false; m_dbOpened = false;
} }
if (m_settings != NULL && m_settings->parent() == NULL) /*if (m_settings != NULL && m_settings->parent() == NULL)
{ {
delete m_settings; delete m_settings;
} }*/
foreach (IPlugin *plugin, m_plugins) foreach (IPlugin *plugin, m_plugins)
{ {

@ -8,5 +8,6 @@
#include "transaction.h" #include "transaction.h"
#include "gridform.h" #include "gridform.h"
#include "permissionservice.h" #include "permissionservice.h"
#include "combodata.h"
#endif // CORE_H #endif // CORE_H

@ -43,7 +43,9 @@ SOURCES += \
savefilterdialog.cpp \ savefilterdialog.cpp \
filterdialog.cpp \ filterdialog.cpp \
itablemodel.cpp \ itablemodel.cpp \
iservice.cpp iservice.cpp \
combodata.cpp \
data/comboitem.cpp
HEADERS += core.h\ HEADERS += core.h\
core_global.h \ core_global.h \
@ -84,7 +86,9 @@ HEADERS += core.h\
filterdialog.h \ filterdialog.h \
itablemodel.h \ itablemodel.h \
data/core_global.h \ data/core_global.h \
iservice.h iservice.h \
combodata.h \
data/comboitem.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib
@ -121,3 +125,5 @@ FORMS += \
OTHER_FILES += \ OTHER_FILES += \
users/metaData.json \ users/metaData.json \
roles/metaData.json roles/metaData.json
TRANSLATIONS = core_cz.ts

@ -0,0 +1,11 @@
#include "comboitem.h"
ComboItem::ComboItem(QObject *parent)
:QObject(parent)
{
}
ComboItem::~ComboItem()
{
}

@ -0,0 +1,21 @@
#ifndef COMBOITEM_H
#define COMBOITEM_H
#include "core_global.h"
#include <QSharedPointer>
#include <QVariant>
#include <QObject>
class CORESHARED_EXPORT ComboItem : public QObject
{
Q_OBJECT
public:
explicit ComboItem(QObject *parent = NULL);
~ComboItem();
virtual bool eq(ComboItem *other) = 0;
virtual QString toString() = 0;
};
#endif // COMBOITEM_H

@ -42,6 +42,7 @@ DEFINES += DATABASE_SQLITE
# #
ODB_FLAGS += -I $$[QT_INSTALL_HEADERS] ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]
ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore ODB_FLAGS += -I $$[QT_INSTALL_HEADERS]/QtCore
ODB_FLAGS += -I $$PWD/core
ODB_FLAGS += -D __PIC__ ODB_FLAGS += -D __PIC__
win32 { win32 {

Loading…
Cancel
Save