298 lines
8.3 KiB
C++
298 lines
8.3 KiB
C++
#include "fsinglegrid.h"
|
||
#include "ui_fsinglegrid.h"
|
||
#include <QMessageBox>
|
||
#include <QFileInfo>
|
||
#include <QFileDialog>
|
||
|
||
FSingleGrid::FSingleGrid(QWidget *parent) :
|
||
QWidget(parent),
|
||
ui(new Ui::FSingleGrid)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
|
||
}
|
||
|
||
FSingleGrid::~FSingleGrid()
|
||
{
|
||
delete ui;
|
||
|
||
|
||
}
|
||
|
||
QTableWidget* FSingleGrid::tableWidget() const
|
||
{
|
||
return ui->sg;
|
||
}
|
||
|
||
void FSingleGrid::initForm(QString aBlockName, QString aNewName, bool btnOpen)
|
||
{
|
||
ui->groupBox->setTitle(aBlockName);
|
||
QStringList headers;
|
||
headers << "Название" << "Файл";
|
||
ui->sg->setHorizontalHeaderLabels(headers);
|
||
ui->sg->setSelectionBehavior(QAbstractItemView::SelectRows); // Выделение строк
|
||
ui->sg->setSelectionMode(QAbstractItemView::SingleSelection); // Одиночное выделение
|
||
ui->sg->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
ui->sg->setColumnWidth(0, 100);
|
||
ui->sg->setColumnWidth(1, 170);
|
||
ui->sg->setObjectName(aNewName);
|
||
ui->btnOpen->setVisible(btnOpen);
|
||
|
||
}
|
||
|
||
void FSingleGrid::toGrid(QString aName, QString aFile)
|
||
{
|
||
// Добавляем новую строку в таблицу
|
||
int row = ui->sg->rowCount();
|
||
ui->sg->insertRow(row);
|
||
QTableWidgetItem *R1 = new QTableWidgetItem(aName);
|
||
QTableWidgetItem *R2 = new QTableWidgetItem(aFile);
|
||
ui->sg->setItem(row, 0, R1);
|
||
ui->sg->setItem(row, 1, R2);
|
||
ui->sg->scrollToBottom();
|
||
}
|
||
|
||
void FSingleGrid::on_btnAdd_clicked()
|
||
{
|
||
QString name = ui->edtName->text().trimmed();
|
||
QString filePath = ui->edtFileName->text().trimmed();
|
||
|
||
if (name.isEmpty()) {
|
||
QMessageBox::warning(this, "Внимание", "Введите название!");
|
||
return;
|
||
}
|
||
|
||
if (filePath.isEmpty()) {
|
||
QMessageBox::warning(this, "Внимание", "Введите путь к файлу!");
|
||
return;
|
||
}
|
||
|
||
// Добавляем в соответствующий менеджер
|
||
if (!addToManager(name, filePath)) {
|
||
QMessageBox::warning(this, "Ошибка",
|
||
currentManagerType == TemplateManager ?
|
||
"Не удалось добавить шаблон. Возможно, такое имя уже существует." :
|
||
"Не удалось добавить файл. Возможно, такое имя уже существует.");
|
||
return;
|
||
}
|
||
|
||
// Добавляем в таблицу
|
||
toGrid(name, filePath);
|
||
|
||
// Сохраняем в БД
|
||
if (db) {
|
||
db->SaveTableWidget(ui->sg);
|
||
}
|
||
|
||
// Очищаем поля
|
||
ui->edtName->clear();
|
||
ui->edtFileName->clear();
|
||
}
|
||
|
||
void FSingleGrid::setDatabase(uDataBase *database)
|
||
{
|
||
db = database;
|
||
}
|
||
|
||
void FSingleGrid::on_btnDel_clicked()
|
||
{
|
||
if (!ui->sg->currentItem()) {
|
||
QMessageBox::warning(this, "Внимание", "Выберите строку для удаления!");
|
||
return;
|
||
}
|
||
|
||
int row = ui->sg->currentItem()->row();
|
||
QString name = ui->sg->item(row, 0)->text();
|
||
|
||
// Удаляем из менеджера
|
||
if (!removeFromManager(name)) {
|
||
QMessageBox::warning(this, "Ошибка", "Не удалось удалить из менеджера!");
|
||
return;
|
||
}
|
||
|
||
// Удаляем строку из таблицы
|
||
ui->sg->removeRow(row);
|
||
|
||
// Сохраняем в БД
|
||
if (db) {
|
||
db->SaveTableWidget(ui->sg);
|
||
}
|
||
}
|
||
|
||
|
||
void FSingleGrid::on_btnEdt_clicked()
|
||
{
|
||
if (!ui->sg->currentItem()) {
|
||
QMessageBox::warning(this, "Внимание", "Выберите строку для редактирования!");
|
||
return;
|
||
}
|
||
|
||
int row = ui->sg->currentItem()->row();
|
||
QString oldName = ui->sg->item(row, 0)->text();
|
||
QString newName = ui->edtName->text().trimmed();
|
||
QString newFilePath = ui->edtFileName->text().trimmed();
|
||
|
||
if (newName.isEmpty()) {
|
||
QMessageBox::warning(this, "Внимание", "Введите новое название!");
|
||
return;
|
||
}
|
||
|
||
if (newFilePath.isEmpty()) {
|
||
QMessageBox::warning(this, "Внимание", "Введите новый путь к файлу!");
|
||
return;
|
||
}
|
||
|
||
// Обновляем в менеджере
|
||
if (!updateInManager(oldName, newName, newFilePath)) {
|
||
QMessageBox::warning(this, "Ошибка",
|
||
currentManagerType == TemplateManager ?
|
||
"Не удалось обновить шаблон. Возможно, новое имя уже существует." :
|
||
"Не удалось обновить файл. Возможно, новое имя уже существует.");
|
||
return;
|
||
}
|
||
|
||
// Обновляем в таблице
|
||
ui->sg->item(row, 0)->setText(newName);
|
||
ui->sg->item(row, 1)->setText(newFilePath);
|
||
|
||
// Сохраняем в БД
|
||
if (db) {
|
||
db->SaveTableWidget(ui->sg);
|
||
}
|
||
}
|
||
|
||
void FSingleGrid::on_btnOpen_clicked()
|
||
{
|
||
// Диалог выбора файла
|
||
QString fileName = QFileDialog::getOpenFileName(
|
||
this,
|
||
"Выберите файл",
|
||
QDir::homePath(),
|
||
"Все файлы (*.*);;"
|
||
"Текстовые файлы (*.txt);;"
|
||
"Аудио (*.mp3);;"
|
||
"Приложения (*.exe *.bat *.cmd)"
|
||
);
|
||
|
||
if (!fileName.isEmpty()) {
|
||
ui->edtFileName->setText(fileName);
|
||
|
||
if (ui->edtName->text().isEmpty()) {
|
||
QFileInfo fileInfo(fileName);
|
||
ui->edtName->setText(fileInfo.baseName());
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void FSingleGrid::on_sg_cellClicked(int row, int column)
|
||
{
|
||
Q_UNUSED(column);
|
||
|
||
if (row >= 0 && row < ui->sg->rowCount()) {
|
||
ui->edtName->setText(ui->sg->item(row, 0)->text());
|
||
ui->edtFileName->setText(ui->sg->item(row, 1)->text());
|
||
}
|
||
|
||
}
|
||
|
||
void FSingleGrid::setSoundManager(MediaFileManager *manager)
|
||
{
|
||
soundManager = manager;
|
||
currentManagerType = SoundManager;
|
||
}
|
||
|
||
void FSingleGrid::setTextManager(MediaFileManager *manager)
|
||
{
|
||
textManager = manager;
|
||
currentManagerType = TextManager;
|
||
}
|
||
|
||
void FSingleGrid::setTemplateManager(NeuralTemplateManager *manager)
|
||
{
|
||
templateManager = manager;
|
||
currentManagerType = TemplateManager;
|
||
}
|
||
|
||
void FSingleGrid::setManagerType(ManagerType type)
|
||
{
|
||
currentManagerType = type;
|
||
}
|
||
|
||
bool FSingleGrid::addToManager(const QString& name, const QString& filePath)
|
||
{
|
||
switch (currentManagerType) {
|
||
case SoundManager:
|
||
if (soundManager) {
|
||
qDebug() << "добавляем ебаный звук";
|
||
return soundManager->addFile(name, filePath);
|
||
}
|
||
break;
|
||
case TextManager:
|
||
if (textManager) {
|
||
qDebug() << "добавляем ебаный текст";
|
||
return textManager->addFile(name, filePath);
|
||
}
|
||
break;
|
||
case TemplateManager:
|
||
if (templateManager) {
|
||
qDebug() << "добавляем ебаный шаблон";
|
||
return templateManager->addTemplate(name, filePath);
|
||
}
|
||
break;
|
||
default:
|
||
qDebug() << "добавляем ебаный нихуя";
|
||
break;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool FSingleGrid::removeFromManager(const QString& name)
|
||
{
|
||
switch (currentManagerType) {
|
||
case SoundManager:
|
||
if (soundManager) {
|
||
return soundManager->removeFile(name);
|
||
}
|
||
break;
|
||
case TextManager:
|
||
if (textManager) {
|
||
return textManager->removeFile(name);
|
||
}
|
||
break;
|
||
case TemplateManager:
|
||
if (templateManager) {
|
||
return templateManager->removeTemplate(name);
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool FSingleGrid::updateInManager(const QString& oldName, const QString& newName, const QString& newFilePath)
|
||
{
|
||
switch (currentManagerType) {
|
||
case SoundManager:
|
||
if (soundManager) {
|
||
return soundManager->updateFile(oldName, newName, newFilePath);
|
||
}
|
||
break;
|
||
case TextManager:
|
||
if (textManager) {
|
||
return textManager->updateFile(oldName, newName, newFilePath);
|
||
}
|
||
break;
|
||
case TemplateManager:
|
||
if (templateManager) {
|
||
return templateManager->updateTemplate(oldName, newName, newFilePath);
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
return false;
|
||
}
|