Files
RRJServer/InstructorsAndTrainees/instructorsandtraineeswidget.cpp

468 lines
16 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <QMessageBox>
#include <QThread>
#include "instructorsandtraineeswidget.h"
#include "ui_instructorsandtraineeswidget.h"
#include "dialogauthorizationinstructor.h"
#include "dialogsettings.h"
InstructorsAndTraineesWidget::InstructorsAndTraineesWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::InstructorsAndTraineesWidget),
connectorToServer(nullptr),
viewerTrainees(nullptr),
viewerInstructors(nullptr),
messangerWidget(nullptr),
messangerController(nullptr),
adminMode(false),
loginInstructorLoggedInLocal(QStringLiteral("")),
nameInstructorLoggedInLocal(QStringLiteral("")),
language("ENG")
{
ui->setupUi(this);
ui->btnSettings->setObjectName("btnSettings");
ui->widget_Top->setObjectName("widgetTop");
ui->groupBox_Trainees->setObjectName("groupBox_Trainees");
#ifndef PROJECT_TYPE_DEBUG
//ui->btnUpdateStyleSheet->setVisible(false);
#endif
qRegisterMetaType<PacketType>("PacketType");
qRegisterMetaType<QList<Instructor>>("QList<Instructor>");
qRegisterMetaType<QList<Trainee>>("QList<Trainee>");
qRegisterMetaType<QList<Group>>("QList<Group>");
qRegisterMetaType<QList<Computer>>("QList<Computer>");
qRegisterMetaType<QList<Classroom>>("QList<Classroom>");
qRegisterMetaType<QList<Module*>>("QList<Module*>");
qRegisterMetaType<QList<QTreeWidgetItem*>>("QList<QTreeWidgetItem*>");
qRegisterMetaType<QList<TaskAmmFim>>("QList<TaskAmmFim>");
qRegisterMetaType<ClientMessage>("ClientMessage");
qDebug() << "InstructorsAndTraineesWidget init thread ID " << QThread::currentThreadId();
updateMyStyleSheet();
setLanguageInterfase();
connectorToServer = new ConnectorToServer(this);
connect(connectorToServer,&ConnectorToServer::sigLoginResult,this,&InstructorsAndTraineesWidget::checkLoginResult);
connect(connectorToServer,&ConnectorToServer::sigDeLoginResult,this,&InstructorsAndTraineesWidget::checkDeLoginResult);
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(viewerInstructors, &ViewerInstructors::signal_BlockAutorization, this, &InstructorsAndTraineesWidget::signal_BlockAutorization);
connect(viewerTrainees, &ViewerTrainees::signal_BlockAutorization, this, &InstructorsAndTraineesWidget::signal_BlockAutorization);
connect(connectorToServer,&ConnectorToServer::signal_ConnectedToServer,this,&InstructorsAndTraineesWidget::slot_ConnectedToServer);
connect(viewerTrainees, &ViewerTrainees::signal_traineeSelected, messangerController, &MessangerController::signal_traineeSelected);
connect(messangerController, &MessangerController::signal_tabMessengerChanged, viewerTrainees, &ViewerTrainees::slot_tabMessengerChanged);
messangerWidget = messangerController->newWidget(this);
//Размещение
ui->verticalLayout_Trainees->addWidget(viewerTrainees);
ui->verticalLayout_Messenger->addWidget(messangerWidget);
ui->verticalLayout_Instructors->addWidget(viewerInstructors);
ui->groupBox_Messenger->setMinimumHeight(500);
ui->groupBox_Messenger->setMaximumWidth(600);
ui->groupBox_Messenger->setMinimumWidth(600);
ui->groupBox_Instructors->setMinimumHeight(400);
ui->groupBox_Instructors->setMaximumHeight(400);
ui->groupBox_Trainees->setMinimumHeight(500);
ui->groupBox_Trainees->setMinimumWidth(700);
ui->groupBox_Instructors->setMinimumWidth(700);
ui->btnAuthorizationInstructor->setEnabled(false);
ui->btnEditorTrainees->setEnabled(false);
ui->btnEditorInstructors->setEnabled(false);
ui->btnSetVersion->hide();
}
InstructorsAndTraineesWidget::~InstructorsAndTraineesWidget()
{
if(authorizationIsCompleted())
deAuthorizationInstructor(loginInstructorLoggedInLocal);
delete messangerController;
delete viewerInstructors;
delete viewerTrainees;
delete connectorToServer;
delete ui;
}
void InstructorsAndTraineesWidget::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения
if (event->type() == QEvent::LanguageChange)
{// переведём окно заново
ui->retranslateUi(this);
updateLabelLoggedInInstructor(loginInstructorLoggedInLocal, nameInstructorLoggedInLocal);
updateLabelServer();
}
}
void InstructorsAndTraineesWidget::updateMyStyleSheet()
{
QString styleSheet = loadStyleSheet();
styleSheet = styleSheet.replace("\n", "");
this->setStyleSheet(styleSheet);
}
QString InstructorsAndTraineesWidget::getLanguage()
{
return language;
}
QString InstructorsAndTraineesWidget::loadStyleSheet()
{
//QString fileName = ":/resources/css/styleSheetMain.css";
QString fileName = "./resources/css/styleSheetMain.css";
QFile styleSheetFile(fileName);
if (!styleSheetFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::critical(this, tr("Attention!"), 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::checkLoginResult(ServerAuthorization *serverAuth)
{
if (serverAuth->Result)
{
loginInstructorLoggedInLocal = serverAuth->Login;
nameInstructorLoggedInLocal = serverAuth->ClientName;
idInstructorLoggedInLocal = serverAuth->Id;
if(loginInstructorLoggedInLocal == QStringLiteral("admin"))
adminMode = true;
else
adminMode = false;
viewerInstructors->setAdminMode(adminMode);
viewerTrainees->setAdminMode(adminMode);
viewerInstructors->setAuthComplited(true);
viewerTrainees->setAuthComplited(true);
ui->btnEditorTrainees->setEnabled(true);
if(adminMode)
ui->btnEditorInstructors->setEnabled(true);
Q_EMIT signal_NeedUpdateUI(true, true);
//ui->btnSetVersion->show();
//ui->btnAuthorizationInstructor->setText(tr("Deauthorization Instructor"));
updateLabelLoggedInInstructor(serverAuth->Login, serverAuth->ClientName);
connectorToServer->setLoginName(nameInstructorLoggedInLocal);
messangerController->initialize(serverAuth->Id);
connectorToServer->sendQueryTasksXML("fim");
connectorToServer->sendQueryTasksXML("amm");
//QMessageBox::information(this, tr("Instructor authorization"), tr("Successfully!"));
}
else
{
ui->btnAuthorizationInstructor->setChecked(false);
QMessageBox::warning(this, tr("Instructor authorization"), tr("Invalid login or password!"));
}
}
void InstructorsAndTraineesWidget::checkDeLoginResult(ServerDeAuthorization *serverDeAuth)
{
if (serverDeAuth->Result)
{
loginInstructorLoggedInLocal = QStringLiteral("");
nameInstructorLoggedInLocal = QStringLiteral("");
adminMode = false;
viewerInstructors->setAdminMode(adminMode);
viewerTrainees->setAdminMode(adminMode);
viewerInstructors->setAuthComplited(false);
viewerTrainees->setAuthComplited(false);
ui->btnEditorTrainees->setEnabled(false);
ui->btnEditorInstructors->setEnabled(false);
//Q_EMIT signal_NeedUpdateUI(true, false);
//ui->btnAuthorizationInstructor->setText(tr("Authorization Instructor"));
updateLabelLoggedInInstructor("","");
//QMessageBox::information(this, tr("Instructor deauthorization"), tr("Successfully!"));
}
else
{
ui->btnAuthorizationInstructor->setChecked(true);
QMessageBox::warning(this, tr("Instructor deauthorization"), tr("Error!"));
}
}
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")));
//ServerSettings serverSettings = connectorToServer->getServerSettings();
//ui->lblServer->setText(serverSettings.Address + ":" +serverSettings.Port);
updateLabelLoggedInInstructor(loginInstructorLoggedInLocal, nameInstructorLoggedInLocal);
updateLabelServer();
}
else
{//Сервер отключен
ui->btnConnectionToServer->setEnabled(true);
ui->btnAuthorizationInstructor->setEnabled(false);
//ui->btnAuthorizationInstructor->setText(tr("Authorization Instructor"));
ui->btnAuthorizationInstructor->setChecked(false);
//ui->btnSetVersion->hide();
ui->lblDBisConnected->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGray.png")));
//ui->lblServer->setText(tr("none"));
viewerInstructors->setAuthComplited(false);
viewerTrainees->setAuthComplited(false);
ui->btnEditorTrainees->setEnabled(false);
ui->btnEditorInstructors->setEnabled(false);
viewerTrainees->deactivate();
viewerInstructors->deactivate();
//ammTasksWidget->deactivate();
//fimTasksWidget->deactivate();
messangerController->clear();
loginInstructorLoggedInLocal = "";
nameInstructorLoggedInLocal = "";
updateLabelLoggedInInstructor(loginInstructorLoggedInLocal, nameInstructorLoggedInLocal);
updateLabelServer();
QMessageBox::warning(this, tr("Warning!"), tr("The server is disabled"));
}
}
bool InstructorsAndTraineesWidget::authorizationInstructorDialog(QWidget* parent)
{
DialogAuthorizationInstructor dlg(parent);
dlg.setWindowTitle(tr("Instructor authorization"));
do
{
switch( dlg.exec() )
{
case QDialog::Accepted:
{
QString login = dlg.getLogin();
QString password = dlg.getPassword();
connectorToServer->authorizationInstructorLocal(login, password);
return true;
}
case QDialog::Rejected:
return false;
default:
return false;
}
}
while(true);
return false;
}
bool InstructorsAndTraineesWidget::deAuthorizationInstructor(QString login)
{
connectorToServer->deAuthorizationInstructorLocal(login);
return true;
}
bool InstructorsAndTraineesWidget::authorizationIsCompleted()
{
if(loginInstructorLoggedInLocal == QStringLiteral(""))
return false;
else
return true;
}
void InstructorsAndTraineesWidget::on_btnConnectionToServer_clicked()
{
connectorToServer->SetConnectToServer();
}
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_InitMessanger,messangerController,&MessangerController::signal_InitMessanger);
}
else
ui->btnAuthorizationInstructor->setChecked(false);
}
else
{//ДеАвторизация Инструктора локальная (Администратора)
if(authorizationIsCompleted())
{
if(deAuthorizationInstructor(loginInstructorLoggedInLocal))
{
disconnect(connectorToServer,&ConnectorToServer::signal_UpdateDB,viewerInstructors,&ViewerInstructors::slot_NeedUpdateUI);
disconnect(connectorToServer,&ConnectorToServer::signal_UpdateDB,viewerTrainees,&ViewerTrainees::slot_NeedUpdateUI);
disconnect(connectorToServer,&ConnectorToServer::signal_InitMessanger,messangerController,&MessangerController::signal_InitMessanger);
viewerTrainees->deactivate();
viewerInstructors->deactivate();
//ammTasksWidget->deactivate();
//fimTasksWidget->deactivate();
messangerController->clear();
}
else
ui->btnAuthorizationInstructor->setChecked(true);
}
}
}
void InstructorsAndTraineesWidget::updateLabelLoggedInInstructor(QString login, QString name)
{
if(authorizationIsCompleted())
{
QString nameLoggedInInstructor = QString("%1 (%2)").arg(name, login);
ui->lblLoggedInInstructor->setText(nameLoggedInInstructor);
if(loginInstructorLoggedInLocal == QStringLiteral("admin"))
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(connectorToServer->getIsConnected())
{
ServerSettings serverSettings = connectorToServer->getServerSettings();
ui->lblServer->setText(serverSettings.Address + " : " +serverSettings.Port);
}
else
ui->lblServer->setText(tr("none"));
}
void InstructorsAndTraineesWidget::setLanguageInterfase()
{
ServerSettings settings;
DialogSettings::loadSettings(&settings);
if(settings.Language == "ENG")
qtLanguageTranslator.load(QString("translations/InstructorsAndTraineesWidget_") + "en_EN", ".");
else
qtLanguageTranslator.load(QString("translations/InstructorsAndTraineesWidget_") + "ru_RU", ".");
qApp->installTranslator(&qtLanguageTranslator);
}
void InstructorsAndTraineesWidget::on_btnSetVersion_clicked()
{
connectorToServer->showVersionSelect();
}
void InstructorsAndTraineesWidget::on_btnSettings_clicked()
{
DialogSettings dlg(connectorToServer, (loginInstructorLoggedInLocal != ""), this);
connect(&dlg, &DialogSettings::signal_LanguageChanged, this, &InstructorsAndTraineesWidget::slot_LanguageChanged);
connect(&dlg, &DialogSettings::signal_UpdateStyleSheet, this, &InstructorsAndTraineesWidget::slot_UpdateStyleSheet);
switch( dlg.exec() )
{
case QDialog::Accepted:
{
language = dlg.getSettings().Language;
if(dlg.settingsServerIsChanged())
{
QMessageBox::warning(this, tr("Attention!"), tr("Server settings have been changed. Please reconnect to the server."));
if(authorizationIsCompleted())
deAuthorizationInstructor(loginInstructorLoggedInLocal);
connectorToServer->StopConnectToServer();
}
break;
}
case QDialog::Rejected:
break;
default:
break;
}
}
void InstructorsAndTraineesWidget::on_btnEditorTrainees_clicked()
{
this->viewerTrainees->on_btnEditorTrainees_clicked();
}
void InstructorsAndTraineesWidget::on_btnEditorInstructors_clicked()
{
this->viewerInstructors->on_btnEditorInstructors_clicked();
}