Browse Source

Fixed bugs caused incorrect window close and termination

Ivan Arkhipov 3 years ago
parent
commit
49b6878299

+ 1 - 1
src/Legacy/legacyapplication.cpp

@@ -26,10 +26,10 @@ LegacyApplication::LegacyApplication() {
 }
 
 LegacyApplication::~LegacyApplication() {
+    // gui will be deleted automatically on close due to Qt::WA_DeleteOnClose attribute
     patch_managers_thread_->terminate();
     patch_managers_thread_->wait();
     delete patch_managers_thread_;
-    delete gui;
 }
 
 bool LegacyApplication::init() {

+ 66 - 66
src/Legacy/widgets/mainwindow.cpp

@@ -30,14 +30,9 @@ class StatusWidget;
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent, Qt::Window | Qt::FramelessWindowHint)
     , ui(new Ui::MainWindow)
-    , current_active_entry_(nullptr)
-    , current_hovered_entry_(nullptr)
-    , status_widget_(nullptr)
-    , help_widget_(nullptr)
-    , about_widget_(nullptr)
-    , menu_hover_widget_(nullptr)
-    , menu_hover_widget_animation_(nullptr)
 {
+    setAttribute(Qt::WA_DeleteOnClose, true);
+
     constructFonts();
     ui->setupUi(this);
 
@@ -87,12 +82,38 @@ MainWindow::MainWindow(QWidget *parent)
     show();
 }
 
-void MainWindow::mousePressEvent(QMouseEvent *event)
+MainWindow::~MainWindow()
 {
-    if (event->button() == Qt::LeftButton) {
-        drag_position_ = event->globalPos() - frameGeometry().topLeft();
-        event->accept();
+    background_update_timer_.stop();
+    delete ui;
+}
+
+void MainWindow::onErrorStatusChanged(AppErrorStatus status)
+{
+    // TODO: Settings widget
+//    status_widget_->onErrorStatusChanged(status);
+}
+
+void MainWindow::updateFontSizes()
+{
+    ui->menuentry_1->setFont(trajan_9pt);
+    ui->menuentry_2->setFont(trajan_9pt);
+    ui->menuentry_4->setFont(trajan_9pt);
+}
+
+void MainWindow::closeEvent(QCloseEvent *event)
+{
+
+    if (show_warning_on_close) {
+        auto result = QMessageBox::question(this, "Подтвердите действие", "Внимание! В настоящий момент выполняются процессы установки/обновления.\n"
+                                                            "Выход из приложения может привести к ошибкам и повреждению файлов игры.\n\n"
+                                                            "Вы уверены, что хотите прервать работу Наследия?");
+        if (result != QMessageBox::Yes) {
+            event->ignore();
+            return;
+        }
     }
+    event->accept();
 }
 
 void MainWindow::mouseMoveEvent(QMouseEvent *event)
@@ -103,6 +124,14 @@ void MainWindow::mouseMoveEvent(QMouseEvent *event)
     }
 }
 
+void MainWindow::mousePressEvent(QMouseEvent *event)
+{
+    if (event->button() == Qt::LeftButton) {
+        drag_position_ = event->globalPos() - frameGeometry().topLeft();
+        event->accept();
+    }
+}
+
 void MainWindow::resizeEvent(QResizeEvent * event)
 {
     if (event->size().width() == -1 || event->size().height() == -1)
@@ -130,6 +159,20 @@ void MainWindow::resizeEvent(QResizeEvent * event)
     updateFontSizes();
 }
 
+bool MainWindow::eventFilter(QObject *, QEvent *event)
+{
+    if (event->type() == QEvent::MouseButtonPress) {
+        mousePressEvent((QMouseEvent*)(event));
+    }
+
+    if (event->type() == QEvent::MouseMove) {
+        mouseMoveEvent((QMouseEvent*)(event));
+    }
+    return false;
+}
+
+
+
 void MainWindow::randomChangeBackground()
 {
     if (!qApp)
@@ -169,11 +212,6 @@ void MainWindow::randomChangeBackground()
     });
 }
 
-MainWindow::~MainWindow()
-{
-    delete ui;
-}
-
 void MainWindow::on_menuentry_1_clicked()
 {
     current_active_entry_ = ui->menuentry_1;
@@ -203,6 +241,16 @@ void MainWindow::onHoverMenuentry(MenuEntry *hovered_entry)
     }
 }
 
+void MainWindow::on_closeButton_clicked()
+{
+    close();
+}
+
+void MainWindow::on_minimizeButton_clicked()
+{
+    setWindowState(Qt::WindowMinimized);
+}
+
 void MainWindow::setupWindowBackgroundAndMask(QPixmap background)
 {
     current_bg_ = background;
@@ -215,6 +263,8 @@ void MainWindow::setupWindowBackgroundAndMask(QPixmap background)
     setPalette(palette);
 }
 
