diff --git a/accommodation/accgrid.cpp b/accommodation/accgrid.cpp new file mode 100644 index 0000000..ee4a4d2 --- /dev/null +++ b/accommodation/accgrid.cpp @@ -0,0 +1,14 @@ +#include "accgrid.h" + +#include "tablemodel.h" + +AccGrid::AccGrid(QWidget *parent) : + GridForm(parent) +{ + setTableModel(new TableModel()); +} + +AccGrid::~AccGrid() +{ +} + diff --git a/accommodation/accgrid.h b/accommodation/accgrid.h new file mode 100644 index 0000000..5f6959c --- /dev/null +++ b/accommodation/accgrid.h @@ -0,0 +1,18 @@ +#ifndef ACCGRID_H +#define ACCGRID_H + +#include +#include "data/person.h" + +#include "accommodation-odb.hxx" + +class AccGrid : public GridForm +{ + Q_OBJECT + +public: + explicit AccGrid(QWidget *parent = NULL); + ~AccGrid(); +}; + +#endif // ACCGRID_H diff --git a/accommodation/accommodation.cpp b/accommodation/accommodation.cpp index faf2b91..b956a0e 100644 --- a/accommodation/accommodation.cpp +++ b/accommodation/accommodation.cpp @@ -2,45 +2,36 @@ #include "accommodation.h" #include -#include "accommodationform.h" #include "data/person.h" #include "accommodationservice.h" +#include "accgrid.h" +#include "acform.h" + Accommodation::Accommodation() { } void Accommodation::initServiceUi() { - m_ui = new AccommodationForm(); + AccGrid *grid = new AccGrid(); + AcForm *form = new AcForm(); + + grid->setForm(form); + grid->setPluginId(pluginId()); AccommodationService *service = new AccommodationService(); + service->setPluginId(pluginId()); m_service = service; + m_ui = grid; } QWidget *Accommodation::ui() { QWidget *ui = IPlugin::ui(); - AccommodationForm *form = qobject_cast(ui); + AccGrid *form = qobject_cast(ui); - form->fillGrid(); + form->fillData(); return ui; } - -/* -QString Accommodation::pluginName() -{ - return "Ubytovani"; -} - -void Accommodation::init(const QJsonObject &metaData) -{ - -} - -QString Accommodation::pluginId() -{ - return "ACCOMMODATION"; -} -*/ diff --git a/accommodation/accommodation.pro b/accommodation/accommodation.pro index 5fa3c85..eba0263 100644 --- a/accommodation/accommodation.pro +++ b/accommodation/accommodation.pro @@ -12,21 +12,19 @@ TEMPLATE = lib DEFINES += ACCOMMODATION_LIBRARY SOURCES += accommodation.cpp \ - accommodationform.cpp \ data/person.cpp \ - dialog.cpp \ accommodationservice.cpp \ tablemodel.cpp \ - acform.cpp + acform.cpp \ + accgrid.cpp HEADERS += accommodation.h\ accommodation_global.h \ - accommodationform.h \ data/person.h \ - dialog.h \ accommodationservice.h \ tablemodel.h \ - acform.h + acform.h \ + accgrid.h unix { target.path = /usr/lib @@ -48,8 +46,6 @@ OTHER_FILES += \ accommodation.json FORMS += \ - accommodationform.ui \ - dialog.ui \ acform.ui ODB_FILES = accommodation/data/person.h diff --git a/accommodation/accommodationform.cpp b/accommodation/accommodationform.cpp deleted file mode 100644 index ffe052f..0000000 --- a/accommodation/accommodationform.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "accommodationform.h" -#include "ui_accommodationform.h" - -#include -#include - -#include "dialog.h" -#include "data/person.h" - -#include "tablemodel.h" - -#include "accommodation-odb.hxx" - -AccommodationForm::AccommodationForm(QWidget *parent) : - QWidget(parent), - ui(new Ui::AccommodationForm) -{ - ui->setupUi(this); -} - -AccommodationForm::~AccommodationForm() -{ - delete ui; -} - -void AccommodationForm::fillGrid() -{ - Service service; - TableModel *model = qobject_cast(ui->tableView->model()); - - if (model == NULL) { - model = new TableModel(); - } - - model->setData(service.all()); - ui->tableView->setModel(model); -} - -void AccommodationForm::on_pushButton_clicked() -{ - Dialog *d = new Dialog(); - if (ui->tableView->model()->rowCount() > 0) - { - d->setData(((TableModel*)ui->tableView->model())->itemFromIndex(ui->tableView->currentIndex())); - } - d->open(); -} diff --git a/accommodation/accommodationform.h b/accommodation/accommodationform.h deleted file mode 100644 index ad5c602..0000000 --- a/accommodation/accommodationform.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef ACCOMMODATIONFORM_H -#define ACCOMMODATIONFORM_H - -#include - -namespace Ui { -class AccommodationForm; -} - -class AccommodationForm : public QWidget -{ - Q_OBJECT - -public: - explicit AccommodationForm(QWidget *parent = 0); - ~AccommodationForm(); - - void fillGrid(); - -private slots: - void on_pushButton_clicked(); - -private: - Ui::AccommodationForm *ui; -}; - -#endif // ACCOMMODATIONFORM_H diff --git a/accommodation/accommodationform.ui b/accommodation/accommodationform.ui deleted file mode 100644 index 820a5df..0000000 --- a/accommodation/accommodationform.ui +++ /dev/null @@ -1,41 +0,0 @@ - - - AccommodationForm - - - - 0 - 0 - 828 - 548 - - - - Form - - - - - - PushButton - - - - - - - QAbstractItemView::SingleSelection - - - QAbstractItemView::SelectRows - - - true - - - - - - - - diff --git a/accommodation/acform.ui b/accommodation/acform.ui index cff0c6f..5e7aabe 100644 --- a/accommodation/acform.ui +++ b/accommodation/acform.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 300 + 833 + 536 @@ -33,6 +33,19 @@ + + + + 610 + 410 + 81 + 22 + + + + PushButton + + diff --git a/accommodation/dialog.cpp b/accommodation/dialog.cpp deleted file mode 100644 index 54071ba..0000000 --- a/accommodation/dialog.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "dialog.h" -#include "ui_dialog.h" - -#include - -#include "data/person.h" -#include "accommodationservice.h" - -#include "acform.h" - -#include "accommodation-odb.hxx" - -Dialog::Dialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::Dialog) -{ - ui->setupUi(this); -} - -Dialog::~Dialog() -{ - delete ui; -} - -void Dialog::setData(QSharedPointer data) -{ - m_data = data; - - AcForm *form = new AcForm(this); - form->setEntity(data); - ui->verticalLayout->addWidget(form); -} - -void Dialog::on_buttonBox_accepted() -{ - IPlugin *plugin = Context::instance().plugins().at(0); - QSharedPointer p(new Person()); - - p->setFirstName(ui->lineEdit->text()); - p->setLastName(ui->lineEdit_2->text()); - - AccommodationService *service = (AccommodationService*)plugin->service(); - service->pokus(p); -} diff --git a/accommodation/dialog.h b/accommodation/dialog.h deleted file mode 100644 index fdcee67..0000000 --- a/accommodation/dialog.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef DIALOG_H -#define DIALOG_H - -#include -#include - -#include "data/person.h" - -namespace Ui { -class Dialog; -} - -class Dialog : public QDialog -{ - Q_OBJECT - -public: - explicit Dialog(QWidget *parent = 0); - ~Dialog(); - - void setData(QSharedPointer data); - -private slots: - void on_buttonBox_accepted(); - -private: - Ui::Dialog *ui; - QSharedPointer m_data; -}; - -#endif // DIALOG_H diff --git a/accommodation/dialog.ui b/accommodation/dialog.ui deleted file mode 100644 index ad30caa..0000000 --- a/accommodation/dialog.ui +++ /dev/null @@ -1,99 +0,0 @@ - - - Dialog - - - - 0 - 0 - 544 - 444 - - - - Dialog - - - - - 30 - 340 - 341 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - 180 - 40 - 113 - 23 - - - - - - - 180 - 90 - 113 - 23 - - - - - - - 60 - 130 - 421 - 191 - - - - - - - - - buttonBox - accepted() - Dialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - Dialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/core/autoform.h b/core/autoform.h index 69410d5..5d6be9b 100644 --- a/core/autoform.h +++ b/core/autoform.h @@ -7,15 +7,18 @@ #include #include #include +#include +#include "iform.h" #include "service.h" #include "ivalidator.h" + template -class AutoForm : public QWidget +class AutoForm : public IForm { public: - explicit AutoForm(QWidget *parent = 0) : QWidget(parent) { + explicit AutoForm(QWidget *parent = 0) : IForm(parent) { m_newRec = false; } @@ -31,6 +34,14 @@ public: bindToUi(); } + QSharedPointer entity() { + return m_entity; + } + + void setNewRec(bool isNew) { + m_newRec = isNew; + } + void registerBinding(QWidget *widget) { if (!m_bindWidgets.contains(widget)) { m_bindWidgets.append(widget); @@ -70,11 +81,6 @@ private: return true; } -signals: - void recordAdded(QSharedPointer entity); - void recordUpdated(QSharedPointer entity); - void validationError(QString errMessage); - public slots: bool saveRecord() { if (!bindToData()) @@ -82,13 +88,10 @@ public slots: return false; } - Service service; - service.save(m_entity); - if (m_newRec) { - emit recordAdded(m_entity); + emit recordAdded(); } else { - emit recordUpdated(m_entity); + emit recordUpdated(); } return true; diff --git a/core/context.cpp b/core/context.cpp index 426759a..3257700 100644 --- a/core/context.cpp +++ b/core/context.cpp @@ -39,7 +39,7 @@ void Context::loadPlugins() IPlugin *corePlugin = new CorePlugin(); m_plugins.append(corePlugin); - QDir pluginsDir(qApp->applicationDirPath() + "/../../plugins"); + QDir pluginsDir(qApp->applicationDirPath() + "/../plugins"); foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) { QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); diff --git a/core/core.h b/core/core.h index b196637..a8f6fcd 100644 --- a/core/core.h +++ b/core/core.h @@ -6,5 +6,6 @@ #include "iplugin.h" #include "imetadataplugin.h" #include "transaction.h" +#include "gridform.h" #endif // CORE_H diff --git a/core/core.pro b/core/core.pro index b1e9b3e..4fa7420 100644 --- a/core/core.pro +++ b/core/core.pro @@ -19,7 +19,11 @@ SOURCES += \ emptystringvalidator.cpp \ data/role.cpp \ data/permission.cpp \ - coreplugin.cpp + coreplugin.cpp \ + igridform.cpp \ + defaultformhandler.cpp \ + formdialog.cpp \ + iform.cpp HEADERS += core.h\ core_global.h \ @@ -37,7 +41,12 @@ HEADERS += core.h\ data/permission.h \ data/core-data.h \ coreplugin.h \ - define.h + define.h \ + gridform.h \ + igridform.h \ + defaultformhandler.h \ + formdialog.h \ + iform.h unix { target.path = /usr/lib @@ -56,3 +65,7 @@ RESOURCES += \ DISTFILES += \ metaData.json + +FORMS += \ + gridform.ui \ + formdialog.ui diff --git a/core/data/core-data.h b/core/data/core-data.h index 3d7a05e..2401cfd 100644 --- a/core/data/core-data.h +++ b/core/data/core-data.h @@ -1,13 +1,13 @@ -#ifndef COREDATA_H -#define COREDATA_H - -class User; -class Permission; -class Role; - -#include "user.h" -#include "role.h" -#include "permission.h" - -#endif // COREDATA_H - +#ifndef COREDATA_H +#define COREDATA_H + +class User; +class Permission; +class Role; + +#include "user.h" +#include "role.h" +#include "permission.h" + +#endif // COREDATA_H + diff --git a/core/defaultformhandler.cpp b/core/defaultformhandler.cpp new file mode 100644 index 0000000..5bb21a2 --- /dev/null +++ b/core/defaultformhandler.cpp @@ -0,0 +1,19 @@ +#include "defaultformhandler.h" + +DefaultFormHandler::DefaultFormHandler() +{ + m_dialog = new FormDialog(); +} + +DefaultFormHandler::~DefaultFormHandler() +{ + delete m_dialog; +} + +void DefaultFormHandler::showForm(IForm *formWidget) +{ + m_dialog->setForm(formWidget); + m_dialog->setModal(true); + m_dialog->show(); +} + diff --git a/core/defaultformhandler.h b/core/defaultformhandler.h new file mode 100644 index 0000000..02429cd --- /dev/null +++ b/core/defaultformhandler.h @@ -0,0 +1,32 @@ +#ifndef DEFAULTFORMHANDLER_H +#define DEFAULTFORMHANDLER_H + +#include "formdialog.h" +#include "iform.h" + +#include "core_global.h" + +class CORESHARED_EXPORT IFormHandler +{ +public: + IFormHandler() {} + virtual ~IFormHandler() {} + + virtual void showForm(IForm *formWidget) = 0; +}; + +class DefaultFormHandler : public IFormHandler +{ +public: + DefaultFormHandler(); + virtual ~DefaultFormHandler(); + +private: + FormDialog *m_dialog; + + // IFormHandler interface +public: + void showForm(IForm *formWidget) override; +}; + +#endif // DEFAULTFORMHANDLER_H diff --git a/core/formdialog.cpp b/core/formdialog.cpp new file mode 100644 index 0000000..50a0eb4 --- /dev/null +++ b/core/formdialog.cpp @@ -0,0 +1,31 @@ +#include "formdialog.h" +#include "ui_formdialog.h" + +FormDialog::FormDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::FormDialog) +{ + ui->setupUi(this); + m_formSet = false; + m_form = NULL; +} + +FormDialog::~FormDialog() +{ + delete ui; +} + +void FormDialog::setForm(IForm *formWidget) +{ + if (m_form == NULL) + { + ui->verticalLayout->addWidget(formWidget); + m_form = formWidget; + setGeometry(formWidget->geometry()); + } +} + +void FormDialog::on_buttonBox_accepted() +{ + m_form->saveRecord(); +} diff --git a/core/formdialog.h b/core/formdialog.h new file mode 100644 index 0000000..ac45614 --- /dev/null +++ b/core/formdialog.h @@ -0,0 +1,32 @@ +#ifndef FORMDIALOG_H +#define FORMDIALOG_H + +#include +#include + +#include "iform.h" + +namespace Ui { +class FormDialog; +} + +class FormDialog : public QDialog +{ + Q_OBJECT + +public: + explicit FormDialog(QWidget *parent = 0); + ~FormDialog(); + + void setForm(IForm *formWidget); + +private slots: + void on_buttonBox_accepted(); + +private: + bool m_formSet; + IForm *m_form; + Ui::FormDialog *ui; +}; + +#endif // FORMDIALOG_H diff --git a/core/formdialog.ui b/core/formdialog.ui new file mode 100644 index 0000000..26d74f9 --- /dev/null +++ b/core/formdialog.ui @@ -0,0 +1,79 @@ + + + FormDialog + + + + 0 + 0 + 543 + 383 + + + + Dialog + + + + 0 + + + 0 + + + 0 + + + 9 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + FormDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + FormDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/core/gridform.h b/core/gridform.h new file mode 100644 index 0000000..5431ee5 --- /dev/null +++ b/core/gridform.h @@ -0,0 +1,124 @@ +#ifndef GRIDFORM_H +#define GRIDFORM_H + +#include + +#include "autoform.h" +#include "autotablemodel.h" +#include "context.h" +#include "iplugin.h" + +#include "igridform.h" + +template +class GridForm : public IGridForm +{ + +public: + explicit GridForm(QWidget *parent = 0) : + IGridForm(parent) +{ + m_form = NULL; + m_tableModel = NULL; + m_formHandler = new DefaultFormHandler(); +} + virtual ~GridForm() + { + delete m_formHandler; + } + + void setForm(AutoForm *form) { + Q_ASSERT(m_form == NULL); + + m_form = form; + //m_form->setParent(this); + + connect(m_form, SIGNAL(recordAdded()), this, SLOT(saveNew())); + connect(m_form, SIGNAL(recordUpdated()), this, SLOT(saveUpdate())); + } + + void setTableModel(AutoTableModel *tableModel) { + Q_ASSERT(m_tableModel == NULL); + + m_tableModel = tableModel; + } + + void setFormHandler(IFormHandler *handler) { + delete m_formHandler; + m_formHandler = handler; + } + +public slots: + void fillData() { + if (m_tableModel == NULL) { + Q_ASSERT(false); + return; + } + + m_tableModel->setData(service()->all()); + tableView()->setModel(m_tableModel); + } + +protected slots: + void saveNew() { + service()->save(m_form->entity()); + m_tableModel->addRow(m_form->entity()); + emit dataChanged(); + } + + void saveUpdate() { + service()->update(m_form->entity()); + emit dataChanged(); + } + +private: + AutoForm *m_form; + AutoTableModel *m_tableModel; + IFormHandler *m_formHandler; + + Service *service() { + IPlugin *plugin = Context::instance().plugin(pluginId()); + if (plugin == NULL) { + Q_ASSERT(false); + return NULL; + } + + Service *service = plugin->service(); + if (service == NULL) { + Q_ASSERT(false); + return NULL; + } + + return service; + } + + // IGridForm interface +protected: + void handleNewRecord() override + { + if (m_form == NULL) + { + Q_ASSERT(false); + return; + } + + m_form->setEntity(QSharedPointer(new T())); + m_form->setNewRec(true); + m_formHandler->showForm(m_form); + } + + void handleEditRecord() override + { + if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0) + { + Q_ASSERT(false); + return; + } + + m_form->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex())); + m_form->setNewRec(false); + m_formHandler->showForm(m_form); + } +}; + +#endif // GRIDFORM_H diff --git a/core/gridform.ui b/core/gridform.ui new file mode 100644 index 0000000..00bda1c --- /dev/null +++ b/core/gridform.ui @@ -0,0 +1,88 @@ + + + GridForm + + + + 0 + 0 + 680 + 480 + + + + Form + + + + + + + + + N + + + + + + + E + + + + + + + D + + + + + + + F + + + + + + + P + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + true + + + + + + + + diff --git a/core/iform.cpp b/core/iform.cpp new file mode 100644 index 0000000..27d2b59 --- /dev/null +++ b/core/iform.cpp @@ -0,0 +1,10 @@ +#include "iform.h" + +IForm::IForm(QWidget *parent) : QWidget(parent) +{ +} + +IForm::~IForm() +{ +} + diff --git a/core/iform.h b/core/iform.h new file mode 100644 index 0000000..c1df4f5 --- /dev/null +++ b/core/iform.h @@ -0,0 +1,23 @@ +#ifndef IFORM_H +#define IFORM_H + +#include + +class IForm : public QWidget +{ + Q_OBJECT + +public: + explicit IForm(QWidget *parent = 0); + virtual ~IForm(); + +signals: + void recordAdded(); + void recordUpdated(); + void validationError(QString errMessage); + +public slots: + virtual bool saveRecord() = 0; +}; + +#endif // IFORM_H diff --git a/core/igridform.cpp b/core/igridform.cpp new file mode 100644 index 0000000..f8befcc --- /dev/null +++ b/core/igridform.cpp @@ -0,0 +1,40 @@ +#include "igridform.h" + +#include "ui_gridform.h" + +IGridForm::IGridForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::GridForm) +{ + ui->setupUi(this); +} + +IGridForm::~IGridForm() +{ +} + +void IGridForm::setPluginId(const QString &pluginId) +{ + m_pluginId = pluginId; +} + +QString IGridForm::pluginId() +{ + return m_pluginId; +} + +QTableView *IGridForm::tableView() +{ + return ui->tableView; +} + + +void IGridForm::on_btnNew_clicked() +{ + handleNewRecord(); +} + +void IGridForm::on_btnEdit_clicked() +{ + handleEditRecord(); +} diff --git a/core/igridform.h b/core/igridform.h new file mode 100644 index 0000000..5aa2e47 --- /dev/null +++ b/core/igridform.h @@ -0,0 +1,52 @@ +#ifndef IGRIDFORM_H +#define IGRIDFORM_H + +#include +#include +#include + +#include "defaultformhandler.h" +#include "core_global.h" + +namespace Ui { + class GridForm; +} + +class CORESHARED_EXPORT IGridForm : public QWidget +{ + Q_OBJECT + +public: + explicit IGridForm(QWidget *parent = 0); + virtual ~IGridForm(); + + void setPluginId(const QString &pluginId); + QString pluginId(); + QTableView *tableView(); + +signals: + void dataChanged(); + +public slots: + virtual void fillData() = 0; + +protected slots: + virtual void saveNew() = 0; + virtual void saveUpdate() = 0; + +protected: + virtual void handleNewRecord() = 0; + virtual void handleEditRecord() = 0; + //virtual void handleDeleteRecord() = 0; + +private slots: + void on_btnNew_clicked(); + void on_btnEdit_clicked(); + +private: + QString m_pluginId; + IFormHandler *m_formHandler; + Ui::GridForm *ui; +}; + +#endif // IGRIDFORM_H diff --git a/core/service.h b/core/service.h index ab353e9..cd15f16 100644 --- a/core/service.h +++ b/core/service.h @@ -48,6 +48,16 @@ public: tx.commit(); } + void update(QSharedPointer entity) { + odb::database *db = Context::instance().db(); + + Q_ASSERT(db); + + Transaction tx; + db->update(entity); + tx.commit(); + } + QSharedPointer loadById(int id) { odb::database *db = Context::instance().db(); diff --git a/odb.pri b/odb.pri old mode 100755 new mode 100644