mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-28 19:55:48 +03:00
MPS->MTD
This commit is contained in:
235
ProgramServerMTD/mainwindow.cpp
Normal file
235
ProgramServerMTD/mainwindow.cpp
Normal file
@@ -0,0 +1,235 @@
|
||||
#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);
|
||||
|
||||
connect(serverLMSWidget, &ServerLMSWidget::signal_updateStateServer, this, &MainWindow::slot_updateStateServer);
|
||||
|
||||
qtLanguageTranslator.load(QString("translations/RRJServer_") + serverLMSWidget->getLanguage(), ".");
|
||||
qApp->installTranslator(&qtLanguageTranslator);
|
||||
|
||||
/* Инициализируем иконку трея, устанавливаем иконку,
|
||||
* а также задаем всплывающую подсказку
|
||||
* */
|
||||
trayIcon = new QSystemTrayIcon(this);
|
||||
//trayIcon->setIcon(this->style()->standardIcon(QStyle::SP_ComputerIcon));
|
||||
trayIcon->setIcon(QPixmap(":/resources/PngServerRRJ_stop.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);
|
||||
}
|
||||
|
||||
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 MTD"), textMsg, icon, 100);
|
||||
}
|
||||
|
||||
void MainWindow::slot_updateStateServer(EStateServer stateServer, EStateBlockAutorization stateBlockAutorization)
|
||||
{
|
||||
if(stateServer == EStateServer::started)
|
||||
{
|
||||
if(stateBlockAutorization == EStateBlockAutorization::unblocked)
|
||||
{
|
||||
trayIcon->setIcon(QPixmap(":/resources/PngServerRRJ_start.png"));
|
||||
}
|
||||
else
|
||||
{
|
||||
trayIcon->setIcon(QPixmap(":/resources/PngServerRRJ_lock.png"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
trayIcon->setIcon(QPixmap(":/resources/PngServerRRJ_stop.png"));
|
||||
}
|
||||
}
|
||||
|
||||
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/RRJServer_")) + 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 MTD"));
|
||||
action_ShowWindow->setText(tr("Expand window"));
|
||||
action_HideWindow->setText(tr("Minimize window"));
|
||||
action_Exit->setText(tr("Exit"));
|
||||
}
|
||||
Reference in New Issue
Block a user