diff --git a/sea_transport/adminpanel.cpp b/sea_transport/adminpanel.cpp index 0cd3210..c596f91 100644 --- a/sea_transport/adminpanel.cpp +++ b/sea_transport/adminpanel.cpp @@ -4,28 +4,35 @@ AdminPanel::AdminPanel(QWidget *parent) : QMainWindow(parent), ui(new Ui::AdminPanel) { ui->setupUi(this); - connect(ui->pb_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested); + uvm = new UsersViewModel(this); - 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_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested); - 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_vessels_add, &QPushButton::clicked, this, &AdminPanel::on_vessel_add); +// connect(ui->pb_vessels_remove, &QPushButton::clicked, this, &AdminPanel::on_vessel_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_users_add, &QPushButton::clicked, this, &AdminPanel::on_user_add); +// connect(ui->pb_users_remove, &QPushButton::clicked, this, &AdminPanel::on_user_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); +// 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, &AdminPanel::on_delivery_point_add); +// connect(ui->pb_dp_remove, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_remove); // ui->tv_vessels->setModel(); -// ui->tv_users->setModel(); + ui->tv_users->setModel(uvm); // ui->tv_dp->setModel(); // ui->tv_storages->setModel(); + + + uvm->update(); } AdminPanel::~AdminPanel() { delete ui; + + delete uvm; } AdminPanel& AdminPanel::set_user(const user_entity &user) { @@ -33,3 +40,39 @@ AdminPanel& AdminPanel::set_user(const user_entity &user) { ui->lab_user->setText(tr("Hello user %1").arg(user.login())); return *this; } + +void AdminPanel::on_logout_requested() { + +} + +void AdminPanel::on_vessel_add() { + +} + +void AdminPanel::on_vessel_remove() { + +} + +void AdminPanel::on_user_add() { + +} + +void AdminPanel::on_user_remove() { + +} + +void AdminPanel::on_storage_add() { + +} + +void AdminPanel::on_storage_remove() { + +} + +void AdminPanel::on_delivery_point_add() { + +} + +void AdminPanel::on_delivery_point_remove() { + +} diff --git a/sea_transport/adminpanel.h b/sea_transport/adminpanel.h index 5dfc5dd..a65ce20 100644 --- a/sea_transport/adminpanel.h +++ b/sea_transport/adminpanel.h @@ -3,6 +3,8 @@ #include +#include "viewmodels/usersviewmodel.h" + #include "entities/user_entity.h" namespace Ui { @@ -15,6 +17,8 @@ class AdminPanel : public QMainWindow user_entity user; + UsersViewModel *uvm; + public: explicit AdminPanel(QWidget *parent = nullptr); ~AdminPanel(); @@ -22,6 +26,10 @@ public: AdminPanel& set_user(const user_entity &user); private slots: + +private: + Ui::AdminPanel *ui; + void on_logout_requested(); void on_vessel_add(); @@ -35,9 +43,6 @@ private slots: void on_delivery_point_add(); void on_delivery_point_remove(); - -private: - Ui::AdminPanel *ui; }; #endif // ADMINPANEL_H diff --git a/sea_transport/authwindow.cpp b/sea_transport/authwindow.cpp index 9af9f9d..76b4bbf 100644 --- a/sea_transport/authwindow.cpp +++ b/sea_transport/authwindow.cpp @@ -2,9 +2,8 @@ #include "ui_authwindow.h" -AuthWindow::AuthWindow(QWidget *parent, bool fr) : QMainWindow(parent), ui(new Ui::AuthWindow) { +AuthWindow::AuthWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::AuthWindow) { ui->setupUi(this); - this->firstRun = fr; connect(ui->btn_login, &QPushButton::clicked, this, &AuthWindow::on_auth_requested); } @@ -24,37 +23,38 @@ void AuthWindow::on_auth_requested() { bool success = false; auto a = apparatus::instance()->get_auth_subsystem(); - if (this->firstRun) { + if (apparatus::isFirstRun()) { success = a.register_user(login, passw, UserRole::ADMINISTRATOR); if (!success) { QMessageBox::critical(this, "Error", "Cannot register you. Check filesystem permission"); return; } else { - apparatus::instance()->serialize_data(); + QMessageBox::information(this, "Info", "You are the first user of system. " + "Your account type is administrator"); } } - auto user = *a.get_user(login, success); + auto user = a.get_user(login, success); if (!success) { QMessageBox::critical(this, "Error", "User not found"); return; } - success = user.verify_password(passw); + success = user->verify_password(passw); if (!success) { QMessageBox::critical(this, "Error", "Wrong password"); return; } - if (user.role() == UserRole::ADMINISTRATOR) { - AdminPanel(nullptr).set_user(user).show(); + if (user->role() == UserRole::ADMINISTRATOR) { + AdminPanel(nullptr).set_user(*user).showNormal(); } - else if (user.role() == UserRole::DISPATCHER) { + else if (user->role() == UserRole::DISPATCHER) { // DispatcherPanel(nullptr, user).set_user(user).show(); } - else if (user.role() == UserRole::SKIPPER) { + else if (user->role() == UserRole::SKIPPER) { // SkipperPanel(nullptr, user).set_user(user).show(); } else { @@ -62,5 +62,4 @@ void AuthWindow::on_auth_requested() { "It may mean corruption of data."); return; } - close(); } diff --git a/sea_transport/authwindow.h b/sea_transport/authwindow.h index c3fbe2a..e0f174e 100644 --- a/sea_transport/authwindow.h +++ b/sea_transport/authwindow.h @@ -1,7 +1,7 @@ #ifndef AUTHWINDOW_H #define AUTHWINDOW_H -#include +#include "system/apparatus.h" #include "adminpanel.h" @@ -12,21 +12,17 @@ namespace Ui { class AuthWindow; } -class AuthWindow : public QMainWindow -{ +class AuthWindow : public QMainWindow { Q_OBJECT - bool firstRun; public: - explicit AuthWindow(QWidget *parent = nullptr, bool fr = false); + explicit AuthWindow(QWidget *parent = nullptr); ~AuthWindow(); - -private slots: - void on_auth_requested(); - private: Ui::AuthWindow *ui; + + void on_auth_requested(); }; #endif // AUTHWINDOW_H diff --git a/sea_transport/main.cpp b/sea_transport/main.cpp index bb4345c..a37cf30 100644 --- a/sea_transport/main.cpp +++ b/sea_transport/main.cpp @@ -7,13 +7,16 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - bool fr = apparatus::isFirstRun(); - apparatus::init(); - AuthWindow w(nullptr, fr); - w.show(); - int ecode = a.exec(); - apparatus::shutdown(); - return ecode; + AuthWindow w(nullptr); + w.show(); + + QObject::connect(&a, &QApplication::aboutToQuit, []() { + apparatus::shutdown(); + if (apparatus::isFirstRun()) { + apparatus::generate_lock_file(); + } + }); + return a.exec(); } diff --git a/sea_transport/sea_transport.pro b/sea_transport/sea_transport.pro index 23e96ba..e5ceb58 100644 --- a/sea_transport/sea_transport.pro +++ b/sea_transport/sea_transport.pro @@ -24,7 +24,8 @@ SOURCES += \ system/auth_system.cpp \ system/object_system.cpp \ usereditdialog.cpp \ - vesseleditdialog.cpp + vesseleditdialog.cpp \ + viewmodels/usersviewmodel.cpp HEADERS += \ adminpanel.h \ @@ -43,7 +44,8 @@ HEADERS += \ system/auth_system.h \ system/object_system.h \ usereditdialog.h \ - vesseleditdialog.h + vesseleditdialog.h \ + viewmodels/usersviewmodel.h FORMS += \ adminpanel.ui \ diff --git a/sea_transport/system/apparatus.cpp b/sea_transport/system/apparatus.cpp index 43747f5..4e4e89f 100644 --- a/sea_transport/system/apparatus.cpp +++ b/sea_transport/system/apparatus.cpp @@ -24,7 +24,14 @@ apparatus::apparatus() { } apparatus::~apparatus() { - this->shutdown(); + +} + +void apparatus::generate_lock_file() { + QFile init("lock"); + init.open(QIODevice::ReadWrite); + init.write("lock"); + init.close(); } apparatus* apparatus::instance() { @@ -36,38 +43,30 @@ apparatus* apparatus::instance() { } bool apparatus::isFirstRun() { - return QFile().exists("init"); + return !QFile().exists("lock"); } -void apparatus::generate_empty_data() { - this->open_stream(); - this->writeGIDS(); - this->serialize_data(); - this->close_stream(); -} - -const auth_system& apparatus::get_auth_subsystem() { +auth_system& apparatus::get_auth_subsystem() { return this->_auth_system; } -const object_system& apparatus::get_object_subsystem() { +object_system& apparatus::get_object_subsystem() { return this->_object_system; } void apparatus::init() { apparatus::_instance = new apparatus(); - apparatus::instance()->open_stream(); - apparatus::instance()->loadGIDS(); - apparatus::instance()->deserialize_data(); - apparatus::instance()->close_stream(); + apparatus::_instance->open_stream(); + apparatus::_instance->loadGIDS(); + apparatus::_instance->deserialize_data(); } void apparatus::shutdown() { - apparatus::instance()->open_stream(); - apparatus::instance()->writeGIDS(); - apparatus::instance()->serialize_data(); - apparatus::instance()->close_stream(); + apparatus::_instance->writeGIDS(); + apparatus::_instance->serialize_data(); + apparatus::_instance->close_stream(); + delete apparatus::_instance; } void apparatus::writeGIDS() { @@ -84,12 +83,11 @@ void apparatus::loadGIDS() { } void apparatus::serialize_data() { - this->_auth_system.init(this->stream); - this->_object_system.init(this->stream); + this->_auth_system.serialize_data(this->stream); + this->_object_system.serialize_data(this->stream); } void apparatus::deserialize_data() { - QFile("init").open(QIODevice::ReadWrite); - this->_auth_system.shutdown(this->stream); - this->_object_system.shutdown(this->stream); + this->_auth_system.deserialize_data(this->stream); + this->_object_system.deserialize_data(this->stream); } diff --git a/sea_transport/system/apparatus.h b/sea_transport/system/apparatus.h index 4761a0a..eb62840 100644 --- a/sea_transport/system/apparatus.h +++ b/sea_transport/system/apparatus.h @@ -35,14 +35,15 @@ public: ~apparatus(); void generate_empty_data(); - const auth_system& get_auth_subsystem(); - const object_system& get_object_subsystem(); + auth_system& get_auth_subsystem(); + object_system& get_object_subsystem(); void serialize_data(); void deserialize_data(); static bool isFirstRun(); + static void generate_lock_file(); static apparatus* instance(); static void init(); static void shutdown(); diff --git a/sea_transport/system/auth_system.cpp b/sea_transport/system/auth_system.cpp index 6b2a0fb..5d70ea1 100644 --- a/sea_transport/system/auth_system.cpp +++ b/sea_transport/system/auth_system.cpp @@ -39,7 +39,11 @@ bool auth_system::register_user(const QString &login, const QString &password, U return false; } -void auth_system::init(QDataStream &stream) { +const QVector auth_system::users() const { + return this->_users; +} + +void auth_system::deserialize_data(QDataStream &stream) { int icnt; stream >> icnt; this->_users.resize(icnt); @@ -48,7 +52,7 @@ void auth_system::init(QDataStream &stream) { } } -void auth_system::shutdown(QDataStream &stream) { +void auth_system::serialize_data(QDataStream &stream) { stream << this->_users.size(); for (auto &item : this->_users) { item.serialize(stream); diff --git a/sea_transport/system/auth_system.h b/sea_transport/system/auth_system.h index 52849e1..c87439f 100644 --- a/sea_transport/system/auth_system.h +++ b/sea_transport/system/auth_system.h @@ -1,7 +1,7 @@ #ifndef AUTH_SYSTEM_H #define AUTH_SYSTEM_H -#include +#include #include "../entities/user_entity.h" @@ -17,8 +17,10 @@ public: bool remove_user(const QString &login); bool register_user(const QString &login, const QString &password, UserRole role); - void init(QDataStream &stream); - void shutdown(QDataStream &stream); + const QVector users() const; + + void deserialize_data(QDataStream &stream); + void serialize_data(QDataStream &stream); }; #endif // AUTH_SYSTEM_H diff --git a/sea_transport/system/object_system.cpp b/sea_transport/system/object_system.cpp index df59433..1aa46b6 100644 --- a/sea_transport/system/object_system.cpp +++ b/sea_transport/system/object_system.cpp @@ -77,7 +77,15 @@ bool object_system::add_vessel(vessel_entity dpoint) { return false; } -void object_system::init(QDataStream &stream) { +const QVector object_system::dpoints() const { + return this->_dpoints; +} + +const QVector object_system::vessels() const { + return this->_vessels; +} + +void object_system::deserialize_data(QDataStream &stream) { int dicnt; stream >> dicnt; this->_dpoints.resize(dicnt); @@ -93,7 +101,7 @@ void object_system::init(QDataStream &stream) { } } -void object_system::shutdown(QDataStream &stream) { +void object_system::serialize_data(QDataStream &stream) { stream << this->_dpoints.size(); for (auto &item : this->_dpoints) { item.serialize(stream); diff --git a/sea_transport/system/object_system.h b/sea_transport/system/object_system.h index 18285bb..095e2e5 100644 --- a/sea_transport/system/object_system.h +++ b/sea_transport/system/object_system.h @@ -25,8 +25,11 @@ public: bool add_vessel(vessel_entity dpoint); - void init(QDataStream &stream); - void shutdown(QDataStream &stream); + const QVector dpoints() const; + const QVector vessels() const; + + void deserialize_data(QDataStream &stream); + void serialize_data(QDataStream &stream); }; #endif // OBJECT_SYSTEM_H diff --git a/sea_transport/viewmodels/usersviewmodel.cpp b/sea_transport/viewmodels/usersviewmodel.cpp new file mode 100644 index 0000000..6ad267c --- /dev/null +++ b/sea_transport/viewmodels/usersviewmodel.cpp @@ -0,0 +1,65 @@ +#include "usersviewmodel.h" + +UsersViewModel::UsersViewModel(QObject *parent) : QAbstractTableModel(parent) { + +} + +int UsersViewModel::rowCount(const QModelIndex & /*parent*/) const { + return apparatus::instance()->get_object_subsystem().vessels().size(); +} + +int UsersViewModel::columnCount(const QModelIndex & /*parent*/) const { + return 3; +} + +QVariant UsersViewModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + switch (section) { + case 0: + return QString("UID"); + case 1: + return QString("Login"); + case 2: + return QString("Role"); + } + } + return QVariant(); +} + +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(); + + switch (col) { + case 0: + return QString::number(item.id()); + case 1: + return item.login(); + case 2: + QString role = "unknown"; + + switch(item.role()) { + case UserRole::ADMINISTRATOR: + role = "Administrator"; + break; + case UserRole::DISPATCHER: + role = "Dispatcher"; + break; + case UserRole::SKIPPER: + role = "Skipper"; + break; + } + + return role; + } + + return "UNKNOWN FIELD"; + } + + return QVariant(); +} + +void UsersViewModel::update() { + this->resetInternalData(); +} diff --git a/sea_transport/viewmodels/usersviewmodel.h b/sea_transport/viewmodels/usersviewmodel.h new file mode 100644 index 0000000..411981b --- /dev/null +++ b/sea_transport/viewmodels/usersviewmodel.h @@ -0,0 +1,22 @@ +#ifndef USERSVIEWMODEL_H +#define USERSVIEWMODEL_H + +#include "system/apparatus.h" + +#include + +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; + +public slots: + void update(); +}; + +#endif // USERSVIEWMODEL_H