From 091d46c880b7eedb9681f5adc6faa27b153782d9 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Wed, 28 Feb 2024 17:43:40 +0800 Subject: [PATCH] Close the popup dialog when screen count changed --- DSView/pv/dialogs/dsdialog.cpp | 5 +++ DSView/pv/dialogs/dsdialog.h | 2 +- DSView/pv/dialogs/dsmessagebox.cpp | 5 +++ DSView/pv/dialogs/dsmessagebox.h | 3 +- DSView/pv/interface/icallbacks.h | 10 +++++ DSView/pv/mainframe.cpp | 29 ++++++++++++- DSView/pv/mainframe.h | 12 ++++- DSView/pv/ui/popupdlglist.cpp | 70 ++++++++++++++++++++++++++++++ DSView/pv/ui/popupdlglist.h | 36 +++++++++++++++ DSView/pv/winnativewidget.cpp | 12 +++-- DSView/pv/winnativewidget.h | 13 +++--- 11 files changed, 182 insertions(+), 15 deletions(-) create mode 100644 DSView/pv/ui/popupdlglist.cpp create mode 100644 DSView/pv/ui/popupdlglist.h diff --git a/DSView/pv/dialogs/dsdialog.cpp b/DSView/pv/dialogs/dsdialog.cpp index 433c0393..ec770bc6 100644 --- a/DSView/pv/dialogs/dsdialog.cpp +++ b/DSView/pv/dialogs/dsdialog.cpp @@ -31,6 +31,7 @@ #include "../dsvdef.h" #include "../config/appconfig.h" #include "../ui/fn.h" +#include "../ui/popupdlglist.h" namespace pv { namespace dialogs { @@ -81,6 +82,8 @@ DSDialog::~DSDialog() DESTROY_QT_OBJECT(_titlebar); DESTROY_QT_OBJECT(_shadow); DESTROY_QT_OBJECT(_base_button); + + PopupDlgList::RemoveDlgFromList(this); } void DSDialog::accept() @@ -129,6 +132,8 @@ int DSDialog::exec() } update_font(); + + PopupDlgList::AddDlgTolist(this); return QDialog::exec(); } diff --git a/DSView/pv/dialogs/dsdialog.h b/DSView/pv/dialogs/dsdialog.h index af94c8d6..a1e7b05d 100644 --- a/DSView/pv/dialogs/dsdialog.h +++ b/DSView/pv/dialogs/dsdialog.h @@ -49,7 +49,7 @@ public: DSDialog(QWidget *parent); DSDialog(QWidget *parent, bool hasClose); DSDialog(QWidget *parent, bool hasClose, bool bBaseButton); - ~DSDialog(); + virtual ~DSDialog(); inline void SetCallback(IDlgCallback *callback){m_callback = callback;} inline QVBoxLayout *layout(){return _main_layout;} diff --git a/DSView/pv/dialogs/dsmessagebox.cpp b/DSView/pv/dialogs/dsmessagebox.cpp index 2c436adb..09bf4384 100644 --- a/DSView/pv/dialogs/dsmessagebox.cpp +++ b/DSView/pv/dialogs/dsmessagebox.cpp @@ -32,6 +32,7 @@ #include "../ui/langresource.h" #include "../config/appconfig.h" #include "../ui/fn.h" +#include "../ui/popupdlglist.h" namespace pv { namespace dialogs { @@ -95,6 +96,8 @@ DSMessageBox::~DSMessageBox() DESTROY_QT_OBJECT(_titlebar); DESTROY_QT_OBJECT(_shadow); DESTROY_QT_OBJECT(_main_layout); + + PopupDlgList::RemoveDlgFromList(this); } void DSMessageBox::accept() @@ -138,6 +141,8 @@ int DSMessageBox::exec() _titlebar->update_font(); } + PopupDlgList::AddDlgTolist(this); + return QDialog::exec(); } diff --git a/DSView/pv/dialogs/dsmessagebox.h b/DSView/pv/dialogs/dsmessagebox.h index 254dee38..78f952c4 100644 --- a/DSView/pv/dialogs/dsmessagebox.h +++ b/DSView/pv/dialogs/dsmessagebox.h @@ -27,7 +27,6 @@ #include #include #include - #include "../toolbars/titlebar.h" namespace pv { @@ -42,7 +41,7 @@ class DSMessageBox : public QDialog public: DSMessageBox(QWidget *parent, const QString title=0); - ~DSMessageBox(); + virtual ~DSMessageBox(); QMessageBox *mBox(); diff --git a/DSView/pv/interface/icallbacks.h b/DSView/pv/interface/icallbacks.h index f93ac7e2..f8a9b6e5 100644 --- a/DSView/pv/interface/icallbacks.h +++ b/DSView/pv/interface/icallbacks.h @@ -115,4 +115,14 @@ public: virtual void update_font()=0; }; +enum ParentNativeEvent{ + EV_SCREEN_DPI_CHANGED = 0, +}; + +class IParentNativeEventCallback +{ +public: + virtual void OnParentNativeEvent(ParentNativeEvent msg)=0; +}; + #endif diff --git a/DSView/pv/mainframe.cpp b/DSView/pv/mainframe.cpp index f07ccf4b..ce45ca56 100644 --- a/DSView/pv/mainframe.cpp +++ b/DSView/pv/mainframe.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include @@ -55,7 +56,9 @@ #include "appcontrol.h" #include "ui/langresource.h" #include "log.h" -#include +#include "dialogs/dsdialog.h" +#include "ui/popupdlglist.h" + #ifdef _WIN32 #include "winnativewidget.h" @@ -170,6 +173,8 @@ MainFrame::MainFrame() installEventFilter(this); + connect(this, SIGNAL(sig_ParentNativeEvent(int)), this, SLOT(OnParentNaitveWindowEvent(int))); + //PrintRegionProc(); } @@ -218,6 +223,7 @@ void MainFrame::AttachNativeWindow() _parentNativeWidget = new WinNativeWidget(x, y, w, h); _parentNativeWidget->setGeometry(x, y, w, h); _parentNativeWidget->SetChildWidget(this); + _parentNativeWidget->SetNativeEventCallback(this); if (_parentNativeWidget->Handle()) { @@ -311,6 +317,27 @@ void MainFrame::MoveEnd() } #endif } + +void MainFrame::OnParentNativeEvent(ParentNativeEvent msg) +{ + sig_ParentNativeEvent((int)msg); +} + +void MainFrame::OnParentNaitveWindowEvent(int msg) +{ +#ifdef _WIN32 + if (_parentNativeWidget != NULL){ + if (msg == EV_SCREEN_DPI_CHANGED){ + QTimer::singleShot(100, this, [this](){ + _parentNativeWidget->UpdateChildDpi(); + _parentNativeWidget->ResizeChild(); + + PopupDlgList::TryCloseAllByScreenChanged(); + }); + } + } +#endif +} void MainFrame::resizeEvent(QResizeEvent *event) { diff --git a/DSView/pv/mainframe.h b/DSView/pv/mainframe.h index 1f2066d3..67aa0b02 100644 --- a/DSView/pv/mainframe.h +++ b/DSView/pv/mainframe.h @@ -66,7 +66,10 @@ struct FormInitInfo bool isMaxSize; }; -class MainFrame : public QFrame, public ITitleParent +class MainFrame : + public QFrame, + public ITitleParent, + public IParentNativeEventCallback { Q_OBJECT @@ -104,6 +107,9 @@ protected: void changeEvent(QEvent *event) override; +signals: + void sig_ParentNativeEvent(int msg); + public slots: void unfreezing(); void show_doc(); @@ -113,6 +119,7 @@ public slots: void showMaximized(); void showMinimized(); void moveToWinNaitiveNormal(); + void OnParentNaitveWindowEvent(int msg); private: void hide_border(); @@ -130,6 +137,9 @@ private: void MoveBegin() override; void MoveEnd() override; + //IParentNativeEventCallback + void OnParentNativeEvent(ParentNativeEvent msg) override; + void SetFormRegion(int x, int y, int w, int h); QRect GetFormRegion(); diff --git a/DSView/pv/ui/popupdlglist.cpp b/DSView/pv/ui/popupdlglist.cpp new file mode 100644 index 00000000..5779b6fd --- /dev/null +++ b/DSView/pv/ui/popupdlglist.cpp @@ -0,0 +1,70 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2024 DreamSourceLab + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "popupdlglist.h" +#include + +namespace{ + std::vector g_popup_dlg_list; + int g_screen_count = 1; +} + +void PopupDlgList::AddDlgTolist(QWidget *w) +{ + if (w != nullptr){ + g_popup_dlg_list.push_back(w); + } + g_screen_count = QGuiApplication::screens().size(); +} + +void PopupDlgList::RemoveDlgFromList(QWidget *w) +{ + if (w != nullptr){ + for (auto it = g_popup_dlg_list.begin(); it != g_popup_dlg_list.end(); ++it) + { + if ((*it) == w){ + g_popup_dlg_list.erase(it); + break; + } + } + } +} + +void PopupDlgList::TryCloseAllByScreenChanged() +{ + int screen_count = QGuiApplication::screens().size(); + + if (screen_count != g_screen_count) + { + int num = g_popup_dlg_list.size(); + + for (int i=0; iisVisible()){ + (*it)->close(); //Close the dialog. + g_popup_dlg_list.erase(it); + --num; + --i; + } + } + } +} \ No newline at end of file diff --git a/DSView/pv/ui/popupdlglist.h b/DSView/pv/ui/popupdlglist.h new file mode 100644 index 00000000..33cf390e --- /dev/null +++ b/DSView/pv/ui/popupdlglist.h @@ -0,0 +1,36 @@ +/* + * This file is part of the DSView project. + * DSView is based on PulseView. + * + * Copyright (C) 2024 DreamSourceLab + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef POPUP_DLG_LIST_H +#define POPUP_DLG_LIST_H + +#include +#include + +class PopupDlgList +{ +public: + static void AddDlgTolist(QWidget *w); + static void RemoveDlgFromList(QWidget *w); + static void TryCloseAllByScreenChanged(); +}; + +#endif \ No newline at end of file diff --git a/DSView/pv/winnativewidget.cpp b/DSView/pv/winnativewidget.cpp index 4b49b70e..b4591adc 100644 --- a/DSView/pv/winnativewidget.cpp +++ b/DSView/pv/winnativewidget.cpp @@ -156,9 +156,15 @@ LRESULT CALLBACK WinNativeWidget::WndProc(HWND hWnd, UINT message, WPARAM wParam break; } case WM_DPICHANGED: - if (self->_is_moving == false){ - self->UpdateChildDpi(); - self->ResizeChild(); + if (self->_is_moving == false){ + if (self->_event_callback != NULL){ + self->_event_callback->OnParentNativeEvent(EV_SCREEN_DPI_CHANGED); + } + else{ + self->UpdateChildDpi(); + self->ResizeChild(); + } + dsv_info("Dpi was changed."); } break; diff --git a/DSView/pv/winnativewidget.h b/DSView/pv/winnativewidget.h index cb3c13e8..595e8c08 100644 --- a/DSView/pv/winnativewidget.h +++ b/DSView/pv/winnativewidget.h @@ -28,6 +28,7 @@ #include #include #include +#include "interface/icallbacks.h" #ifndef WM_DPICHANGED #define WM_DPICHANGED 0x02E0 @@ -35,12 +36,6 @@ namespace pv { -class IParentNaitveEventCallback -{ -public: - virtual void OnParentNativeEvent(int msg)=0; -}; - class WinNativeWidget { public: @@ -74,6 +69,10 @@ public: QScreen* GetPointScreen(); + inline void SetNativeEventCallback(IParentNativeEventCallback *callback){ + _event_callback = callback; + } + private: QScreen* screenFromWindow(HWND hwnd); @@ -81,7 +80,7 @@ private: QWidget* childWidget; HWND _childWindow; HWND _hWnd; - IParentNaitveEventCallback *_event_callback; + IParentNativeEventCallback *_event_callback; bool _is_moving; };