#include "fcreatechat.h" #include "ui_fcreatechat.h" #include #include FCreateChat::FCreateChat(QWidget *parent) : QDialog(parent) , m_chatServer(nullptr) , m_StyleChat(nullptr) , ui(new Ui::FCreateChat) , m_isEditMode(false) , m_existingServerName("") { ui->setupUi(this); setWindowTitle("TTW Bot app: Создать чат"); m_StyleChat = nullptr; QStringList fontList; // Создаем сервер чата на порту 7998 (можно сделать настраиваемым) m_chatServer = nullptr; // Подключаем кнопки connect(ui->btnTest, &QPushButton::clicked, this, &FCreateChat::onBtnTestClicked); connect(ui->btnAdd, &QPushButton::clicked, this, &FCreateChat::onBtnAddClicked); } FCreateChat::~FCreateChat() { if (m_StyleChat) { delete m_StyleChat; } delete ui; } void FCreateChat::setEditMode(bool isEditMode) { m_isEditMode = isEditMode; if (isEditMode) { ui->btnAdd->setText("Изменить"); setWindowTitle("TTW Bot app: Редактировать чат"); } else { ui->btnAdd->setText("Создать"); setWindowTitle("TTW Bot app: Создать чат"); } } void FCreateChat::loadExistingServer(HttpServerChat *server, const QString &name) { if (!server) return; m_isEditMode = true; m_chatServer = server; m_existingServerName = name; setEditMode(true); FSettingsWS *settingsWS = ui->widget; if (settingsWS) { settingsWS->sbPort->setValue(server->port()); settingsWS->cbFreez->setChecked(server->isFreez()); settingsWS->sbTime->setValue(server->getMessageTimeout()); settingsWS->sbCount->setValue(server->getMaxMsgCount()); } ui->lineEdit->setText(name); FColorSetting *colorSetting = ui->wBlock; if (colorSetting) { QString backgroundColor = colorSetting->hexToColorName(server->getBackgroundColor()); colorSetting->cbBackgroundColor->setCurrentText(backgroundColor); QString blockColor = colorSetting->hexToColorName(server->getBlockColor()); colorSetting->cbBlockColor->setCurrentText(blockColor); QString borderColor = colorSetting->hexToColorName(server->getBorderColor()); colorSetting->cbBorderColor->setCurrentText(borderColor); colorSetting->sbBorderSize->setValue(server->getBorderSize()); colorSetting->sbPadding->setValue(server->getPadding()); colorSetting->hsBlockTransparant->setValue(server->getTransparency()); } FFontSetting *fontSetting = ui->wFont; if (fontSetting) { QString fontFamily = colorSetting->hexToColorName(server->getFontFamily()); fontSetting->cbFontStyle->setCurrentText(fontFamily); fontSetting->sbFontSize->setValue(server->getFontSize()); QString fontColor = colorSetting->hexToColorName(server->getFontColor()); fontSetting->cbFontColor->setCurrentText(fontColor); } } void FCreateChat::createServer() { if (m_chatServer) { m_chatServer->stop(); delete m_chatServer; m_chatServer = nullptr; } // Получаем настройки из формы FSettingsWS *settingsWS = ui->widget; if (!settingsWS) { QMessageBox::warning(this, "Ошибка", "Не найдены настройки сервера"); return; } // Получаем порт из настроек int port = settingsWS->sbPort->value(); // Получаем список шрифтов из FFontSetting FFontSetting *fontSetting = ui->wFont; QStringList fontList; if (fontSetting && fontSetting->cbFontStyle) { // Добавляем все шрифты из комбобокса for (int i = 0; i < fontSetting->cbFontStyle->count(); ++i) { fontList.append(fontSetting->cbFontStyle->itemText(i)); } } // Получаем цвет фона из FColorSetting FColorSetting *colorSetting = ui->wBlock; QString backgroundColor = "#89F336"; // По умолчанию черный if (colorSetting && colorSetting->cbBackgroundColor) { QColor bgColor = colorSetting->cbBackgroundColor->currentData().value(); if (!bgColor.isValid()) { bgColor = QColor(colorSetting->cbBackgroundColor->currentText()); } backgroundColor = bgColor.name(); } // Создаем сервер с полученными настройками m_chatServer = new HttpServerChat(fontList, port, backgroundColor, nullptr); // Запускаем сервер if (!m_chatServer->start()) { QMessageBox::warning(this, "Ошибка", QString("Не удалось запустить сервер чата на порту %1").arg(port)); } else { } } void FCreateChat::onBtnTestClicked() { // Для теста создаем временный сервер или используем существующий if (!m_chatServer || !m_isEditMode) { // Если нет сервера или это не режим редактирования, создаем временный if (m_chatServer && !m_isEditMode) { m_chatServer->stop(); delete m_chatServer; } createServer(); } if (!m_chatServer) { QMessageBox::warning(this, "Ошибка", "Не удалось создать сервер"); return; } // Создаем тестовое сообщение createTestMessage(true); } void FCreateChat::onBtnAddClicked() { if (m_isEditMode && m_chatServer) { FSettingsWS *settingsWS = ui->widget; FColorSetting *colorSetting = ui->wBlock; FFontSetting *fontSetting = ui->wFont; if (!settingsWS || !colorSetting || !fontSetting) { QMessageBox::warning(this, "Ошибка", "Не найдены настройки сервера"); return; } int newPort = settingsWS->sbPort->value(); bool portChanged = (newPort != m_chatServer->port()); if (portChanged) { m_chatServer->stop(); delete m_chatServer; m_chatServer = nullptr; createServer(); } if (!m_chatServer) { QMessageBox::warning(this, "Ошибка", "Не удалось обновить сервер"); return; } // Применяем все настройки QColor bgColor = colorSetting->cbBackgroundColor->currentData().value(); if (!bgColor.isValid()) { bgColor = QColor(colorSetting->cbBackgroundColor->currentText()); } m_chatServer->changeBackground(bgColor.name()); m_chatServer->setBlockColor(colorSetting->cbBlockColor->currentText()); m_chatServer->setBorderColor(colorSetting->cbBorderColor->currentText()); m_chatServer->setBorderSize(colorSetting->sbBorderSize->value()); m_chatServer->setPadding(colorSetting->sbPadding->value()); m_chatServer->setTransparency(colorSetting->hsBlockTransparant->value()); m_chatServer->setFontFamily(fontSetting->cbFontStyle->currentText()); m_chatServer->setFontSize(fontSetting->sbFontSize->value()); m_chatServer->setFontColor(fontSetting->cbFontColor->currentText()); bool freez = settingsWS->cbFreez->isChecked(); m_chatServer->setFreez(freez); // Удаление по времени = НЕ freez bool deleteByTime = !freez; int maxCount = settingsWS->sbCount->value(); m_chatServer->setDeleteMode(deleteByTime, maxCount); m_chatServer->setMessageTimeout(settingsWS->sbTime->value()); QString newName = ui->lineEdit->text(); if (newName.isEmpty()) { newName = QString("Чат сервер (порт %1)").arg(m_chatServer->port()); } emit serverUpdated(m_chatServer, newName); accept(); } else { // Режим создания createServer(); if (!m_chatServer) { QMessageBox::warning(this, "Ошибка", "Не удалось создать сервер"); return; } createTestMessage(false); QString name = ui->lineEdit->text(); if (name.isEmpty()) { name = QString("Чат сервер (порт %1)").arg(m_chatServer->port()); } emit serverCreated(m_chatServer, name); m_chatServer = nullptr; accept(); } } void FCreateChat::createTestMessage(bool isTest) { if (!m_chatServer) { QMessageBox::warning(this, "Ошибка", "Сервер чата не запущен"); return; } StyleChat style; // Получаем настройки из виджетов // 1. Получаем настройки цвета блока из FColorSetting FColorSetting *colorSetting = ui->wBlock; if (colorSetting) { QColor blockColor = colorSetting->cbBlockColor->currentData().value(); if (!blockColor.isValid()) { blockColor = QColor(colorSetting->cbBlockColor->currentText()); } style.blockColor = blockColor.name(); QColor borderColor = colorSetting->cbBorderColor->currentData().value(); if (!borderColor.isValid()) { borderColor = QColor(colorSetting->cbBorderColor->currentText()); } style.borderColor = borderColor.name(); style.borderSize = colorSetting->sbBorderSize->value(); style.padding = colorSetting->sbPadding->value(); // Прозрачность style.transparency = colorSetting->hsBlockTransparant->value(); // Цвет фона страницы QColor pageBackgroundColor = colorSetting->cbBackgroundColor->currentData().value(); if (!pageBackgroundColor.isValid()) { pageBackgroundColor = QColor(colorSetting->cbBackgroundColor->currentText()); } style.bColor = pageBackgroundColor.name(); // Обновляем цвет фона в сервере m_chatServer->changeBackground(style.bColor); } // 2. Получаем настройки шрифта из FFontSetting FFontSetting *fontSetting = ui->wFont; if (fontSetting) { style.fontFamily = fontSetting->cbFontStyle->currentText(); style.fontSize = fontSetting->sbFontSize->value(); QColor fontColor = fontSetting->cbFontColor->currentData().value(); if (!fontColor.isValid()) { fontColor = QColor(fontSetting->cbFontColor->currentText()); } style.fontColor = fontColor.name(); } // 3. Получаем настройки из FSettingsWS FSettingsWS *settingsWS = ui->widget; if (settingsWS) { // Используем sbTime как время отображения сообщения style.timeMsg = settingsWS->sbTime->value(); // Получаем состояние чекбокса "Вечно" (freez) style.freez = settingsWS->cbFreez->isChecked(); // Настраиваем режим удаления bool deleteByTime = !settingsWS->cbFreez->isChecked(); // Если "Вечно" выбрано, то НЕ удаляем по времени int maxCount = settingsWS->sbCount->value(); m_chatServer->setDeleteMode(deleteByTime, maxCount); } // Заполняем тестовые данные style.nick = isTest ? "Тестовый пользователь" : "Пользователь"; style.context = ui->lineEdit->text().isEmpty() ? (isTest ? "Это тестовое сообщение!" : "Новое сообщение") : ui->lineEdit->text(); // Добавляем сообщение в сервер m_chatServer->addMessage(style); // Очищаем поле ввода, если это не тест if (!isTest) { ui->lineEdit->clear(); } }