From 2c2ddc9d6150a04cdb67e7a61b7f17f6af0fe55d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 16 Dec 2023 09:41:03 +0300 Subject: [PATCH] UI AddressesPage: Simplify setupIn/ExcludeAddresses() --- src/ui/form/opt/pages/addressespage.cpp | 75 ++++++++++++------------- src/ui/form/opt/pages/addressespage.h | 6 +- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/ui/form/opt/pages/addressespage.cpp b/src/ui/form/opt/pages/addressespage.cpp index 340785d6..bfbc2379 100644 --- a/src/ui/form/opt/pages/addressespage.cpp +++ b/src/ui/form/opt/pages/addressespage.cpp @@ -32,8 +32,8 @@ AddressesPage::AddressesPage(OptionsController *ctrl, QWidget *parent) : OptBase { setupUi(); - setupAddressGroup(); setupZones(); + setupAddressGroup(); } AddressGroup *AddressesPage::addressGroup() const @@ -103,8 +103,7 @@ void AddressesPage::setupUi() m_tabBar->addTab(IconCache::icon(":/icons/ip_block.png"), QString()); // Address Columns - setupIncludeAddresses(); - setupExcludeAddresses(); + setupAddressColumns(); setupAddressesUseAllEnabled(); @@ -118,40 +117,38 @@ void AddressesPage::setupUi() this->setLayout(layout); } -void AddressesPage::setupIncludeAddresses() +void AddressesPage::setupAddressColumns() { - m_includeAddresses = new AddressesColumn(); - - connect(m_includeAddresses->cbUseAll(), &QCheckBox::toggled, this, [&](bool checked) { - addressGroup()->setIncludeAll(checked); - - checkAddressGroupEdited(); - }); - connect(m_includeAddresses->editIpText(), &QPlainTextEdit::textChanged, this, [&] { - const auto ipText = m_includeAddresses->editIpText()->toPlainText(); - - addressGroup()->setIncludeText(ipText); - - checkAddressGroupEdited(); - }); + m_includeAddresses = setupAddressColumn(/*include=*/true); + m_excludeAddresses = setupAddressColumn(/*include=*/false); } -void AddressesPage::setupExcludeAddresses() +AddressesColumn *AddressesPage::setupAddressColumn(bool include) { - m_excludeAddresses = new AddressesColumn(); + auto addressesColumn = new AddressesColumn(); - connect(m_excludeAddresses->cbUseAll(), &QCheckBox::toggled, this, [&](bool checked) { - addressGroup()->setExcludeAll(checked); + connect(addressesColumn->cbUseAll(), &QCheckBox::toggled, this, [=, this](bool checked) { + if (include) { + addressGroup()->setIncludeAll(checked); + } else { + addressGroup()->setExcludeAll(checked); + } checkAddressGroupEdited(); }); - connect(m_excludeAddresses->editIpText(), &QPlainTextEdit::textChanged, this, [&] { - const auto ipText = m_excludeAddresses->editIpText()->toPlainText(); + connect(addressesColumn->editIpText(), &QPlainTextEdit::textChanged, this, [=, this] { + const auto ipText = addressesColumn->editIpText()->toPlainText(); - addressGroup()->setExcludeText(ipText); + if (include) { + addressGroup()->setIncludeText(ipText); + } else { + addressGroup()->setExcludeText(ipText); + } checkAddressGroupEdited(); }); + + return addressesColumn; } void AddressesPage::setupAddressesUseAllEnabled() @@ -200,20 +197,6 @@ void AddressesPage::setupSplitterButtons() layout->addWidget(m_btAddLocals, 0, Qt::AlignHCenter); } -void AddressesPage::setupAddressGroup() -{ - connect(this, &AddressesPage::addressGroupChanged, this, &AddressesPage::updateGroup); - - const auto refreshAddressGroup = [&] { - const int tabIndex = m_tabBar->currentIndex(); - setAddressGroupIndex(tabIndex); - }; - - refreshAddressGroup(); - - connect(m_tabBar, &QTabBar::currentChanged, this, refreshAddressGroup); -} - void AddressesPage::setupZones() { connect(m_includeAddresses->btSelectZones(), &ZonesSelector::zonesChanged, this, [&] { @@ -233,6 +216,20 @@ void AddressesPage::setupZones() }); } +void AddressesPage::setupAddressGroup() +{ + connect(this, &AddressesPage::addressGroupChanged, this, &AddressesPage::updateGroup); + + const auto refreshAddressGroup = [&] { + const int tabIndex = m_tabBar->currentIndex(); + setAddressGroupIndex(tabIndex); + }; + + refreshAddressGroup(); + + connect(m_tabBar, &QTabBar::currentChanged, this, refreshAddressGroup); +} + void AddressesPage::updateGroup() { m_includeAddresses->cbUseAll()->setChecked(addressGroup()->includeAll()); diff --git a/src/ui/form/opt/pages/addressespage.h b/src/ui/form/opt/pages/addressespage.h index 8cbd738d..e71da37f 100644 --- a/src/ui/form/opt/pages/addressespage.h +++ b/src/ui/form/opt/pages/addressespage.h @@ -33,14 +33,14 @@ private: void retranslateAddressesPlaceholderText(); void setupUi(); - void setupIncludeAddresses(); - void setupExcludeAddresses(); + void setupAddressColumns(); + AddressesColumn *setupAddressColumn(bool include); void setupAddressesUseAllEnabled(); void setupSplitter(); void setupSplitterButtons(); - void setupAddressGroup(); void setupZones(); + void setupAddressGroup(); void updateGroup();