This commit is contained in:
2025-12-05 12:20:47 +03:00
parent 57673d0ee4
commit 05fce073f1
450 changed files with 58 additions and 58 deletions

View File

@@ -0,0 +1,727 @@
#include <QMessageBox>
#include <QThread>
#include <QTimer>
#include "instructorsandtraineeswidget.h"
#include "ui_instructorsandtraineeswidget.h"
#include "dialogauthorization.h"
#include "dialogsettings.h"
#include "specialmessagebox.h"
#include "hashtools.h"
#include "widgettools.h"
const QString InstructorsAndTraineesWidget::languageENG = "en_EN";
const QString InstructorsAndTraineesWidget::languageRUS = "ru_RU";
InstructorsAndTraineesWidget::InstructorsAndTraineesWidget(QWidget *parent) :
QWidget(parent),
connectorToServer(nullptr),
viewerTrainees(nullptr),
viewerInstructors(nullptr),
messangerWidget(nullptr),
messangerController(nullptr),
dlgTasksCommon(nullptr),
dlgSettings(nullptr),
dlgAuthorization(nullptr),
waitAnimationWidget(nullptr),
flSettingsServerIsChanged(false),
flTryConnectToServer(false),
cntTryConnectToServer(0),
flTryLogin(false),
language(languageENG),
ui(new Ui::InstructorsAndTraineesWidget)
{
ui->setupUi(this);
//Пока не нужно
ui->btnTasksCommon->setVisible(false);
qDebug() << "InstructorsAndTraineesWidget init thread ID " << QThread::currentThreadId();
ui->widget_Control->setObjectName("widgetControl");
registerMetaType();
updateMyStyleSheet();
setLanguageInterfase();
connectorToServer = new ConnectorToServer();
connect(connectorToServer, &ConnectorToServer::sigLoginResult, this, &InstructorsAndTraineesWidget::slot_checkLoginResult);
connect(connectorToServer, &ConnectorToServer::sigDeLoginResult, this, &InstructorsAndTraineesWidget::slot_checkDeLoginResult);
connect(connectorToServer, &ConnectorToServer::sigServerBlocked, this, &InstructorsAndTraineesWidget::slot_ServerBlocked);
connect(connectorToServer, &ConnectorToServer::sigErrorAuth, this, &InstructorsAndTraineesWidget::slot_ErrorAuth);
connect(connectorToServer, &ConnectorToServer::signal_SetVersion, this, &InstructorsAndTraineesWidget::slot_SetVersion);
connect(connectorToServer, &ConnectorToServer::signal_AnswerDocsChanged,this, &InstructorsAndTraineesWidget::slot_AnswerDocsChanged);
messangerController = new MessangerController(connectorToServer, this);
viewerTrainees = new ViewerTrainees(connectorToServer, messangerController, this);
viewerInstructors = new ViewerInstructors(connectorToServer, this);
connect(this, &InstructorsAndTraineesWidget::signal_NeedUpdateUI, viewerTrainees, &ViewerTrainees::slot_NeedUpdateUI);
connect(this, &InstructorsAndTraineesWidget::signal_NeedUpdateUI, viewerInstructors, &ViewerInstructors::slot_NeedUpdateUI);
connect(connectorToServer, &ConnectorToServer::signal_ConnectedToServer, this, &InstructorsAndTraineesWidget::slot_ConnectedToServer);
connect(viewerTrainees, &ViewerTrainees::signal_traineeSelected, this, &InstructorsAndTraineesWidget::slot_traineeSelected);
connect(viewerInstructors, &ViewerInstructors::signal_instructorSelected, this, &InstructorsAndTraineesWidget::slot_instructorSelected);
connect(messangerController, &MessangerController::signal_receiveMessage_fromInstructor, viewerInstructors, &ViewerInstructors::slot_receiveMessage);
connect(messangerController, &MessangerController::signal_receiveMessage_fromTrainee, viewerTrainees, &ViewerTrainees::slot_receiveMessage);
viewerTrainees->clearSelection();
viewerInstructors->clearSelection();
//Размещение
ui->verticalLayout_Trainees->addWidget(viewerTrainees);
ui->verticalLayout_Instructors->addWidget(viewerInstructors);
ui->groupBox_Messenger->setMinimumHeight(600);
ui->groupBox_Messenger->setMaximumWidth(500);
ui->groupBox_Messenger->setMinimumWidth(500);
ui->groupBox_Instructors->setMinimumHeight(200);
ui->groupBox_Instructors->setMaximumHeight(300);
ui->groupBox_Trainees->setMinimumHeight(400);
ui->groupBox_Trainees->setMinimumWidth(600);
ui->groupBox_Instructors->setMinimumWidth(600);
//Доступность кнопок
ui->btnAuthorizationInstructor->setEnabled(false);
ui->btnEditorTrainees->setEnabled(false);
ui->btnEditorInstructors->setEnabled(false);
ui->btnPersonalCard->setEnabled(false);
waitAnimationWidget = new WaitAnimationWidget;
QMovie *movie = new QMovie(":/resources/icons/762.gif");
waitAnimationWidget->setParent(this);
waitAnimationWidget->initialize(movie,this);
ui->btnConnectionToServer->click();
}
InstructorsAndTraineesWidget::~InstructorsAndTraineesWidget()
{
if(authorizationIsCompleted())
deAuthorizationInstructor(instructorLoggedInLocal.getLogin());
if(dlgTasksCommon)
{
dlgTasksCommon->close();
delete dlgTasksCommon;
dlgTasksCommon = nullptr;
}
if(dlgSettings)
{
dlgSettings->close();
delete dlgSettings;
dlgSettings = nullptr;
}
if(dlgAuthorization)
{
dlgAuthorization->close();
delete dlgAuthorization;
dlgAuthorization = nullptr;
}
delete messangerController;
delete viewerInstructors;
delete viewerTrainees;
delete connectorToServer;
waitAnimationWidget->hideWithStop();
delete waitAnimationWidget;
delete ui;
}
QString InstructorsAndTraineesWidget::getLanguage()
{
return language;
}
void InstructorsAndTraineesWidget::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения
if (event->type() == QEvent::LanguageChange)
{// переведём окно заново
ui->retranslateUi(this);
updateLabelLoggedInInstructor(instructorLoggedInLocal);
updateLabelServer();
}
}
void InstructorsAndTraineesWidget::resizeEvent(QResizeEvent *event)
{
QSize size = event->size();
waitAnimationWidget->resize(size);
}
void InstructorsAndTraineesWidget::updateMyStyleSheet()
{
QString styleSheet = loadStyleSheet();
styleSheet = styleSheet.replace("\n", "");
this->setStyleSheet(styleSheet);
}
QString InstructorsAndTraineesWidget::loadStyleSheet()
{
QString fileName = "./resources/css/styleSheetMain.css";
QFile styleSheetFile(fileName);
if (!styleSheetFile.open(QFile::ReadOnly | QFile::Text))
{
SpecMsgBox::CriticalClose(this, tr("The file could not be opened ") + fileName);
return QStringLiteral("");
}
else
{
QByteArray byteArray = styleSheetFile.readAll();
styleSheetFile.close();
QString style = byteArray;
return style;
}
}
void InstructorsAndTraineesWidget::slot_LanguageChanged(QString language)
{
qtLanguageTranslator.load(QString(QStringLiteral("translations/InstructorsAndTraineesWidget_")) + language, QStringLiteral("."));
qApp->installTranslator(&qtLanguageTranslator);
emit signal_LanguageChanged(language);
}
void InstructorsAndTraineesWidget::slot_UpdateStyleSheet()
{
updateMyStyleSheet();
}
void InstructorsAndTraineesWidget::slot_checkLoginResult(ServerAuthorization *serverAuth)
{
if (serverAuth->Result)
{
instructorLoggedInLocal.setLogin(serverAuth->Login);
instructorLoggedInLocal.setName(serverAuth->ClientName);
instructorLoggedInLocal.setID(serverAuth->Id.toInt());
if(serverAuth->Login == QStringLiteral("admin"))
instructorLoggedInLocal.setIsAdmin(true);
else
instructorLoggedInLocal.setIsAdmin(false);
viewerInstructors->setAdminMode(instructorLoggedInLocal.getIsAdmin());
viewerTrainees->setAdminMode(instructorLoggedInLocal.getIsAdmin());
viewerInstructors->setAuthComplited(true);
viewerTrainees->setAuthComplited(true);
ui->btnEditorTrainees->setEnabled(true);
if(instructorLoggedInLocal.getIsAdmin())
ui->btnEditorInstructors->setEnabled(true);
emit signal_NeedUpdateUI(true, true);
updateLabelLoggedInInstructor(instructorLoggedInLocal);
connectorToServer->setLoginName(instructorLoggedInLocal.getName());
messangerController->setUserLocalGUI_ID(serverAuth->Id.toInt());
connectorToServer->sendQueryTasksXML("fim");
connectorToServer->sendQueryTasksXML("amm");
viewerTrainees->activate();
viewerInstructors->activate();
waitAnimationWidget->hideWithStop();
flTryLogin = false;
}
else
{
//waitAnimationWidget->hideWithStop();
//ui->btnAuthorizationInstructor->setChecked(false);
}
}
void InstructorsAndTraineesWidget::slot_checkDeLoginResult(ServerDeAuthorization *serverDeAuth)
{
if (serverDeAuth->Result)
{
instructorLoggedInLocal.setLogin(QStringLiteral(""));
instructorLoggedInLocal.setName(QStringLiteral(""));
instructorLoggedInLocal.setIsAdmin(false);
instructorLoggedInLocal.setID(0);
viewerInstructors->setAdminMode(instructorLoggedInLocal.getIsAdmin());
viewerTrainees->setAdminMode(instructorLoggedInLocal.getIsAdmin());
viewerInstructors->setAuthComplited(false);
viewerTrainees->setAuthComplited(false);
ui->btnEditorTrainees->setEnabled(false);
ui->btnEditorInstructors->setEnabled(false);
ui->btnPersonalCard->setEnabled(false);
updateLabelLoggedInInstructor(instructorLoggedInLocal);
}
else
{
ui->btnAuthorizationInstructor->setChecked(true);
SpecMsgBox::CriticalClose(this, tr("Instructor deauthorization") + "\n" + tr("Error!"));
}
}
void InstructorsAndTraineesWidget::slot_ServerBlocked()
{
if(flTryLogin)
{
flTryLogin = false;
waitAnimationWidget->hideWithStop();
ui->btnAuthorizationInstructor->setChecked(false);
SpecMsgBox::WarningClose(this, tr("Instructor authorization.") + "\n" + tr("Server blocked!"));
}
}
void InstructorsAndTraineesWidget::slot_ErrorAuth(QString error)
{
if(flTryLogin)
{
flTryLogin = false;
waitAnimationWidget->hideWithStop();
ui->btnAuthorizationInstructor->setChecked(false);
QString errorTextMsg = "";
if(error == NOTIFY_ERROR_AUTH_DB)
{
errorTextMsg = tr("Database error!");
}
else if(error == NOTIFY_ERROR_AUTH_ARCHIVED)
{
errorTextMsg = tr("The user is archived!");
}
else if(error == NOTIFY_ERROR_AUTH_ALREADYLOGIN)
{
errorTextMsg = tr("The user is already logged in!");
}
else if(error == NOTIFY_ERROR_AUTH_LOGINORPASSWORD)
{
errorTextMsg = tr("Login or password error!");
}
SpecMsgBox::WarningClose(this, tr("Instructor authorization.") + "\n" + errorTextMsg);
}
}
void InstructorsAndTraineesWidget::slot_SetVersion(StreamingVersionData *serverVersion)
{
QString viewName = serverVersion->getViewName();
ui->lblVersionText->setText(viewName);
}
void InstructorsAndTraineesWidget::slot_ConnectedToServer(bool state)
{
if(state)
{//Сервер подключен
ui->btnConnectionToServer->setEnabled(false);
ui->btnAuthorizationInstructor->setEnabled(true);
ui->lblDBisConnected->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGreen.png")));
updateLabelLoggedInInstructor(instructorLoggedInLocal);
flTryConnectToServer = false;
cntTryConnectToServer = 0;
waitAnimationWidget->hideWithStop();
updateLabelServer();
ui->btnAuthorizationInstructor->click();
}
else
{//Сервер отключен
//ui->btnConnectionToServer->setEnabled(true);
ui->btnAuthorizationInstructor->setEnabled(false);
ui->btnAuthorizationInstructor->setChecked(false);
ui->lblDBisConnected->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGray.png")));
viewerInstructors->setAuthComplited(false);
viewerTrainees->setAuthComplited(false);
ui->btnEditorTrainees->setEnabled(false);
ui->btnEditorInstructors->setEnabled(false);
ui->btnPersonalCard->setEnabled(false);
viewerTrainees->deactivate();
viewerInstructors->deactivate();
if(dlgSettings)
{
//dlgSettings->deactivate();
dlgSettings->close();
delete dlgSettings;
dlgSettings = nullptr;
}
if(dlgAuthorization)
{
dlgAuthorization->close();
delete dlgAuthorization;
dlgAuthorization = nullptr;
}
messangerController->deleteAllWidgets();
instructorLoggedInLocal.setLogin(QStringLiteral(""));
instructorLoggedInLocal.setName(QStringLiteral(""));
instructorLoggedInLocal.setIsAdmin(false);
instructorLoggedInLocal.setID(0);
updateLabelLoggedInInstructor(instructorLoggedInLocal);
if(flTryConnectToServer)
{
if(cntTryConnectToServer < 10)
{
cntTryConnectToServer++;
QTimer::singleShot(1000, this, SLOT(slot_TryConnectToServer()));
}
else
{
flTryConnectToServer = false;
cntTryConnectToServer = 0;
ui->btnConnectionToServer->setEnabled(true);
waitAnimationWidget->hideWithStop();
}
}
else
{
flTryConnectToServer = false;
cntTryConnectToServer = 0;
ui->btnConnectionToServer->setEnabled(true);
}
if(!flSettingsServerIsChanged)
{
if(!flTryConnectToServer)
{
WidgetTools::closeAllChildWidgets(this, "SpecMsgBox");
SpecMsgBox::WarningClose(this, tr("The server is not available!"));
}
}
else
flSettingsServerIsChanged = false;
updateLabelServer();
flTryLogin = false;
}
}
void InstructorsAndTraineesWidget::slot_traineeSelected(int id_trainee)
{
ui->btnPersonalCard->setEnabled(false);
viewerInstructors->clearSelection();
/*Messanger*/
messangerController->deleteWidget(messangerWidget);
if(id_trainee)
{
Trainee trainee = connectorToServer->getTrainee(id_trainee);
if(trainee.getID())
{
messangerWidget = messangerController->newWidget(this, &trainee, ui->verticalLayout_Messenger);
ui->btnPersonalCard->setEnabled(true);
}
}
}
void InstructorsAndTraineesWidget::slot_instructorSelected(int id_instructor)
{
ui->btnPersonalCard->setEnabled(false);
viewerTrainees->clearSelection();
/*Messanger*/
messangerController->deleteWidget(messangerWidget);
if(id_instructor && id_instructor != instructorLoggedInLocal.getID())
{
Instructor instructor = connectorToServer->getInstructor(id_instructor);
if(instructor.getID())
messangerWidget = messangerController->newWidget(this, &instructor, ui->verticalLayout_Messenger);
}
}
void InstructorsAndTraineesWidget::slot_AnswerDocsChanged()
{
viewerTrainees->getAmmTasksWidgetCommon()->setDocsActualed(false);
}
void InstructorsAndTraineesWidget::slot_TryConnectToServer()
{
updateLabelServer();
connectorToServer->SetConnectToServer();
}
bool InstructorsAndTraineesWidget::authorizationInstructorDialog(QWidget* parent)
{
dlgAuthorization = new DialogAuthorization(parent);
dlgAuthorization->setWindowTitle(tr("Instructor authorization"));
dlgAuthorization->setWindowFlags(dlgAuthorization->windowFlags() & ~Qt::WindowContextHelpButtonHint);
dlgAuthorization->setStyleSheet(this->styleSheet());
#ifdef PROJECT_TYPE_DEBUG
dlgAuthorization->setLogin("admin");
dlgAuthorization->setPassword("admin");
#endif
do
{
switch( dlgAuthorization->exec() )
{
case QDialog::Accepted:
{
QString login = dlgAuthorization->getLogin();
QString password = dlgAuthorization->getPassword();
// Вычисление MD5 хэша
password = HashTools::hashingMD5string(password);
waitAnimationWidget->showWithPlay();
flTryLogin = true;
connectorToServer->sendAuthorizationInstructorLocal(login, password);
return true;
}
case QDialog::Rejected:
return false;
default:
return false;
}
}
while(true);
if(dlgAuthorization)
{
delete dlgAuthorization;
dlgAuthorization = nullptr;
}
return false;
}
bool InstructorsAndTraineesWidget::deAuthorizationInstructor(QString login)
{
connectorToServer->sendDeAuthorizationInstructorLocal(login);
return true;
}
bool InstructorsAndTraineesWidget::authorizationIsCompleted()
{
if(instructorLoggedInLocal.getLogin() == QStringLiteral(""))
return false;
else
return true;
}
void InstructorsAndTraineesWidget::on_btnConnectionToServer_clicked()
{
waitAnimationWidget->showWithPlay();
ui->btnConnectionToServer->setEnabled(false);
flTryConnectToServer = true;
cntTryConnectToServer = 1;
updateLabelServer();
QTimer::singleShot(1000, this, SLOT(slot_TryConnectToServer()));
}
void InstructorsAndTraineesWidget::on_btnAuthorizationInstructor_clicked()
{
bool stateIsChecked = ui->btnAuthorizationInstructor->isChecked();
if(stateIsChecked)
{//Авторизация Инструктора локальная (Администратора)
if(authorizationInstructorDialog(this))
{
connect(connectorToServer,&ConnectorToServer::signal_UpdateDB,viewerInstructors,&ViewerInstructors::slot_NeedUpdateUI);
connect(connectorToServer,&ConnectorToServer::signal_UpdateDB,viewerTrainees,&ViewerTrainees::slot_NeedUpdateUI);
connect(connectorToServer,&ConnectorToServer::signal_reInitMessangerByUsers,messangerController,&MessangerController::slot_reinitMessangers);
}
else
ui->btnAuthorizationInstructor->setChecked(false);
}
else
{//ДеАвторизация Инструктора локальная (Администратора)
if(authorizationIsCompleted())
{
if(deAuthorizationInstructor(instructorLoggedInLocal.getLogin()))
{
disconnect(connectorToServer,&ConnectorToServer::signal_UpdateDB,viewerInstructors,&ViewerInstructors::slot_NeedUpdateUI);
disconnect(connectorToServer,&ConnectorToServer::signal_UpdateDB,viewerTrainees,&ViewerTrainees::slot_NeedUpdateUI);
disconnect(connectorToServer,&ConnectorToServer::signal_reInitMessangerByUsers,messangerController,&MessangerController::slot_reinitMessangers);
viewerTrainees->deactivate();
viewerInstructors->deactivate();
if(dlgSettings)
dlgSettings->deactivate();
messangerController->deleteAllWidgets();
}
else
ui->btnAuthorizationInstructor->setChecked(true);
}
}
}
void InstructorsAndTraineesWidget::updateLabelLoggedInInstructor(Instructor& instructor)
{
if(authorizationIsCompleted())
{
QString nameLoggedInInstructor = QString("%1 (%2)").arg(instructor.getName(), instructor.getLogin());
ui->lblLoggedInInstructor->setText(nameLoggedInInstructor);
if(instructor.getIsAdmin())
ui->lblLoggedIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/admin.png")));
else
ui->lblLoggedIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/instructor.png")));
}
else
{
ui->lblLoggedInInstructor->setText(tr("none"));
ui->lblLoggedIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGray.png")));
}
}
void InstructorsAndTraineesWidget::updateLabelServer()
{
if(!connectorToServer)
return;
if(flTryConnectToServer)
{
ui->lblServer->setText(tr("Connection attempt") + QString(" %1 ").arg(cntTryConnectToServer) + "...");
}
else
{
if(connectorToServer->getIsConnected())
{
ServerSettings serverSettings = connectorToServer->getServerSettings();
ui->lblServer->setText(tr("connected") + " " + serverSettings.Address + " : " +serverSettings.Port);
}
else
ui->lblServer->setText(tr("not connected"));
}
}
void InstructorsAndTraineesWidget::setLanguageInterfase()
{
ServerSettings settings;
DialogSettings::loadSettings(&settings);
if(settings.Language == "ENG")
{
qtLanguageTranslator.load(QString("translations/InstructorsAndTraineesWidget_") + languageENG, ".");
language = languageENG;
}
else
{
qtLanguageTranslator.load(QString("translations/InstructorsAndTraineesWidget_") + languageRUS, ".");
language = languageRUS;
}
qApp->installTranslator(&qtLanguageTranslator);
emit signal_LanguageChanged(language);
}
void InstructorsAndTraineesWidget::on_btnSettings_clicked()
{
dlgSettings = new DialogSettings(connectorToServer, authorizationIsCompleted(), this);
dlgSettings->setWindowFlags(dlgSettings->windowFlags() & ~Qt::WindowContextHelpButtonHint);
connect(dlgSettings, &DialogSettings::signal_LanguageChanged, this, &InstructorsAndTraineesWidget::slot_LanguageChanged);
connect(dlgSettings, &DialogSettings::signal_UpdateStyleSheet, this, &InstructorsAndTraineesWidget::slot_UpdateStyleSheet);
switch( dlgSettings->exec() )
{
case QDialog::Accepted:
{
language = dlgSettings->getSettings().Language;
if(dlgSettings->settingsServerIsChanged())
{
SpecMsgBox::WarningClose(this, tr("Server settings have been changed.\nPlease reconnect to the server."));
flSettingsServerIsChanged = true;
if(authorizationIsCompleted())
deAuthorizationInstructor(instructorLoggedInLocal.getLogin());
connectorToServer->StopConnectToServer();
}
break;
}
case QDialog::Rejected:
break;
default:
break;
}
if(dlgSettings)
{
delete dlgSettings;
dlgSettings = nullptr;
}
}
void InstructorsAndTraineesWidget::on_btnEditorTrainees_clicked()
{
this->viewerTrainees->on_btnEditorTrainees_clicked();
}
void InstructorsAndTraineesWidget::on_btnEditorInstructors_clicked()
{
this->viewerInstructors->on_btnEditorInstructors_clicked();
}
void InstructorsAndTraineesWidget::on_btnPersonalCard_clicked()
{
this->viewerTrainees->on_btnPersonalCard_clicked();
}
void InstructorsAndTraineesWidget::on_btnTasksCommon_clicked()
{
AMMtasksWidget* ammTasksWidgetCommon = viewerTrainees->getAmmTasksWidgetCommon();
FIMtasksWidget* fimTasksWidgetCommon = viewerTrainees->getFimTasksWidgetCommon();
if(!ammTasksWidgetCommon || !fimTasksWidgetCommon)
return;
dlgTasksCommon = new DialogTasksCommon(connectorToServer, ammTasksWidgetCommon, fimTasksWidgetCommon, this);
dlgTasksCommon->exec();
ammTasksWidgetCommon->setParent(this);
fimTasksWidgetCommon->setParent(this);
if(dlgTasksCommon)
{
delete dlgTasksCommon;
dlgTasksCommon = nullptr;
}
}