+
+
 void MainWindow::makeConnections()
 {
     connect(&PatchInstaller::instance(), &PatchInstaller::started, this, [this](){show_warning_on_close = true;});
@@ -277,44 +327,6 @@ void MainWindow::hideAllContentWidgets()
     about_widget_->hide();
 }
 
-void MainWindow::updateFontSizes()
-{
-    ui->menuentry_1->setFont(trajan_9pt);
-    ui->menuentry_2->setFont(trajan_9pt);
-    ui->menuentry_4->setFont(trajan_9pt);
-}
-
-void MainWindow::on_closeButton_clicked()
-{
-    close();
-}
-
-void MainWindow::closeEvent(QCloseEvent *event)
-{
-    if (show_warning_on_close) {
-        auto result = QMessageBox::question(this, "Подтвердите действие", "Внимание! В настоящий момент выполняются процессы установки/обновления.\n"
-                                                            "Выход из приложения может привести к ошибкам и повреждению файлов игры.\n\n"
-                                                            "Вы уверены, что хотите прервать работу Наследия?");
-        if (result == QMessageBox::Yes) {
-            qApp->quit();
-        }
-    } else {
-        qApp->quit();
-    }
-    event->ignore();
-}
-
-void MainWindow::onErrorStatusChanged(AppErrorStatus status)
-{
-    // TODO: Settings widget
-    status_widget_->onErrorStatusChanged(status);
-}
-
-void MainWindow::on_minimizeButton_clicked()
-{
-    setWindowState(Qt::WindowMinimized);
-}
-
 void MainWindow::setEventFilterRecursive(QObject *widget)
 {
     if (!widget) {
@@ -327,15 +339,3 @@ void MainWindow::setEventFilterRecursive(QObject *widget)
         setEventFilterRecursive(child);
     }
 }
-
-bool MainWindow::eventFilter(QObject *, QEvent *event)
-{
-    if (event->type() == QEvent::MouseButtonPress) {
-        mousePressEvent((QMouseEvent*)(event));
-    }
-
-    if (event->type() == QEvent::MouseMove) {
-        mouseMoveEvent((QMouseEvent*)(event));
-    }
-    return false;
-}

+ 9 - 12
src/Legacy/widgets/mainwindow.h

@@ -32,10 +32,6 @@ class MainWindow : public QMainWindow
 public:
     explicit MainWindow(QWidget *parent = 0);
     ~MainWindow();
-    int getLastDialogResult();
-
-protected:
-    virtual void closeEvent(QCloseEvent *event) override;
 
 public slots:
     void onErrorStatusChanged(AppErrorStatus status);
@@ -45,6 +41,7 @@ signals:
     void messageDialogFinished(QObject* emitter, int result);
 
 protected:
+    virtual void closeEvent(QCloseEvent *event) override;
     void mouseMoveEvent(QMouseEvent *event) override;
     void mousePressEvent(QMouseEvent *event) override;
     void resizeEvent(QResizeEvent *event) override;
@@ -85,17 +82,17 @@ private:
 private:
     bool show_warning_on_close = false;
 
-    Ui::MainWindow *ui;
+    Ui::MainWindow *ui = nullptr;
 
-    MenuEntry* current_active_entry_;
-    MenuEntry* current_hovered_entry_;
+    MenuEntry* current_active_entry_ = nullptr;
+    MenuEntry* current_hovered_entry_ = nullptr;
 
-    StatusWidget *status_widget_;
-    HelpWidget *help_widget_;
-    AboutWidget *about_widget_;
+    StatusWidget *status_widget_ = nullptr;
+    HelpWidget *help_widget_ = nullptr;
+    AboutWidget *about_widget_ = nullptr;
 
-    QWidget* menu_hover_widget_;
-    QPropertyAnimation* menu_hover_widget_animation_;
+    QWidget* menu_hover_widget_ = nullptr;
+    QPropertyAnimation* menu_hover_widget_animation_ = nullptr;
     QTimer menu_hover_checker_timer_;
 
     QPoint drag_position_;

+ 0 - 2
src/Legacy/widgets/menuentry.cpp

@@ -8,8 +8,6 @@ MenuEntry::MenuEntry(QWidget* parent, Qt::WindowFlags)
     connect(this, &MenuEntry::clicked, this, &MenuEntry::on_clicked);
 }
 
