Files
RRJServer/TestServerLMS/mainwindow.cpp
2025-10-20 12:44:37 +03:00

204 lines
7.6 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)
{
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();
errorCheck();
//this->showMaximized();
/* Инициализируем иконку трея, устанавливаем иконку из набора системных иконок,
* а также задаем всплывающую подсказку
* */
trayIcon = new QSystemTrayIcon(this);
//trayIcon->setIcon(this->style()->standardIcon(QStyle::SP_ComputerIcon));
trayIcon->setIcon(QPixmap(":/resources/IcoServerRRJ.ico"));
trayIcon->setToolTip(tr("Server LMS"));
/* После чего создаем контекстное меню из двух пунктов*/
QMenu * menu = new QMenu(this);
QAction * viewWindow = new QAction(trUtf8("Развернуть окно"), this);
QAction * hideWindow = new QAction(trUtf8("Свернуть окно"), this);
QAction * quitAction = new QAction(trUtf8("Выход"), this);
/* подключаем сигналы нажатий на пункты меню к соответсвующим слотам.
* Первый пункт меню разворачивает приложение из трея,
* а второй пункт меню завершает приложение
* */
connect(viewWindow, SIGNAL(triggered()), this, SLOT(show()));
connect(hideWindow, SIGNAL(triggered()), this, SLOT(hide()));
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
menu->addAction(viewWindow);
menu->addAction(hideWindow);
menu->addAction(quitAction);
/* Устанавливаем контекстное меню на иконку
* и показываем иконку приложения в трее
* */
trayIcon->setContextMenu(menu);
trayIcon->show();
/* Также подключаем сигнал нажатия на иконку к обработчику
* данного нажатия
* */
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
//this->hide();
//this->close();
//this->showEvent();
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);
trayIcon->showMessage(tr("Server LMS"),
tr("Приложение свернуто в трей. Для того чтобы, "
"развернуть окно приложения, щелкните по иконке приложения в трее"),
icon,
2000);
}
/* Метод, который обрабатывает событие закрытия окна приложения
* */
void MainWindow::closeEvent(QCloseEvent * event)
{
/* Если окно видимо и чекбокс отмечен, то завершение приложения
* игнорируется, а окно просто скрывается, что сопровождается
* соответствующим всплывающим сообщением
*/
if(this->isVisible() /*&& ui->trayCheckBox->isChecked()*/)
{
//flFirstShow = false;
event->ignore();
this->hide();
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);
trayIcon->showMessage(tr("Server LMS"),
tr("Приложение свернуто в трей. Для того чтобы, "
"развернуть окно приложения, щелкните по иконке приложения в трее"),
icon,
2000);
}
}
/*
void MainWindow::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
if(flFirstShow)
this->close();
}*/
/* Метод, который обрабатывает нажатие на иконку приложения в трее
* */
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
/* Событие игнорируется в том случае, если чекбокс не отмечен
* */
//if(ui->trayCheckBox->isChecked())
{
/* иначе, если окно видимо, то оно скрывается,
* и наоборот, если скрыто, то разворачивается на экран
* */
if(!this->isVisible()){
this->show();
} else
{
this->hide();
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);
trayIcon->showMessage(tr("Server LMS"),
tr("Приложение свернуто в трей. Для того чтобы, "
"развернуть окно приложения, щелкните по иконке приложения в трее"),
icon,
2000);
}
}
break;
default:
break;
}
}
void MainWindow::exit()
{
QApplication::exit(0);
}
MainWindow::~MainWindow()
{
delete m_serverLMSWidget;
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);
}
}
}