From 243b31bb0ee14a15a8c089a07fdd436a91dc662d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 31 Jan 2024 14:46:56 +0300 Subject: [PATCH] UI: Programs: Refactor Edit Dialog handling --- src/ui/form/prog/programalertwindow.cpp | 2 +- src/ui/form/prog/programeditdialog.cpp | 4 ++-- src/ui/form/prog/programeditdialog.h | 3 ++- src/ui/form/prog/programswindow.cpp | 24 +++++++++++++++--------- src/ui/form/prog/programswindow.h | 4 ++-- src/ui/util/window/widgetwindow.cpp | 7 ++++++- src/ui/util/window/widgetwindow.h | 4 +++- 7 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/ui/form/prog/programalertwindow.cpp b/src/ui/form/prog/programalertwindow.cpp index 585a80cf..c150f9ce 100644 --- a/src/ui/form/prog/programalertwindow.cpp +++ b/src/ui/form/prog/programalertwindow.cpp @@ -3,6 +3,6 @@ #include "programscontroller.h" ProgramAlertWindow::ProgramAlertWindow(QWidget *parent) : - ProgramEditDialog(new ProgramsController(this), parent) + ProgramEditDialog(new ProgramsController(this), parent, Qt::Window) { } diff --git a/src/ui/form/prog/programeditdialog.cpp b/src/ui/form/prog/programeditdialog.cpp index a432e24c..a5f4afc3 100644 --- a/src/ui/form/prog/programeditdialog.cpp +++ b/src/ui/form/prog/programeditdialog.cpp @@ -42,8 +42,8 @@ enum ScheduleTimeType : qint8 { ScheduleTimeAt, }; -ProgramEditDialog::ProgramEditDialog(ProgramsController *ctrl, QWidget *parent) : - WidgetWindow(parent), m_ctrl(ctrl) +ProgramEditDialog::ProgramEditDialog(ProgramsController *ctrl, QWidget *parent, Qt::WindowFlags f) : + WidgetWindow(parent, (f == Qt::Widget ? Qt::Dialog : f)), m_ctrl(ctrl) { setupUi(); setupController(); diff --git a/src/ui/form/prog/programeditdialog.h b/src/ui/form/prog/programeditdialog.h index 8f9b98c6..f64b1ea8 100644 --- a/src/ui/form/prog/programeditdialog.h +++ b/src/ui/form/prog/programeditdialog.h @@ -26,7 +26,8 @@ class ProgramEditDialog : public WidgetWindow Q_OBJECT public: - explicit ProgramEditDialog(ProgramsController *ctrl, QWidget *parent = nullptr); + explicit ProgramEditDialog( + ProgramsController *ctrl, QWidget *parent = nullptr, Qt::WindowFlags f = {}); ProgramsController *ctrl() const { return m_ctrl; } FortManager *fortManager() const; diff --git a/src/ui/form/prog/programswindow.cpp b/src/ui/form/prog/programswindow.cpp index 18507428..0a5cb533 100644 --- a/src/ui/form/prog/programswindow.cpp +++ b/src/ui/form/prog/programswindow.cpp @@ -41,7 +41,6 @@ ProgramsWindow::ProgramsWindow(QWidget *parent) : m_stateWatcher(new WidgetWindowStateWatcher(this)) { setupUi(); - setupAppEditForm(); setupController(); setupStateWatcher(); } @@ -382,17 +381,10 @@ void ProgramsWindow::setupTableAppsChanged() connect(m_appListView, &TableView::currentIndexChanged, this, refreshTableAppsChanged); } -void ProgramsWindow::setupAppEditForm() -{ - m_formAppEdit = new ProgramEditDialog(ctrl(), this); -} - bool ProgramsWindow::editProgramByPath(const QString &appPath) { - if (m_formAppEdit->isVisible()) { - m_formAppEdit->activate(); + if (checkAppEditFormOpened()) return false; - } const auto appRow = appListModel()->appRowByPath(appPath); @@ -450,9 +442,23 @@ void ProgramsWindow::editSelectedPrograms() void ProgramsWindow::openAppEditForm(const AppRow &appRow, const QVector &appIdList) { + if (!m_formAppEdit) { + m_formAppEdit = new ProgramEditDialog(ctrl(), this); + } + m_formAppEdit->initialize(appRow, appIdList); m_formAppEdit->activate(); + m_formAppEdit->centerTo(this); +} + +bool ProgramsWindow::checkAppEditFormOpened() const +{ + if (m_formAppEdit && m_formAppEdit->isVisible()) { + m_formAppEdit->activate(); + return true; + } + return false; } void ProgramsWindow::updateSelectedApps(bool blocked, bool killProcess) diff --git a/src/ui/form/prog/programswindow.h b/src/ui/form/prog/programswindow.h index 291cc97e..5c3490b6 100644 --- a/src/ui/form/prog/programswindow.h +++ b/src/ui/form/prog/programswindow.h @@ -69,12 +69,12 @@ private: void setupAppInfoRow(); void setupTableAppsChanged(); - void setupAppEditForm(); - void addNewProgram(); void addNewWildcard(); void editSelectedPrograms(); + void openAppEditForm(const AppRow &appRow, const QVector &appIdList = {}); + bool checkAppEditFormOpened() const; void updateSelectedApps(bool blocked, bool killProcess = false); void deleteSelectedApps(); diff --git a/src/ui/util/window/widgetwindow.cpp b/src/ui/util/window/widgetwindow.cpp index 655b9d62..a6a2b448 100644 --- a/src/ui/util/window/widgetwindow.cpp +++ b/src/ui/util/window/widgetwindow.cpp @@ -2,7 +2,7 @@ #include -WidgetWindow::WidgetWindow(QWidget *parent) : QWidget(parent) { } +WidgetWindow::WidgetWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) { } void WidgetWindow::showWindow(bool activate) { @@ -18,6 +18,11 @@ void WidgetWindow::exposeWindow() exposeWidget(this); } +void WidgetWindow::centerTo(QWidget *w) +{ + this->move(w->frameGeometry().topLeft() + w->rect().center() - this->rect().center()); +} + void WidgetWindow::showWidget(QWidget *w, bool activate) { if (w->isMinimized()) { diff --git a/src/ui/util/window/widgetwindow.h b/src/ui/util/window/widgetwindow.h index abbdadfd..a737a7fb 100644 --- a/src/ui/util/window/widgetwindow.h +++ b/src/ui/util/window/widgetwindow.h @@ -8,7 +8,7 @@ class WidgetWindow : public QWidget Q_OBJECT public: - explicit WidgetWindow(QWidget *parent = nullptr); + explicit WidgetWindow(QWidget *parent = nullptr, Qt::WindowFlags f = {}); virtual quint32 windowCode() const { return 0; } virtual bool deleteOnClose() const { return true; } @@ -19,6 +19,8 @@ public: void showWindow(bool activate = true); void exposeWindow(); + void centerTo(QWidget *w); + static void showWidget(QWidget *w, bool activate = true); static void exposeWidget(QWidget *w);