PSQL 30.10.2024

This commit is contained in:
krivoshein
2024-10-30 15:33:26 +03:00
commit 024cd38bd6
256 changed files with 31332 additions and 0 deletions

View File

@@ -0,0 +1,346 @@
#include <QDomDocument>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include "databaseinstructors.h"
DataBaseInstructors::DataBaseInstructors(QString appPath, DataBaseLMS* dbLMS)
{
appDirPath = appPath;
this->dbLMS = dbLMS;
if(this->dbLMS != nullptr)
LoadInstructorsPSQL();
else
LoadInstructorsXML();
}
DataBaseInstructors::~DataBaseInstructors()
{
if(dbLMS == nullptr)
SaveInstructorsXML();
}
void DataBaseInstructors::LoadInstructorsXML()
{
QDomDocument instructorsDOM;
QString xmlFileName = appDirPath + QStringLiteral("/DBXML/instructors.xml");
QFile xmlInFile(xmlFileName);
if (!xmlInFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::critical(nullptr, tr("Attention!"), tr("The file could not be opened ") + xmlFileName);
qDebug() << QStringLiteral("LoadInstructorsXML: Не удалось открыть файл ") + xmlFileName;
return;
}
else
{
listOfInstructors.clear();
instructorsDOM.setContent(xmlInFile.readAll());
xmlInFile.close();
QDomNode allInstructorsNode = instructorsDOM.namedItem(QStringLiteral("allInstructors"));
for(int i = 0; i < allInstructorsNode.childNodes().count(); i++)
{
QDomNode instructorNode = allInstructorsNode.childNodes().at(i);
if(instructorNode.nodeName().toLower() == QStringLiteral("instructor"))
{//Инструктор
Instructor instructor;
instructor.setName(instructorNode.toElement().attribute(QStringLiteral("name")));
instructor.setLogin(instructorNode.toElement().attribute(QStringLiteral("login")));
instructor.setPassword(instructorNode.toElement().attribute(QStringLiteral("password")));
instructor.setIsAdmin(instructorNode.toElement().attribute(QStringLiteral("isAdmin")) == QStringLiteral("да") ? true : false);
instructor.setArchived(instructorNode.toElement().attribute(QStringLiteral("archived")) == QStringLiteral("да") ? true : false);
instructor.setLoggedIn(false);
listOfInstructors.append(instructor);
}
}//for(int i = 0; i < allInstructorsNode.childNodes().count(); i++)
}//else
}
void DataBaseInstructors::SaveInstructorsXML()
{
QDomDocument instructorsDOM;
QFile blankFile(QStringLiteral(":/blankXML/instructors.xml"));
if (! blankFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::critical(nullptr, tr("Attention!"), tr("The file could not be read :/blankXML/instructors.xml"));
qDebug() << "SaveInstructorsXML: Не удалось считать файл :/blankXML/instructors.xml";
return;
}
instructorsDOM.setContent(blankFile.readAll());
blankFile.close();
QDomNode allInstructorsNode = instructorsDOM.namedItem(QStringLiteral("allInstructors"));
//Инструкторы
for(Instructor instructor : listOfInstructors)
{
QDomNode instructorNode = instructorsDOM.createElement(QStringLiteral("instructor"));
allInstructorsNode.appendChild(instructorNode);
instructorNode.toElement().setAttribute(QStringLiteral("name"), instructor.getName());
instructorNode.toElement().setAttribute(QStringLiteral("login"), instructor.getLogin());
instructorNode.toElement().setAttribute(QStringLiteral("password"), instructor.getPassword());
instructorNode.toElement().setAttribute(QStringLiteral("isAdmin"), instructor.getIsAdmin() ? QStringLiteral("да") : QStringLiteral("нет"));
instructorNode.toElement().setAttribute(QStringLiteral("archived"), instructor.getArchived() ? QStringLiteral("да") : QStringLiteral("нет"));
}
QString xmlFileName = appDirPath + QStringLiteral("/DBXML/instructors.xml");
QFile xmlOutFile(xmlFileName);
if (!xmlOutFile.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::critical(nullptr, tr("Attention!"), tr("Failed to write the file ") + xmlFileName);
qDebug() << QStringLiteral("SaveInstructorsXML: Не удалось записать файл ") + xmlFileName;
return;
}
QTextStream outFile(&xmlOutFile);
instructorsDOM.save(outFile, 4);
xmlOutFile.close();
}
void DataBaseInstructors::LoadInstructorsPSQL()
{
listOfInstructors.clear();
listOfInstructors = dbLMS->selectAllInstructors();
}
bool DataBaseInstructors::AuthorizationInstructor(QString login, QString password)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getArchived())
continue;
if(listOfInstructors[i].getLogin() == login && listOfInstructors[i].getPassword() == password)
{
listOfInstructors[i].setLoggedIn(true);
return true;
}
}
return false;
}
bool DataBaseInstructors::deAuthorizationInstructor(QString login)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
//if(listOfInstructors[i].getArchived())
//continue;
if(listOfInstructors[i].getLogin() == login)
{
listOfInstructors[i].setLoggedIn(false);
return true;
}
}
return false;
}
Instructor DataBaseInstructors::getInstructor(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
return listOfInstructors[i];
}
return Instructor();
}
QString DataBaseInstructors::getNameInstructorByLogin(QString login)
{
for(Instructor instructor : listOfInstructors)
{
if(instructor.getLogin() == login)
return instructor.getName();
}
return QString(QStringLiteral(""));
}
QString DataBaseInstructors::getAuthorizedInstructorName()
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getLoggedIn())
return listOfInstructors[i].getName();
}
return QStringLiteral("");
}
QString DataBaseInstructors::newInstructor()
{
Instructor instructor;
instructor.setName(generateDefaultNameInstructor());
instructor.setLogin(generateDefaultLoginInstructor());
instructor.setPassword(QStringLiteral("<password>"));
instructor.setIsAdmin(false);
instructor.setArchived(false);
instructor.setLoggedIn(false);
bool result = dbLMS->insertInstructor(instructor);
if(result)
{
listOfInstructors.append(instructor);
return instructor.getName();
}
else
return QStringLiteral("");
}
void DataBaseInstructors::deleteInstructor(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
{
int id = listOfInstructors[i].getID();
bool result = dbLMS->deleteInstructor(id);
if(result)
listOfInstructors.removeAt(i);
return;
}
}
}
void DataBaseInstructors::toArchiveInstructor(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(! listOfInstructors[i].getArchived())
{
Instructor instructor = listOfInstructors[i];
instructor.setArchived(true);
bool result = dbLMS->updateInstructor(instructor);
if(result)
listOfInstructors[i].setArchived(true);
}
}
}
void DataBaseInstructors::fromeArchiveInstructor(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
if(listOfInstructors[i].getArchived())
{
Instructor instructor = listOfInstructors[i];
instructor.setArchived(false);
bool result = dbLMS->updateInstructor(instructor);
if(result)
listOfInstructors[i].setArchived(false);
}
}
}
bool DataBaseInstructors::editInstructor(QString name, Instructor instructor)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
{
if( (!checkExistNameInstructor(instructor.getName()) || instructor.getName() == name) &&
(!checkExistLoginInstructor(instructor.getLogin()) || instructor.getLogin() == listOfInstructors[i].getLogin()) )
{
instructor.setID(listOfInstructors[i].getID());
bool result = dbLMS->updateInstructor(instructor);
if(result)
{
listOfInstructors.replace(i, instructor);
return true;
}
else
return false;
}
}
}
return false;
}
bool DataBaseInstructors::isAdmin(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
return listOfInstructors[i].getIsAdmin();
}
return false;
}
bool DataBaseInstructors::isArchived(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
return listOfInstructors[i].getArchived();
}
return false;
}
QString DataBaseInstructors::generateDefaultNameInstructor()
{
int numInstructor = 0;
QString name;
do
{
name = QStringLiteral("<") + tr("Instructor") + QStringLiteral(" ") + QString::number(++numInstructor) + QStringLiteral(">");
}while(checkExistNameInstructor(name));
return name;
}
bool DataBaseInstructors::checkExistNameInstructor(QString name)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getName() == name)
return true;
}
return false;
}
QString DataBaseInstructors::generateDefaultLoginInstructor()
{
int numInstructor = 0;
QString login;
do
{
login = QStringLiteral("<I") + QString::number(++numInstructor) + QStringLiteral(">");
}while(checkExistLoginInstructor(login));
return login;
}
bool DataBaseInstructors::checkExistLoginInstructor(QString login)
{
//Инструкторы
for(int i = 0; i < listOfInstructors.count(); i++)
{
if(listOfInstructors[i].getLogin() == login)
return true;
}
return false;
}

