Files
RRJServer/TestServerLMS/mainwindow.cpp
2025-10-20 13:33:10 +03:00

196 lines
6.3 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 "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_serverLMSWidget(nullptr),
trayIcon(nullptr),
menu(nullptr),
action_ShowWindow(nullptr),
action_HideWindow(nullptr),
action_Exit(nullptr)
{
ui->setupUi(this);
//Задаём два пункта с текстом локалей в комбобоксе
ui->cmbLanguage->addItems(QStringList() << "English" << "Русский");
m_serverLMSWidget = new ServerLMSWidget(this);
ui->verticalLayout_1->addWidget(m_serverLMSWidget);
connect(this, &MainWindow::signal_LanguageChanged, m_serverLMSWidget, &ServerLMSWidget::slot_LanguageChanged);
//this->move(0, 0);
//this->showNormal();
//this->showMaximized();
errorCheck();
/* Инициализируем иконку трея, устанавливаем иконку,
* а также задаем всплывающую подсказку
* */
trayIcon = new QSystemTrayIcon(this);
//trayIcon->setIcon(this->style()->standardIcon(QStyle::SP_ComputerIcon));
trayIcon->setIcon(QPixmap(":/resources/IcoServerRRJ.ico"));
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_IconActivated(QSystemTrayIcon::ActivationReason)));
slot_Menu_HideWindow();
}
/* Метод, который обрабатывает событие закрытия окна приложения
* */
void MainWindow::closeEvent(QCloseEvent * event)
{
/* Если окно видимо и чекбокс отмечен, то завершение приложения
* игнорируется, а окно просто скрывается, что сопровождается
* соответствующим всплывающим сообщением
*/
if(this->isVisible())
{
event->ignore();
slot_Menu_HideWindow();
}
}
/* Метод, который обрабатывает нажатие на иконку приложения в трее
* */
void MainWindow::slot_IconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
/* иначе, если окно видимо, то оно скрывается,
* и наоборот, если скрыто, то разворачивается на экран
* */
if(!this->isVisible())
{
slot_Menu_ShowWindow();
}
else
{
slot_Menu_HideWindow();
}
break;
default:
break;
}
}
void MainWindow::exit()
{
QApplication::exit(0);
}
MainWindow::~MainWindow()
{
delete m_serverLMSWidget;
delete trayIcon;
delete ui;
}
void MainWindow::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения
if (event->type() == QEvent::LanguageChange)
{// переведём окно заново
ui->retranslateUi(this);
}
}
void MainWindow::on_cmbLanguage_currentIndexChanged(const QString &arg1)
{
QString language;
if(arg1 == QStringLiteral("English"))
language = QString("en_EN");
else
language = QString("ru_RU");
qtLanguageTranslator.load(QString("translations/testServerLMS_") + language, ".");
qApp->installTranslator(&qtLanguageTranslator);
emit signal_LanguageChanged(language);
}
void MainWindow::errorCheck()
{
if(m_serverLMSWidget->hasError() == 100)
{
QMessageBox msgBox;
msgBox.setWindowTitle("Ошибка!");
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::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);
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);
trayIcon->showMessage(tr("Server LMS"),
tr("The application is minimized to the tray. "
"To maximize the application window, click the application icon in the tray."),
icon,
2000);
}
void MainWindow::slot_Menu_Exit()
{
this->hide();
this->close();
}