Files
RRJServer/TrayServerLMS/mainwindow.cpp
2025-11-01 13:19:52 +03:00

213 lines
6.9 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 "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
serverLMSWidget(nullptr),
trayIcon(nullptr),
menu(nullptr),
action_ShowWindow(nullptr),
action_HideWindow(nullptr),
action_Exit(nullptr)
{
ui->setupUi(this);
/* Инициализируем иконку трея, устанавливаем иконку,
* а также задаем всплывающую подсказку
* */
trayIcon = new QSystemTrayIcon(this);
//trayIcon->setIcon(this->style()->standardIcon(QStyle::SP_ComputerIcon));
trayIcon->setIcon(QPixmap(":/resources/database-management.png"));
trayIcon->setToolTip(tr("Server LMS"));
/* После чего создаем контекстное меню*/
menu = new QMenu(this);
action_ShowWindow = new QAction(tr("Expand window"), this);
action_HideWindow = new QAction(tr("Minimize window"), this);
action_Exit = new QAction(tr("Exit"), this);
/* подключаем сигналы нажатий на пункты меню к соответсвующим слотам.
* */
connect(action_ShowWindow, SIGNAL(triggered()), this, SLOT(slot_Menu_ShowWindow()));
connect(action_HideWindow, SIGNAL(triggered()), this, SLOT(slot_Menu_HideWindow()));
connect(action_Exit, SIGNAL(triggered()), this, SLOT(slot_Menu_Exit()));
menu->addAction(action_ShowWindow);
menu->addAction(action_HideWindow);
menu->addAction(action_Exit);
/* Устанавливаем контекстное меню на иконку
* и показываем иконку приложения в трее
* */
trayIcon->setContextMenu(menu);
trayIcon->show();
/* Также подключаем сигнал нажатия на иконку к обработчику
* данного нажатия
* */
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(slot_Activated(QSystemTrayIcon::ActivationReason)));
serverLMSWidget = new ServerLMSWidget(this);
ui->verticalLayout_Main->addWidget(serverLMSWidget);
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_Menu_ShowWindow);
connect(serverLMSWidget, &ServerLMSWidget::signal_Menu_HideWindow, this, &MainWindow::slot_Menu_HideWindow);
qtLanguageTranslator.load(QString("translations/TrayServerLMS_") + serverLMSWidget->getLanguage(), ".");
qApp->installTranslator(&qtLanguageTranslator);
slot_Tray_ShowMessage(tr("Starting the server..."));
serverLMSWidget->start();
errorCheck();
}
/* Метод, который обрабатывает событие закрытия окна приложения
* */
void MainWindow::closeEvent(QCloseEvent * event)
{
/* Если окно видимо, то завершение приложения
* игнорируется, а окно просто скрывается, что сопровождается
* соответствующим всплывающим сообщением
*/
if(this->isVisible())
{
event->ignore();
slot_Menu_HideWindow();
}
}
/* Метод, который обрабатывает нажатие на иконку приложения в трее
* */
void MainWindow::slot_Activated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
/* если окно видимо, то оно скрывается,
* и наоборот, если скрыто, то разворачивается на экран
* */
if(!this->isVisible())
{
slot_Menu_ShowWindow();
}
else
{
slot_Menu_HideWindow();
}
break;
default:
break;
}
}
void MainWindow::slot_MessageClicked()
{
if(!this->isVisible())
{
slot_Menu_ShowWindow();
}
}
void MainWindow::exit()
{
QApplication::exit(0);
}
MainWindow::~MainWindow()
{
delete serverLMSWidget;
delete trayIcon;
delete menu;
delete action_ShowWindow;
delete action_HideWindow;
delete action_Exit;
delete ui;
}
void MainWindow::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения
if (event->type() == QEvent::LanguageChange)
{// переведём окно заново
ui->retranslateUi(this);
trayIcon->setToolTip(tr("Server LMS"));
action_ShowWindow->setText(tr("Expand window"));
action_HideWindow->setText(tr("Minimize window"));
action_Exit->setText(tr("Exit"));
}
}
void MainWindow::errorCheck()
{
if(serverLMSWidget->hasError() == 100)
{
slot_Menu_ShowWindow();
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Error!"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(tr("No Client files found!"));
msgBox.setInformativeText(tr("* check Application for the presence of a folder with a build \n"
"* check SharedData for a folder with the base version and the name base"));
msgBox.setStandardButtons(QMessageBox::Close);
msgBox.show();
int ret = msgBox.exec();
if (ret == QMessageBox::Close)
{
//выключение с задержкой, так как eventLoop инициализируется позже
QTimer::singleShot(1000,this,&MainWindow::slot_Menu_Exit);
}
}
}
void MainWindow::slot_Menu_ShowWindow()
{
this->show();
action_ShowWindow->setEnabled(false);
action_HideWindow->setEnabled(true);
}
void MainWindow::slot_Menu_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_Menu_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 LMS"),
textMsg,
icon, 100);
}
void MainWindow::slot_LanguageChanged(QString language)
{
qtLanguageTranslator.load(QString(QStringLiteral("translations/TrayServerLMS_")) + language, QStringLiteral("."));
qApp->installTranslator(&qtLanguageTranslator);
}