Рефакт

This commit is contained in:
2025-10-20 13:33:10 +03:00
parent f4ffc7c393
commit ad8a544e45
2 changed files with 83 additions and 82 deletions

View File

@@ -7,7 +7,11 @@ MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_serverLMSWidget(nullptr),
trayIcon(nullptr)
trayIcon(nullptr),
menu(nullptr),
action_ShowWindow(nullptr),
action_HideWindow(nullptr),
action_Exit(nullptr)
{
ui->setupUi(this);
@@ -22,11 +26,11 @@ MainWindow::MainWindow(QWidget *parent) :
//this->move(0, 0);
//this->showNormal();
errorCheck();
//this->showMaximized();
/* Инициализируем иконку трея, устанавливаем иконку из набора системных иконок,
errorCheck();
/* Инициализируем иконку трея, устанавливаем иконку,
* а также задаем всплывающую подсказку
* */
trayIcon = new QSystemTrayIcon(this);
@@ -34,23 +38,21 @@ MainWindow::MainWindow(QWidget *parent) :
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);
/* После чего создаем контекстное меню*/
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(viewWindow, SIGNAL(triggered()), this, SLOT(show()));
connect(hideWindow, SIGNAL(triggered()), this, SLOT(hide()));
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
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(viewWindow);
menu->addAction(hideWindow);
menu->addAction(quitAction);
menu->addAction(action_ShowWindow);
menu->addAction(action_HideWindow);
menu->addAction(action_Exit);
/* Устанавливаем контекстное меню на иконку
* и показываем иконку приложения в трее
@@ -62,18 +64,9 @@ MainWindow::MainWindow(QWidget *parent) :
* данного нажатия
* */
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
this, SLOT(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);
slot_Menu_HideWindow();
}
/* Метод, который обрабатывает событие закрытия окна приложения
@@ -84,58 +77,29 @@ void MainWindow::closeEvent(QCloseEvent * event)
* игнорируется, а окно просто скрывается, что сопровождается
* соответствующим всплывающим сообщением
*/
if(this->isVisible() /*&& ui->trayCheckBox->isChecked()*/)
if(this->isVisible())
{
//flFirstShow = false;
event->ignore();
this->hide();
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);
trayIcon->showMessage(tr("Server LMS"),
tr("Приложение свернуто в трей. Для того чтобы, "
"развернуть окно приложения, щелкните по иконке приложения в трее"),
icon,
2000);
slot_Menu_HideWindow();
}
}
/*
void MainWindow::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
if(flFirstShow)
this->close();
}*/
/* Метод, который обрабатывает нажатие на иконку приложения в трее
* */
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
void MainWindow::slot_IconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason){
case QSystemTrayIcon::Trigger:
/* Событие игнорируется в том случае, если чекбокс не отмечен
* */
//if(ui->trayCheckBox->isChecked())
{
/* иначе, если окно видимо, то оно скрывается,
* и наоборот, если скрыто, то разворачивается на экран
* */
if(!this->isVisible()){
this->show();
} else
if(!this->isVisible())
{
this->hide();
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);
trayIcon->showMessage(tr("Server LMS"),
tr("Приложение свернуто в трей. Для того чтобы, "
"развернуть окно приложения, щелкните по иконке приложения в трее"),
icon,
2000);
slot_Menu_ShowWindow();
}
else
{
slot_Menu_HideWindow();
}
break;
default:
@@ -152,6 +116,7 @@ void MainWindow::exit()
MainWindow::~MainWindow()
{
delete m_serverLMSWidget;
delete trayIcon;
delete ui;
}
@@ -201,3 +166,30 @@ void MainWindow::errorCheck()
}
}
}
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();
}

View File

@@ -25,29 +25,35 @@ public:
~MainWindow();
protected:
// Метод получения событий в главном окне приложения
// В нём будет производиться проверка события смены перевода приложения
/* Метод получения событий в главном окне приложения
* В нём будет производиться проверка события смены перевода приложения
*/
void changeEvent(QEvent * event) override;
/* Виртуальная функция родительского класса в нашем классе
* переопределяется для изменения поведения приложения,
* чтобы оно сворачивалось в трей, когда мы этого хотим
*/
void closeEvent(QCloseEvent * event);
void closeEvent(QCloseEvent * event) override;
private slots:
/* Слот, который будет принимать сигнал от события
* нажатия на иконку приложения в трее
*/
void iconActivated(QSystemTrayIcon::ActivationReason reason);
//Слот нажатия на иконку приложения в трее
void slot_IconActivated(QSystemTrayIcon::ActivationReason reason);
public slots:
//Слоты нажатия на пункты меню
void slot_Menu_ShowWindow();
void slot_Menu_HideWindow();
void slot_Menu_Exit();
private slots:
//Слот смены языка
void on_cmbLanguage_currentIndexChanged(const QString &arg1);
signals:
//сигнал об изменении языка интерфейса
void signal_LanguageChanged(QString language);
private slots:
void on_cmbLanguage_currentIndexChanged(const QString &arg1);
private:
void exit();
void errorCheck();
@@ -57,7 +63,10 @@ private:
ServerLMSWidget* m_serverLMSWidget;
QTranslator qtLanguageTranslator;
/* Объявляем объект будущей иконки приложения для трея */
QSystemTrayIcon* trayIcon;
QMenu * menu;
QAction * action_ShowWindow;
QAction * action_HideWindow;
QAction * action_Exit;
};
#endif // MAINWINDOW_H