From 6a042c46a19b5fbe0d6622be3079c164c2c5a6da Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Sat, 20 May 2017 23:40:57 +0200 Subject: [PATCH] Implemented first page of camp wizard. --- addressbook/addressbookservice.cpp | 19 +++- addressbook/addressbookservice.h | 5 +- camp/camp.json | 5 +- camp/campgrid.cpp | 3 + camp/campservice.cpp | 27 +++++ camp/campservice.h | 11 ++- camp/campwizard.cpp | 153 +++++++++++++++++++++++++++++ camp/campwizard.h | 38 +++++++ camp/campwizard.ui | 36 +++++-- camp/data/addressitem.cpp | 11 +++ camp/data/addressitem.h | 5 + camp/data/campdata.cpp | 10 ++ camp/data/campdata.h | 2 + 13 files changed, 310 insertions(+), 15 deletions(-) diff --git a/addressbook/addressbookservice.cpp b/addressbook/addressbookservice.cpp index 1f491fd..97a6979 100644 --- a/addressbook/addressbookservice.cpp +++ b/addressbook/addressbookservice.cpp @@ -10,9 +10,22 @@ AddressBookService::~AddressBookService() { } -QList > AddressBookService::all(const QString &where) +AddressbookDataPtr AddressBookService::copyAddress(AddressbookDataPtr address) { - Service srv; - return srv.all(where); + AddressbookDataPtr newAddress(new AddressbookData()); + + newAddress->setTitle(address->title()); + newAddress->setFirstName(address->firstName()); + newAddress->setLastName(address->lastName()); + newAddress->setBirthDate(address->birthDate()); + newAddress->setIdCardNumber(address->idCardNumber()); + newAddress->setZtp(address->ztp()); + newAddress->setAddressStreet(address->addressStreet()); + newAddress->setAddressHouseNumber(address->addressHouseNumber()); + newAddress->setAddressZipCode(address->addressZipCode()); + newAddress->setAddressCity(address->addressCity()); + newAddress->setCountry(address->country()); + + return newAddress; } diff --git a/addressbook/addressbookservice.h b/addressbook/addressbookservice.h index 6b8ee4d..4d458ec 100644 --- a/addressbook/addressbookservice.h +++ b/addressbook/addressbookservice.h @@ -3,17 +3,18 @@ #include #include +#include #include "data/addressbookdata.h" #include "addressbook_global.h" -class ADDRESSBOOKSHARED_EXPORT AddressBookService +class ADDRESSBOOKSHARED_EXPORT AddressBookService : public Service { public: AddressBookService(); ~AddressBookService(); - QList > all(const QString &where = ""); + AddressbookDataPtr copyAddress(AddressbookDataPtr address); }; #endif // ADDRESSBOOKSERVICE_H diff --git a/camp/camp.json b/camp/camp.json index 4dae0af..fc5d329 100644 --- a/camp/camp.json +++ b/camp/camp.json @@ -8,7 +8,7 @@ "default" : "", "CZ" : "" }, - "schemaVersion" : 2, + "schemaVersion" : 3, "sql" : [ "CREATE TABLE \"CampData\" ( \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, @@ -77,6 +77,9 @@ CREATE TABLE \"PersonPrice\" ( ", "ALTER TABLE Sale ADD \"description\" TEXT NULL; +", + +"ALTER TABLE AddressItem ADD \"owner\" INTEGER NULL; " ], diff --git a/camp/campgrid.cpp b/camp/campgrid.cpp index 8560401..a4532c2 100644 --- a/camp/campgrid.cpp +++ b/camp/campgrid.cpp @@ -1,5 +1,6 @@ #include "campgrid.h" #include "campwizard.h" +#include "campservice.h" CampGrid::CampGrid(QWidget *parent) : GridForm(parent) { @@ -8,8 +9,10 @@ CampGrid::CampGrid(QWidget *parent) : GridForm(parent) void CampGrid::handleNewRecord() { + CampService srv; CampWizard *wizard = new CampWizard(); wizard->setAttribute(Qt::WA_DeleteOnClose); + wizard->setData(srv.create()); wizard->show(); } diff --git a/camp/campservice.cpp b/camp/campservice.cpp index f0fe322..d3d6580 100644 --- a/camp/campservice.cpp +++ b/camp/campservice.cpp @@ -14,5 +14,32 @@ void CampService::addPerson(CampDataPtr data, AddressbookDataPtr address) addrItem->setLastName(address->lastName()); addrItem->setCampData(data); + if (data->people().isEmpty()) + { + setOwner(data, addrItem); + } + data->addPerson(addrItem); } + +void CampService::setOwner(CampDataPtr data, AddressItemPtr person) +{ + foreach (AddressItemPtr p, data->people()) { + p->setOwner(false); + } + + person->setOwner(true); + + data->setOwnerFirstame(person->firstName()); + data->setOwnerLastname(person->lastName()); + data->setOwnerAddress(person->address()); +} + +CampDataPtr CampService::create() +{ + CampDataPtr data(new CampData); + data->setStart(QDate::currentDate()); + data->setEnd(QDate::currentDate()); + + return data; +} diff --git a/camp/campservice.h b/camp/campservice.h index 56929dc..1760707 100644 --- a/camp/campservice.h +++ b/camp/campservice.h @@ -1,11 +1,18 @@ #ifndef CAMPSERVICE_H #define CAMPSERVICE_H +#include +#include +#include "data/camp-data.h" +#include "camp-odb.hxx" -class CampService +class CampService : public Service { public: CampService(); + void addPerson(CampDataPtr data, AddressbookDataPtr address); + void setOwner(CampDataPtr data, AddressItemPtr person); + CampDataPtr create(); }; -#endif // CAMPSERVICE_H \ No newline at end of file +#endif // CAMPSERVICE_H diff --git a/camp/campwizard.cpp b/camp/campwizard.cpp index f2cb170..2f7b23f 100644 --- a/camp/campwizard.cpp +++ b/camp/campwizard.cpp @@ -1,15 +1,100 @@ #include "campwizard.h" #include "ui_campwizard.h" +#include "campservice.h" +#include +#include +AddressHelper::AddressHelper(QObject *parent) + :QObject(parent) +{ + m_address = AddressbookDataPtr(new AddressbookData); +} + +QSharedPointer AddressHelper::address() const +{ + return m_address; +} + +void AddressHelper::setAddress(const QSharedPointer &address) +{ + if (qobject_cast(address.data()) != NULL) + { + m_address = qSharedPointerDynamicCast(address); + } +} + +AddressbookDataPtr AddressHelper::addr() const +{ + return m_address; +} + +void AddressHelper::setAddr(const AddressbookDataPtr &address) +{ + m_address = address; +} + +AddressbookDataPtr AddressHelper::newAddress() +{ + AddressBookService adbSrv; + m_copyAddress = adbSrv.copyAddress(m_address); + + return m_copyAddress; +} + +AddressbookDataPtr AddressHelper::copyAddress() +{ + return m_copyAddress; +} + +/////////////////////////////////////////////////////////// +/// \brief CampWizard::CampWizard +/// \param parent +/// CampWizard::CampWizard(QWidget *parent) : QWizard(parent), ui(new Ui::CampWizard) { ui->setupUi(this); + m_peopleModel = new AutoTableModel(this); + ui->tablePeople->setModel(m_peopleModel); + ui->tablePeople->hideColumn(2); + ui->tablePeople->hideColumn(3); + ui->tablePeople->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + ui->tablePeople->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); + + connect(ui->tablePeople->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](QModelIndex, QModelIndex){ + ui->btnRemove->setEnabled(!m_data->people().isEmpty()); + ui->btnOwner->setEnabled(!m_data->people().isEmpty()); + }); + m_dataBinder = new ObjectBinder(this); m_dataBinder->registerBinding(ui->start); m_dataBinder->registerBinding(ui->end); + + m_addrHelper = new AddressHelper(this); + + Service addrSrv; + m_addrHelperBinder = new ObjectBinder(this); + m_addrHelperBinder->registerBinding(ui->address, ComboData::createComboData(addrSrv.all())); + m_addrHelperBinder->setData(m_addrHelper); + + m_addressBinder = new ObjectBinder(this); + m_addressBinder->registerBinding(ui->title); + m_addressBinder->registerBinding(ui->firstName); + m_addressBinder->registerBinding(ui->lastName); + m_addressBinder->registerBinding(ui->birthDate); + m_addressBinder->registerBinding(ui->idCardNumber); + m_addressBinder->registerBinding(ui->ztp); + m_addressBinder->registerBinding(ui->addressStreet); + m_addressBinder->registerBinding(ui->addressHouseNumber); + m_addressBinder->registerBinding(ui->addressZipCode); + m_addressBinder->registerBinding(ui->addressCity); + + Service coutrySrv; + m_addressBinder->registerBinding(ui->country, ComboData::createComboData(coutrySrv.all())); + + m_bindAddrCombo = true; } CampWizard::~CampWizard() @@ -22,9 +107,77 @@ void CampWizard::setData(const CampDataPtr &data) m_data = data; m_dataBinder->setData(data.data()); + m_dataBinder->bindToUi(); + m_addrHelperBinder->bindToUi(); } void CampWizard::on_btnAdd_clicked() { + m_addressBinder->bindToData(); + + CampService srv; + AddressBookService adbSrv; + if (ui->groupNew->isChecked()) + { + AddressbookDataPtr newAddr = adbSrv.copyAddress(m_addrHelper->copyAddress()); + adbSrv.save(newAddr); + srv.addPerson(m_data, newAddr); + + m_bindAddrCombo = false; + m_addrHelperBinder->registerBinding(ui->address, ComboData::createComboData(adbSrv.all())); + m_addrHelperBinder->bindToUi(); + m_bindAddrCombo = true; + } + else + { + srv.addPerson(m_data, m_addrHelper->addr()); + } + + m_peopleModel->setData(m_data->people()); +} + + +void CampWizard::on_address_currentIndexChanged(int) +{ + if (m_bindAddrCombo) + { + AddressBookService adbSrv; + m_addrHelperBinder->bindToData(); + m_addressBinder->setData(m_addrHelper->newAddress().data()); + m_addressBinder->bindToUi(); + } +} + +void CampWizard::on_btnRemove_clicked() +{ + AddressItemPtr selAddr = m_peopleModel->itemFromIndex(ui->tablePeople->currentIndex()); + m_data->removePerson(selAddr); + m_peopleModel->setData(m_data->people()); + + ui->btnOwner->setEnabled(false); + ui->btnRemove->setEnabled(false); +} + +void CampWizard::on_btnOwner_clicked() +{ + CampService srv; + AddressItemPtr selAddr = m_peopleModel->itemFromIndex(ui->tablePeople->currentIndex()); + srv.setOwner(m_data, selAddr); + + QModelIndex currIndex = ui->tablePeople->currentIndex(); + m_peopleModel->setData(m_data->people()); + ui->tablePeople->setCurrentIndex(currIndex); +} + +void CampWizard::on_groupNew_clicked(bool checked) +{ + if (checked) + { + ui->address->setEnabled(false); + } + else + { + ui->address->setEnabled(true); + } } diff --git a/camp/campwizard.h b/camp/campwizard.h index bd0583d..35f84c4 100644 --- a/camp/campwizard.h +++ b/camp/campwizard.h @@ -4,6 +4,30 @@ #include #include "data/camp-data.h" #include +#include +#include + +class AddressHelper : public QObject +{ + Q_OBJECT + Q_PROPERTY(QSharedPointer address READ address WRITE setAddress) + +public: + AddressHelper(QObject *parent = NULL); + + QSharedPointer address() const; + void setAddress(const QSharedPointer &address); + + AddressbookDataPtr addr() const; + void setAddr(const AddressbookDataPtr &address); + + AddressbookDataPtr newAddress(); + AddressbookDataPtr copyAddress(); + +private: + AddressbookDataPtr m_address; + AddressbookDataPtr m_copyAddress; +}; namespace Ui { class CampWizard; @@ -22,10 +46,24 @@ public: private slots: void on_btnAdd_clicked(); + void on_address_currentIndexChanged(int index); + + void on_btnRemove_clicked(); + + void on_btnOwner_clicked(); + + void on_groupNew_clicked(bool checked); + private: Ui::CampWizard *ui; CampDataPtr m_data; ObjectBinder *m_dataBinder; + ObjectBinder *m_addrHelperBinder; + ObjectBinder *m_addressBinder; + AddressHelper *m_addrHelper; + + AutoTableModel *m_peopleModel; + bool m_bindAddrCombo; }; #endif // CAMPWIZARD_H diff --git a/camp/campwizard.ui b/camp/campwizard.ui index 901aded..af97135 100644 --- a/camp/campwizard.ui +++ b/camp/campwizard.ui @@ -89,6 +89,12 @@ + + + 450 + 16777215 + + @@ -118,12 +124,12 @@ - 450 + 0 0 - New address + &New address true @@ -245,6 +251,12 @@ + + + 450 + 16777215 + + true @@ -354,7 +366,7 @@ - true + false true @@ -378,12 +390,22 @@ - + + + + 380 + 0 + + + + QAbstractItemView::NoEditTriggers + + + QAbstractItemView::SelectRows + + - widget_9 - tableView - tablePeople diff --git a/camp/data/addressitem.cpp b/camp/data/addressitem.cpp index 7886784..a9d17ea 100644 --- a/camp/data/addressitem.cpp +++ b/camp/data/addressitem.cpp @@ -5,6 +5,7 @@ AddressItem::AddressItem(QObject *parent) : QObject(parent) { m_id = 0; m_price = 0; + m_owner = false; } int AddressItem::id() const @@ -86,3 +87,13 @@ void AddressItem::setAdbItem(const AddressbookDataPtr &adbItem) { m_adbItem = adbItem; } + +bool AddressItem::owner() const +{ + return m_owner; +} + +void AddressItem::setOwner(bool owner) +{ + m_owner = owner; +} diff --git a/camp/data/addressitem.h b/camp/data/addressitem.h index 296b6c1..c82117f 100644 --- a/camp/data/addressitem.h +++ b/camp/data/addressitem.h @@ -20,6 +20,7 @@ class AddressItem : public QObject Q_PROPERTY(QString lastName READ lastName WRITE setLastName) Q_PROPERTY(QString address READ address WRITE setAddress) Q_PROPERTY(QDecDouble price READ price WRITE setPrice) + Q_PROPERTY(bool owner READ owner WRITE setOwner) public: explicit AddressItem(QObject *parent = 0); @@ -48,6 +49,9 @@ public: AddressbookDataPtr adbItem() const; void setAdbItem(const AddressbookDataPtr &adbItem); + bool owner() const; + void setOwner(bool owner); + private: friend class odb::access; #pragma db id auto @@ -60,6 +64,7 @@ private: #pragma db not_null QWeakPointer m_campData; PersonPricePtr m_personPrice; + bool m_owner; }; #endif // ADDRESSITEM_H diff --git a/camp/data/campdata.cpp b/camp/data/campdata.cpp index 8f5de66..022bd7a 100644 --- a/camp/data/campdata.cpp +++ b/camp/data/campdata.cpp @@ -84,6 +84,11 @@ void CampData::addServiceItem(ServiceItemPtr serviceItem) m_services.append(serviceItem); } +void CampData::removeServiceItem(ServiceItemPtr serviceItem) +{ + m_services.removeOne(serviceItem); +} + QOdbList CampData::people() const { return m_people; @@ -99,6 +104,11 @@ void CampData::addPerson(AddressItemPtr person) m_people.append(person); } +void CampData::removePerson(AddressItemPtr person) +{ + m_people.removeOne(person); +} + QDecDouble CampData::totalPrice() const { return TO_DEC(m_totalPrice); diff --git a/camp/data/campdata.h b/camp/data/campdata.h index 018424b..65622ee 100644 --- a/camp/data/campdata.h +++ b/camp/data/campdata.h @@ -48,10 +48,12 @@ public: QOdbList > services() const; void setServices(const QOdbList > &services); void addServiceItem(ServiceItemPtr serviceItem); + void removeServiceItem(ServiceItemPtr serviceItem); QOdbList people() const; void setPeople(const QOdbList &people); void addPerson(AddressItemPtr person); + void removePerson(AddressItemPtr person); QDecDouble totalPrice() const; void setTotalPrice(QDecDouble totalPrice);