Vessel edit dialog

This commit is contained in:
Andrew nuark G 2020-12-23 18:43:41 +07:00
parent 9686b96d8a
commit eb097588ba
21 changed files with 589 additions and 97 deletions

View file

@ -78,10 +78,51 @@ void AdminPanel::on_logout_requested() {
this->close(); 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(); vvm->update();
dpvm->update();
} }
void AdminPanel::on_vessel_remove() { void AdminPanel::on_vessel_remove() {

View file

@ -53,7 +53,7 @@
<enum>QTabWidget::Rounded</enum> <enum>QTabWidget::Rounded</enum>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>2</number>
</property> </property>
<property name="documentMode"> <property name="documentMode">
<bool>false</bool> <bool>false</bool>
@ -68,6 +68,9 @@
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2"> <item row="0" column="0" colspan="2">
<widget class="QTableView" name="tv_users"> <widget class="QTableView" name="tv_users">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior"> <property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum> <enum>QAbstractItemView::SelectRows</enum>
</property> </property>
@ -173,6 +176,9 @@
</item> </item>
<item row="0" column="1" colspan="2"> <item row="0" column="1" colspan="2">
<widget class="QTableView" name="tv_vessels"> <widget class="QTableView" name="tv_vessels">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior"> <property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum> <enum>QAbstractItemView::SelectRows</enum>
</property> </property>
@ -234,6 +240,9 @@
</item> </item>
<item row="0" column="1" colspan="2"> <item row="0" column="1" colspan="2">
<widget class="QTableView" name="tv_dp"> <widget class="QTableView" name="tv_dp">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior"> <property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum> <enum>QAbstractItemView::SelectRows</enum>
</property> </property>

View file

@ -10,6 +10,18 @@
<height>127</height> <height>127</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>318</width>
<height>127</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>318</width>
<height>127</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>

View file

@ -10,6 +10,18 @@
<height>386</height> <height>386</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>394</width>
<height>386</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>394</width>
<height>386</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>

View file

@ -1,5 +1,10 @@
#include "cargo_entity.h" #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) { cargo_entity::cargo_entity(const QString &title, unsigned int volume) : _title(title), _volume(volume) {
this->_id = volume; this->_id = volume;
auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5); auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5);

View file