View File

@@ -0,0 +1,61 @@
#ifndef DATABASEINSTRUCTORS_H
#define DATABASEINSTRUCTORS_H
#include <QList>
#include <QObject>
#include "instructorsAndTrainees_global.h"
#include "instructor.h"
#include "databaselms.h"
class INSTRUCTORSANDTRAINEES_EXPORT DataBaseInstructors : QObject
{
Q_OBJECT
public:
DataBaseInstructors(QString appPath, DataBaseLMS* dbLMS = nullptr);
~DataBaseInstructors();
void LoadInstructorsXML();
void SaveInstructorsXML();
void LoadInstructorsPSQL();
bool AuthorizationInstructor(QString login, QString password);
bool deAuthorizationInstructor(QString login);
QList<Instructor> getListInstructors(){ return listOfInstructors; } //Для загрузки
Instructor getInstructor(QString name);
QString getNameInstructorByLogin(QString login);
QString getAuthorizedInstructorName();
QString newInstructor();
void deleteInstructor(QString name);
void toArchiveInstructor(QString name);
void fromeArchiveInstructor(QString name);
bool editInstructor(QString name, Instructor instructor);
bool isAdmin(QString name);
bool isArchived(QString name);
private:
QString generateDefaultNameInstructor();
bool checkExistNameInstructor(QString name);
QString generateDefaultLoginInstructor();
bool checkExistLoginInstructor(QString login);
private:
QList<Instructor> listOfInstructors;
QString appDirPath;
DataBaseLMS* dbLMS;
};
#endif // DATABASEINSTRUCTORS_H

