Merge branch 'master' of https://git.bukova.info/repos/git/prodejna
						commit
						ca4a4e743d
					
				@ -0,0 +1,93 @@
 | 
			
		||||
#include "objectbinder.h"
 | 
			
		||||
#include <QDecDouble.hh>
 | 
			
		||||
 | 
			
		||||
ObjectBinder::ObjectBinder(QObject *parent)
 | 
			
		||||
    :QObject(parent)
 | 
			
		||||
{
 | 
			
		||||
    m_data = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ObjectBinder::registerBinding(QWidget *widget) {
 | 
			
		||||
    if (!m_bindWidgets.contains(widget)) {
 | 
			
		||||
        m_bindWidgets.append(widget);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ObjectBinder::registerBinding(QComboBox *combo, const QList<ComboData> &values) {
 | 
			
		||||
    m_bindCombos[combo] = values;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ObjectBinder::registerValidator(IValidator *validator) {
 | 
			
		||||
    m_validators.append(validator);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ObjectBinder::setData(QObject *data)
 | 
			
		||||
{
 | 
			
		||||
    m_data = data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ObjectBinder::bindToUi() {
 | 
			
		||||
    foreach (QWidget *widget, m_bindWidgets) {
 | 
			
		||||
        const char* prop = widget->metaObject()->userProperty().name();
 | 
			
		||||
        QVariant value = m_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 = m_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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ObjectBinder::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 (m_data->property(widget->objectName().toStdString().c_str()).canConvert<QDecDouble>())
 | 
			
		||||
        {
 | 
			
		||||
            QDecDouble dec(val.toDouble());
 | 
			
		||||
            val = QVariant::fromValue(dec);
 | 
			
		||||
        }
 | 
			
		||||
        m_data->setProperty(widget->objectName().toStdString().c_str(), val);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    foreach (QComboBox *combo, m_bindCombos.keys()) {
 | 
			
		||||
        m_data->setProperty(combo->objectName().toStdString().c_str(), combo->currentData());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
#ifndef OBJECTBINDER_H
 | 
			
		||||
#define OBJECTBINDER_H
 | 
			
		||||
 | 
			
		||||
#include <QList>
 | 
			
		||||
#include <QWidget>
 | 
			
		||||
#include <QComboBox>
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QMetaProperty>
 | 
			
		||||
#include "ivalidator.h"
 | 
			
		||||
#include "combodata.h"
 | 
			
		||||
#include "core_global.h"
 | 
			
		||||
 | 
			
		||||
class CORESHARED_EXPORT ObjectBinder : public QObject
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit ObjectBinder(QObject *parent = NULL);
 | 
			
		||||
 | 
			
		||||
    void registerBinding(QWidget *widget);
 | 
			
		||||
    void registerBinding(QComboBox *combo, const QList<ComboData> &values);
 | 
			
		||||
    void registerValidator(IValidator *validator);
 | 
			
		||||
    void setData(QObject *data);
 | 
			
		||||
    void bindToUi();
 | 
			
		||||
    bool bindToData();
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
    void validationError(QString msg);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    QList<QWidget*> m_bindWidgets;
 | 
			
		||||
    QHash<QComboBox*, QList<ComboData> > m_bindCombos;
 | 
			
		||||
    QList<IValidator*> m_validators;
 | 
			
		||||
    QObject *m_data;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // OBJECTBINDER_H
 | 
			
		||||
@ -0,0 +1,46 @@
 | 
			
		||||
#include "directsaleitem.h"
 | 
			
		||||
 | 
			
		||||
DirectSaleItem::DirectSaleItem(QObject *parent) : QObject(parent)
 | 
			
		||||
{
 | 
			
		||||
    m_count = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int DirectSaleItem::id()
 | 
			
		||||
{
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString DirectSaleItem::name()
 | 
			
		||||
{
 | 
			
		||||
    return m_name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QDecDouble DirectSaleItem::unitPrice()
 | 
			
		||||
{
 | 
			
		||||
    return m_unitPrice;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString DirectSaleItem::pluginId()
 | 
			
		||||
{
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int DirectSaleItem::count() const
 | 
			
		||||
{
 | 
			
		||||
    return m_count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DirectSaleItem::setCount(int count)
 | 
			
		||||
{
 | 
			
		||||
    m_count = count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DirectSaleItem::setName(const QString &name)
 | 
			
		||||
{
 | 
			
		||||
    m_name = name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DirectSaleItem::setUnitPrice(const QDecDouble &unitPrice)
 | 
			
		||||
{
 | 
			
		||||
    m_unitPrice = unitPrice;
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,42 @@
 | 
			
		||||
#ifndef DIRECTSALEITEM_H
 | 
			
		||||
#define DIRECTSALEITEM_H
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QString>
 | 
			
		||||
#include "ishopitem.h"
 | 
			
		||||
 | 
			
		||||
class DirectSaleItem : public QObject, public IShopItem
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
    Q_PROPERTY(QString name READ name WRITE setName)
 | 
			
		||||
    Q_PROPERTY(QDecDouble unitPrice READ unitPrice WRITE setUnitPrice)
 | 
			
		||||
    Q_PROPERTY(int count READ count WRITE setCount)
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit DirectSaleItem(QObject *parent = 0);
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
 | 
			
		||||
    // IShopItem interface
 | 
			
		||||
public:
 | 
			
		||||
    int id() override;
 | 
			
		||||
    QString name() override;
 | 
			
		||||
    QDecDouble unitPrice() override;
 | 
			
		||||
    QString pluginId() override;
 | 
			
		||||
 | 
			
		||||
    int count() const;
 | 
			
		||||
    void setCount(int count);
 | 
			
		||||
 | 
			
		||||
    void setName(const QString &name);
 | 
			
		||||
 | 
			
		||||
    void setUnitPrice(const QDecDouble &unitPrice);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    QString m_name;
 | 
			
		||||
    QDecDouble m_unitPrice;
 | 
			
		||||
    int m_count;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // DIRECTSALEITEM_H
 | 
			
		||||
@ -0,0 +1,37 @@
 | 
			
		||||
#include "shopservice.h"
 | 
			
		||||
 | 
			
		||||
ShopService::ShopService()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QSharedPointer<Voucher> ShopService::createVoucher()
 | 
			
		||||
{
 | 
			
		||||
    QSharedPointer<Voucher> voucher(new Voucher);
 | 
			
		||||
    voucher->setStatus(Voucher::NEW);
 | 
			
		||||
    return voucher;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ShopService::addShopItem(QSharedPointer<Voucher> voucher, QSharedPointer<IShopItem> item, int count)
 | 
			
		||||
{
 | 
			
		||||
    QSharedPointer<VoucherItem> vItem(new VoucherItem);
 | 
			
		||||
    vItem->setName(item->name());
 | 
			
		||||
    vItem->setUnitPrice(item->unitPrice());
 | 
			
		||||
    vItem->setCount(count);
 | 
			
		||||
    vItem->setRefId(item->id());
 | 
			
		||||
    vItem->setItemPlugin(item->pluginId());
 | 
			
		||||
 | 
			
		||||
    voucher->addItem(vItem);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ShopService::calculate(QSharedPointer<Voucher> voucher)
 | 
			
		||||
{
 | 
			
		||||
    QDecDouble total;
 | 
			
		||||
 | 
			
		||||
    foreach (QSharedPointer<VoucherItem> item, voucher->items()) {
 | 
			
		||||
        QDecDouble itemPrice = item->unitPrice() * item->count();
 | 
			
		||||
        total += itemPrice;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    voucher->setTotalPrice(total);
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,20 @@
 | 
			
		||||
#ifndef SHOPSERVICE_H
 | 
			
		||||
#define SHOPSERVICE_H
 | 
			
		||||
 | 
			
		||||
#include <QSharedPointer>
 | 
			
		||||
 | 
			
		||||
#include <core.h>
 | 
			
		||||
 | 
			
		||||
#include "data/shop-data.h"
 | 
			
		||||
#include "ishopitem.h"
 | 
			
		||||
 | 
			
		||||
class ShopService : public Service<Voucher>
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    ShopService();
 | 
			
		||||
    QSharedPointer<Voucher> createVoucher();
 | 
			
		||||
    void addShopItem(QSharedPointer<Voucher> voucher, QSharedPointer<IShopItem> item, int count);
 | 
			
		||||
    void calculate(QSharedPointer<Voucher> voucher);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // SHOPSERVICE_H
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue