Files
RRJServer/DB_IaT/InstructorsAndTrainees/instructors/editorinstructors.cpp
2024-11-02 13:43:57 +03:00

200 lines
6.0 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 "editorinstructors.h"
#include "dialogeditinstructor.h"
#include "ui_editorinstructors.h"
EditorInstructors::EditorInstructors(DataBaseInstructors* db, bool adminMode, QWidget *parent) :
//QDialog(parent),
InstructorsView(db, CommonView::TypeView::control, parent),
ui(new Ui::EditorInstructors)
{
ui->setupUi((QDialog*)this);
preparationTreeWidget(ui->treeWidget);
setNotLoggedInVisible(true);
loadInstructorsFromDB();
}
EditorInstructors::~EditorInstructors()
{
delete ui;
}
void EditorInstructors::on_btnNewInstructor_clicked()
{
QString name = dbInstructors->newInstructor();
loadInstructorsFromDB();
setCurrentInstructor(name);
}
void EditorInstructors::on_btnDeleteInstructor_clicked()
{
QTreeWidgetItem *treeItemCurrent = ui->treeWidget->currentItem();
if(treeItemCurrent != nullptr)
{
QTreeWidgetItem *treeItemParent = treeItemCurrent->parent();
if(treeItemParent == nullptr)
{//Выбран Инструктор
QString name = treeItemCurrent->text(0);
if(dbInstructors->isAdmin(name))
{//Это Админ!
QMessageBox::critical(this, tr("Error!"), tr("You cannot delete the Administrator."));
return;
}
if(QMessageBox::warning(this, tr("Attention!"), tr("The deletion will be irrevocable.\nDelete it anyway?"), QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok)
{
dbInstructors->deleteInstructor(name);
loadInstructorsFromDB();
}
}
}
}
void EditorInstructors::on_btnToOrFromArchive_clicked()
{
QTreeWidgetItem *treeItemCurrent = ui->treeWidget->currentItem();
if(treeItemCurrent != nullptr)
{
QTreeWidgetItem *treeItemParent = treeItemCurrent->parent();
if(treeItemParent == nullptr)
{//Выбран Инструктор
QString name = treeItemCurrent->text(0);
Instructor instructor = dbInstructors->getInstructor(name);
if(instructor.getArchived())
{//Архивный
instructor.setArchived(false);
if(dbInstructors->editInstructor(name, instructor))
{
loadInstructorsFromDB();
setCurrentInstructor(instructor.getName());
}
}
else
{//Не Архивный
instructor.setArchived(true);
if(dbInstructors->editInstructor(name, instructor))
{
if(!archiveVisible)
ui->btnArchive->click();
loadInstructorsFromDB();
setCurrentInstructor(instructor.getName());
}
}
}
}
}
void EditorInstructors::on_btnEdit_clicked()
{
QTreeWidgetItem *treeItemCurrent = ui->treeWidget->currentItem();
if(treeItemCurrent == nullptr)
return;
QTreeWidgetItem *treeItemParent = treeItemCurrent->parent();
if(treeItemParent == nullptr)
{//Выбран Инструктор
QString name = treeItemCurrent->text(0);
DialogEditInstructor dlg(this);
dlg.setInstructor(dbInstructors->getInstructor(name));
switch( dlg.exec() )
{
case QDialog::Accepted:
{
if(dbInstructors->editInstructor(name, dlg.getInstructor()))
{
loadInstructorsFromDB();
setCurrentInstructor(dlg.getInstructor().getName());
}
else
QMessageBox::critical(this, tr("Editing error!"),
tr("An existing instructor name or login has been entered.\nThe changes will not be accepted."));
break;
}
case QDialog::Rejected:
break;
default:
break;
}
}
}
void EditorInstructors::on_btnArchive_clicked()
{
bool state = ui->btnArchive->isChecked();
setArchiveVisible(state);
loadInstructorsFromDB();
}
void EditorInstructors::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
//Определяем доступность и функционал кнопок для выбранного элемента
if(current == nullptr)
return;
QTreeWidgetItem *treeItemParent = current->parent();
if(treeItemParent == nullptr)
{//Выбран инструктор
QString name = current->text(0);
if(dbInstructors->isArchived(name))
{//Архивный
ui->btnToOrFromArchive->setText(tr("From archive"));
ui->btnToOrFromArchive->setIcon(QIcon(QStringLiteral(":/icons/instructorFromArchive.png")));
}
else
{//Не Архивный
ui->btnToOrFromArchive->setText(tr("To archive"));
ui->btnToOrFromArchive->setIcon(QIcon(QStringLiteral(":/icons/instructorArchive.png")));
}
ui->btnNewInstructor->setEnabled(true);
if(dbInstructors->isAdmin(name))
{//Это Админ! Удалять/Архивировать нельзя!
ui->btnDeleteInstructor->setEnabled(false);
ui->btnToOrFromArchive->setEnabled(false);
}
else
{
ui->btnToOrFromArchive->setEnabled(true);
if(dbInstructors->isArchived(name))
ui->btnDeleteInstructor->setEnabled(true);
else
ui->btnDeleteInstructor->setEnabled(false);
}
ui->btnEdit->setEnabled(true);
ui->btnArchive->setEnabled(true);
}
}
void EditorInstructors::setCurrentInstructor(QString name)
{
for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem * item = treeWidget->topLevelItem(i);
if(item != nullptr)
if(item->text(0) == name)
{
treeWidget->setCurrentItem(item);
break;
}
}
}