View File

@@ -0,0 +1,14 @@
#include "dialogauthorizationinstructor.h"
#include "ui_dialogauthorizationinstructor.h"
DialogAuthorizationInstructor::DialogAuthorizationInstructor(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogAuthorizationInstructor)
{
ui->setupUi(this);
}
DialogAuthorizationInstructor::~DialogAuthorizationInstructor()
{
delete ui;
}

View File

@@ -0,0 +1,33 @@
#ifndef DIALOGAUTHORIZATIONINSTRUCTOR_H
#define DIALOGAUTHORIZATIONINSTRUCTOR_H
#include <QDialog>
#include "ui_dialogauthorizationinstructor.h"
namespace Ui {
class DialogAuthorizationInstructor;
}
class DialogAuthorizationInstructor : public QDialog
{
Q_OBJECT
public:
explicit DialogAuthorizationInstructor(QWidget *parent = nullptr);
~DialogAuthorizationInstructor();
QString getLogin()
{
return ui->editLogin->text();
}
QString getPassword()
{
return ui->editPassword->text();
}
private:
Ui::DialogAuthorizationInstructor *ui;
};
#endif // DIALOGAUTHORIZATIONINSTRUCTOR_H

View File

@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogAuthorizationInstructor</class>
<widget class="QDialog" name="DialogAuthorizationInstructor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>140</height>
</rect>
</property>
<property name="font">
<font>
<family>Tahoma</family>
</font>
</property>
<property name="windowTitle">
<string>Instructor authorization</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="editLogin">
<property name="text">
<string>admin</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="editPassword">
<property name="text">
<string>admin</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DialogAuthorizationInstructor</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DialogAuthorizationInstructor</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,42 @@
#include "dialogeditinstructor.h"
#include "ui_dialogeditinstructor.h"
DialogEditInstructor::DialogEditInstructor(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogEditInstructor)
{
ui->setupUi(this);
}
DialogEditInstructor::~DialogEditInstructor()
{
delete ui;
}
void DialogEditInstructor::setInstructor(Instructor instructor)
{
ui->editName ->setText(instructor.getName());
ui->editLogin->setText(instructor.getLogin());
ui->editPassword->setText(instructor.getPassword());
ui->checkIsAdmin->setChecked(instructor.getIsAdmin());
ui->checkArchived->setChecked(instructor.getArchived());
ui->checkLoggedIn->setChecked(instructor.getLoggedIn());
if(instructor.getIsAdmin())
ui->editName->setEnabled(false);
else
ui->editName->setEnabled(true);
}
Instructor DialogEditInstructor::getInstructor()
{
Instructor instructor;
instructor.setName(ui->editName->text());
instructor.setLogin(ui->editLogin->text());
instructor.setPassword(ui->editPassword->text());
instructor.setIsAdmin(ui->checkIsAdmin->isChecked());
instructor.setArchived(ui->checkArchived->isChecked());
instructor.setLoggedIn(ui->checkLoggedIn->isChecked());
return instructor;
}

