diff --git a/sea_transport/adminpanel.cpp b/sea_transport/adminpanel.cpp
index 4ea9230..6095b12 100644
--- a/sea_transport/adminpanel.cpp
+++ b/sea_transport/adminpanel.cpp
@@ -78,10 +78,51 @@ void AdminPanel::on_logout_requested() {
this->close();
}
-void AdminPanel::on_vessel_add_edit(bool /*edit*/) {
+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() {
diff --git a/sea_transport/adminpanel.ui b/sea_transport/adminpanel.ui
index ac236f6..e472f2f 100644
--- a/sea_transport/adminpanel.ui
+++ b/sea_transport/adminpanel.ui
@@ -53,7 +53,7 @@
QTabWidget::Rounded
- 0
+ 2
false
@@ -68,6 +68,9 @@
-
+
+ QAbstractItemView::SingleSelection
+
QAbstractItemView::SelectRows
@@ -173,6 +176,9 @@
-
+
+ QAbstractItemView::SingleSelection
+
QAbstractItemView::SelectRows
@@ -234,6 +240,9 @@
-
+
+ QAbstractItemView::SingleSelection
+
QAbstractItemView::SelectRows
diff --git a/sea_transport/cargoeditdialog.ui b/sea_transport/cargoeditdialog.ui
index 4b2f8bf..fd1b1f7 100644
--- a/sea_transport/cargoeditdialog.ui
+++ b/sea_transport/cargoeditdialog.ui
@@ -10,6 +10,18 @@
127
+
+
+ 318
+ 127
+
+
+
+
+ 318
+ 127
+
+
Dialog
diff --git a/sea_transport/deliverypointeditdialog.ui b/sea_transport/deliverypointeditdialog.ui
index c330679..0d7c4ae 100644
--- a/sea_transport/deliverypointeditdialog.ui
+++ b/sea_transport/deliverypointeditdialog.ui
@@ -10,6 +10,18 @@
386
+
+
+ 394
+ 386
+
+
+
+
+ 394
+ 386
+
+
Dialog
diff --git a/sea_transport/entities/cargo_entity.cpp b/sea_transport/entities/cargo_entity.cpp
index 9835444..a5d136c 100644
--- a/sea_transport/entities/cargo_entity.cpp
+++ b/sea_transport/entities/cargo_entity.cpp
@@ -1,5 +1,10 @@
#include "cargo_entity.h"
+
+cargo_entity::cargo_entity() {
+ this->_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);
diff --git a/sea_transport/entities/cargo_entity.h b/sea_transport/entities/cargo_entity.h
index 9b69169..60d8c69 100644
--- a/sea_transport/entities/cargo_entity.h
+++ b/sea_transport/entities/cargo_entity.h
@@ -10,12 +10,12 @@
class cargo_entity : public IEntity {
private:
- entity_id _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;
diff --git a/sea_transport/entities/dpoint_entity.cpp b/sea_transport/entities/dpoint_entity.cpp
index 04d2f32..55a682d 100644
--- a/sea_transport/entities/dpoint_entity.cpp
+++ b/sea_transport/entities/dpoint_entity.cpp
@@ -1,5 +1,10 @@
#include "dpoint_entity.h"
+
+dpoint_entity::dpoint_entity() {
+ this->_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);
@@ -29,15 +34,24 @@ 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) {
-// std::remove_if(this->_storages.begin(), this->_storages.end(), [sid](storage_entity ent) {
-// return ent.id() == sid;
-// });
-
QVector st(this->_storages);
for (int i = 0; i < st.length(); i++) {
diff --git a/sea_transport/entities/dpoint_entity.h b/sea_transport/entities/dpoint_entity.h
index 202e002..0a536ba 100644
--- a/sea_transport/entities/dpoint_entity.h
+++ b/sea_transport/entities/dpoint_entity.h
@@ -12,13 +12,13 @@
class dpoint_entity : public IEntity {
private:
- entity_id _id;
+ entity_id _id = 0;
entity_id _dispatcher_id;
QString _title;
QVector _storages;
public:
- dpoint_entity() = default;
+ dpoint_entity();
dpoint_entity(entity_id dispatcher_id, const QString &title);
entity_id id() const;
@@ -26,6 +26,7 @@ public:
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);
diff --git a/sea_transport/entities/storage_entity.cpp b/sea_transport/entities/storage_entity.cpp
index 96909e6..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) {
+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;
@@ -25,7 +28,7 @@ const QVector storage_entity::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 4d502f2..c09e2b5 100644
--- a/sea_transport/entities/storage_entity.h
+++ b/sea_transport/entities/storage_entity.h
@@ -12,12 +12,12 @@ class storage_entity : public IEntity {
private:
static entity_id __global_id;
- entity_id _id;
+ 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;
diff --git a/sea_transport/entities/user_entity.cpp b/sea_transport/entities/user_entity.cpp
index a213906..9c628e6 100644
--- a/sea_transport/entities/user_entity.cpp
+++ b/sea_transport/entities/user_entity.cpp
@@ -1,10 +1,18 @@
#include "user_entity.h"
+
+user_entity::user_entity() {
+ this->_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();
}
diff --git a/sea_transport/entities/user_entity.h b/sea_transport/entities/user_entity.h
index 7f875a7..6ab2de9 100644
--- a/sea_transport/entities/user_entity.h
+++ b/sea_transport/entities/user_entity.h
@@ -15,13 +15,13 @@ enum class UserRole {
class user_entity : public IEntity {
private:
- entity_id _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;
diff --git a/sea_transport/entities/vessel_entity.cpp b/sea_transport/entities/vessel_entity.cpp
index 4739ee4..383d116 100644
--- a/sea_transport/entities/vessel_entity.cpp
+++ b/sea_transport/entities/vessel_entity.cpp
@@ -3,38 +3,69 @@
entity_id vessel_entity::__global_id = 0;
-vessel_entity::vessel_entity(entity_id skipper_id, entity_id harbor_id, unsigned int capacity) : _skipper_id(skipper_id), _harbor_id(harbor_id), _capacity(capacity) {
+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;
}
-entity_id vessel_entity::skipper() const {
- return this->_skipper_id;
+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;
+ 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();
@@ -49,7 +80,7 @@ void vessel_entity::withdraw_cargo(entity_id oid, bool &success) {
}
void vessel_entity::serialize(QDataStream &output) {
- output << this->_id << this->_harbor_id;
+ output << this->_id << this->_skipper << this->_harbor_id;
output << this->_capacity << this->_cargo.size();
for (auto item : this->_cargo) {
item.serialize(output);
@@ -57,7 +88,7 @@ void vessel_entity::serialize(QDataStream &output) {
}
void vessel_entity::deserialize(QDataStream &input) {
- input >> this->_id >> this->_harbor_id;
+ 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 48c2bbc..a554a2b 100644
--- a/sea_transport/entities/vessel_entity.h
+++ b/sea_transport/entities/vessel_entity.h
@@ -12,23 +12,27 @@ class vessel_entity : public IEntity {
private:
static entity_id __global_id;
- entity_id _id;
- entity_id _skipper_id;
+ entity_id _id = 0;
+ QString _skipper;
entity_id _harbor_id;
- unsigned int _capacity;
+ unsigned int _capacity = 50000;
QVector _cargo;
public:
- vessel_entity() = default;
- vessel_entity(entity_id skipper_id, entity_id harbor_id, unsigned int capacity);
+ vessel_entity();
+ vessel_entity(QString skipper, entity_id harbor_id, unsigned int capacity);
entity_id id() const;
- entity_id skipper() 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);
diff --git a/sea_transport/storageeditdialog.cpp b/sea_transport/storageeditdialog.cpp
index b8eaefb..e7426c0 100644
--- a/sea_transport/storageeditdialog.cpp
+++ b/sea_transport/storageeditdialog.cpp
@@ -18,8 +18,12 @@ StorageEditDialog::StorageEditDialog(QWidget *parent) : QDialog(parent), ui(new
}
foreach (auto mIdx, sel) {
- auto cdata = mIdx.data().toInt();
- qDebug() << cdata << '\n';
+ 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!");
+ }
}
});
diff --git a/sea_transport/storageeditdialog.ui b/sea_transport/storageeditdialog.ui
index 89c6b29..08e60c2 100644
--- a/sea_transport/storageeditdialog.ui
+++ b/sea_transport/storageeditdialog.ui
@@ -10,6 +10,18 @@
336
+
+
+ 400
+ 336
+
+
+
+
+ 400
+ 336
+
+
Dialog
diff --git a/sea_transport/usereditdialog.ui b/sea_transport/usereditdialog.ui
index 8ec17a4..e924ca5 100644
--- a/sea_transport/usereditdialog.ui
+++ b/sea_transport/usereditdialog.ui
@@ -10,6 +10,18 @@
270
+
+
+ 304
+ 270
+
+
+
+
+ 304
+ 270
+
+
Dialog
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/vesselsviewmodel.cpp b/sea_transport/viewmodels/vesselsviewmodel.cpp
index 7d4f2ba..bbfeef5 100644
--- a/sea_transport/viewmodels/vesselsviewmodel.cpp
+++ b/sea_transport/viewmodels/vesselsviewmodel.cpp
@@ -9,7 +9,7 @@ int VesselsViewModel::rowCount(const QModelIndex &/*parent*/) const {
}
int VesselsViewModel::columnCount(const QModelIndex &/*parent*/) const {
- return 5;
+ return 6;
}
QVariant VesselsViewModel::headerData(int section, Qt::Orientation orientation, int role) const {
@@ -18,12 +18,14 @@ QVariant VesselsViewModel::headerData(int section, Qt::Orientation orientation,
case 0:
return QString("VID");
case 1:
- return QString("Harbor");
+ return QString("Skipper");
case 2:
- return QString("Capacity");
+ return QString("Harbor");
case 3:
- return QString("Cargo count");
+ return QString("Capacity");
case 4:
+ return QString("Cargo count");
+ case 5:
return QString("Cargo volume");
}
}
@@ -33,20 +35,22 @@ QVariant VesselsViewModel::headerData(int section, Qt::Orientation orientation,
QVariant VesselsViewModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole) {
auto item = apparatus::instance()->get_object_subsystem()->vessels()[index.row()];
- bool s = false;
- auto harbor = apparatus::instance()->get_object_subsystem()->get_dpoint(item.harbor(), s);
+ 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 (s? harbor->title() : "##ERROR##");
+ return item.skipper();
case 2:
- return item.capacity();
+ return (hs? harbor->title() : tr("##ERROR[%1]##").arg(item.harbor()));
case 3:
- return item.cargo().length();
+ return item.capacity();
case 4:
+ return item.cargo().length();
+ case 5:
int cvol = 0;
foreach (auto cargo, item.cargo()) {
cvol += cargo.volume();