refactor
This commit is contained in:
+170
-68
@@ -1,4 +1,5 @@
|
||||
#include "fcreatenotify.h"
|
||||
#include "udatabase.h"
|
||||
#include "ui_fcreatenotify.h"
|
||||
#include <QFileDialog>
|
||||
#include <QDebug>
|
||||
@@ -7,15 +8,19 @@
|
||||
#include <QCloseEvent>
|
||||
#include <QDesktopServices>
|
||||
#include <QApplication>
|
||||
#include "filemanager.h"
|
||||
|
||||
FCreateNotify::FCreateNotify(QWidget *parent) :
|
||||
FCreateNotify::FCreateNotify(uDataBase *database, QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::FCreateNotify),
|
||||
m_server(nullptr),
|
||||
m_notificationCounter(0)
|
||||
m_isEditMode(false),
|
||||
m_database(database),
|
||||
m_existingServerName(""),
|
||||
m_server(nullptr), // Добавьте
|
||||
m_notificationCounter(0) // Добавьте
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
FileManager::instance().initializeFolderStructure();
|
||||
// Создаем структуру папок для статических файлов
|
||||
QDir appDir(QApplication::applicationDirPath());
|
||||
appDir.mkpath("sounds");
|
||||
@@ -27,8 +32,7 @@ FCreateNotify::FCreateNotify(QWidget *parent) :
|
||||
"donation", "follow", "subscription", "raid",
|
||||
"bits", "host", "merch", "goal", "poll", "prediction"
|
||||
};
|
||||
ui->cbEvent->addItems(events);
|
||||
ui->cbEvent->setCurrentIndex(0);
|
||||
|
||||
// Устанавливаем значения по умолчанию
|
||||
ui->edtHeader->setText("Тестовый пользователь");
|
||||
ui->edtMessage->setText("Это тестовое уведомление!");
|
||||
@@ -40,7 +44,7 @@ FCreateNotify::FCreateNotify(QWidget *parent) :
|
||||
// Информация для пользователя
|
||||
ui->btnTest->setToolTip("Создать тестовое уведомление и открыть браузер");
|
||||
ui->btnAdd->setToolTip("Добавить уведомление для отображения");
|
||||
m_server = new HttpServer(nullptr);
|
||||
|
||||
}
|
||||
|
||||
FCreateNotify::~FCreateNotify()
|
||||
@@ -101,25 +105,7 @@ void FCreateNotify::on_btnTest_clicked()
|
||||
createNotification(true);
|
||||
}
|
||||
|
||||
void FCreateNotify::on_btnAdd_clicked()
|
||||
{
|
||||
// Создаем сервер с текущими настройками
|
||||
createServer();
|
||||
|
||||
if (!m_server) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось создать сервер");
|
||||
return;
|
||||
}
|
||||
|
||||
// Получаем название сервера
|
||||
QString name = QString("Уведомления (порт %1)").arg(m_server->port());
|
||||
|
||||
// Отправляем сигнал с созданным сервером
|
||||
emit serverCreated(m_server, name);
|
||||
|
||||
// Закрываем окно
|
||||
accept();
|
||||
}
|
||||
|
||||
void FCreateNotify::createNotification(bool isTest)
|
||||
{
|
||||
@@ -142,39 +128,24 @@ void FCreateNotify::createNotification(bool isTest)
|
||||
QString("Тестовое сообщение #%1").arg(m_notificationCounter) :
|
||||
ui->edtMessage->text();
|
||||
|
||||
// Добавляем тип события
|
||||
notif.content = QString("[%1] %2")
|
||||
.arg(ui->cbEvent->currentText())
|
||||
.arg(notif.content);
|
||||
|
||||
// Картинка
|
||||
QString imgPath = ui->edtFileImg->text();
|
||||
if (!imgPath.isEmpty() && QFile::exists(imgPath)) {
|
||||
QFileInfo imgInfo(imgPath);
|
||||
QString destFileName = imgInfo.fileName();
|
||||
QString destPath = getAbsolutePath("imgs/" + destFileName);
|
||||
|
||||
// Копируем, если файл еще не существует
|
||||
if (!QFile::exists(destPath)) {
|
||||
QFile::copy(imgPath, destPath);
|
||||
QString newFileName;
|
||||
if (FileManager::instance().copyToUserData(imgPath, FileManager::WebServerImages,
|
||||
FileManager::CopyWithNewName, &newFileName)) {
|
||||
notif.url = FileManager::instance().getWebPath(FileManager::WebServerImages, newFileName);
|
||||
}
|
||||
|
||||
notif.url = "/imgs/" + destFileName;
|
||||
}
|
||||
|
||||
// Звук
|
||||
QString soundPath = ui->edtFileSong->text();
|
||||
if (!soundPath.isEmpty() && QFile::exists(soundPath)) {
|
||||
QFileInfo soundInfo(soundPath);
|
||||
QString destFileName = soundInfo.fileName();
|
||||
QString destPath = getAbsolutePath("sounds/" + destFileName);
|
||||
|
||||
// Копируем, если файл еще не существует
|
||||
if (!QFile::exists(destPath)) {
|
||||
QFile::copy(soundPath, destPath);
|
||||
QString newFileName;
|
||||
if (FileManager::instance().copyToUserData(soundPath, FileManager::WebServerSounds,
|
||||
FileManager::CopyWithNewName, &newFileName)) {
|
||||
notif.soundURL = FileManager::instance().getWebPath(FileManager::WebServerSounds, newFileName);
|
||||
}
|
||||
|
||||
notif.soundURL = "/sounds/" + destFileName;
|
||||
}
|
||||
|
||||
// ПОЛУЧАЕМ НАСТРОЙКИ ИЗ ВИДЖЕТОВ
|
||||
@@ -277,26 +248,6 @@ void FCreateNotify::createNotification(bool isTest)
|
||||
}
|
||||
}
|
||||
|
||||
void FCreateNotify::copyFileToAppDir(const QString &sourcePath, const QString &destSubDir)
|
||||
{
|
||||
QFileInfo sourceInfo(sourcePath);
|
||||
QString destPath = getAbsolutePath(destSubDir + "/" + sourceInfo.fileName());
|
||||
|
||||
// Копируем файл, если он еще не существует или изменился
|
||||
if (!QFile::exists(destPath) ||
|
||||
QFileInfo(sourcePath).lastModified() > QFileInfo(destPath).lastModified()) {
|
||||
QFile::remove(destPath); // Удаляем старую версию
|
||||
if (QFile::copy(sourcePath, destPath)) {
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString FCreateNotify::getAbsolutePath(const QString &relativePath)
|
||||
{
|
||||
return QApplication::applicationDirPath() + "/" + relativePath;
|
||||
}
|
||||
|
||||
void FCreateNotify::on_btnOpenImg_clicked()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(this,
|
||||
@@ -336,3 +287,154 @@ void FCreateNotify::onServerStarted(bool success)
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось запустить веб-сервер");
|
||||
}
|
||||
}
|
||||
|
||||
void FCreateNotify::setEditMode(bool isEditMode)
|
||||
{
|
||||
m_isEditMode = isEditMode;
|
||||
if (isEditMode) {
|
||||
ui->btnAdd->setText("Изменить");
|
||||
setWindowTitle("TTW Bot app: Редактировать уведомления");
|
||||
} else {
|
||||
ui->btnAdd->setText("Создать");
|
||||
setWindowTitle("TTW Bot app: Создать уведомления");
|
||||
}
|
||||
}
|
||||
|
||||
void FCreateNotify::loadExistingServer(HttpServer *server, const QString &name)
|
||||
{
|
||||
if (!server) return;
|
||||
|
||||
m_isEditMode = true;
|
||||
m_server = server;
|
||||
m_existingServerName = name;
|
||||
|
||||
setEditMode(true);
|
||||
|
||||
// Устанавливаем порт
|
||||
FSettingsWS *settingsWS = ui->widget;
|
||||
if (settingsWS) {
|
||||
settingsWS->sbPort->setValue(server->port());
|
||||
settingsWS->sbTime->setValue(server->getDuration());
|
||||
}
|
||||
|
||||
// Устанавливаем название
|
||||
ui->lineEdit->setText(name); // Предполагается, что есть lineEdit для имени
|
||||
|
||||
// Устанавливаем цвета
|
||||
FColorSetting *colorSetting = ui->wBlock;
|
||||
if (colorSetting) {
|
||||
colorSetting->cbBlockColor->setCurrentText(server->getBlockColor());
|
||||
colorSetting->cbBorderColor->setCurrentText(server->getBorderColor());
|
||||
colorSetting->sbBorderSize->setValue(server->getBorderSize());
|
||||
colorSetting->cbBackgroundColor->setCurrentText(server->getPageBackgroundColor());
|
||||
colorSetting->hsBlockTransparant->setValue(server->getTransparency());
|
||||
}
|
||||
|
||||
// Устанавливаем шрифты
|
||||
FFontSetting *fontHeaderSetting = ui->wFont;
|
||||
if (fontHeaderSetting) {
|
||||
QString titleFamily;
|
||||
int titleSize;
|
||||
QString titleColor;
|
||||
server->getTitleFont(titleFamily, titleSize, titleColor);
|
||||
|
||||
fontHeaderSetting->cbFontStyle->setCurrentText(titleFamily);
|
||||
fontHeaderSetting->sbFontSize->setValue(titleSize);
|
||||
fontHeaderSetting->cbFontColor->setCurrentText(titleColor);
|
||||
}
|
||||
|
||||
FFontSetting *fontMessageSetting = ui->wFont_2;
|
||||
if (fontMessageSetting) {
|
||||
QString contentFamily;
|
||||
int contentSize;
|
||||
QString contentColor;
|
||||
server->getContentFont(contentFamily, contentSize, contentColor);
|
||||
|
||||
fontMessageSetting->cbFontStyle->setCurrentText(contentFamily);
|
||||
fontMessageSetting->sbFontSize->setValue(contentSize);
|
||||
fontMessageSetting->cbFontColor->setCurrentText(contentColor);
|
||||
}
|
||||
}
|
||||
|
||||
// Обновите on_btnAdd_clicked для поддержки редактирования:
|
||||
void FCreateNotify::on_btnAdd_clicked()
|
||||
{
|
||||
if (m_isEditMode && m_server) {
|
||||
// Режим редактирования
|
||||
FSettingsWS *settingsWS = ui->widget;
|
||||
FColorSetting *colorSetting = ui->wBlock;
|
||||
FFontSetting *fontHeaderSetting = ui->wFont;
|
||||
FFontSetting *fontMessageSetting = ui->wFont_2;
|
||||
|
||||
if (!settingsWS || !colorSetting || !fontHeaderSetting || !fontMessageSetting) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не найдены настройки сервера");
|
||||
return;
|
||||
}
|
||||
|
||||
int newPort = settingsWS->sbPort->value();
|
||||
bool portChanged = (newPort != m_server->port());
|
||||
|
||||
if (portChanged) {
|
||||
m_server->stop();
|
||||
delete m_server;
|
||||
m_server = nullptr;
|
||||
createServer();
|
||||
}
|
||||
|
||||
if (!m_server) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось обновить сервер");
|
||||
return;
|
||||
}
|
||||
|
||||
// Применяем настройки к серверу
|
||||
// Цвета
|
||||
m_server->setBlockColor(colorSetting->cbBlockColor->currentText());
|
||||
m_server->setBorderColor(colorSetting->cbBorderColor->currentText());
|
||||
m_server->setBorderSize(colorSetting->sbBorderSize->value());
|
||||
m_server->setPageBackgroundColor(colorSetting->cbBackgroundColor->currentText());
|
||||
m_server->setTransparency(colorSetting->hsBlockTransparant->value());
|
||||
|
||||
// Шрифты
|
||||
m_server->setTitleFont(
|
||||
fontHeaderSetting->cbFontStyle->currentText(),
|
||||
fontHeaderSetting->sbFontSize->value(),
|
||||
fontHeaderSetting->cbFontColor->currentText()
|
||||
);
|
||||
|
||||
m_server->setContentFont(
|
||||
fontMessageSetting->cbFontStyle->currentText(),
|
||||
fontMessageSetting->sbFontSize->value(),
|
||||
fontMessageSetting->cbFontColor->currentText()
|
||||
);
|
||||
|
||||
// Длительность
|
||||
m_server->setDuration(settingsWS->sbTime->value());
|
||||
|
||||
QString newName = ui->lineEdit->text();
|
||||
if (newName.isEmpty()) {
|
||||
newName = QString("Уведомления (порт %1)").arg(m_server->port());
|
||||
}
|
||||
|
||||
emit serverUpdated(m_server, newName);
|
||||
accept();
|
||||
} else {
|
||||
// Режим создания (существующий код)
|
||||
createServer();
|
||||
|
||||
if (!m_server) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось создать сервер");
|
||||
return;
|
||||
}
|
||||
|
||||
createNotification(false);
|
||||
|
||||
QString name = ui->lineEdit->text();
|
||||
if (name.isEmpty()) {
|
||||
name = QString("Уведомления (порт %1)").arg(m_server->port());
|
||||
}
|
||||
|
||||
emit serverCreated(m_server, name);
|
||||
m_server = nullptr;
|
||||
accept();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user