View File

@@ -0,0 +1,27 @@
#ifndef DIALOGEDITINSTRUCTOR_H
#define DIALOGEDITINSTRUCTOR_H
#include <QDialog>
#include "instructor.h"
#include "ui_dialogeditinstructor.h"
namespace Ui {
class DialogEditInstructor;
}
class DialogEditInstructor : public QDialog
{
Q_OBJECT
public:
explicit DialogEditInstructor(QWidget *parent = nullptr);
~DialogEditInstructor();
void setInstructor(Instructor instructor);
Instructor getInstructor();
private:
Ui::DialogEditInstructor *ui;
};
#endif // DIALOGEDITINSTRUCTOR_H

View File

@@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogEditInstructor</class>
<widget class="QDialog" name="DialogEditInstructor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>286</height>
</rect>
</property>
<property name="font">
<font>
<family>Tahoma</family>
</font>
</property>
<property name="windowTitle">
<string>Instructor</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="editName">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="editLogin"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="editPassword"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QCheckBox" name="checkIsAdmin">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Administrator</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/admin.png</normaloff>
<disabledoff>:/icons/admin.png</disabledoff>:/icons/admin.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QCheckBox" name="checkArchived">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Archived</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/archive.png</normaloff>
<disabledoff>:/icons/archive.png</disabledoff>:/icons/archive.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<widget class="QCheckBox" name="checkLoggedIn">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Logged</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/circleGreen.png</normaloff>
<disabledoff>:/icons/circleGreen.png</disabledoff>:/icons/circleGreen.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../resources.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DialogEditInstructor</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DialogEditInstructor</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,189 @@
#include <QMessageBox>
#include "editorinstructors.h"
#include "dialogeditinstructor.h"
#include "ui_editorinstructors.h"
EditorInstructors::EditorInstructors(DataBaseInstructors* db, 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);
if(dbInstructors->isArchived(name))
{//Архивный
dbInstructors->fromeArchiveInstructor(name);
loadInstructorsFromDB();
setCurrentInstructor(name);
}
else
{//Не Архивный
dbInstructors->toArchiveInstructor(name);
loadInstructorsFromDB();
if(archiveVisible)
setCurrentInstructor(name);
}
}
}
}
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;
}
}
}

View File

@@ -0,0 +1,38 @@
#ifndef DIALOGINSTRUCTORS_H
#define DIALOGINSTRUCTORS_H
#include <QDialog>
#include <QTreeWidget>
#include "databaseinstructors.h"
#include "instructorsview.h"
namespace Ui {
class EditorInstructors;
}
//Диалог для просмотра и управления БД Инструкторов
class EditorInstructors : /*public QDialog,*/ public InstructorsView
{
Q_OBJECT
public:
explicit EditorInstructors(DataBaseInstructors* db, QWidget *parent = nullptr);
~EditorInstructors();
private Q_SLOTS:
void on_btnNewInstructor_clicked();
void on_btnDeleteInstructor_clicked();
void on_btnToOrFromArchive_clicked();
void on_btnEdit_clicked();
void on_btnArchive_clicked();
void on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
private:
void setCurrentInstructor(QString name);
private:
Ui::EditorInstructors *ui;
};
#endif // DIALOGINSTRUCTORS_H

View File

