Files
RRJServer/ProgramServerMPS/mainwindow.cpp
2025-12-05 11:48:24 +03:00

223 lines
7.2 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 <QTimer>
#include <QThread>
#include <QMenu>
#include "specialmessagebox.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
serverLMSWidget(nullptr),
trayIcon(nullptr),
trayMenu(nullptr),
action_ShowWindow(nullptr),
action_HideWindow(nullptr),
action_Exit(nullptr)
{
ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowMinimizeButtonHint);
serverLMSWidget = new ServerLMSWidget(this);
ui->verticalLayout_Main->addWidget(serverLMSWidget);
connect(serverLMSWidget, &ServerLMSWidget::signal_hasError, this, &MainWindow::slot_hasError);
connect(serverLMSWidget, &ServerLMSWidget::signal_LanguageChanged, this, &MainWindow::slot_LanguageChanged);
connect(serverLMSWidget, &ServerLMSWidget::signal_Tray_ShowMessage, this, &MainWindow::slot_Tray_ShowMessage);
connect(serverLMSWidget, &ServerLMSWidget::signal_Menu_ShowWindow, this, &MainWindow::slot_TrayMenu_ShowWindow);
connect(serverLMSWidget, &ServerLMSWidget::signal_Menu_HideWindow, this, &MainWindow::slot_TrayMenu_HideWindow);
qtLanguageTranslator.load(QString("translations/TrayServerLMS_") + serverLMSWidget->getLanguage(), ".");
qApp->installTranslator(&qtLanguageTranslator);
/* Инициализируем иконку трея, устанавливаем иконку,
* а также задаем всплывающую подсказку
* */
trayIcon = new QSystemTrayIcon(this);
//trayIcon->setIcon(this->style()->standardIcon(QStyle::SP_ComputerIcon));
trayIcon->setIcon(QPixmap(":/resources/PngServerRRJ.png"));
/* После чего создаем контекстное меню для иконки трея*/
trayMenu = new QMenu(this);
action_ShowWindow = new QAction(this);
action_HideWindow = new QAction(this);
action_Exit = new QAction(this);
/* подключаем сигналы нажатий на пункты меню к соответсвующим слотам.
* */
connect(action_ShowWindow, &QAction::triggered, this, &MainWindow::slot_TrayMenu_ShowWindow);
connect(action_HideWindow, &QAction::triggered, this, &MainWindow::slot_TrayMenu_HideWindow);
connect(action_Exit, &QAction::triggered, this, &MainWindow::slot_TrayMenu_Exit);
trayMenu->addAction(action_ShowWindow);
trayMenu->addAction(action_HideWindow);
trayMenu->addAction(action_Exit);
updateTrayTitles();
/* Устанавливаем контекстное меню на иконку
* и показываем иконку приложения в трее
* */
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
slot_Tray_ShowMessage(tr("Starting the server..."));
QTimer::singleShot(1000,this,&MainWindow::slot_LazyInitialization);
}
MainWindow::~MainWindow()
{
delete serverLMSWidget;
delete trayIcon;
delete trayMenu;
delete action_ShowWindow;
delete action_HideWindow;
delete action_Exit;
delete ui;
}
/* Метод, который обрабатывает событие закрытия окна приложения
* */
void MainWindow::closeEvent(QCloseEvent * event)
{
/* Если окно видимо, то завершение приложения
* игнорируется, а окно просто скрывается
*/
if(this->isVisible())
{
event->ignore();
slot_TrayMenu_HideWindow();
}
}
void MainWindow::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения
if (event->type() == QEvent::LanguageChange)
{// переведём окно заново
ui->retranslateUi(this);
//и все, что связано с треем
updateTrayTitles();
}
}
/* Метод, который обрабатывает нажатие на иконку приложения в трее
* */
void MainWindow::slot_TrayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
/* если окно видимо, то оно скрывается,
* и наоборот, если скрыто, то разворачивается на экран
* */
if(!this->isVisible())
{
slot_TrayMenu_ShowWindow();
}
else
{
slot_TrayMenu_HideWindow();
}
break;
default:
break;
}
}
/* Метод, который обрабатывает нажатие на сообщение из трея
* */
void MainWindow::slot_TrayMessageClicked()
{
if(!this->isVisible())
{
slot_TrayMenu_ShowWindow();
}
}
void MainWindow::slot_TrayMenu_ShowWindow()
{
this->show();
action_ShowWindow->setEnabled(false);
action_HideWindow->setEnabled(true);
}
void MainWindow::slot_TrayMenu_HideWindow()
{
this->hide();
action_ShowWindow->setEnabled(true);
action_HideWindow->setEnabled(false);
/*
slot_Tray_ShowMessage(tr("The application is minimized to the tray.\n"
"To maximize the application window, click the application icon in the tray."));
*/
}
void MainWindow::slot_TrayMenu_Exit()
{
this->hide();
this->close();
}
void MainWindow::slot_Tray_ShowMessage(QString textMsg, QSystemTrayIcon::MessageIcon iconMsg)
{
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(iconMsg);
trayIcon->showMessage(tr("Server MPS"),
textMsg,
icon, 100);
}
void MainWindow::slot_LazyInitialization()
{
serverLMSWidget->start();
/* Также подключаем сигнал нажатия на иконку к обработчику
* данного нажатия
* */
connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::slot_TrayIconActivated);
/* Также подключаем сигнал нажатия на всплывающее сообщение к обработчику
* данного нажатия
* */
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &MainWindow::slot_TrayMessageClicked);
}
void MainWindow::slot_hasError(int code)
{
errorCheck();
}
void MainWindow::slot_LanguageChanged(QString language)
{
qtLanguageTranslator.load(QString(QStringLiteral("translations/TrayServerLMS_")) + language, QStringLiteral("."));
qApp->installTranslator(&qtLanguageTranslator);
}
void MainWindow::exit()
{
QApplication::exit(0);
}
void MainWindow::errorCheck()
{
if(serverLMSWidget->hasError() == 100)
{
slot_TrayMenu_ShowWindow();
//выключение с задержкой, так как eventLoop инициализируется позже
QTimer::singleShot(1000,this,&MainWindow::slot_TrayMenu_Exit);
}
}
void MainWindow::updateTrayTitles()
{
trayIcon->setToolTip(tr("Server MPS"));
action_ShowWindow->setText(tr("Expand window"));
action_HideWindow->setText(tr("Minimize window"));
action_Exit->setText(tr("Exit"));
}