From afbe8a8ee63629a57cfeefbba8964953d9f9afd3 Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Mon, 23 Oct 2023 20:32:01 +0200 Subject: [PATCH] start replacing raw to smart pointers for mupp. --- src/musredit_qt6/mupp/PmuppGui.cpp | 163 ++++++++++++-------------- src/musredit_qt6/mupp/PmuppGui.h | 60 +++++----- src/musredit_qt6/mupp/PmuppScript.cpp | 23 +--- src/musredit_qt6/mupp/PmuppScript.h | 7 +- 4 files changed, 115 insertions(+), 138 deletions(-) diff --git a/src/musredit_qt6/mupp/PmuppGui.cpp b/src/musredit_qt6/mupp/PmuppGui.cpp index e3dff16a..9e89e049 100644 --- a/src/musredit_qt6/mupp/PmuppGui.cpp +++ b/src/musredit_qt6/mupp/PmuppGui.cpp @@ -242,13 +242,8 @@ PmuppGui::PmuppGui(QStringList fln) fDatime = dt.toSecsSinceEpoch(); fMuppInstance = -1; - fMuppPlot = nullptr; fNormalizeAction = nullptr; - fNormalize = false; - - fVarDlg = nullptr; - fMacroPath = QString("./"); fMacroName = QString(""); @@ -287,135 +282,134 @@ PmuppGui::PmuppGui(QStringList fln) setupHelpActions(); // create central widget - fCentralWidget = new QWidget(this); + fCentralWidget = std::make_unique(this); // setup widgets - fBoxLayout_Main = new QBoxLayout(QBoxLayout::TopToBottom); + fBoxLayout_Main = std::make_unique(QBoxLayout::TopToBottom); fBoxLayout_Main->setGeometry(QRect(10, 40, 600, 500)); - fBoxLayout_Top = new QBoxLayout(QBoxLayout::LeftToRight); - fGridLayout_Left = new QGridLayout(); - fGridLayout_Right = new QGridLayout(); - fBoxLayout_Cmd = new QBoxLayout(QBoxLayout::LeftToRight); + fBoxLayout_Top = std::make_unique(QBoxLayout::LeftToRight); + fGridLayout_Left = std::make_unique(); + fGridLayout_Right = std::make_unique(); // label for the collection/parameter list widgets - fColLabel = new QLabel(this); + fColLabel = std::make_unique(this); fColLabel->setText("Collection -> Parameter"); fColLabel->setMaximumHeight(15); // central widget for the collection/parameter list widgets - fColParamSplitter = new QSplitter(Qt::Horizontal, this); + fColParamSplitter = std::make_unique(Qt::Horizontal, this); fColParamSplitter->setMinimumSize(550,330); - fColList = new QListWidget(this); + fColList = std::make_unique(this); fColList->setSelectionMode(QAbstractItemView::ExtendedSelection); - fParamList = new QListWidget(this); + fParamList = std::make_unique(this); // the next two lines enable drag and drop facility (drag) fParamList->setSelectionMode(QAbstractItemView::SingleSelection); fParamList->setDragEnabled(true); - fColParamSplitter->addWidget(fColList); - fColParamSplitter->addWidget(fParamList); + fColParamSplitter->addWidget(fColList.get()); + fColParamSplitter->addWidget(fParamList.get()); - fRemoveCollection = new QPushButton("Remove Collection", this); - fRefreshCollection = new QPushButton("Refresh Collection", this); + fRemoveCollection = std::make_unique("Remove Collection", this); + fRefreshCollection = std::make_unique("Refresh Collection", this); - fGridLayout_Left->addWidget(fColLabel, 1, 1, 1, 2); - fGridLayout_Left->addWidget(fColParamSplitter, 2, 1, 1, 2); - fGridLayout_Left->addWidget(fRemoveCollection, 3, 1); - fGridLayout_Left->addWidget(fRefreshCollection, 3, 2); + fGridLayout_Left->addWidget(fColLabel.get(), 1, 1, 1, 2); + fGridLayout_Left->addWidget(fColParamSplitter.get(), 2, 1, 1, 2); + fGridLayout_Left->addWidget(fRemoveCollection.get(), 3, 1); + fGridLayout_Left->addWidget(fRefreshCollection.get(), 3, 2); // X-axis - fXaxisLabel = new QLabel("x-axis"); - fViewX = new QListWidget(this); + fXaxisLabel = std::make_unique("x-axis"); + fViewX = std::make_unique(this); // the next two lines enable drag and drop facility (copy drop) fViewX->viewport()->setAcceptDrops(true); fViewX->setDropIndicatorShown(true); - fAddX = new QPushButton("add X", this); - fRemoveX = new QPushButton("remove X", this); + fAddX = std::make_unique("add X", this); + fRemoveX = std::make_unique("remove X", this); // Y-axis - fYaxisLabel = new QLabel("y-axis"); - fViewY = new QListWidget(this); + fYaxisLabel = std::make_unique("y-axis"); + fViewY = std::make_unique(this); // the next two lines enable drag and drop facility (copy drop) fViewY->viewport()->setAcceptDrops(true); fViewY->setDropIndicatorShown(true); - fAddY = new QPushButton("add Y", this); - fRemoveY = new QPushButton("remove Y", this); + fAddY = std::make_unique("add Y", this); + fRemoveY = std::make_unique("remove Y", this); // 2 column button's for the right grid - fAddDitto = new QPushButton("Add Ditto", this); - fPlot = new QPushButton("Plot", this); + fAddDitto = std::make_unique("Add Ditto", this); + fPlot = std::make_unique("Plot", this); - fGridLayout_Right->addWidget(fXaxisLabel, 1, 1); - fGridLayout_Right->addWidget(fYaxisLabel, 1, 2); - fGridLayout_Right->addWidget(fViewX, 2, 1); - fGridLayout_Right->addWidget(fViewY, 2, 2); - fGridLayout_Right->addWidget(fAddX, 3, 1); - fGridLayout_Right->addWidget(fAddY, 3, 2); - fGridLayout_Right->addWidget(fRemoveX, 4, 1); - fGridLayout_Right->addWidget(fRemoveY, 4, 2); - fGridLayout_Right->addWidget(fAddDitto, 5, 1, 1, 2); - fGridLayout_Right->addWidget(fPlot, 6, 1, 1, 2); + fGridLayout_Right->addWidget(fXaxisLabel.get(), 1, 1); + fGridLayout_Right->addWidget(fYaxisLabel.get(), 1, 2); + fGridLayout_Right->addWidget(fViewX.get(), 2, 1); + fGridLayout_Right->addWidget(fViewY.get(), 2, 2); + fGridLayout_Right->addWidget(fAddX.get(), 3, 1); + fGridLayout_Right->addWidget(fAddY.get(), 3, 2); + fGridLayout_Right->addWidget(fRemoveX.get(), 4, 1); + fGridLayout_Right->addWidget(fRemoveY.get(), 4, 2); + fGridLayout_Right->addWidget(fAddDitto.get(), 5, 1, 1, 2); + fGridLayout_Right->addWidget(fPlot.get(), 6, 1, 1, 2); - fBoxLayout_Top->addLayout(fGridLayout_Left); - fBoxLayout_Top->addLayout(fGridLayout_Right); + fBoxLayout_Top->addLayout(fGridLayout_Left.get()); + fBoxLayout_Top->addLayout(fGridLayout_Right.get()); - fCmdLineHistory = new QPlainTextEdit(this); + fCmdLineHistory = std::make_unique(this); fCmdLineHistory->setReadOnly(true); fCmdLineHistory->setMaximumBlockCount(50); // fill cmd history for (int i=0; iinsertPlainText(QString("%1\n").arg(fCmdHistory[i])); - fCmdLine = new QLineEdit(this); + fCmdLine = std::make_unique(this); fCmdLine->installEventFilter(this); fCmdLine->setText("$ "); fCmdLine->setFocus(); // sets the keyboard focus - fExitButton = new QPushButton("E&xit", this); + fExitButton = std::make_unique("E&xit", this); QVBoxLayout *cmdLayout = new QVBoxLayout; QLabel *cmdLabel = new QLabel("History:"); cmdLayout->addWidget(cmdLabel); - cmdLayout->addWidget(fCmdLineHistory); - cmdLayout->addWidget(fCmdLine); + cmdLayout->addWidget(fCmdLineHistory.get()); + cmdLayout->addWidget(fCmdLine.get()); - fBoxLayout_Cmd = new QBoxLayout(QBoxLayout::LeftToRight); + fBoxLayout_Cmd = std::make_unique(QBoxLayout::LeftToRight); fBoxLayout_Cmd->addLayout(cmdLayout); - fBoxLayout_Cmd->addWidget(fExitButton); - fBoxLayout_Cmd->setAlignment(fCmdLine, Qt::AlignBottom); - fBoxLayout_Cmd->setAlignment(fExitButton, Qt::AlignBottom); + fBoxLayout_Cmd->addWidget(fExitButton.get()); + fBoxLayout_Cmd->setAlignment(fCmdLine.get(), Qt::AlignBottom); + fBoxLayout_Cmd->setAlignment(fExitButton.get(), Qt::AlignBottom); - fCmdSplitter = new QSplitter(Qt::Vertical, this); + fCmdSplitter = std::make_unique(Qt::Vertical, this); QWidget *topWidget = new QWidget(this); // only needed since splitter needs a QWidget - topWidget->setLayout(fBoxLayout_Top); + topWidget->setLayout(fBoxLayout_Top.get()); QWidget *cmdWidget = new QWidget(this); // only needed since splitter needs a QWidget - cmdWidget->setLayout(fBoxLayout_Cmd); + cmdWidget->setLayout(fBoxLayout_Cmd.get()); fCmdSplitter->addWidget(topWidget); fCmdSplitter->addWidget(cmdWidget); - fBoxLayout_Main->addWidget(fCmdSplitter); + fBoxLayout_Main->addWidget(fCmdSplitter.get()); // establish all the necessary signal/slots - connect(fRefreshCollection, SIGNAL( pressed() ), this, SLOT( refresh() )); - connect(fRemoveCollection, SIGNAL( pressed() ), this, SLOT( remove() )); - connect(fAddX, SIGNAL( pressed() ), this, SLOT( addX() )); - connect(fAddY, SIGNAL( pressed() ), this, SLOT( addY() )); - connect(fRemoveX, SIGNAL( pressed() ), this, SLOT( removeX() )); - connect(fRemoveY, SIGNAL( pressed() ), this, SLOT( removeY() )); - connect(fAddDitto, SIGNAL( pressed() ), this, SLOT( addDitto() )); - connect(fPlot, SIGNAL( pressed()), this, SLOT( plot()) ); - connect(fCmdLine, SIGNAL ( returnPressed() ), this, SLOT( handleCmds() )); - connect(fExitButton, SIGNAL( pressed() ), this, SLOT( aboutToQuit() )); + connect(fRefreshCollection.get(), SIGNAL( pressed() ), this, SLOT( refresh() )); + connect(fRemoveCollection.get(), SIGNAL( pressed() ), this, SLOT( remove() )); + connect(fAddX.get(), SIGNAL( pressed() ), this, SLOT( addX() )); + connect(fAddY.get(), SIGNAL( pressed() ), this, SLOT( addY() )); + connect(fRemoveX.get(), SIGNAL( pressed() ), this, SLOT( removeX() )); + connect(fRemoveY.get(), SIGNAL( pressed() ), this, SLOT( removeY() )); + connect(fAddDitto.get(), SIGNAL( pressed() ), this, SLOT( addDitto() )); + connect(fPlot.get(), SIGNAL( pressed()), this, SLOT( plot()) ); + connect(fCmdLine.get(), SIGNAL ( returnPressed() ), this, SLOT( handleCmds() )); + connect(fExitButton.get(), SIGNAL( pressed() ), this, SLOT( aboutToQuit() )); - connect(fColList, SIGNAL( currentRowChanged(int) ), this, SLOT( updateParamList(int) )); - connect(fColList, SIGNAL( itemDoubleClicked(QListWidgetItem*) ), this, SLOT( editCollName(QListWidgetItem*) )); + connect(fColList.get(), SIGNAL( currentRowChanged(int) ), this, SLOT( updateParamList(int) )); + connect(fColList.get(), SIGNAL( itemDoubleClicked(QListWidgetItem*) ), this, SLOT( editCollName(QListWidgetItem*) )); - connect(fViewX, SIGNAL( currentRowChanged(int) ), this, SLOT( refreshY() )); - connect(fViewX, SIGNAL( itemChanged(QListWidgetItem*) ), this, SLOT( dropOnViewX(QListWidgetItem*) )); - connect(fViewY, SIGNAL( itemChanged(QListWidgetItem*) ), this, SLOT( dropOnViewY(QListWidgetItem*) )); + connect(fViewX.get(), SIGNAL( currentRowChanged(int) ), this, SLOT( refreshY() )); + connect(fViewX.get(), SIGNAL( itemChanged(QListWidgetItem*) ), this, SLOT( dropOnViewX(QListWidgetItem*) )); + connect(fViewY.get(), SIGNAL( itemChanged(QListWidgetItem*) ), this, SLOT( dropOnViewY(QListWidgetItem*) )); // deal with parameter data specifics if (dataAtStartup) @@ -423,8 +417,8 @@ PmuppGui::PmuppGui(QStringList fln) connect(fParamDataHandler, SIGNAL( newData() ), this, SLOT( handleNewData() )); - fCentralWidget->setLayout(fBoxLayout_Main); - setCentralWidget(fCentralWidget); + fCentralWidget->setLayout(fBoxLayout_Main.get()); + setCentralWidget(fCentralWidget.get()); // in case there is no db/dat file list given open the db/dat file open menu automatically. if (fln.size() == 0) { @@ -593,11 +587,11 @@ void PmuppGui::setupToolActions() menu->addSeparator(); - fNormalizeAction = new QAction(tr( "Normalize" ), this); + fNormalizeAction = std::make_unique(tr( "Normalize" ), this); fNormalizeAction->setStatusTip( tr("Plot Data Normalized (y-axis)") ); fNormalizeAction->setCheckable(true); - connect( fNormalizeAction, SIGNAL( changed() ), this, SLOT( normalize() ) ); - menu->addAction(fNormalizeAction); + connect( fNormalizeAction.get(), SIGNAL( changed() ), this, SLOT( normalize() ) ); + menu->addAction(fNormalizeAction.get()); } //----------------------------------------------------------------------------- @@ -1308,13 +1302,10 @@ void PmuppGui::addVar() } // call variable dialog - if (fVarDlg != nullptr) { - delete fVarDlg; - fVarDlg = nullptr; - } - fVarDlg = new PVarDialog(collection_list, fDarkTheme); - connect(fVarDlg, SIGNAL(check_request(QString,QVector)), this, SLOT(check(QString,QVector))); - connect(fVarDlg, SIGNAL(add_request(QString,QVector)), this, SLOT(add(QString,QVector))); + fVarDlg.reset(); + fVarDlg = std::make_unique(collection_list, fDarkTheme); + connect(fVarDlg.get(), SIGNAL(check_request(QString,QVector)), this, SLOT(check(QString,QVector))); + connect(fVarDlg.get(), SIGNAL(add_request(QString,QVector)), this, SLOT(add(QString,QVector))); fVarDlg->show(); } @@ -2235,7 +2226,7 @@ void PmuppGui::startMuppPlot() // feed the mupp instance arg << QString("%1").arg(fMuppInstance); - fMuppPlot = new QProcess(this); + fMuppPlot = std::make_unique(this); if (fMuppPlot == nullptr) { QMessageBox::critical(this, "ERROR", "Couldn't invoke QProcess for mupp_plot!"); return; diff --git a/src/musredit_qt6/mupp/PmuppGui.h b/src/musredit_qt6/mupp/PmuppGui.h index b4931f93..3c895655 100644 --- a/src/musredit_qt6/mupp/PmuppGui.h +++ b/src/musredit_qt6/mupp/PmuppGui.h @@ -30,6 +30,8 @@ #ifndef _PMUPPGUI_H_ #define _PMUPPGUI_H_ +#include + #include #include #include @@ -171,44 +173,44 @@ private: QString fMacroPath; QString fMacroName; - QWidget *fCentralWidget; + std::unique_ptr fCentralWidget; QMenu *fRecentFilesMenu; ///< recent file menu QAction *fRecentFilesAction[MAX_RECENT_FILES]; ///< array of the recent file actions - QAction *fNormalizeAction; + std::unique_ptr fNormalizeAction; - QBoxLayout *fBoxLayout_Main; // top->bottom (0) - QBoxLayout *fBoxLayout_Top; // left->right (1) - QGridLayout *fGridLayout_Left; // 2 columns, 3 rows - QGridLayout *fGridLayout_Right; // 2 columns, 6 rows - QBoxLayout *fBoxLayout_Cmd; // left->right (1) + std::unique_ptr fBoxLayout_Main; // top->bottom (0) + std::unique_ptr fBoxLayout_Top; // left->right (1) + std::unique_ptr fGridLayout_Left; // 2 columns, 3 rows + std::unique_ptr fGridLayout_Right; // 2 columns, 6 rows + std::unique_ptr fBoxLayout_Cmd; // left->right (1) - QLabel *fColLabel; - QSplitter *fColParamSplitter; - QListWidget *fColList; - QListWidget *fParamList; - QPushButton *fRemoveCollection; - QPushButton *fRefreshCollection; - QLabel *fXaxisLabel; - QLabel *fYaxisLabel; - QListWidget *fViewX; - QListWidget *fViewY; - QPushButton *fAddX; - QPushButton *fAddY; - QPushButton *fAddDitto; - QPushButton *fRemoveX; - QPushButton *fRemoveY; - QPushButton *fPlot; - QSplitter *fCmdSplitter; - QPlainTextEdit *fCmdLineHistory; - QLineEdit *fCmdLine; - QPushButton *fExitButton; + std::unique_ptr fColLabel; + std::unique_ptr fColParamSplitter; + std::unique_ptr fColList; + std::unique_ptr fParamList; + std::unique_ptr fRemoveCollection; + std::unique_ptr fRefreshCollection; + std::unique_ptr fXaxisLabel; + std::unique_ptr fYaxisLabel; + std::unique_ptr fViewX; + std::unique_ptr fViewY; + std::unique_ptr fAddX; + std::unique_ptr fAddY; + std::unique_ptr fAddDitto; + std::unique_ptr fRemoveX; + std::unique_ptr fRemoveY; + std::unique_ptr fPlot; + std::unique_ptr fCmdSplitter; + std::unique_ptr fCmdLineHistory; + std::unique_ptr fCmdLine; + std::unique_ptr fExitButton; QVector fCmdHistory; ///< command history buffer - PVarDialog *fVarDlg; ///< variable dialog + std::unique_ptr fVarDlg; ///< variable dialog - QProcess *fMuppPlot; ///< mupp plotter + std::unique_ptr fMuppPlot; ///< mupp plotter void setupFileActions(); void setupToolActions(); diff --git a/src/musredit_qt6/mupp/PmuppScript.cpp b/src/musredit_qt6/mupp/PmuppScript.cpp index 903d8d76..27418a0b 100644 --- a/src/musredit_qt6/mupp/PmuppScript.cpp +++ b/src/musredit_qt6/mupp/PmuppScript.cpp @@ -49,31 +49,14 @@ PmuppScript::PmuppScript(QStringList script) : fScript(script) { - fParamDataHandler = 0; fLoadPath = QString("./"); fSavePath = QString("./"); fSelected = -2; // nothing selected fNorm = false; - fAdmin = new PmuppAdmin(); + fAdmin = std::make_unique(); } -//-------------------------------------------------------------------------- -/** - * @brief PmuppScript::~PmuppScript. Dtor - */ -PmuppScript::~PmuppScript() -{ - if (fParamDataHandler) { - delete fParamDataHandler; - fParamDataHandler = 0; - } - - if (fAdmin) { - delete fAdmin; - fAdmin = 0; - } -} //-------------------------------------------------------------------------- /** * @brief PmuppScript::executeScript. Handles the script commands. @@ -81,8 +64,8 @@ PmuppScript::~PmuppScript() */ int PmuppScript::executeScript() { - fParamDataHandler = new PParamDataHandler(); - if (fParamDataHandler == 0) { + fParamDataHandler = std::make_unique(); + if (fParamDataHandler == nullptr) { std::cerr << std::endl << "**ERROR** couldn't invoke data handler ..." << std::endl << std::endl; return -1; } diff --git a/src/musredit_qt6/mupp/PmuppScript.h b/src/musredit_qt6/mupp/PmuppScript.h index a73b2cde..e725d05c 100644 --- a/src/musredit_qt6/mupp/PmuppScript.h +++ b/src/musredit_qt6/mupp/PmuppScript.h @@ -30,6 +30,8 @@ #ifndef _PMUPPSCRIPT_H_ #define _PMUPPSCRIPT_H_ +#include + #include #include @@ -55,7 +57,6 @@ class PmuppScript : public QObject public: PmuppScript(QStringList script); - ~PmuppScript(); void setLoadPath(const QString cmd); QString getLoadPath() { return fLoadPath; } @@ -79,10 +80,10 @@ class PmuppScript : public QObject void finished(); private: - PmuppAdmin *fAdmin; ///< admin object + std::unique_ptr fAdmin; ///< admin object QStringList fScript; ///< script source - PParamDataHandler *fParamDataHandler; ///< parameter data handler + std::unique_ptr fParamDataHandler; ///< parameter data handler int fSelected; ///< -2=nothing selected, -1=all selected, >=0 is the index if the selected collection PmuppPlotEntry fPlotEntry; ///< plot entry object