diff --git a/sea_transport/adminpanel.cpp b/sea_transport/adminpanel.cpp index d424284..6095b12 100644 --- a/sea_transport/adminpanel.cpp +++ b/sea_transport/adminpanel.cpp @@ -6,78 +6,298 @@ AdminPanel::AdminPanel(QWidget *parent) : QMainWindow(parent), ui(new Ui::AdminP ui->setupUi(this); -// connect(ui->pb_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested); + connect(ui->pb_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested); -// connect(ui->pb_vessels_add, &QPushButton::clicked, this, &AdminPanel::on_vessel_add); -// connect(ui->pb_vessels_remove, &QPushButton::clicked, this, &AdminPanel::on_vessel_remove); + connect(ui->pb_vessels_add, &QPushButton::clicked, this, [this](){ + this->on_vessel_add_edit(false); + }); + connect(ui->pb_vessels_edit, &QPushButton::clicked, this, [this](){ + this->on_vessel_add_edit(true); + }); + connect(ui->pb_vessels_remove, &QPushButton::clicked, this, &AdminPanel::on_vessel_remove); -// connect(ui->pb_users_add, &QPushButton::clicked, this, &AdminPanel::on_user_add); -// connect(ui->pb_users_remove, &QPushButton::clicked, this, &AdminPanel::on_user_remove); + connect(ui->pb_users_add, &QPushButton::clicked, this, [this](){ + this->on_user_add_edit(false); + }); + connect(ui->pb_users_edit, &QPushButton::clicked, this, [this](){ + this->on_user_add_edit(true); + }); + connect(ui->pb_users_remove, &QPushButton::clicked, this, &AdminPanel::on_user_remove); -// connect(ui->pb_users_add, &QPushButton::clicked, this, &AdminPanel::on_storage_add); -// connect(ui->pb_users_remove, &QPushButton::clicked, this, &AdminPanel::on_storage_remove); + connect(ui->pb_dp_add, &QPushButton::clicked, this, [this](){ + this->on_delivery_point_add_edit(false); + }); + connect(ui->pb_dp_edit, &QPushButton::clicked, this, [this](){ + this->on_delivery_point_add_edit(true); + }); + connect(ui->pb_dp_remove, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_remove); -// connect(ui->pb_dp_add, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_add); -// connect(ui->pb_dp_remove, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_remove); - - -// ui->tv_vessels->setModel(); uvm = new UsersViewModel(this); - ui->tv_users->setModel(uvm); + ui->tv_users->setModel(this->uvm); -// ui->tv_dp->setModel(); + vvm = new VesselsViewModel(this); + ui->tv_vessels->setModel(vvm); -// ui->tv_storages->setModel(); + dpvm = new DeliveryPointsViewModel(this); + ui->tv_dp->setModel(dpvm); + connect(ui->tv_users->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_users_remove->setEnabled(selected.length() > 0); + ui->pb_users_edit->setEnabled(selected.length() == 1); + }); - uvm->update(); + connect(ui->tv_vessels->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_vessels_remove->setEnabled(selected.length() > 0); + ui->pb_vessels_edit->setEnabled(selected.length() == 1); + }); + + connect(ui->tv_dp->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_dp_remove->setEnabled(selected.length() > 0); + ui->pb_dp_edit->setEnabled(selected.length() == 1); + }); + + ui->tw_tabs->setCurrentIndex(0); } AdminPanel::~AdminPanel() { delete ui; delete uvm; + delete vvm; + delete dpvm; } AdminPanel& AdminPanel::set_user(const user_entity &user) { this->user = user; - ui->lab_user->setText(tr("Hello user %1").arg(user.login())); + ui->lab_user->setText(tr("Hello, **%1**").arg(user.login())); return *this; } void AdminPanel::on_logout_requested() { - + this->close(); } -void AdminPanel::on_vessel_add() { +void AdminPanel::on_vessel_add_edit(bool edit) { + auto selected = ui->tv_vessels->selectionModel()->selectedRows(); + if (edit && selected.length() != 1) { + return; + } + if (apparatus::instance()->get_object_subsystem()->dpoints().isEmpty()) { + QMessageBox::critical(this, "Error", "No harbors to assign. At least one required."); + return; + } + + int skippers = 0; + foreach (auto user, apparatus::instance()->get_auth_subsystem()->users()) { + skippers += user.role() == UserRole::SKIPPER; + } + if (skippers == 0) { + QMessageBox::critical(this, "Error", "No skippers to assign. At least one required."); + return; + } + + vessel_entity ves; + if (edit) { + int idx = selected[0].row(); + ves = apparatus::instance()->get_object_subsystem()->vessels()[idx]; + } + + VesselEditDialog ved(this); + ved.setWindowTitle(edit? "Edit vessel" : "New vessel"); + ved.set_vessel(&ves, edit); + if (ved.exec() != UserEditDialog::Accepted) { + return; + } + + auto data = ved.vessel(); + if (edit) { + apparatus::instance()->get_object_subsystem()->remove_vessel(ves.id()); + QMessageBox::information(this, "Info", "Vessel edited successfully"); + } + else { + QMessageBox::information(this, "Info", "Vessel created successfully"); + } + apparatus::instance()->get_object_subsystem()->add_vessel(*data); + + vvm->update(); + dpvm->update(); } void AdminPanel::on_vessel_remove() { + auto selected = ui->tv_vessels->selectionModel()->selectedRows(); + if (selected.length() == 0) { + return; + } + QMessageBox delConf(this); + delConf.setIcon(QMessageBox::Question); + delConf.setWindowTitle(tr("Deletion confirmation")); + delConf.setText(tr("Are you sure you want to delete these vessels?")); + delConf.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + delConf.setDefaultButton(QMessageBox::No); + if (delConf.exec() == QMessageBox::No) { + return; + } + + foreach (auto mIdx, selected) { + entity_id oid = mIdx.data().toULongLong(); + apparatus::instance()->get_object_subsystem()->remove_vessel(oid); + } + + vvm->update(); } -void AdminPanel::on_user_add() { +void AdminPanel::on_user_add_edit(bool edit) { + auto selected = ui->tv_users->selectionModel()->selectedRows(); + if (edit && selected.length() != 1) { + return; + } + user_entity usr; + if (edit) { + int idx = selected[0].row(); + usr = apparatus::instance()->get_auth_subsystem()->users()[idx]; + if (usr.id() == this->user.id()) { + QMessageBox::critical(this, "Error", "You cannot edit yourself"); + return; + } + } + + UserEditDialog ued(this); + ued.setWindowTitle(edit? "Edit user" : "New user"); + ued.set_user(&usr, edit); + if (ued.exec() != UserEditDialog::Accepted) { + return; + } + + auto data = ued.user_data(); + if (edit) { + bool success; + auto user = apparatus::instance()->get_auth_subsystem()->get_user(usr.login(), success); + if (success) { + user->set_password(data->password); + user->set_role(data->role); + QMessageBox::information(this, "Info", "User edited successfully (note: you cannot change login)"); + } + else { + QMessageBox::critical(this, "Error", "Error while editing user"); + return; + } + } + else { + bool success = apparatus::instance()->get_auth_subsystem()->register_user(data->login, data->password, data->role); + if (success) { + QMessageBox::information(this, "Info", "User created successfully"); + } + else { + QMessageBox::critical(this, "Error", "Error while creating user"); + return; + } + } + + uvm->update(); } void AdminPanel::on_user_remove() { + auto selected = ui->tv_users->selectionModel()->selectedRows(); + if (selected.length() == 0) { + return; + } + QMessageBox delConf(this); + delConf.setIcon(QMessageBox::Question); + delConf.setWindowTitle(tr("Deletion confirmation")); + delConf.setText(tr("Are you sure you want to delete these users?")); + delConf.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + delConf.setDefaultButton(QMessageBox::No); + if (delConf.exec() == QMessageBox::No) { + return; + } + + auto _u = apparatus::instance()->get_auth_subsystem()->users(); + foreach (auto mIdx, selected) { + int idx = mIdx.row(); + auto ent = _u[idx]; + if (ent.id() == user.id()) { + QMessageBox::critical(this, "Error", "You cannot delete yourself!"); + break; + } + apparatus::instance()->get_auth_subsystem()->remove_user(ent.login()); + } + + uvm->update(); } -void AdminPanel::on_storage_add() { +void AdminPanel::on_delivery_point_add_edit(bool edit) { + auto selected = ui->tv_dp->selectionModel()->selectedRows(); + if (edit && selected.length() != 1) { + return; + } -} + dpoint_entity dpoint; + if (edit) { + int idx = selected[0].row(); + dpoint = apparatus::instance()->get_object_subsystem()->dpoints()[idx]; + } -void AdminPanel::on_storage_remove() { + DeliveryPointEditDialog dped(this); + dped.setWindowTitle(edit? "Edit delivery point" : "New delivery point"); + dped.set_dpoint(&dpoint, edit); + if (dped.exec() != UserEditDialog::Accepted) { + return; + } -} + auto data = dped.dpoint(); + if (edit) { + bool success; + auto dp = apparatus::instance()->get_object_subsystem()->get_dpoint(dpoint.id(), success); + if (success) { + QMessageBox::information(this, "Info", "Successfully edited delivery point"); + } + else { + QMessageBox::critical(this, "Error", "Error editing delivery point"); + return; + } -void AdminPanel::on_delivery_point_add() { + dp->set_title(data->title()); + dp->set_storages(data->storages()); + } + else { + bool success = apparatus::instance()->get_object_subsystem()->add_dpoint(*data); + if (success) { + QMessageBox::information(this, "Info", "Successfully created delivery point"); + } + else { + QMessageBox::critical(this, "Error", "Error creating delivery point"); + return; + } + } + dpvm->update(); } void AdminPanel::on_delivery_point_remove() { + auto selected = ui->tv_dp->selectionModel()->selectedRows(); + if (selected.length() == 0) { + return; + } + QMessageBox delConf(this); + delConf.setIcon(QMessageBox::Question); + delConf.setWindowTitle(tr("Deletion confirmation")); + delConf.setText(tr("Are you sure you want to delete these delivery points?")); + delConf.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + delConf.setDefaultButton(QMessageBox::No); + if (delConf.exec() == QMessageBox::No) { + return; + } + + foreach (auto mIdx, selected) { + entity_id oid = mIdx.data().toULongLong(); + apparatus::instance()->get_object_subsystem()->remove_dpoint(oid); + } + + dpvm->update(); } diff --git a/sea_transport/adminpanel.h b/sea_transport/adminpanel.h index a65ce20..827c4b1 100644 --- a/sea_transport/adminpanel.h +++ b/sea_transport/adminpanel.h @@ -2,10 +2,19 @@ #define ADMINPANEL_H #include +#include +#include + +#include "usereditdialog.h" +#include "vesseleditdialog.h" +#include "deliverypointeditdialog.h" #include "viewmodels/usersviewmodel.h" +#include "viewmodels/vesselsviewmodel.h" +#include "viewmodels/deliverypointsviewmodel.h" #include "entities/user_entity.h" +#include "entities/dpoint_entity.h" namespace Ui { class AdminPanel; @@ -18,6 +27,8 @@ class AdminPanel : public QMainWindow user_entity user; UsersViewModel *uvm; + VesselsViewModel *vvm; + DeliveryPointsViewModel *dpvm; public: explicit AdminPanel(QWidget *parent = nullptr); @@ -32,16 +43,13 @@ private: void on_logout_requested(); - void on_vessel_add(); + void on_vessel_add_edit(bool edit); void on_vessel_remove(); - void on_user_add(); + void on_user_add_edit(bool edit); void on_user_remove(); - void on_storage_add(); - void on_storage_remove(); - - void on_delivery_point_add(); + void on_delivery_point_add_edit(bool edit); void on_delivery_point_remove(); }; diff --git a/sea_transport/adminpanel.ui b/sea_transport/adminpanel.ui index 1542c03..e472f2f 100644 --- a/sea_transport/adminpanel.ui +++ b/sea_transport/adminpanel.ui @@ -6,153 +6,21 @@ 0 0 - 1200 - 600 + 1162 + 532 + + + 0 + 0 + + MainWindow - - - - - - - Vessels - - - - - - - - - - - - Add - - - - - - - Remove - - - - - - - - - - - - - Users - - - - - - - - - - - - Add - - - - - - - Remove - - - - - - - - - - - - - Delivery points - - - - - - - - - - - - Add - - - - - - - Remove - - - - - - - - - - - - - Storages - - - - - - - - - - - - Add - - - - - - - Remove - - - - - - - - - - - Hello, user %1 - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - + @@ -166,19 +34,229 @@ + + + + Hello, %1 + + + Qt::MarkdownText + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + QTabWidget::Rounded + + + 2 + + + false + + + false + + + + Users + + + + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + true + + + + + + + Qt::Horizontal + + + + 953 + 20 + + + + + + + + + + Add + + + + + + + false + + + Edit + + + + + + + false + + + Remove + + + + + + + + + + Vessels + + + + + + + + Add + + + + + + + false + + + Edit + + + + + + + false + + + Remove + + + + + + + + + Qt::Horizontal + + + + 953 + 20 + + + + + + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + true + + + + + + + + Delivery points + + + + + + Qt::Horizontal + + + + 953 + 20 + + + + + + + + + + Add + + + + + + + false + + + Edit + + + + + + + false + + + Remove + + + + + + + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + true + + + + + + + - - - - 0 - 0 - 1200 - 21 - - - - diff --git a/sea_transport/authwindow.cpp b/sea_transport/authwindow.cpp index f8a13ff..271365f 100644 --- a/sea_transport/authwindow.cpp +++ b/sea_transport/authwindow.cpp @@ -34,6 +34,8 @@ void AuthWindow::on_auth_requested() { QMessageBox::information(this, "Info", "You are the first user of system. " "Your account type is administrator"); } + + apparatus::instance()->save(); } auto user = a->get_user(login, success); diff --git a/sea_transport/cargoeditdialog.cpp b/sea_transport/cargoeditdialog.cpp index b481bbf..a55ba79 100644 --- a/sea_transport/cargoeditdialog.cpp +++ b/sea_transport/cargoeditdialog.cpp @@ -1,14 +1,29 @@ #include "cargoeditdialog.h" #include "ui_cargoeditdialog.h" -CargoEditDialog::CargoEditDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::CargoEditDialog) -{ +CargoEditDialog::CargoEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::CargoEditDialog) { ui->setupUi(this); + + connect(ui->pb_save, &QPushButton::clicked, this, &CargoEditDialog::accept); + connect(ui->pb_discard, &QPushButton::clicked, this, &CargoEditDialog::reject); } -CargoEditDialog::~CargoEditDialog() -{ +CargoEditDialog::~CargoEditDialog() { delete ui; } + +cargo_entity* CargoEditDialog::cargo() { + return this->_cargo; +} + +void CargoEditDialog::accept() { + bool emptyTitle = ui->et_title->text().trimmed().isEmpty(); + if (emptyTitle) { + QMessageBox::critical(this, "Error", "Title cannot be empty"); + return; + } + + this->_cargo = new cargo_entity(ui->et_title->text().trimmed(), ui->sb_volume->value()); + + QDialog::accept(); +} diff --git a/sea_transport/cargoeditdialog.h b/sea_transport/cargoeditdialog.h index 2da4b33..fcab6ea 100644 --- a/sea_transport/cargoeditdialog.h +++ b/sea_transport/cargoeditdialog.h @@ -2,21 +2,28 @@ #define CARGOEDITDIALOG_H #include +#include + +#include "entities/cargo_entity.h" namespace Ui { class CargoEditDialog; } -class CargoEditDialog : public QDialog -{ +class CargoEditDialog : public QDialog { Q_OBJECT + Ui::CargoEditDialog *ui; + + cargo_entity *_cargo; public: explicit CargoEditDialog(QWidget *parent = nullptr); ~CargoEditDialog(); -private: - Ui::CargoEditDialog *ui; + cargo_entity* cargo(); + +public slots: + void accept() Q_DECL_OVERRIDE; }; #endif // CARGOEDITDIALOG_H diff --git a/sea_transport/cargoeditdialog.ui b/sea_transport/cargoeditdialog.ui index a6b62e3..fd1b1f7 100644 --- a/sea_transport/cargoeditdialog.ui +++ b/sea_transport/cargoeditdialog.ui @@ -7,9 +7,21 @@ 0 0 318 - 279 + 127 + + + 318 + 127 + + + + + 318 + 127 + + Dialog @@ -19,12 +31,12 @@ - Cargo ID: + Title - + @@ -33,28 +45,17 @@ - Quantity: + Volume: - - - + + + 1 - - - - - - Destination: - - - - - - - Choose... + + 500000 @@ -76,14 +77,14 @@ - + Discard - + Save diff --git a/sea_transport/deliverypointeditdialog.cpp b/sea_transport/deliverypointeditdialog.cpp index a010715..c57d7e4 100644 --- a/sea_transport/deliverypointeditdialog.cpp +++ b/sea_transport/deliverypointeditdialog.cpp @@ -1,14 +1,106 @@ #include "deliverypointeditdialog.h" #include "ui_deliverypointeditdialog.h" -DeliveryPointEditDialog::DeliveryPointEditDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::DeliveryPointEditDialog) -{ + +DeliveryPointEditDialog::DeliveryPointEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::DeliveryPointEditDialog) { ui->setupUi(this); + + this->svm = new QStringListModel(this); + ui->lv_storages->setModel(this->svm); + + connect(ui->lv_storages->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_storage_remove->setEnabled(selected.length() > 0); + ui->pb_storage_edit->setEnabled(selected.length() == 1); + }); + + connect(ui->pb_storage_remove, &QPushButton::clicked, [this]() { + auto sel = ui->lv_storages->selectionModel()->selectedRows(); + if (sel.length() == 0) { + return; + } + + foreach (auto mIdx, sel) { + auto cuid = mIdx.data().toString().toULongLong(); + this->_dp->remove_storage(cuid); + } + this->update_list(); + }); + + connect(ui->pb_storage_edit, &QPushButton::clicked, [this]() { + this->on_storage_edit_add(true); + }); + + connect(ui->pb_storage_add, &QPushButton::clicked, [this]() { + this->on_storage_edit_add(false); + }); + + connect(ui->pb_save, &QPushButton::clicked, this, &DeliveryPointEditDialog::accept); + connect(ui->pb_discard, &QPushButton::clicked, this, &DeliveryPointEditDialog::reject); } -DeliveryPointEditDialog::~DeliveryPointEditDialog() -{ +DeliveryPointEditDialog::~DeliveryPointEditDialog() { delete ui; } + +void DeliveryPointEditDialog::update_list() { + QStringList slist; + foreach (auto storage, this->_dp->storages()) { + slist << QString::number(storage.id()); + } + this->svm->setStringList(slist); +} + +dpoint_entity* DeliveryPointEditDialog::dpoint() const { + return this->_dp; +} + +void DeliveryPointEditDialog::on_storage_edit_add(bool edit) { + auto selected = ui->lv_storages->selectionModel()->selectedRows(); + if (edit && selected.length() != 1) { + return; + } + + storage_entity stor; + if (edit) { + int idx = selected[0].row(); + stor = this->_dp->storages()[idx]; + } + + StorageEditDialog sed(this); + sed.setWindowTitle(edit? "Edit storage" : "New storage"); + sed.set_storage(&stor, edit); + if (sed.exec() != StorageEditDialog::Accepted) { + return; + } + + auto n_storage = sed.storage(); + if (edit) { + this->_dp->remove_storage(stor.id()); + } + this->_dp->add_storage(*n_storage); + + this->update_list(); +} + +void DeliveryPointEditDialog::set_dpoint(dpoint_entity* dpoint, bool edit) { + this->_dp = new dpoint_entity(*dpoint); + + if (edit) { + ui->et_title->setText(dpoint->title()); + this->update_list(); + } +} + +void DeliveryPointEditDialog::accept() { + bool emptyTitle = ui->et_title->text().trimmed().isEmpty(); + if (emptyTitle) {; + QString message = "Some errors happend, while saving:" + "
- Title cannot be empty (all spaces - empty too)"; + QMessageBox::critical(this, "Error", message); + return; + } + + this->_dp->set_title(ui->et_title->text().trimmed()); + + QDialog::accept(); +} diff --git a/sea_transport/deliverypointeditdialog.h b/sea_transport/deliverypointeditdialog.h index 6b044e2..3eeb00b 100644 --- a/sea_transport/deliverypointeditdialog.h +++ b/sea_transport/deliverypointeditdialog.h @@ -1,22 +1,42 @@ #ifndef DELIVERYPOINTEDITDIALOG_H #define DELIVERYPOINTEDITDIALOG_H +#include #include +#include +#include +#include +#include + +#include "entities/dpoint_entity.h" +#include "system/apparatus.h" +#include "storageeditdialog.h" + namespace Ui { class DeliveryPointEditDialog; } -class DeliveryPointEditDialog : public QDialog -{ +class DeliveryPointEditDialog : public QDialog { Q_OBJECT + Ui::DeliveryPointEditDialog *ui; + + QStringListModel *svm; + dpoint_entity *_dp; + + void update_list(); public: explicit DeliveryPointEditDialog(QWidget *parent = nullptr); ~DeliveryPointEditDialog(); -private: - Ui::DeliveryPointEditDialog *ui; + dpoint_entity* dpoint() const; + void set_dpoint(dpoint_entity* dpoint, bool edit); + +public slots: + void on_storage_edit_add(bool edit); + + void accept() Q_DECL_OVERRIDE; }; #endif // DELIVERYPOINTEDITDIALOG_H diff --git a/sea_transport/deliverypointeditdialog.ui b/sea_transport/deliverypointeditdialog.ui index 4a0ab3d..0d7c4ae 100644 --- a/sea_transport/deliverypointeditdialog.ui +++ b/sea_transport/deliverypointeditdialog.ui @@ -10,10 +10,22 @@ 386 + + + 394 + 386 + + + + + 394 + 386 + + Dialog - + @@ -44,28 +56,51 @@ - - - - - - - - Add storage - - - - - - - false - - - Remove storage - - - - + + + Storages IDs + + + + + + QAbstractItemView::NoEditTriggers + + + + + + + + + Add storage + + + + + + + false + + + Edit storage + + + + + + + false + + + Remove storage + + + + + + + @@ -83,14 +118,14 @@ - + Discard - + Save diff --git a/sea_transport/entities/cargo_entity.cpp b/sea_transport/entities/cargo_entity.cpp index 4104050..1fee72e 100644 --- a/sea_transport/entities/cargo_entity.cpp +++ b/sea_transport/entities/cargo_entity.cpp @@ -1,11 +1,19 @@ #include "cargo_entity.h" + +entity_id cargo_entity::__global_id = 0; + +cargo_entity::cargo_entity() { + this->_id = ++cargo_entity::__global_id + QRandomGenerator().generate64(); +} + cargo_entity::cargo_entity(const QString &title, unsigned int volume) : _title(title), _volume(volume) { this->_id = volume; auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5); for (auto bit : hash) { this->_id += bit; } + this->_id += QRandomGenerator().generate64(); } entity_id cargo_entity::id() const { @@ -27,3 +35,11 @@ void cargo_entity::serialize(QDataStream &output) { void cargo_entity::deserialize(QDataStream &input) { input >> this->_id >> this->_title >> this->_volume; } + +void cargo_entity::preloadGlobalId(entity_id gid) { + cargo_entity::__global_id = gid; +} + +entity_id cargo_entity::GID() { + return cargo_entity::__global_id; +} diff --git a/sea_transport/entities/cargo_entity.h b/sea_transport/entities/cargo_entity.h index 8d84b3a..79f928d 100644 --- a/sea_transport/entities/cargo_entity.h +++ b/sea_transport/entities/cargo_entity.h @@ -4,17 +4,20 @@ #include "IEntity.h" #include +#include #include class cargo_entity : public IEntity { private: - entity_id _id; + static entity_id __global_id; + + entity_id _id = 0; QString _title; - unsigned int _volume; + unsigned int _volume = 50000; public: - cargo_entity() = default; + cargo_entity(); cargo_entity(const QString &title, unsigned int volume); entity_id id() const; @@ -23,6 +26,8 @@ public: void serialize(QDataStream &output); void deserialize(QDataStream &input); + static void preloadGlobalId(entity_id gid); + static entity_id GID(); }; #endif // CARGO_ENTITY_H diff --git a/sea_transport/entities/dpoint_entity.cpp b/sea_transport/entities/dpoint_entity.cpp index abc256a..13b7873 100644 --- a/sea_transport/entities/dpoint_entity.cpp +++ b/sea_transport/entities/dpoint_entity.cpp @@ -1,25 +1,75 @@ #include "dpoint_entity.h" -dpoint_entity::dpoint_entity(const QString &title) : _title(title) { - this->_id = 0; + +entity_id dpoint_entity::__global_id = 0; + +dpoint_entity::dpoint_entity() { + this->_id = ++dpoint_entity::__global_id + QRandomGenerator().generate64(); +} + +dpoint_entity::dpoint_entity(entity_id dispatcher_id, const QString &title) : _dispatcher_id(dispatcher_id), _title(title) { + this->_id = dispatcher_id; auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5); for (auto bit : hash) { this->_id += bit; } + this->_id += QRandomGenerator().generate64(); } entity_id dpoint_entity::id() const { return this->_id; } +entity_id dpoint_entity::dispatcher() const { + return this->_dispatcher_id; +} + QString dpoint_entity::title() const { return this->_title; } +void dpoint_entity::set_title(const QString &new_title) { + this->_title = new_title; +} + const QVector dpoint_entity::storages() { return this->_storages; } +storage_entity* dpoint_entity::get_storage(entity_id sid, bool &success) { + success = false; + for (int i = 0; i < this->_storages.length(); i++) { + if (this->_storages[i].id() != sid) { + continue; + } + success = true; + return &this->_storages[i]; + } + + return nullptr; +} + +void dpoint_entity::set_storages(QVector storages) { + this->_storages = storages; +} + +void dpoint_entity::remove_storage(entity_id sid) { + QVector st(this->_storages); + + for (int i = 0; i < st.length(); i++) { + if (st[i].id() == sid) { + st.removeAt(i); + break; + } + } + + this->set_storages(st); +} + +void dpoint_entity::add_storage(storage_entity ent) { + this->_storages.push_back(ent); +} + void dpoint_entity::serialize(QDataStream &output) { output << this->_id << this->_title << this->_storages.size(); for (auto &item : this->_storages) { @@ -35,3 +85,11 @@ void dpoint_entity::deserialize(QDataStream &input) { this->_storages[i].deserialize(input); } } + +void dpoint_entity::preloadGlobalId(entity_id gid) { + dpoint_entity::__global_id = gid; +} + +entity_id dpoint_entity::GID() { + return dpoint_entity::__global_id; +} diff --git a/sea_transport/entities/dpoint_entity.h b/sea_transport/entities/dpoint_entity.h index 037f8ff..75e3774 100644 --- a/sea_transport/entities/dpoint_entity.h +++ b/sea_transport/entities/dpoint_entity.h @@ -6,25 +6,37 @@ #include #include +#include #include class dpoint_entity : public IEntity { private: - entity_id _id; + static entity_id __global_id; + + entity_id _id = 0; + entity_id _dispatcher_id; QString _title; QVector _storages; public: - dpoint_entity() = default; - dpoint_entity(const QString &title); + dpoint_entity(); + dpoint_entity(entity_id dispatcher_id, const QString &title); entity_id id() const; + entity_id dispatcher() const; QString title() const; + void set_title(const QString &new_title); const QVector storages(); + storage_entity* get_storage(entity_id sid, bool &success); + void set_storages(QVector storages); + void remove_storage(entity_id sid); + void add_storage(storage_entity ent); void serialize(QDataStream &output); void deserialize(QDataStream &input); + static void preloadGlobalId(entity_id gid); + static entity_id GID(); }; #endif // DPOINT_ENTITY_H diff --git a/sea_transport/entities/storage_entity.cpp b/sea_transport/entities/storage_entity.cpp index 5e55f4c..49f5920 100644 --- a/sea_transport/entities/storage_entity.cpp +++ b/sea_transport/entities/storage_entity.cpp @@ -3,10 +3,13 @@ entity_id storage_entity::__global_id = 0; -storage_entity::storage_entity(unsigned int capacity) : _capacity(capacity) { - this->_id = ++storage_entity::__global_id; +storage_entity::storage_entity() { + this->_id = ++storage_entity::__global_id + QRandomGenerator().generate64(); } +storage_entity::storage_entity(unsigned int capacity) : _capacity(capacity) { + this->_id = ++storage_entity::__global_id + QRandomGenerator().generate64(); +} entity_id storage_entity::id() const { return this->_id; @@ -16,12 +19,16 @@ unsigned int storage_entity::capacity() const { return this->_capacity; } +void storage_entity::set_capacity(unsigned int new_capacity) { + this->_capacity = new_capacity; +} + const QVector storage_entity::cargo() { return this->_cargo; } void storage_entity::add_cargo(cargo_entity object, bool &success) { - success = ((int)this->_capacity - (int)object.volume()) > 0; + success = ((int)this->_capacity - (int)object.volume()) >= 0; if (success) { this->_cargo.push_back(object); this->_capacity -= object.volume(); diff --git a/sea_transport/entities/storage_entity.h b/sea_transport/entities/storage_entity.h index 3b8abf5..c09e2b5 100644 --- a/sea_transport/entities/storage_entity.h +++ b/sea_transport/entities/storage_entity.h @@ -5,23 +5,24 @@ #include "cargo_entity.h" #include -#include +#include class storage_entity : public IEntity { private: static entity_id __global_id; - entity_id _id; - unsigned int _capacity; + entity_id _id = 0; + unsigned int _capacity = 500000; QVector _cargo; public: - storage_entity() = default; + storage_entity(); storage_entity(unsigned int capacity); entity_id id() const; unsigned int capacity() const; + void set_capacity(unsigned int new_capacity); const QVector cargo(); void add_cargo(cargo_entity object, bool &success); diff --git a/sea_transport/entities/user_entity.cpp b/sea_transport/entities/user_entity.cpp index 5cd56a7..9d0e641 100644 --- a/sea_transport/entities/user_entity.cpp +++ b/sea_transport/entities/user_entity.cpp @@ -1,7 +1,21 @@ #include "user_entity.h" + +entity_id user_entity::__global_id = 0; + +user_entity::user_entity() { + this->_id = ++user_entity::__global_id + QRandomGenerator().generate64(); +} + user_entity::user_entity(const QString &login, const QString &password, UserRole role) : _login(login), _role(role) { this->_pwd_hash = QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256); + foreach (auto bit, this->_pwd_hash) { + this->_id += bit; + } + foreach (auto bit, QCryptographicHash::hash(login.toLocal8Bit(), QCryptographicHash::Sha3_256)) { + this->_id += bit; + } + this->_id += QRandomGenerator().generate64(); } entity_id user_entity::id() const { @@ -20,6 +34,14 @@ bool user_entity::verify_password(const QString &password) const { return (this->_pwd_hash == QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256)); } +void user_entity::set_password(const QString &new_password) { + this->_pwd_hash = QCryptographicHash::hash(new_password.toLocal8Bit(), QCryptographicHash::Sha3_256); +} + +void user_entity::set_role(UserRole new_role) { + this->_role = new_role; +} + void user_entity::serialize(QDataStream &output) { output << this->_id << this->_login << this->_role << this->_pwd_hash; } @@ -27,3 +49,11 @@ void user_entity::serialize(QDataStream &output) { void user_entity::deserialize(QDataStream &input) { input >> this->_id >> this->_login >> this->_role >> this->_pwd_hash; } + +void user_entity::preloadGlobalId(entity_id gid) { + user_entity::__global_id = gid; +} + +entity_id user_entity::GID() { + return user_entity::__global_id; +} diff --git a/sea_transport/entities/user_entity.h b/sea_transport/entities/user_entity.h index 890279c..f3387a1 100644 --- a/sea_transport/entities/user_entity.h +++ b/sea_transport/entities/user_entity.h @@ -4,6 +4,7 @@ #include "IEntity.h" #include +#include #include @@ -14,22 +15,28 @@ enum class UserRole { class user_entity : public IEntity { private: - entity_id _id; + static entity_id __global_id; + + entity_id _id = 0; QString _login; UserRole _role; QByteArray _pwd_hash; public: - user_entity() = default; + user_entity(); user_entity(const QString &login, const QString &password, UserRole role); entity_id id() const; const QString login() const; UserRole role() const; bool verify_password(const QString &password) const; + void set_password(const QString &new_password); + void set_role(UserRole new_role); void serialize(QDataStream &output); void deserialize(QDataStream &input); + static void preloadGlobalId(entity_id gid); + static entity_id GID(); }; #endif // USER_ENTITY_H diff --git a/sea_transport/entities/vessel_entity.cpp b/sea_transport/entities/vessel_entity.cpp index b4ccdc7..383d116 100644 --- a/sea_transport/entities/vessel_entity.cpp +++ b/sea_transport/entities/vessel_entity.cpp @@ -3,29 +3,84 @@ entity_id vessel_entity::__global_id = 0; -vessel_entity::vessel_entity(const dpoint_entity &harbor, unsigned int capacity) : _harbor(harbor), _capacity(capacity) { - this->_id = ++vessel_entity::__global_id; +vessel_entity::vessel_entity() { + this->_id = ++vessel_entity::__global_id + QRandomGenerator().generate64(); +} + +vessel_entity::vessel_entity(QString skipper, entity_id harbor_id, unsigned int capacity) : _skipper(skipper), _harbor_id(harbor_id), _capacity(capacity) { + this->_id = ++vessel_entity::__global_id + harbor_id + capacity + QRandomGenerator().generate64(); } entity_id vessel_entity::id() const { return this->_id; } -const dpoint_entity vessel_entity::harbor() const { - return this->_harbor; +QString vessel_entity::skipper() const { + return this->_skipper; +} + +void vessel_entity::set_skipper(const QString &new_skipper) { + this->_skipper = new_skipper; +} + +entity_id vessel_entity::harbor() const { + return this->_harbor_id; +} + +void vessel_entity::set_harbor(entity_id new_harbor) { + this->_harbor_id = new_harbor; } unsigned int vessel_entity::capacity() const { return this->_capacity; } +void vessel_entity::set_capacity(unsigned int new_capacity) { + this->_capacity = new_capacity; +} + const QVector vessel_entity::cargo() { return this->_cargo; } +void vessel_entity::add_cargo(cargo_entity object, bool &success) { + success = ((int)this->_capacity - (int)object.volume()) >= 0; + if (success) { + this->_cargo.push_back(object); + this->_capacity -= object.volume(); + } +} + +cargo_entity vessel_entity::get_cargo(entity_id oid, bool &found) { + cargo_entity ent; + found = false; + + auto vit = this->_cargo.begin(); + for (; vit != this->_cargo.end(); vit++) { + if ((*vit).id() == oid) { + ent = *vit; + found = true; + break; + } + } + return ent; +} + +void vessel_entity::withdraw_cargo(entity_id oid, bool &success) { + success = false; + auto vit = this->_cargo.begin(); + for (; vit != this->_cargo.end(); vit++) { + if ((*vit).id() == oid) { + this->_capacity += (*vit).volume(); + this->_cargo.erase(vit); + success = true; + break; + } + } +} + void vessel_entity::serialize(QDataStream &output) { - output << this->_id; - this->_harbor.serialize(output); + output << this->_id << this->_skipper << this->_harbor_id; output << this->_capacity << this->_cargo.size(); for (auto item : this->_cargo) { item.serialize(output); @@ -33,8 +88,7 @@ void vessel_entity::serialize(QDataStream &output) { } void vessel_entity::deserialize(QDataStream &input) { - input >> this->_id; - this->_harbor.deserialize(input); + input >> this->_id >> this->_skipper >> this->_harbor_id; int icnt; input >> this->_capacity >> icnt; this->_cargo.resize(icnt); diff --git a/sea_transport/entities/vessel_entity.h b/sea_transport/entities/vessel_entity.h index f381f37..a554a2b 100644 --- a/sea_transport/entities/vessel_entity.h +++ b/sea_transport/entities/vessel_entity.h @@ -5,25 +5,36 @@ #include "cargo_entity.h" #include "dpoint_entity.h" +#include + class vessel_entity : public IEntity { private: static entity_id __global_id; - entity_id _id; - dpoint_entity _harbor; - unsigned int _capacity; + entity_id _id = 0; + QString _skipper; + entity_id _harbor_id; + unsigned int _capacity = 50000; QVector _cargo; public: - vessel_entity() = default; - vessel_entity(const dpoint_entity &harbor, unsigned int capacity); + vessel_entity(); + vessel_entity(QString skipper, entity_id harbor_id, unsigned int capacity); entity_id id() const; - const dpoint_entity harbor() const; + QString skipper() const; + void set_skipper(const QString &new_skipper); + entity_id harbor() const; + void set_harbor(entity_id new_harbor); unsigned int capacity() const; + void set_capacity(unsigned int new_capacity); const QVector cargo(); + void add_cargo(cargo_entity object, bool &success); + cargo_entity get_cargo(entity_id oid, bool &found); + void withdraw_cargo(entity_id oid, bool &success); + void serialize(QDataStream &output); void deserialize(QDataStream &input); static void preloadGlobalId(entity_id gid); diff --git a/sea_transport/sea_transport.pro b/sea_transport/sea_transport.pro index e5ceb58..99e2c50 100644 --- a/sea_transport/sea_transport.pro +++ b/sea_transport/sea_transport.pro @@ -25,7 +25,10 @@ SOURCES += \ system/object_system.cpp \ usereditdialog.cpp \ vesseleditdialog.cpp \ - viewmodels/usersviewmodel.cpp + viewmodels/cargoviewmodel.cpp \ + viewmodels/deliverypointsviewmodel.cpp \ + viewmodels/usersviewmodel.cpp \ + viewmodels/vesselsviewmodel.cpp HEADERS += \ adminpanel.h \ @@ -45,7 +48,10 @@ HEADERS += \ system/object_system.h \ usereditdialog.h \ vesseleditdialog.h \ - viewmodels/usersviewmodel.h + viewmodels/cargoviewmodel.h \ + viewmodels/deliverypointsviewmodel.h \ + viewmodels/usersviewmodel.h \ + viewmodels/vesselsviewmodel.h FORMS += \ adminpanel.ui \ diff --git a/sea_transport/storageeditdialog.cpp b/sea_transport/storageeditdialog.cpp index bfb4dc4..e7426c0 100644 --- a/sea_transport/storageeditdialog.cpp +++ b/sea_transport/storageeditdialog.cpp @@ -1,14 +1,86 @@ #include "storageeditdialog.h" #include "ui_storageeditdialog.h" -StorageEditDialog::StorageEditDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::StorageEditDialog) -{ +StorageEditDialog::StorageEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::StorageEditDialog) { ui->setupUi(this); + + this->cvm = new CargoViewModel(this); + ui->tv_cargo->setModel(this->cvm); + + connect(ui->tv_cargo->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_cargo_remove->setEnabled(selected.length() > 0); + }); + + connect(ui->pb_cargo_remove, &QPushButton::clicked, [this]() { + auto sel = ui->tv_cargo->selectionModel()->selectedRows(); + if (sel.length() == 0) { + return; + } + + foreach (auto mIdx, sel) { + auto oid = mIdx.data().toULongLong(); + bool success; + this->_storage->withdraw_cargo(oid, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot remove some of this cargo!"); + } + } + }); + + connect(ui->pb_cargo_add, &QPushButton::clicked, this, &StorageEditDialog::on_cargo_add); + + connect(ui->pb_save, &QPushButton::clicked, this, &StorageEditDialog::accept); + connect(ui->pb_discard, &QPushButton::clicked, this, &StorageEditDialog::reject); } -StorageEditDialog::~StorageEditDialog() -{ +StorageEditDialog::~StorageEditDialog() { delete ui; } + +storage_entity* StorageEditDialog::storage() { + return this->_storage; +} + +void StorageEditDialog::set_storage(storage_entity *ent, bool edit) { + this->_storage = new storage_entity(*ent); + + if (edit) { + ui->sb_capacity->setValue(ent->capacity()); + this->cvm->set_data(this->_storage->cargo()); + } + + ui->lab_capacity_current->setText(QString::number(this->_storage->capacity())); +} + +void StorageEditDialog::on_cargo_add() { + CargoEditDialog ced(this); + ced.setWindowTitle("New cargo"); + if (ced.exec() != CargoEditDialog::Accepted) { + return; + } + + bool success; + this->_storage->add_cargo(*ced.cargo(), success); + if (success) { + this->cvm->set_data(this->_storage->cargo()); + QMessageBox::information(this, "Success", "Cargo successfully put into storage"); + } + else { + QMessageBox::critical(this, "Error", "Not enough space to put cargo"); + } +} + +void StorageEditDialog::accept() { + int cvs = 0; + foreach (auto c, this->_storage->cargo()) { + cvs += c.volume(); + } + if (cvs > ui->sb_capacity->value()) { + QMessageBox::critical(this, "Error", "Cargo volume bigger than capacity"); + return; + } + + this->_storage->set_capacity(ui->sb_capacity->value()); + + QDialog::accept(); +} diff --git a/sea_transport/storageeditdialog.h b/sea_transport/storageeditdialog.h index bf70a54..372833a 100644 --- a/sea_transport/storageeditdialog.h +++ b/sea_transport/storageeditdialog.h @@ -1,22 +1,36 @@ #ifndef STORAGEEDITDIALOG_H #define STORAGEEDITDIALOG_H +#include #include +#include + +#include "entities/storage_entity.h" +#include "viewmodels/cargoviewmodel.h" +#include "cargoeditdialog.h" + namespace Ui { class StorageEditDialog; } -class StorageEditDialog : public QDialog -{ +class StorageEditDialog : public QDialog { Q_OBJECT + Ui::StorageEditDialog *ui; + + CargoViewModel *cvm; + storage_entity *_storage; public: explicit StorageEditDialog(QWidget *parent = nullptr); ~StorageEditDialog(); -private: - Ui::StorageEditDialog *ui; + storage_entity* storage(); + void set_storage(storage_entity *ent, bool edit); + +public slots: + void on_cargo_add(); + void accept() Q_DECL_OVERRIDE; }; #endif // STORAGEEDITDIALOG_H diff --git a/sea_transport/storageeditdialog.ui b/sea_transport/storageeditdialog.ui index 95b17c4..08e60c2 100644 --- a/sea_transport/storageeditdialog.ui +++ b/sea_transport/storageeditdialog.ui @@ -7,37 +7,93 @@ 0 0 400 - 190 + 336 + + + 400 + 336 + + + + + 400 + 336 + + Dialog - + - + - Storage num.: + Capacity (new): - + + + 1 + + + 500000 + + - + - Capacity: + Capacity (current): - + + + 0 + + + + + + Storage cargo + + + + + + + + + + + Add cargo + + + + + + + false + + + Remove cargo + + + + + + + + @@ -54,14 +110,14 @@ - + Discard - + Save diff --git a/sea_transport/system/apparatus.cpp b/sea_transport/system/apparatus.cpp index 91c2ec3..3e8e2c7 100644 --- a/sea_transport/system/apparatus.cpp +++ b/sea_transport/system/apparatus.cpp @@ -39,9 +39,12 @@ void apparatus::save() { QDataStream stream(&f); // saving GIDs - entity_id vgid = vessel_entity::GID(); + entity_id cgid = cargo_entity::GID(); + entity_id dgid = dpoint_entity::GID(); entity_id sgid = storage_entity::GID(); - stream << vgid << sgid; + entity_id ugid = user_entity::GID(); + entity_id vgid = vessel_entity::GID(); + stream << cgid << dgid << sgid << ugid << vgid; // serializing data this->_auth_system->serialize_data(&stream); @@ -52,17 +55,20 @@ void apparatus::save() { void apparatus::load() { if (_instance == nullptr) { - throw std::runtime_error("HOW DU FUCK INSTANCE IS NULL????"); + throw std::runtime_error("NO WAY INSTANCE IS NULL!"); } QFile f(apparatus::filename); f.open(QIODevice::ReadOnly); QDataStream stream(&f); // loading GIDs - entity_id vgid, sgid; - stream >> vgid >> sgid; - vessel_entity::preloadGlobalId(vgid); + entity_id cgid, dgid, sgid, ugid, vgid = ugid = sgid = dgid = cgid = 0; + stream >> cgid >> dgid >> sgid >> ugid >> vgid; + cargo_entity::preloadGlobalId(cgid); + dpoint_entity::preloadGlobalId(dgid); storage_entity::preloadGlobalId(sgid); + user_entity::preloadGlobalId(ugid); + vessel_entity::preloadGlobalId(vgid); // deserializing data this->_auth_system->deserialize_data(&stream); diff --git a/sea_transport/system/auth_system.cpp b/sea_transport/system/auth_system.cpp index a6c53aa..f10adc4 100644 --- a/sea_transport/system/auth_system.cpp +++ b/sea_transport/system/auth_system.cpp @@ -1,9 +1,8 @@ #include "apparatus.h" #include "auth_system.h" -#include -const user_entity* auth_system::get_user(const QString &login, bool &success) { +user_entity* auth_system::get_user(const QString &login, bool &success) { user_entity *out = nullptr; success = false; @@ -34,11 +33,7 @@ bool auth_system::register_user(const QString &login, const QString &password, U bool exists = false; this->get_user(login, exists); if (!exists) { - std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl; this->_users.push_back(user_entity(login, password, role)); - std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl; - apparatus::instance()->save(); - std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl; return true; } diff --git a/sea_transport/system/auth_system.h b/sea_transport/system/auth_system.h index ecc42ae..21dd056 100644 --- a/sea_transport/system/auth_system.h +++ b/sea_transport/system/auth_system.h @@ -13,7 +13,7 @@ private: public: auth_system() = default; - const user_entity* get_user(const QString &login, bool &success); + user_entity* get_user(const QString &login, bool &success); bool remove_user(const QString &login); bool register_user(const QString &login, const QString &password, UserRole role); diff --git a/sea_transport/system/object_system.cpp b/sea_transport/system/object_system.cpp index 6b6456c..1a94dcb 100644 --- a/sea_transport/system/object_system.cpp +++ b/sea_transport/system/object_system.cpp @@ -1,7 +1,7 @@ #include "object_system.h" -const dpoint_entity* object_system::get_dpoint(entity_id oid, bool &success) { +dpoint_entity* object_system::get_dpoint(entity_id oid, bool &success) { dpoint_entity *out = nullptr; success = false; @@ -39,7 +39,7 @@ bool object_system::add_dpoint(dpoint_entity dpoint) { return false; } -const vessel_entity* object_system::get_vessel(entity_id oid, bool &success) { +vessel_entity* object_system::get_vessel(entity_id oid, bool &success) { vessel_entity *out = nullptr; success = false; @@ -66,11 +66,11 @@ bool object_system::remove_vessel(entity_id oid) { return false; } -bool object_system::add_vessel(vessel_entity dpoint) { +bool object_system::add_vessel(vessel_entity vessel) { bool exists = false; - this->get_dpoint(dpoint.id(), exists); + this->get_vessel(vessel.id(), exists); if (!exists) { - this->_vessels.push_back(dpoint); + this->_vessels.push_back(vessel); return true; } diff --git a/sea_transport/system/object_system.h b/sea_transport/system/object_system.h index 2239a42..7551201 100644 --- a/sea_transport/system/object_system.h +++ b/sea_transport/system/object_system.h @@ -16,11 +16,11 @@ private: public: object_system() = default; - const dpoint_entity* get_dpoint(entity_id oid, bool &success); + dpoint_entity* get_dpoint(entity_id oid, bool &success); bool remove_dpoint(entity_id oid); bool add_dpoint(dpoint_entity dpoint); - const vessel_entity* get_vessel(entity_id oid, bool &success); + vessel_entity* get_vessel(entity_id oid, bool &success); bool remove_vessel(entity_id oid); bool add_vessel(vessel_entity dpoint); diff --git a/sea_transport/usereditdialog.cpp b/sea_transport/usereditdialog.cpp index a052c47..3239ffe 100644 --- a/sea_transport/usereditdialog.cpp +++ b/sea_transport/usereditdialog.cpp @@ -1,14 +1,72 @@ #include "usereditdialog.h" #include "ui_usereditdialog.h" -UserEditDialog::UserEditDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::UserEditDialog) -{ + +UserEditDialog::UserEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::UserEditDialog) { ui->setupUi(this); + + connect(ui->btn_save, &QPushButton::clicked, this, &UserEditDialog::accept); + connect(ui->btn_discard, &QPushButton::clicked, this, &UserEditDialog::reject); } -UserEditDialog::~UserEditDialog() -{ +UserEditDialog::~UserEditDialog() { delete ui; } + +user_data_struct* UserEditDialog::user_data() const { + return this->_user_data; +} + +void UserEditDialog::set_user(user_entity* user, bool edit) { + if (edit) { + this->_user_data = new user_data_struct(); + + ui->et_login->setText(user->login()); + ui->et_password->setText("##########UNEDITED##########"); + ui->cb_role->setCurrentIndex((int)user->role()); + ui->cb_role->setEnabled(false); + } + else { + this->_user_data = new user_data_struct{}; + } + this->_user_data->edit = edit; +} + +void UserEditDialog::accept() { + UserRole role; + switch (ui->cb_role->currentIndex()) { + case 0: + role = UserRole::ADMINISTRATOR; + break; + case 1: + role = UserRole::DISPATCHER; + break; + case 2: + role = UserRole::SKIPPER; + break; + } + bool emptyTitle = ui->et_login->text().trimmed().isEmpty(); + bool emptyPassword = ui->et_password->text().trimmed().isEmpty(); + if (emptyTitle || emptyPassword) { + QMessageBox errDlg(this); + errDlg.setTextFormat(Qt::RichText); + errDlg.setWindowTitle(tr("Error")); + errDlg.setIcon(QMessageBox::Critical); + QString message = tr("Some errors happend, while saving:"); + if (emptyTitle) { + message.append("
- Title cannot be empty (all spaces - empty too)"); + } + if (emptyPassword) { + message.append("
- Password cannot be empty (all spaces - empty too)"); + } + errDlg.setText(message); + errDlg.exec(); + return; + } + + this->_user_data->login = ui->et_login->text().trimmed(); + this->_user_data->password = ui->et_password->text().trimmed(); + this->_user_data->role = role; + + QDialog::accept(); +} diff --git a/sea_transport/usereditdialog.h b/sea_transport/usereditdialog.h index 26a93e7..ada392e 100644 --- a/sea_transport/usereditdialog.h +++ b/sea_transport/usereditdialog.h @@ -2,21 +2,37 @@ #define USEREDITDIALOG_H #include +#include + +#include "entities/user_entity.h" + namespace Ui { class UserEditDialog; } -class UserEditDialog : public QDialog -{ +struct user_data_struct { + QString login; + QString password; + UserRole role; + bool edit; +}; + +class UserEditDialog : public QDialog { Q_OBJECT + Ui::UserEditDialog *ui; + + user_data_struct *_user_data; public: explicit UserEditDialog(QWidget *parent = nullptr); ~UserEditDialog(); -private: - Ui::UserEditDialog *ui; + user_data_struct* user_data() const; + void set_user(user_entity* user, bool edit); + +public slots: + void accept() Q_DECL_OVERRIDE; }; #endif // USEREDITDIALOG_H diff --git a/sea_transport/usereditdialog.ui b/sea_transport/usereditdialog.ui index 674bbb2..e924ca5 100644 --- a/sea_transport/usereditdialog.ui +++ b/sea_transport/usereditdialog.ui @@ -10,6 +10,18 @@ 270
+ + + 304 + 270 + + + + + 304 + 270 + + Dialog @@ -68,7 +80,7 @@
- Captain + Skipper
diff --git a/sea_transport/vesseleditdialog.cpp b/sea_transport/vesseleditdialog.cpp index dcc2e3b..a49384a 100644 --- a/sea_transport/vesseleditdialog.cpp +++ b/sea_transport/vesseleditdialog.cpp @@ -1,14 +1,269 @@ #include "vesseleditdialog.h" #include "ui_vesseleditdialog.h" -VesselEditDialog::VesselEditDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::VesselEditDialog) -{ +VesselEditDialog::VesselEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::VesselEditDialog) { ui->setupUi(this); + + this->cvm = new CargoViewModel(this); + ui->tv_cargo->setModel(this->cvm); + + connect(ui->tv_cargo->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_cargo_remove->setEnabled(selected.length() > 0); + }); + + connect(ui->pb_cargo_remove, &QPushButton::clicked, [this]() { + auto sel = ui->tv_cargo->selectionModel()->selectedRows(); + if (sel.length() == 0) { + return; + } + + foreach (auto mIdx, sel) { + auto oid = mIdx.data().toULongLong(); + bool success; + this->_vessel->withdraw_cargo(oid, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot remove some of this cargo!"); + } + } + }); + + foreach (auto port, apparatus::instance()->get_object_subsystem()->dpoints()) { + ui->cb_port->addItem(tr("%1 :%2").arg(port.title()).arg(port.id())); + } + + foreach (auto user, apparatus::instance()->get_auth_subsystem()->users()) { + if (user.role() != UserRole::SKIPPER) { + continue; + } + ui->cb_skippers->addItem(tr("%1 :%2").arg(user.login()).arg(user.id())); + } + + connect(ui->pb_cargo_add, &QPushButton::clicked, this, &VesselEditDialog::on_cargo_add); + + connect(ui->pb_withdraw_from_harbor, &QPushButton::clicked, this, &VesselEditDialog::on_withdraw_from_harbor); + connect(ui->pb_withdraw_from_vessel, &QPushButton::clicked, this, &VesselEditDialog::on_withdraw_from_vessel); + + connect(ui->pb_save, &QPushButton::clicked, this, &VesselEditDialog::accept); + connect(ui->pb_discard, &QPushButton::clicked, this, &VesselEditDialog::reject); } -VesselEditDialog::~VesselEditDialog() -{ +VesselEditDialog::~VesselEditDialog() { delete ui; } + +void VesselEditDialog::select_proper_skipper() { + int i = 0; + foreach (auto user, apparatus::instance()->get_auth_subsystem()->users()) { + if (user.role() != UserRole::SKIPPER || user.login() != this->_vessel->skipper()) { + i += 1; + continue; + } + ui->cb_port->setCurrentIndex(i); + return; + } + + QMessageBox::critical(this, "Error", "Cannot find this vessel's skipper."); +} + +void VesselEditDialog::select_proper_port() { + int i = 0; + foreach (auto port, apparatus::instance()->get_object_subsystem()->dpoints()) { + if (port.id() != this->_vessel->harbor()) { + i += 1; + continue; + } + ui->cb_port->setCurrentIndex(i); + return; + } + + QMessageBox::critical(this, "Error", "Cannot find this vessel's harbor."); +} + +vessel_entity* VesselEditDialog::vessel() { + return this->_vessel; +} + +void VesselEditDialog::set_vessel(vessel_entity *ves, bool edit) { + this->_vessel = new vessel_entity(*ves); + + if (edit) { + this->select_proper_skipper(); + this->select_proper_port(); + ui->sb_capacity->setValue(this->_vessel->capacity()); + this->cvm->set_data(this->_vessel->cargo()); + ui->pb_withdraw_from_harbor->setEnabled(true); + ui->pb_withdraw_from_vessel->setEnabled(true); + } + ui->lab_capacity_current->setText(QString::number(this->_vessel->capacity())); +} + +void VesselEditDialog::on_cargo_add() { + CargoEditDialog ced(this); + ced.setWindowTitle("New cargo"); + if (ced.exec() != CargoEditDialog::Accepted) { + return; + } + + bool success; + this->_vessel->add_cargo(*ced.cargo(), success); + if (success) { + this->cvm->set_data(this->_vessel->cargo()); + QMessageBox::information(this, "Success", "Cargo successfully put into storage"); + } + else { + QMessageBox::critical(this, "Error", "Not enough space to put cargo"); + } +} + +void VesselEditDialog::on_withdraw_from_harbor() { + QMessageBox::information(this, "Note", "Please note, old storage will be used.\n" + "Also, movement cannot be undone by discarding vessel edit dialog"); + + bool success; + auto dpoint = apparatus::instance()->get_object_subsystem()->get_dpoint(this->_vessel->harbor(), success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot find associated harbor in DB"); + return; + } + if (dpoint->storages().isEmpty()) { + QMessageBox::information(this, "Note", "Vessel has no storages"); + return; + } + + QStringList harbor_storage; + foreach (auto storage, dpoint->storages()) { + harbor_storage << QString::number(storage.id()); + } + bool ok; + QString storage_id_str = QInputDialog::getItem(this, "Select storage", "Storages in harbor:", harbor_storage, 0, false, &ok); + if (!ok || storage_id_str.isEmpty()) { + QMessageBox::information(this, "Aborted", "Operation aborted by user."); + return; + } + + entity_id sid = storage_id_str.toULongLong(); + auto storage = dpoint->get_storage(sid, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot find associated storage in harbor"); + return; + } + if (storage->cargo().isEmpty()) { + QMessageBox::information(this, "Note", "Storage has no cargo"); + return; + } + + QStringList storage_cargo; + foreach (auto storage, storage->cargo()) { + storage_cargo << tr("%1 :%2").arg(storage.title()).arg(storage.id()); + } + QString cargo_id_str = QInputDialog::getItem(this, "Select cargo", "Cargo in storage:", storage_cargo, 0, false, &ok); + if (!ok || cargo_id_str.isEmpty()) { + QMessageBox::information(this, "Aborted", "Operation aborted by user."); + return; + } + + entity_id cid = cargo_id_str.split(":")[1].toULongLong(); + auto cargo = storage->get_cargo(cid, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot find cargo in storage"); + return; + } + + this->_vessel->add_cargo(cargo, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot add cargo to vessel"); + return; + } + storage->withdraw_cargo(cid, success); + if (!success) { + throw std::runtime_error("Cannot withdraw from storage"); + } + + QMessageBox::information(this, "Info", "Successfully withdrawed cargo from harbor"); + cvm->set_data(this->_vessel->cargo()); +} + +void VesselEditDialog::on_withdraw_from_vessel() { + if (this->_vessel->cargo().isEmpty()) { + QMessageBox::information(this, "Note", "Vessel has no cargo"); + return; + } + + QMessageBox::information(this, "Note", "Please note, old storage will be used.\n" + "Also, movement cannot be undone by discarding vessel edit dialog"); + + bool success; + auto dpoint = apparatus::instance()->get_object_subsystem()->get_dpoint(this->_vessel->harbor(), success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot find associated harbor in DB"); + return; + } + + QStringList vessel_cargo; + foreach (auto storage, this->_vessel->cargo()) { + vessel_cargo << tr("%1 :%2").arg(storage.title()).arg(storage.id()); + } + bool ok; + QString cargo_id_str = QInputDialog::getItem(this, "Select cargo", "Cargo in storage:", vessel_cargo, 0, false, &ok); + if (!ok || cargo_id_str.isEmpty()) { + QMessageBox::information(this, "Aborted", "Operation aborted by user."); + return; + } + + entity_id cid = cargo_id_str.split(":")[1].toULongLong(); + auto cargo = this->_vessel->get_cargo(cid, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot find cargo in vessel"); + return; + } + + QStringList harbor_storage; + foreach (auto storage, dpoint->storages()) { + harbor_storage << QString::number(storage.id()); + } + QString storage_id_str = QInputDialog::getItem(this, "Select storage", "Storages in harbor:", harbor_storage, 0, false, &ok); + if (!ok || storage_id_str.isEmpty()) { + QMessageBox::information(this, "Aborted", "Operation aborted by user."); + return; + } + entity_id sid = storage_id_str.toULongLong(); + auto storage = dpoint->get_storage(sid, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot find associated storage in harbor"); + return; + } + + storage->add_cargo(cargo, success); + if (!success) { + QMessageBox::critical(this, "Error", "Cannot add cargo to storage"); + return; + } + this->_vessel->withdraw_cargo(cid, success); + if (!success) { + throw std::runtime_error("Cannot withdraw from vessel"); + } + + QMessageBox::information(this, "Info", "Successfully withdrawed cargo from vessel"); + cvm->set_data(this->_vessel->cargo()); +} + +void VesselEditDialog::accept() { + int cvs = 0; + foreach (auto c, this->_vessel->cargo()) { + cvs += c.volume(); + } + if (cvs > ui->sb_capacity->value()) { + QMessageBox::critical(this, "Error", "Cargo volume bigger than capacity"); + return; + } + + auto slog = ui->cb_skippers->currentText().split(":")[0].trimmed(); + auto hid = ui->cb_port->currentText().split(":")[1].trimmed().toULongLong(); + auto cap = ui->sb_capacity->value(); + + this->_vessel->set_skipper(slog); + this->_vessel->set_harbor(hid); + this->_vessel->set_capacity(cap); + + QDialog::accept(); +} diff --git a/sea_transport/vesseleditdialog.h b/sea_transport/vesseleditdialog.h index 8aa08af..f616972 100644 --- a/sea_transport/vesseleditdialog.h +++ b/sea_transport/vesseleditdialog.h @@ -2,21 +2,40 @@ #define VESSELEDITDIALOG_H #include +#include +#include + +#include "entities/vessel_entity.h" +#include "system/apparatus.h" +#include "viewmodels/cargoviewmodel.h" +#include "cargoeditdialog.h" namespace Ui { class VesselEditDialog; } -class VesselEditDialog : public QDialog -{ +class VesselEditDialog : public QDialog { Q_OBJECT + Ui::VesselEditDialog *ui; + + CargoViewModel *cvm; + vessel_entity *_vessel; + + void select_proper_skipper(); + void select_proper_port(); public: explicit VesselEditDialog(QWidget *parent = nullptr); ~VesselEditDialog(); -private: - Ui::VesselEditDialog *ui; + vessel_entity* vessel(); + void set_vessel(vessel_entity *ves, bool edit); + +public slots: + void on_cargo_add(); + void on_withdraw_from_harbor(); + void on_withdraw_from_vessel(); + void accept() Q_DECL_OVERRIDE; }; #endif // VESSELEDITDIALOG_H diff --git a/sea_transport/vesseleditdialog.ui b/sea_transport/vesseleditdialog.ui index f802ae8..f70bdc3 100644 --- a/sea_transport/vesseleditdialog.ui +++ b/sea_transport/vesseleditdialog.ui @@ -10,100 +10,146 @@ 425 + + + 361 + 425 + + + + + 361 + 425 + + Dialog - + - - QFormLayout::ExpandingFieldsGrow - - - 24 - - - 8 - - - 4 - - - 4 - - - 4 - - - 4 - - Vessel num.: + Skipper: - - - - Home port: + Harbor: - - - - 0 - 0 - - - - Choose... - - + - Max. capacity: + Capacity (new): + + + - + + + 1 + + + 50000 + + + + + + + Capacity (current): + + + + + + + 50000 + + - - - + + + QAbstractItemView::SelectRows + + + + + + + + + Remove cargo + + - - + + Add cargo + + + + false + + + Get from harbor + + + + + + + false + + + Move to harbor + + + + + + + Qt::Vertical + + + + 20 + 64 + + + + - + Discard - + Save diff --git a/sea_transport/viewmodels/cargoviewmodel.cpp b/sea_transport/viewmodels/cargoviewmodel.cpp new file mode 100644 index 0000000..ef6621a --- /dev/null +++ b/sea_transport/viewmodels/cargoviewmodel.cpp @@ -0,0 +1,54 @@ +#include "cargoviewmodel.h" + +CargoViewModel::CargoViewModel(QObject *parent) : QAbstractTableModel(parent) { + +} + +int CargoViewModel::rowCount(const QModelIndex &/*parent*/) const { + return this->_data.length(); +} + +int CargoViewModel::columnCount(const QModelIndex &/*parent*/) const { + return 3; +} + +QVariant CargoViewModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + switch (section) { + case 0: + return QString("SID"); + case 1: + return QString("Title"); + case 2: + return QString("Volume"); + } + } + return QVariant(); +} + +QVariant CargoViewModel::data(const QModelIndex &index, int role) const { + if (role == Qt::DisplayRole) { + auto item = this->_data[index.row()]; + + int col = index.column(); + switch (col) { + case 0: + return QString::number(item.id()); + case 1: + return item.title(); + case 2: + return item.volume(); + } + + return "UNKNOWN FIELD"; + } + + return QVariant(); +} + +void CargoViewModel::set_data(const QVector &new_data) { + this->beginResetModel(); + this->_data.clear(); + this->_data += new_data; + this->endResetModel(); +} diff --git a/sea_transport/viewmodels/cargoviewmodel.h b/sea_transport/viewmodels/cargoviewmodel.h new file mode 100644 index 0000000..26db7b1 --- /dev/null +++ b/sea_transport/viewmodels/cargoviewmodel.h @@ -0,0 +1,24 @@ +#ifndef CARGOVIEWMODEL_H +#define CARGOVIEWMODEL_H + +#include +#include + +#include "entities/cargo_entity.h" + +class CargoViewModel : public QAbstractTableModel { + Q_OBJECT + + QVector _data; + +public: + CargoViewModel(QObject *parent); + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + + void set_data(const QVector &new_data); +}; + +#endif // CARGOVIEWMODEL_H diff --git a/sea_transport/viewmodels/deliverypointsviewmodel.cpp b/sea_transport/viewmodels/deliverypointsviewmodel.cpp new file mode 100644 index 0000000..487e768 --- /dev/null +++ b/sea_transport/viewmodels/deliverypointsviewmodel.cpp @@ -0,0 +1,60 @@ +#include "deliverypointsviewmodel.h" + +DeliveryPointsViewModel::DeliveryPointsViewModel(QObject *parent) : QAbstractTableModel(parent) { + +} + +int DeliveryPointsViewModel::rowCount(const QModelIndex &/*parent*/) const { + return apparatus::instance()->get_object_subsystem()->dpoints().length(); +} + +int DeliveryPointsViewModel::columnCount(const QModelIndex &/*parent*/) const { + return 4; +} + +QVariant DeliveryPointsViewModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + switch (section) { + case 0: + return QString("DPID"); + case 1: + return QString("Title"); + case 2: + return QString("Storages count"); + case 3: + return QString("Storages total volume"); + } + } + return QVariant(); +} + +QVariant DeliveryPointsViewModel::data(const QModelIndex &index, int role) const { + if (role == Qt::DisplayRole) { + auto item = apparatus::instance()->get_object_subsystem()->dpoints()[index.row()]; + + int col = index.column(); + switch (col) { + case 0: + return QString::number(item.id()); + case 1: + return item.title(); + case 2: + return item.storages().length(); + case 3: + int tvol = 0; + foreach (auto storage, item.storages()) { + tvol += storage.capacity(); + } + return tvol; + } + + return "UNKNOWN FIELD"; + } + + return QVariant(); +} + +void DeliveryPointsViewModel::update() { + this->beginResetModel(); + this->endResetModel(); +} diff --git a/sea_transport/viewmodels/deliverypointsviewmodel.h b/sea_transport/viewmodels/deliverypointsviewmodel.h new file mode 100644 index 0000000..bfbfb40 --- /dev/null +++ b/sea_transport/viewmodels/deliverypointsviewmodel.h @@ -0,0 +1,22 @@ +#ifndef DELIVERYPOINTSVIEWMODEL_H +#define DELIVERYPOINTSVIEWMODEL_H + +#include "system/apparatus.h" + +#include + +class DeliveryPointsViewModel : public QAbstractTableModel { + Q_OBJECT + +public: + DeliveryPointsViewModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + +public slots: + void update(); +}; + +#endif // DELIVERYPOINTSVIEWMODEL_H diff --git a/sea_transport/viewmodels/usersviewmodel.cpp b/sea_transport/viewmodels/usersviewmodel.cpp index 93785f2..9269d34 100644 --- a/sea_transport/viewmodels/usersviewmodel.cpp +++ b/sea_transport/viewmodels/usersviewmodel.cpp @@ -4,11 +4,11 @@ UsersViewModel::UsersViewModel(QObject *parent) : QAbstractTableModel(parent) { } -int UsersViewModel::rowCount(const QModelIndex & /*parent*/) const { - return apparatus::instance()->get_object_subsystem()->vessels().size(); +int UsersViewModel::rowCount(const QModelIndex &/*parent*/) const { + return apparatus::instance()->get_auth_subsystem()->users().length(); } -int UsersViewModel::columnCount(const QModelIndex & /*parent*/) const { +int UsersViewModel::columnCount(const QModelIndex &/*parent*/) const { return 3; } @@ -29,29 +29,29 @@ QVariant UsersViewModel::headerData(int section, Qt::Orientation orientation, in QVariant UsersViewModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { auto item = apparatus::instance()->get_auth_subsystem()->users()[index.row()]; - int col = index.column(); + int col = index.column(); switch (col) { case 0: return QString::number(item.id()); case 1: return item.login(); case 2: - QString role = "unknown"; + QString _role = "unknown"; switch(item.role()) { case UserRole::ADMINISTRATOR: - role = "Administrator"; + _role = "Administrator"; break; case UserRole::DISPATCHER: - role = "Dispatcher"; + _role = "Dispatcher"; break; case UserRole::SKIPPER: - role = "Skipper"; + _role = "Skipper"; break; } - return role; + return _role; } return "UNKNOWN FIELD"; @@ -61,5 +61,6 @@ QVariant UsersViewModel::data(const QModelIndex &index, int role) const { } void UsersViewModel::update() { - this->resetInternalData(); + this->beginResetModel(); + this->endResetModel(); } diff --git a/sea_transport/viewmodels/usersviewmodel.h b/sea_transport/viewmodels/usersviewmodel.h index 411981b..40e3813 100644 --- a/sea_transport/viewmodels/usersviewmodel.h +++ b/sea_transport/viewmodels/usersviewmodel.h @@ -5,15 +5,15 @@ #include -class UsersViewModel : public QAbstractTableModel -{ +class UsersViewModel : public QAbstractTableModel { Q_OBJECT + public: UsersViewModel(QObject *parent = nullptr); - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; public slots: void update(); diff --git a/sea_transport/viewmodels/vesselsviewmodel.cpp b/sea_transport/viewmodels/vesselsviewmodel.cpp new file mode 100644 index 0000000..bbfeef5 --- /dev/null +++ b/sea_transport/viewmodels/vesselsviewmodel.cpp @@ -0,0 +1,70 @@ +#include "vesselsviewmodel.h" + +VesselsViewModel::VesselsViewModel(QObject *parent) : QAbstractTableModel(parent) { + +} + +int VesselsViewModel::rowCount(const QModelIndex &/*parent*/) const { + return apparatus::instance()->get_object_subsystem()->vessels().length(); +} + +int VesselsViewModel::columnCount(const QModelIndex &/*parent*/) const { + return 6; +} + +QVariant VesselsViewModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + switch (section) { + case 0: + return QString("VID"); + case 1: + return QString("Skipper"); + case 2: + return QString("Harbor"); + case 3: + return QString("Capacity"); + case 4: + return QString("Cargo count"); + case 5: + return QString("Cargo volume"); + } + } + return QVariant(); +} + +QVariant VesselsViewModel::data(const QModelIndex &index, int role) const { + if (role == Qt::DisplayRole) { + auto item = apparatus::instance()->get_object_subsystem()->vessels()[index.row()]; + bool hs = false; + auto harbor = apparatus::instance()->get_object_subsystem()->get_dpoint(item.harbor(), hs); + + int col = index.column(); + switch (col) { + case 0: + return QString::number(item.id()); + case 1: + return item.skipper(); + case 2: + return (hs? harbor->title() : tr("##ERROR[%1]##").arg(item.harbor())); + case 3: + return item.capacity(); + case 4: + return item.cargo().length(); + case 5: + int cvol = 0; + foreach (auto cargo, item.cargo()) { + cvol += cargo.volume(); + } + return cvol; + } + + return "UNKNOWN FIELD"; + } + + return QVariant(); +} + +void VesselsViewModel::update() { + this->beginResetModel(); + this->endResetModel(); +} diff --git a/sea_transport/viewmodels/vesselsviewmodel.h b/sea_transport/viewmodels/vesselsviewmodel.h new file mode 100644 index 0000000..b861fb9 --- /dev/null +++ b/sea_transport/viewmodels/vesselsviewmodel.h @@ -0,0 +1,22 @@ +#ifndef VESSELSVIEWMODEL_H +#define VESSELSVIEWMODEL_H + +#include "system/apparatus.h" + +#include + +class VesselsViewModel : public QAbstractTableModel { + Q_OBJECT + +public: + VesselsViewModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + +public slots: + void update(); +}; + +#endif // VESSELSVIEWMODEL_H diff --git a/sea_transport_project.pro.user b/sea_transport_project.pro.user index be69ce2..c77d533 100644 --- a/sea_transport_project.pro.user +++ b/sea_transport_project.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -169,7 +169,7 @@ true QtProjectManager.QMakeBuildStep - false + true @@ -221,7 +221,7 @@ true QtProjectManager.QMakeBuildStep - false + true diff --git a/st_test/tst_st_test.cpp b/st_test/tst_st_test.cpp index 0e015bb..fca6bde 100644 --- a/st_test/tst_st_test.cpp +++ b/st_test/tst_st_test.cpp @@ -133,7 +133,7 @@ void st_test::dpoint_entity_serialization_test() { f.open(QIODevice::WriteOnly); stream.setDevice(&f); - ent1 = dpoint_entity("some_test_point"); + ent1 = dpoint_entity(0, "some_test_point"); ent1.serialize(stream); stream.setDevice(nullptr); @@ -164,8 +164,8 @@ void st_test::vessel_entity_serialization_test() { f.open(QIODevice::WriteOnly); stream.setDevice(&f); - dpoint_entity test_harbor("test_harbor_for_vessel"); - ent1 = vessel_entity(test_harbor, 256); + dpoint_entity test_harbor(0, "test_harbor_for_vessel"); + ent1 = vessel_entity(0, test_harbor.id(), 256); ent1.serialize(stream); stream.setDevice(nullptr); @@ -181,7 +181,7 @@ void st_test::vessel_entity_serialization_test() { } QVERIFY2( - ent1.id() == ent2.id() && ent1.harbor().id() == ent2.harbor().id() && ent1.capacity() == ent2.capacity(), + ent1.id() == ent2.id() && ent1.harbor() == ent2.harbor() && ent1.capacity() == ent2.capacity(), "Delivery Point entity not serialized properly" ); } @@ -265,7 +265,7 @@ void st_test::apparatus_check_auth_subsystem() { void st_test::apparatus_check_object_subsystem() { apparatus::init(); auto os = apparatus::instance()->get_object_subsystem(); - dpoint_entity p("test"); + dpoint_entity p(0, "test"); { bool test = os->add_dpoint(p); QVERIFY(test);