@@ -0,0 +1,299 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EditorInstructors</class>
<widget class="QDialog" name="EditorInstructors">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1300</width>
<height>800</height>
</rect>
</property>
<property name="windowTitle">
<string>List instructors</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QWidget" name="widget" native="true">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(240, 240, 240);</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_1">
<item>
<layout class="QVBoxLayout" name="verticalLayout_1">
<item>
<widget class="QTreeWidget" name="treeWidget">
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="iconSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<property name="columnCount">
<number>1</number>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>127</width>
<height>0</height>
</size>
</property>
<property name="font">
<font>
<family>Tahoma</family>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(180, 180, 180);</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QToolButton" name="btnNewInstructor">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>55</height>
</size>
</property>
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>New instructor</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/addInstructor.png</normaloff>:/icons/addInstructor.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnDeleteInstructor">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>55</height>
</size>
</property>
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Delete instructor</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/deleteInstructor.png</normaloff>:/icons/deleteInstructor.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnToOrFromArchive">
<property name="minimumSize">
<size>
<width>160</width>
<height>55</height>
</size>
</property>
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>To archive</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/instructorArchive.png</normaloff>:/icons/instructorArchive.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>55</height>
</size>
</property>
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Edit</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/edit.png</normaloff>:/icons/edit.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="btnArchive">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>160</width>
<height>55</height>
</size>
</property>
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Show archive</string>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/icons/archive.png</normaloff>:/icons/archive.png</iconset>
</property>
<property name="iconSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../resources.qrc"/>
</resources>
<connections/>
</ui>

View File

@@ -0,0 +1,125 @@
#include <QHeaderView>
#include <QTranslator>
#include "instructorsview.h"
InstructorsView::InstructorsView(DataBaseInstructors* db, TypeView type, QWidget *parent):
CommonView(type, parent),
dbInstructors(db)
{
}
void InstructorsView::preparationTreeWidget(QTreeWidget* tree)
{
treeWidget = tree;
if(treeWidget == nullptr)
return;
treeWidget->setColumnCount(6);
reSetHeadTreeWidget();
treeWidget->header()->setStyleSheet(QStringLiteral("font-size: 12pt;"));
treeWidget->setColumnWidth(0, 200);
treeWidget->setColumnWidth(1, 130);
treeWidget->setColumnWidth(2, 130);
treeWidget->setColumnWidth(3, 130);
treeWidget->setColumnWidth(4, 130);
treeWidget->setColumnWidth(5, 130);
if(typeView == TypeView::onlyView)
{//onlyView
treeWidget->setColumnHidden(1, true);
treeWidget->setColumnHidden(2, true);
treeWidget->setColumnHidden(4, true);
}
else
{//control
treeWidget->setColumnHidden(4, true);
}
}
void InstructorsView::loadInstructorsFromDB()
{
if(treeWidget == nullptr)
return;
//Обновление дерева
treeWidget->clear();
//Инструкторы
QList<Instructor> listInstructors;
listInstructors = dbInstructors->getListInstructors();
for(Instructor instructor : listInstructors)
{
QTreeWidgetItem *ItemInstructor = new QTreeWidgetItem(treeWidget);
ItemInstructor->setText(0, instructor.getName());
ItemInstructor->setText(1, instructor.getLogin());
ItemInstructor->setText(2, instructor.getPassword());
if(instructor.getArchived())
{//Архивный
ItemInstructor->setText(4, tr("yes"));
ItemInstructor->setIcon(0, QIcon(QStringLiteral(":/icons/instructorArchive.png")));
setItemColorArchive(ItemInstructor);
}
else
{//Не Архивный
ItemInstructor->setText(4, tr("no"));
ItemInstructor->setIcon(0, QIcon(QStringLiteral(":/icons/instructor.png")));
setItemColorNoArchive(ItemInstructor);
}
if(instructor.getIsAdmin())
{//Админ
ItemInstructor->setText(3, tr("yes"));
ItemInstructor->setIcon(0, QIcon(QStringLiteral(":/icons/admin.png")));
}
else
{//Не Админ
ItemInstructor->setText(3, tr("no"));
}
if(instructor.getLoggedIn())
{//Залогинен
ItemInstructor->setText(5, tr("yes"));
ItemInstructor->setIcon(5, QIcon(QStringLiteral(":/icons/circleGreen.png")));
}
else
{//Не Залогинен
ItemInstructor->setText(5, tr("no"));
ItemInstructor->setIcon(5, QIcon(QStringLiteral(":/icons/circleGray.png")));
}
//Скрываем архивных (при необходимости)
if(instructor.getArchived())
if(!archiveVisible)
ItemInstructor->setHidden(true);
//Скрываем незалогиненых (при необходимости)
if(! instructor.getLoggedIn())
if(! notLoggedInVisible)
ItemInstructor->setHidden(true);
}
treeWidget->setSortingEnabled(true);
treeWidget->sortItems(0, Qt::SortOrder::AscendingOrder);
treeWidget->expandAll();
if(typeView == TypeView::control)
{
QTreeWidgetItem * item = treeWidget->topLevelItem(0);
if(item != nullptr)
treeWidget->setCurrentItem(item);
}
}
void InstructorsView::reSetHeadTreeWidget()
{
QStringList listHeaders = {tr("Instructor"), tr("Login"), tr("Password"), tr("Administrator"), tr("Archived"), tr("Logged")};
treeWidget->setHeaderLabels(listHeaders);
}