-MenuEntry::~MenuEntry() {}
-
 void MenuEntry::mousePressEvent(QMouseEvent* event) {
     event->ignore();
     emit entryIsPressed(this);

+ 1 - 2
src/Legacy/widgets/menuentry.h

@@ -11,7 +11,6 @@ class MenuEntry : public QLabel {
 
 public:
     explicit MenuEntry(QWidget* parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
-    ~MenuEntry();
 
 signals:
     void clicked();
@@ -19,7 +18,7 @@ signals:
     void entryIsHovered(MenuEntry*);
 
 protected:
-    void mousePressEvent(QMouseEvent* event);
+    void mousePressEvent(QMouseEvent* event) override;
     void mouseMoveEvent(QMouseEvent *event) override;
 
 private slots:

+ 0 - 2
src/Legacy/widgets/progressbar.cpp

@@ -14,8 +14,6 @@ void ProgressBar::resizeEvent(QResizeEvent*) {
     repaint();
 }
 
-#include <QDebug>
-
 void ProgressBar::paintEvent(QPaintEvent *pe)
 {
     double widWidth = width();

+ 18 - 22
src/Legacy/widgets/statuswidget.cpp

@@ -50,24 +50,22 @@ StatusWidget::StatusWidget(QWidget *parent)
 
     ui->b_components_status->hide();
 
-    generateRandomTooltipMessage();
-    random_tooltip_generator_timer_.setInterval(5 * 1000);
-    connect(&random_tooltip_generator_timer_, &QTimer::timeout, this, &StatusWidget::generateRandomTooltipMessage);
-    random_tooltip_generator_timer_.start();
-
-    process_completed_tooltip_hide_timer_.setInterval(4 * 1000); // 4 seconds message "Installation completed" will be shown
-    process_completed_tooltip_hide_timer_.stop();
-    connect(&process_completed_tooltip_hide_timer_, &QTimer::timeout, this, [this](){unsetToolTipMessage(E_PROCESS); process_completed_tooltip_hide_timer_.stop();});
-
     _components_status_opacity_effect = new QGraphicsOpacityEffect();
+    _components_status_opacity_animation = new QPropertyAnimation(_components_status_opacity_effect, "opacity");
+    _components_status_opacity_animation->setStartValue(0);
+    _components_status_opacity_animation->setDuration(1000);
+
     ui->b_components_status->setGraphicsEffect(_components_status_opacity_effect);
 }
 
 StatusWidget::~StatusWidget()
 {
+    _components_status_opacity_animation->stop();
+    _components_status_opacity_animation->deleteLater();
     _components_status_opacity_effect->deleteLater();
+    random_tooltip_generator_timer_.stop();
+    process_completed_tooltip_hide_timer_.stop();
     delete ui;
-    ui = nullptr;
 }
 
 void StatusWidget::updateFontsSizes()
@@ -372,22 +370,20 @@ void StatusWidget::on_check_for_updates_button_clicked()
 }
 
 void StatusWidget::showComponentsStatus() {
-    QPropertyAnimation *animation = new QPropertyAnimation(_components_status_opacity_effect, "opacity");
-    animation->setDuration(1000);
-    animation->setStartValue(0);
-    animation->setEndValue(1);
-    animation->start(QPropertyAnimation::DeleteWhenStopped);
+    _components_status_opacity_animation->disconnect();
     ui->b_components_status->show();
+    _components_status_opacity_animation->stop();
+    _components_status_opacity_animation->setEndValue(1);
+    _components_status_opacity_animation->start();
 }
 
 void StatusWidget::hideComponentsStatus() {
-    QPropertyAnimation *animation = new QPropertyAnimation(_components_status_opacity_effect, "opacity");
-    animation->setDuration(1000);
-    animation->setStartValue(1);
-    animation->setEndValue(0);
-    animation->setEasingCurve(QEasingCurve::InBack);
-    animation->start(QPropertyAnimation::DeleteWhenStopped);
-    connect(animation, &QPropertyAnimation::finished, ui->b_components_status, &QWidget::hide);
+    if (this->isVisible()) {
+        _components_status_opacity_animation->stop();
+        _components_status_opacity_animation->setEndValue(0);
+        _components_status_opacity_animation->start();
+        connect(_components_status_opacity_animation, &QPropertyAnimation::finished, ui->b_components_status, &QWidget::hide);
+    }
 }
 
 void StatusWidget::onTranslationComponentsNeedToPatch() {

+ 1 - 1
src/Legacy/widgets/statuswidget.h

@@ -106,7 +106,7 @@ private:
     QString error_timeout_message_ = "";
 
     QGraphicsOpacityEffect* _components_status_opacity_effect;
-
+    QPropertyAnimation* _components_status_opacity_animation;
     bool _need_to_install_patches = false;
     bool _need_to_update = false;
 };

+ 6 - 6
src/Legacy/widgets/switchbutton.cpp

@@ -89,12 +89,12 @@ SwitchButton::SwitchButton(QWidget* parent, Style style)
 
 SwitchButton::~SwitchButton()
 {
-  delete _circle;
-  delete _background;
-  delete _labeloff;
-  delete _labelon;
-  delete __btn_move;
-  delete __back_move;
+  _circle->deleteLater();
+  _background->deleteLater();
+  _labeloff->deleteLater();
+  _labelon->deleteLater();
+  __btn_move->deleteLater();
+  __back_move->deleteLater();
 }
 
 void SwitchButton::paintEvent(QPaintEvent*)