@ -10,12 +10,12 @@
class cargo_entity : public IEntity { class cargo_entity : public IEntity {
private: private:
entity_id _id; entity_id _id = 0;
QString _title; QString _title;
unsigned int _volume; unsigned int _volume = 50000;
public: public:
cargo_entity() = default; cargo_entity();
cargo_entity(const QString &title, unsigned int volume); cargo_entity(const QString &title, unsigned int volume);
entity_id id() const; entity_id id() const;

View file

@ -1,5 +1,10 @@
#include "dpoint_entity.h" #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) { dpoint_entity::dpoint_entity(entity_id dispatcher_id, const QString &title) : _dispatcher_id(dispatcher_id), _title(title) {
this->_id = dispatcher_id; this->_id = dispatcher_id;
auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5); auto hash = QCryptographicHash::hash(title.toLocal8Bit(), QCryptographicHash::Md5);
@ -29,15 +34,24 @@ const QVector<storage_entity> dpoint_entity::storages() {
return this->_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<storage_entity> storages) { void dpoint_entity::set_storages(QVector<storage_entity> storages) {
this->_storages = storages; this->_storages = storages;
} }
void dpoint_entity::remove_storage(entity_id sid) { 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<storage_entity> st(this->_storages); QVector<storage_entity> st(this->_storages);
for (int i = 0; i < st.length(); i++) { for (int i = 0; i < st.length(); i++) {

View file

@ -12,13 +12,13 @@
class dpoint_entity : public IEntity { class dpoint_entity : public IEntity {
private: private:
entity_id _id; entity_id _id = 0;
entity_id _dispatcher_id; entity_id _dispatcher_id;
QString _title; QString _title;
QVector<storage_entity> _storages; QVector<storage_entity> _storages;
public: public:
dpoint_entity() = default; dpoint_entity();
dpoint_entity(entity_id dispatcher_id, const QString &title); dpoint_entity(entity_id dispatcher_id, const QString &title);
entity_id id() const; entity_id id() const;
@ -26,6 +26,7 @@ public:
QString title() const; QString title() const;
void set_title(const QString &new_title); void set_title(const QString &new_title);
const QVector<storage_entity> storages(); const QVector<storage_entity> storages();
storage_entity* get_storage(entity_id sid, bool &success);
void set_storages(QVector<storage_entity> storages); void set_storages(QVector<storage_entity> storages);
void remove_storage(entity_id sid); void remove_storage(entity_id sid);
void add_storage(storage_entity ent); void add_storage(storage_entity ent);

View file

@ -3,10 +3,13 @@
entity_id storage_entity::__global_id = 0; 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(); 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 { entity_id storage_entity::id() const {
return this->_id; return this->_id;
@ -25,7 +28,7 @@ const QVector<cargo_entity> storage_entity::cargo() {
} }
void storage_entity::add_cargo(cargo_entity object, bool &success) { 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) { if (success) {
this->_cargo.push_back(object); this->_cargo.push_back(object);
this->_capacity -= object.volume(); this->_capacity -= object.volume();

View file

@ -12,12 +12,12 @@ class storage_entity : public IEntity {
private: private:
static entity_id __global_id; static entity_id __global_id;
entity_id _id; entity_id _id = 0;
unsigned int _capacity = 500000; unsigned int _capacity = 500000;
QVector<cargo_entity> _cargo; QVector<cargo_entity> _cargo;
public: public:
storage_entity() = default; storage_entity();
storage_entity(unsigned int capacity); storage_entity(unsigned int capacity);
entity_id id() const; entity_id id() const;

View file

@ -1,10 +1,18 @@
#include "user_entity.h" #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) { 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); this->_pwd_hash = QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256);
foreach (auto bit, this->_pwd_hash) { foreach (auto bit, this->_pwd_hash) {
this->_id += bit; this->_id += bit;
} }
foreach (auto bit, QCryptographicHash::hash(login.toLocal8Bit(), QCryptographicHash::Sha3_256)) {
this->_id += bit;
}
this->_id += QRandomGenerator().generate64(); this->_id += QRandomGenerator().generate64();
} }

View file

@ -15,13 +15,13 @@ enum class UserRole {
class user_entity : public IEntity { class user_entity : public IEntity {
private: private:
entity_id _id; entity_id _id = 0;
QString _login; QString _login;
UserRole _role; UserRole _role;
QByteArray _pwd_hash; QByteArray _pwd_hash;
public: public:
user_entity() = default; user_entity();
user_entity(const QString &login, const QString &password, UserRole role); user_entity(const QString &login, const QString &password, UserRole role);
entity_id id() const; entity_id id() const;

View file

@ -3,38 +3,69 @@
entity_id vessel_entity::__global_id = 0; 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(); 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 { entity_id vessel_entity::id() const {
return this->_id; return this->_id;
} }
entity_id vessel_entity::skipper() const { QString vessel_entity::skipper() const {
return this->_skipper_id; return this->_skipper;
}
void vessel_entity::set_skipper(const QString &new_skipper) {
this->_skipper = new_skipper;
} }
entity_id vessel_entity::harbor() const { entity_id vessel_entity::harbor() const {
return this->_harbor_id; return this->_harbor_id;
} }
void vessel_entity::set_harbor(entity_id new_harbor) {
this->_harbor_id = new_harbor;
}
unsigned int vessel_entity::capacity() const { unsigned int vessel_entity::capacity() const {
return this->_capacity; return this->_capacity;
} }
void vessel_entity::set_capacity(unsigned int new_capacity) {
this->_capacity = new_capacity;
}
const QVector<cargo_entity> vessel_entity::cargo() { const QVector<cargo_entity> vessel_entity::cargo() {
return this->_cargo; return this->_cargo;
} }
void vessel_entity::add_cargo(cargo_entity object, bool &success) { 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) { if (success) {
this->_cargo.push_back(object); this->_cargo.push_back(object);
this->_capacity -= object.volume(); 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) { void vessel_entity::withdraw_cargo(entity_id oid, bool &success) {
success = false; success = false;
auto vit = this->_cargo.begin(); 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) { 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(); output << this->_capacity << this->_cargo.size();
for (auto item : this->_cargo) { for (auto item : this->_cargo) {
item.serialize(output); item.serialize(output);
@ -57,7 +88,7 @@ void vessel_entity::serialize(QDataStream &output) {
} }
void vessel_entity::deserialize(QDataStream &input) { void vessel_entity::deserialize(QDataStream &input) {
input >> this->_id >> this->_harbor_id; input >> this->_id >> this->_skipper >> this->_harbor_id;
int icnt; int icnt;
input >> this->_capacity >> icnt; input >> this->_capacity >> icnt;
this->_cargo.resize(icnt); this->_cargo.resize(icnt);

View file

@ -12,23 +12,27 @@ class vessel_entity : public IEntity {
private: private:
static entity_id __global_id; static entity_id __global_id;
entity_id _id; entity_id _id = 0;
entity_id _skipper_id; QString _skipper;
entity_id _harbor_id; entity_id _harbor_id;
unsigned int _capacity; unsigned int _capacity = 50000;
QVector<cargo_entity> _cargo; QVector<cargo_entity> _cargo;
public: public:
vessel_entity() = default; vessel_entity();
vessel_entity(entity_id skipper_id, entity_id harbor_id, unsigned int capacity); vessel_entity(QString skipper, entity_id harbor_id, unsigned int capacity);
entity_id id() const; entity_id id() const;
entity_id skipper() const; QString skipper() const;
void set_skipper(const QString &new_skipper);
entity_id harbor() const; entity_id harbor() const;
void set_harbor(entity_id new_harbor);
unsigned int capacity() const; unsigned int capacity() const;
void set_capacity(unsigned int new_capacity);
const QVector<cargo_entity> cargo(); const QVector<cargo_entity> cargo();
void add_cargo(cargo_entity object, bool &success); 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 withdraw_cargo(entity_id oid, bool &success);
void serialize(QDataStream &output); void serialize(QDataStream &output);

View file

@ -18,8 +18,12 @@ StorageEditDialog::StorageEditDialog(QWidget *parent) : QDialog(parent), ui(new
} }
foreach (auto mIdx, sel) { foreach (auto mIdx, sel) {
auto cdata = mIdx.data().toInt(); auto oid = mIdx.data().toULongLong();
qDebug() << cdata << '\n'; bool success;
this->_storage->withdraw_cargo(oid, success);
if (!success) {
QMessageBox::critical(this, "Error", "Cannot remove some of this cargo!");
}
} }
}); });

View file

@ -10,6 +10,18 @@
<height>336</height> <height>336</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>400</width>
<height>336</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>400</width>
<height>336</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>

View file

@ -10,6 +10,18 @@
<height>270</height> <height>270</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>304</width>
<height>270</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>304</width>
<height>270</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>

View file

@ -1,14 +1,269 @@
#include "vesseleditdialog.h" #include "vesseleditdialog.h"
#include "ui_vesseleditdialog.h" #include "ui_vesseleditdialog.h"
VesselEditDialog::VesselEditDialog(QWidget *parent) : VesselEditDialog::VesselEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::VesselEditDialog) {
QDialog(parent),
ui(new Ui::VesselEditDialog)
{
ui->setupUi(this); 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; 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();
}

View file

@ -2,21 +2,40 @@
#define VESSELEDITDIALOG_H #define VESSELEDITDIALOG_H
#include <QDialog> #include <QDialog>
#include <QInputDialog>
#include <QMessageBox>
#include "entities/vessel_entity.h"
#include "system/apparatus.h"
#include "viewmodels/cargoviewmodel.h"
#include "cargoeditdialog.h"
namespace Ui { namespace Ui {
class VesselEditDialog; class VesselEditDialog;
} }
class VesselEditDialog : public QDialog class VesselEditDialog : public QDialog {
{
Q_OBJECT Q_OBJECT
Ui::VesselEditDialog *ui;
CargoViewModel *cvm;
vessel_entity *_vessel;
void select_proper_skipper();
void select_proper_port();
public: public:
explicit VesselEditDialog(QWidget *parent = nullptr); explicit VesselEditDialog(QWidget *parent = nullptr);
~VesselEditDialog(); ~VesselEditDialog();
private: vessel_entity* vessel();
Ui::VesselEditDialog *ui; 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 #endif // VESSELEDITDIALOG_H

View file

@ -10,100 +10,146 @@
<height>425</height> <height>425</height>
</rect> </rect>
</property> </property>
<property name="minimumSize">
<size>
<width>361</width>
<height>425</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>361</width>
<height>425</height>
</size>
</property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QFormLayout" name="formLayout"> <layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<property name="horizontalSpacing">
<number>24</number>
</property>
<property name="verticalSpacing">
<number>8</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<number>4</number>
</property>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>Vessel num.:</string> <string>Skipper:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QLineEdit" name="et_vessel_num"/>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
<property name="text"> <property name="text">
<string>Home port:</string> <string>Harbor:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QPushButton" name="btn_choose_home_port"> <widget class="QComboBox" name="cb_port"/>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Choose...</string>
</property>
</widget>
</item> </item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label_6"> <widget class="QLabel" name="label_6">
<property name="text"> <property name="text">
<string>Max. capacity:</string> <string>Capacity (new):</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QComboBox" name="cb_skippers"/>
</item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="QLineEdit" name="et_max_capacity"/> <widget class="QSpinBox" name="sb_capacity">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>50000</number>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Capacity (current):</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="lab_capacity_current">
<property name="text">
<string>50000</string>
</property>
</widget>
</item> </item>
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout"> <widget class="QTableView" name="tv_cargo">
<item> <property name="selectionBehavior">
<widget class="QListView" name="listView"/> <enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QPushButton" name="pb_cargo_remove">
<property name="text">
<string>Remove cargo</string>
</property>
</widget>
</item> </item>
<item> <item row="0" column="0">
<widget class="QPushButton" name="btn_add_cargo"> <widget class="QPushButton" name="pb_cargo_add">
<property name="text"> <property name="text">
<string>Add cargo</string> <string>Add cargo</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0">
<widget class="QPushButton" name="pb_withdraw_from_harbor">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Get from harbor</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="pb_withdraw_from_vessel">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Move to harbor</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>64</height>
</size>
</property>
</spacer>
</item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<item> <item>
<widget class="QPushButton" name="btn_discard"> <widget class="QPushButton" name="pb_discard">
<property name="text"> <property name="text">
<string>Discard</string> <string>Discard</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="btn_save"> <widget class="QPushButton" name="pb_save">
<property name="text"> <property name="text">
<string>Save</string> <string>Save</string>
</property> </property>

View file

@ -9,7 +9,7 @@ int VesselsViewModel::rowCount(const QModelIndex &/*parent*/) const {
} }
int VesselsViewModel::columnCount(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 { 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: case 0:
return QString("VID"); return QString("VID");
case 1: case 1:
return QString("Harbor"); return QString("Skipper");
case 2: case 2:
return QString("Capacity"); return QString("Harbor");
case 3: case 3:
return QString("Cargo count"); return QString("Capacity");
case 4: case 4:
return QString("Cargo count");
case 5:
return QString("Cargo volume"); 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 { QVariant VesselsViewModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
auto item = apparatus::instance()->get_object_subsystem()->vessels()[index.row()]; auto item = apparatus::instance()->get_object_subsystem()->vessels()[index.row()];
bool s = false; bool hs = false;
auto harbor = apparatus::instance()->get_object_subsystem()->get_dpoint(item.harbor(), s); auto harbor = apparatus::instance()->get_object_subsystem()->get_dpoint(item.harbor(), hs);
int col = index.column(); int col = index.column();
switch (col) { switch (col) {
case 0: case 0:
return QString::number(item.id()); return QString::number(item.id());
case 1: case 1:
return (s? harbor->title() : "##ERROR##"); return item.skipper();
case 2: case 2:
return item.capacity(); return (hs? harbor->title() : tr("##ERROR[%1]##").arg(item.harbor()));
case 3: case 3:
return item.cargo().length(); return item.capacity();
case 4: case 4:
return item.cargo().length();
case 5:
int cvol = 0; int cvol = 0;
foreach (auto cargo, item.cargo()) { foreach (auto cargo, item.cargo()) {
cvol += cargo.volume(); cvol += cargo.volume();