Files
RRJServer/DB_IaT/InstructorsAndTrainees/instructors/editorinstructors.cpp
2024-11-06 16:06:32 +03:00

199 lines
5.9 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) :
InstructorsView(db, CommonView::TypeView::control, adminMode, parent),
ui(new Ui::EditorInstructors)
{
ui->setupUi((QDialog*)this);
preparationTreeWidget(ui->treeWidget);
setNotLoggedInVisible(true);
loadInstructorsFromDB();
}
EditorInstructors::~EditorInstructors()
{
delete ui;
}
void EditorInstructors::on_btnNewInstructor_clicked()
{
int id = dbInstructors->newInstructor();
loadInstructorsFromDB();
setCurrentInstructor(id);
}
void EditorInstructors::on_btnDeleteInstructor_clicked()
{
QTreeWidgetItem *treeItemCurrent = ui->treeWidget->currentItem();
if(treeItemCurrent != nullptr)
{
QTreeWidgetItem *treeItemParent = treeItemCurrent->parent();
if(treeItemParent == nullptr)
{//Выбран Инструктор
int id = treeItemCurrent->text(0).toInt();
if(dbInstructors->isAdmin(id))
{//Это Админ!
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(id);
loadInstructorsFromDB();
}
}
}
}
void EditorInstructors::on_btnToOrFromArchive_clicked()
{
QTreeWidgetItem *treeItemCurrent = ui->treeWidget->currentItem();
if(treeItemCurrent != nullptr)
{
QTreeWidgetItem *treeItemParent = treeItemCurrent->parent();
if(treeItemParent == nullptr)
{//Выбран Инструктор
int id = treeItemCurrent->text(0).toInt();
Instructor instructor = dbInstructors->getInstructor(id);
if(instructor.getArchived())
{//Архивный
instructor.setArchived(false);
if(int id_edit = dbInstructors->editInstructor(instructor))
{
loadInstructorsFromDB();
setCurrentInstructor(id_edit);
}
}
else
{//Не Архивный
instructor.setArchived(true);
if(int id_edit = dbInstructors->editInstructor(instructor))
{
if(!archiveVisible)
ui->btnArchive->click();
loadInstructorsFromDB();
setCurrentInstructor(id_edit);
}
}
}
}
}
void EditorInstructors::on_btnEdit_clicked()
{
QTreeWidgetItem *treeItemCurrent = ui->treeWidget->currentItem();
if(treeItemCurrent == nullptr)
return;
QTreeWidgetItem *treeItemParent = treeItemCurrent->parent();
if(treeItemParent == nullptr)
{//Выбран Инструктор
int id = treeItemCurrent->text(0).toInt();
DialogEditInstructor dlg(this);
dlg.setInstructor(dbInstructors->getInstructor(id));
switch( dlg.exec() )
{
case QDialog::Accepted:
{
if(int id_edit = dbInstructors->editInstructor(dlg.getInstructor()))
{
loadInstructorsFromDB();
setCurrentInstructor(id_edit);
}
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)
{//Выбран инструктор
int id = current->text(0).toInt();
if(dbInstructors->isArchived(id))
{//Архивный
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(id))
{//Это Админ! Удалять/Архивировать нельзя!
ui->btnDeleteInstructor->setEnabled(false);
ui->btnToOrFromArchive->setEnabled(false);
}
else
{
ui->btnToOrFromArchive->setEnabled(true);
if(dbInstructors->isArchived(id))
ui->btnDeleteInstructor->setEnabled(true);
else
ui->btnDeleteInstructor->setEnabled(false);
}
ui->btnEdit->setEnabled(true);
ui->btnArchive->setEnabled(true);
}
}
void EditorInstructors::setCurrentInstructor(int id)
{
for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem * item = treeWidget->topLevelItem(i);
if(item != nullptr)
if(item->text(0).toInt() == id)
{
treeWidget->setCurrentItem(item);
break;
}
}
}