From 6cdfaea1e2c0b5369fca18d70b340b1842ddc5da Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Fri, 22 Dec 2023 11:17:45 +0300 Subject: [PATCH] UI: Refactor menu's widget relayout --- src/ui/FortFirewallUI.pro | 2 ++ src/ui/form/controls/controlutil.cpp | 5 ++++- src/ui/form/controls/menuwidget.cpp | 21 +++++++++++++++++++++ src/ui/form/controls/menuwidget.h | 20 ++++++++++++++++++++ src/ui/form/controls/zonesselector.cpp | 2 -- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/ui/form/controls/menuwidget.cpp create mode 100644 src/ui/form/controls/menuwidget.h diff --git a/src/ui/FortFirewallUI.pro b/src/ui/FortFirewallUI.pro index 9cc377ec..fe64dcbb 100644 --- a/src/ui/FortFirewallUI.pro +++ b/src/ui/FortFirewallUI.pro @@ -47,6 +47,7 @@ SOURCES += \ form/controls/labelspincombo.cpp \ form/controls/listview.cpp \ form/controls/mainwindow.cpp \ + form/controls/menuwidget.cpp \ form/controls/plaintextedit.cpp \ form/controls/sidebutton.cpp \ form/controls/spinbox.cpp \ @@ -242,6 +243,7 @@ HEADERS += \ form/controls/labelspincombo.h \ form/controls/listview.h \ form/controls/mainwindow.h \ + form/controls/menuwidget.h \ form/controls/plaintextedit.h \ form/controls/sidebutton.h \ form/controls/spinbox.h \ diff --git a/src/ui/form/controls/controlutil.cpp b/src/ui/form/controls/controlutil.cpp index 08e72bdf..e8dba0fa 100644 --- a/src/ui/form/controls/controlutil.cpp +++ b/src/ui/form/controls/controlutil.cpp @@ -19,6 +19,7 @@ #include "labeldoublespin.h" #include "labelspin.h" #include "labelspincombo.h" +#include "menuwidget.h" #include "sidebutton.h" #include "spinbox.h" @@ -176,13 +177,15 @@ QMenu *ControlUtil::createMenuByLayout(QBoxLayout *layout, QWidget *parent) { auto menu = createMenu(parent); - auto menuWidget = new QWidget(); + auto menuWidget = new MenuWidget(); menuWidget->setLayout(layout); auto wa = new QWidgetAction(menu); wa->setDefaultWidget(menuWidget); menu->addAction(wa); + menu->connect(menuWidget, &MenuWidget::layoutRequested, [=] { relayoutMenu(menu, wa); }); + return menu; } diff --git a/src/ui/form/controls/menuwidget.cpp b/src/ui/form/controls/menuwidget.cpp new file mode 100644 index 00000000..29751b45 --- /dev/null +++ b/src/ui/form/controls/menuwidget.cpp @@ -0,0 +1,21 @@ +#include "menuwidget.h" + +#include + +MenuWidget::MenuWidget(QWidget *parent) : QWidget(parent) { } + +bool MenuWidget::event(QEvent *event) +{ + const QEvent::Type type = event->type(); + const bool res = QWidget::event(event); + + switch (type) { + case QEvent::LayoutRequest: { + emit layoutRequested(); + } break; + default: + break; + } + + return res; +} diff --git a/src/ui/form/controls/menuwidget.h b/src/ui/form/controls/menuwidget.h new file mode 100644 index 00000000..962059f7 --- /dev/null +++ b/src/ui/form/controls/menuwidget.h @@ -0,0 +1,20 @@ +#ifndef MENUWIDGET_H +#define MENUWIDGET_H + +#include + +class MenuWidget : public QWidget +{ + Q_OBJECT + +public: + explicit MenuWidget(QWidget *parent = nullptr); + +signals: + void layoutRequested(); + +protected: + bool event(QEvent *event) override; +}; + +#endif // MENUWIDGET_H diff --git a/src/ui/form/controls/zonesselector.cpp b/src/ui/form/controls/zonesselector.cpp index 49bd63a3..e5659b9d 100644 --- a/src/ui/form/controls/zonesselector.cpp +++ b/src/ui/form/controls/zonesselector.cpp @@ -158,8 +158,6 @@ void ZonesSelector::createZonesMenu() m_menuLayout->addWidget(cb); } - - ControlUtil::relayoutMenu(m_menuZones); } void ZonesSelector::updateZonesMenu()