#include #include #include #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_IconActivated(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); serverLMSWidget->start(); qtLanguageTranslator.load(QString("translations/TrayServerLMS_") + serverLMSWidget->getLanguage(), "."); qApp->installTranslator(&qtLanguageTranslator); //this->move(0, 0); //this->showNormal(); //this->showMaximized(); errorCheck(); //QThread::sleep(2); 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 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) { 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::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, 100); } void MainWindow::slot_Menu_Exit() { this->hide(); this->close(); } void MainWindow::slot_Tray_ShowMessage(QString textMsg) { QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Warning); 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); }