View File

@@ -0,0 +1,27 @@
#ifndef INSTRUCTORSVIEW_H
#define INSTRUCTORSVIEW_H
#include "instructorsAndTrainees_global.h"
#include "databaseinstructors.h"
#include "commonview.h"
//Родительский класс представления БД Инструкторов (для просмотра и управления)
class INSTRUCTORSANDTRAINEES_EXPORT InstructorsView: public CommonView
{
Q_OBJECT
public:
InstructorsView(DataBaseInstructors* db, TypeView type, QWidget *parent = nullptr);
protected:
void preparationTreeWidget(QTreeWidget* tree);
void loadInstructorsFromDB();
void reSetHeadTreeWidget();
protected:
DataBaseInstructors* dbInstructors;
};
#endif // INSTRUCTORSVIEW_H

View File

@@ -0,0 +1,47 @@
#include "viewerinstructors.h"
#include "ui_viewerinstructors.h"
ViewerInstructors::ViewerInstructors(DataBaseInstructors* db, QWidget *parent) :
//QWidget(parent),
InstructorsView(db, CommonView::TypeView::onlyView, parent),
ui(new Ui::ViewerInstructors)
{
ui->setupUi(this);
// Сделаем первоначальную инициализацию перевода для окна виджета
qtLanguageTranslator.load(QString(QStringLiteral("translations/InstructorsAndTrainees_")) + QString(QStringLiteral("ru_RU")), QStringLiteral("."));
qApp->installTranslator(&qtLanguageTranslator);
preparationTreeWidget(ui->treeWidget);
setNotLoggedInVisible(true);
loadInstructorsFromDB();
}
ViewerInstructors::~ViewerInstructors()
{
delete ui;
}
void ViewerInstructors::setFilterInstructorLoggedIn(bool enabled)
{
setNotLoggedInVisible(!enabled);
loadInstructorsFromDB();
}
void ViewerInstructors::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения
if (event->type() == QEvent::LanguageChange)
{
ui->retranslateUi(this); // переведём окно заново
reSetHeadTreeWidget();
loadInstructorsFromDB();
}
}
void ViewerInstructors::slot_LanguageChanged(QString language)
{
qtLanguageTranslator.load(QString(QStringLiteral("translations/InstructorsAndTrainees_")) + language, QStringLiteral("."));
qApp->installTranslator(&qtLanguageTranslator);
}

View File

@@ -0,0 +1,44 @@
#ifndef INSTRUCTORSWIDGET_H
#define INSTRUCTORSWIDGET_H
#include <QWidget>
#include "instructorsAndTrainees_global.h"
#include "databaseinstructors.h"
#include "instructorsview.h"
namespace Ui {
class ViewerInstructors;
}
//Виджет только для просмотра БД Инструкторов
class INSTRUCTORSANDTRAINEES_EXPORT ViewerInstructors : /*public QWidget,*/ public InstructorsView
{
Q_OBJECT
public:
explicit ViewerInstructors(DataBaseInstructors* db, QWidget *parent = nullptr);
~ViewerInstructors();
protected:
// Метод получения событий
// В нём будет производиться проверка события смены перевода приложения
void changeEvent(QEvent * event) override;
public Q_SLOTS:
void slot_LanguageChanged(QString language);
public:
void updateInstructors()
{
loadInstructorsFromDB();
}
void setFilterInstructorLoggedIn( bool enabled );
private:
Ui::ViewerInstructors *ui;
};
#endif // INSTRUCTORSWIDGET_H

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ViewerInstructors</class>
<widget class="QWidget" name="ViewerInstructors">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="font">
<font>
<family>Tahoma</family>
</font>
</property>
<property name="windowTitle">
<string>Instructors</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTreeWidget" name="treeWidget">
<property name="font">
<font>
<family>Tahoma</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="iconSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>