From 77c37cf8b7277a44b60fb37fc48c1e5aba943aaa Mon Sep 17 00:00:00 2001 From: joern274 Date: Sun, 16 Jun 2024 13:51:03 +0200 Subject: [PATCH] Manage BooleanFunctionEdit fields without memory leak --- .../gatelibrary_pages/bool_wizardpage.h | 5 +++- .../gatelibrary_pages/bool_wizardpage.cpp | 30 ++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plugins/gui/include/gui/gatelibrary_management/gatelibrary_pages/bool_wizardpage.h b/plugins/gui/include/gui/gatelibrary_management/gatelibrary_pages/bool_wizardpage.h index fc5379b2963..615441f1810 100644 --- a/plugins/gui/include/gui/gatelibrary_management/gatelibrary_pages/bool_wizardpage.h +++ b/plugins/gui/include/gui/gatelibrary_management/gatelibrary_pages/bool_wizardpage.h @@ -71,16 +71,19 @@ namespace hal { class BoolWizardPage:public QWizardPage { - friend BooleanFunctionEdit; + Q_OBJECT public: BoolWizardPage(QWidget* parent = nullptr); void initializePage() override; bool isComplete() const override; void setData(GateType* gate); + private Q_SLOTS: + void handleStateChanged(const QString& stat); private: QGridLayout* mLayout; GateLibraryWizard* mWizard; + QList mEditFunctions; GateType* mGate = nullptr; }; } diff --git a/plugins/gui/src/gatelibrary_management/gatelibrary_pages/bool_wizardpage.cpp b/plugins/gui/src/gatelibrary_management/gatelibrary_pages/bool_wizardpage.cpp index 5f8af2b146b..2123f34aed2 100644 --- a/plugins/gui/src/gatelibrary_management/gatelibrary_pages/bool_wizardpage.cpp +++ b/plugins/gui/src/gatelibrary_management/gatelibrary_pages/bool_wizardpage.cpp @@ -58,7 +58,8 @@ namespace hal setState(nextState); } //-------------------------------------------- - BoolWizardPage::BoolWizardPage(QWidget* parent) : QWizardPage(parent) + BoolWizardPage::BoolWizardPage(QWidget* parent) + : QWizardPage(parent) { setTitle("Boolean functions"); setSubTitle("Enter the boolean functions"); @@ -74,6 +75,13 @@ namespace hal for (PinItem* pi : inputPins) input_pins.insert(pi->getName().toStdString()); + if (mEditFunctions.isEmpty()) + { + for (BooleanFunctionEdit* bfe : mEditFunctions) + delete bfe; + mEditFunctions.clear(); + } + if(mGate != nullptr){ auto boolFunctions = mGate->get_boolean_functions(); auto list = QList>(); @@ -85,6 +93,8 @@ namespace hal mLayout->addWidget(label, boolFuncCnt, 0); mLayout->addWidget(lineEdit, boolFuncCnt, 1); lineEdit->setText(QString::fromStdString(bf.second.to_string())); + connect(lineEdit,&BooleanFunctionEdit::stateChanged,this,&BoolWizardPage::handleStateChanged); + mEditFunctions.append(lineEdit); boolFuncCnt++; } } @@ -103,6 +113,8 @@ namespace hal BooleanFunctionEdit* lineEdit = new BooleanFunctionEdit(input_pins, this); mLayout->addWidget(label, rowCount, 0); mLayout->addWidget(lineEdit, rowCount, 1); + connect(lineEdit,&BooleanFunctionEdit::stateChanged,this,&BoolWizardPage::handleStateChanged); + mEditFunctions.append(lineEdit); rowCount++; } } @@ -114,16 +126,21 @@ namespace hal setLayout(mLayout); } - void BoolWizardPage::setData(GateType *gate){ + void BoolWizardPage::setData(GateType *gate) + { mGate = gate; } - bool BoolWizardPage::isComplete() const{ + void BoolWizardPage::handleStateChanged(const QString& stat) + { + Q_UNUSED(stat); + Q_EMIT completeChanged(); + } - for(int i = 0; irowCount(); i++) + bool BoolWizardPage::isComplete() const + { + for(BooleanFunctionEdit* lineEdit : mEditFunctions) { - BooleanFunctionEdit* lineEdit = static_cast(mLayout->itemAtPosition(i, 1)->widget()); - connect(lineEdit, &BooleanFunctionEdit::editingFinished, this, &BoolWizardPage::completeChanged); if(!lineEdit->isValid()) { return false; @@ -131,5 +148,4 @@ namespace hal } return true; } - }