From 4d516d659db72f7dfe184b6076600c9cf0e6418b Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Tue, 24 Oct 2023 09:22:42 +0200 Subject: [PATCH] Raw -> Smart Pointers for musredit qt5. --- src/musredit_qt5/musredit/PAdmin.h | 6 +- .../musredit/PChangeDefaultPathsDialog.cpp | 7 +-- .../musredit/PChangeDefaultPathsDialog.h | 4 +- .../musredit/PDumpOutputHandler.cpp | 26 ++++----- .../musredit/PDumpOutputHandler.h | 13 +++-- .../musredit/PFitOutputHandler.cpp | 27 ++++----- src/musredit_qt5/musredit/PFitOutputHandler.h | 13 +++-- src/musredit_qt5/musredit/PTextEdit.cpp | 55 ++++++++----------- src/musredit_qt5/musredit/PTextEdit.h | 20 ++++--- src/musredit_qt5/musredit/main.cpp | 5 +- 10 files changed, 84 insertions(+), 92 deletions(-) diff --git a/src/musredit_qt5/musredit/PAdmin.h b/src/musredit_qt5/musredit/PAdmin.h index 48b09a86..24c47d7a 100644 --- a/src/musredit_qt5/musredit/PAdmin.h +++ b/src/musredit_qt5/musredit/PAdmin.h @@ -36,7 +36,7 @@ #include #include -#include +#include "musredit.h" class PAdmin; @@ -208,8 +208,8 @@ class PAdmin : public QObject bool fEnableMusrT0; ///< flag indicating if musrT0 shall be enabled at startup from within musredit (default: yes). bool fDarkThemeIconsMenu; ///< flag indicating if dark theme icons shall be used in the menu (default: no) bool fDarkThemeIconsToolbar; ///< flag indicating if dark theme icons shall be used in the toolbar (default: no) - int fEditWidth; ///< startup edit width - int fEditHeight; ///< startup edit height + int fEditWidth{900}; ///< startup edit width + int fEditHeight{800}; ///< startup edit height QString fBeamline; ///< name of the beamline. Used to generate default run header lines. QString fInstitute; ///< name of the institute. Used to generate default run header lines. diff --git a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp index da7152dc..9061f248 100644 --- a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp +++ b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.cpp @@ -92,7 +92,7 @@ bool PDefaultPathsXMLParser::parse(QIODevice *device) if (fXml.hasError()) { QString msg; msg = QString("%1 Line %2, column %3").arg(fXml.errorString()).arg(fXml.lineNumber()).arg(fXml.columnNumber()); - QMessageBox::critical(0, "**ERROR**", msg, QMessageBox::Ok, QMessageBox::NoButton); + QMessageBox::critical(nullptr, "**ERROR**", msg, QMessageBox::Ok, QMessageBox::NoButton); return false; } @@ -204,7 +204,7 @@ PDefaultPaths::PDefaultPaths() : QObject() PDefaultPathsXMLParser handler(fPrefPathName, this); if (!handler.isValid()) { fValid = false; - QMessageBox::critical(0, "ERROR", + QMessageBox::critical(nullptr, "ERROR", "Error parsing musrfit_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.", QMessageBox::Ok, QMessageBox::NoButton); return; @@ -219,8 +219,7 @@ PDefaultPaths::PDefaultPaths() : QObject() */ PChangeDefaultPathsDialog::PChangeDefaultPathsDialog() { - fDefaultPath = 0; - fDefaultPath = new PDefaultPaths(); + fDefaultPath = std::make_unique(); if (!fDefaultPath->isValid()) return; diff --git a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h index d3024d0f..5647ab6c 100644 --- a/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h +++ b/src/musredit_qt5/musredit/PChangeDefaultPathsDialog.h @@ -30,6 +30,8 @@ #ifndef _PCHANGEDEFAULTPATHSDIALOG_H_ #define _PCHANGEDEFAULTPATHSDIALOG_H_ +#include + #include #include #include @@ -98,7 +100,7 @@ class PChangeDefaultPathsDialog : public QDialog, private Ui::PChangeDefaultPath void saveDefaultPathList(); private: - PDefaultPaths *fDefaultPath; + std::unique_ptr fDefaultPath; }; #endif // _PCHANGEDEFAULTPATHSDIALOG_H_ diff --git a/src/musredit_qt5/musredit/PDumpOutputHandler.cpp b/src/musredit_qt5/musredit/PDumpOutputHandler.cpp index 8172fff8..8efd573d 100644 --- a/src/musredit_qt5/musredit/PDumpOutputHandler.cpp +++ b/src/musredit_qt5/musredit/PDumpOutputHandler.cpp @@ -45,20 +45,20 @@ PDumpOutputHandler::PDumpOutputHandler(QVector &cmd) return; // Layout - fVbox = new QVBoxLayout( this ); - fOutput = new QTextEdit(); - fVbox->addWidget(fOutput); + fVbox = std::make_unique( this ); + fOutput = std::make_unique(); + fVbox->addWidget(fOutput.get()); fOutput->setMinimumSize(600, 755); fOutput->setReadOnly(true); - connect( fOutput, SIGNAL( destroyed() ), this, SLOT( quitButtonPressed() ) ); - fQuitButton = new QPushButton( tr("Quit") ); - fVbox->addWidget(fQuitButton); - connect( fQuitButton, SIGNAL( clicked() ), this, SLOT( quitButtonPressed() ) ); + connect( fOutput.get(), SIGNAL( destroyed() ), this, SLOT( quitButtonPressed() ) ); + fQuitButton = std::make_unique( tr("Quit") ); + fVbox->addWidget(fQuitButton.get()); + connect( fQuitButton.get(), SIGNAL( clicked() ), this, SLOT( quitButtonPressed() ) ); resize( 600, 800 ); fQuitButton->setFocus(); // QProcess related code - fProc = new QProcess( this ); + fProc = std::make_unique( this ); // make sure that the system environment variables are properly set QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); @@ -75,15 +75,15 @@ PDumpOutputHandler::PDumpOutputHandler(QVector &cmd) for (int i=1; istart(program, arguments); if ( !fProc->waitForStarted() ) { // error handling QString msg(tr("Could not execute the output command: ")+cmd[0]); - QMessageBox::critical( 0, + QMessageBox::critical( nullptr, tr("Fatal error"), msg, tr("Quit") ); @@ -132,10 +132,6 @@ PDumpOutputHandler::~PDumpOutputHandler() } } - if (fProc) { - delete fProc; - fProc = 0; - } } //---------------------------------------------------------------------------------------------------- diff --git a/src/musredit_qt5/musredit/PDumpOutputHandler.h b/src/musredit_qt5/musredit/PDumpOutputHandler.h index 22bd6fac..86230489 100644 --- a/src/musredit_qt5/musredit/PDumpOutputHandler.h +++ b/src/musredit_qt5/musredit/PDumpOutputHandler.h @@ -30,6 +30,9 @@ #ifndef _PDUMPOUTPUTHANDLER_H_ #define _PDUMPOUTPUTHANDLER_H_ +#include +#include + #include #include #include @@ -39,8 +42,6 @@ #include #include -#include - //--------------------------------------------------------------------------------------- /** *

This class is the capturing the output of musrfit and displays it in a dialog so @@ -62,11 +63,11 @@ class PDumpOutputHandler : public QDialog private: Q_PID fProcPID; ///< keeps the process PID - QProcess *fProc; ///< pointer to the dump_header process + std::unique_ptr fProc; ///< pointer to the dump_header process - QVBoxLayout *fVbox; ///< pointer to the dialog layout manager - QTextEdit *fOutput; ///< the captured dump_header output is written (read only) into this text edit object. - QPushButton *fQuitButton; ///< quit button + std::unique_ptr fVbox; ///< pointer to the dialog layout manager + std::unique_ptr fOutput; ///< the captured dump_header output is written (read only) into this text edit object. + std::unique_ptr fQuitButton; ///< quit button }; #endif // _PDUMPOUTPUTHANDLER_H_ diff --git a/src/musredit_qt5/musredit/PFitOutputHandler.cpp b/src/musredit_qt5/musredit/PFitOutputHandler.cpp index f0ffe149..ddf1e5bc 100644 --- a/src/musredit_qt5/musredit/PFitOutputHandler.cpp +++ b/src/musredit_qt5/musredit/PFitOutputHandler.cpp @@ -46,22 +46,21 @@ PFitOutputHandler::PFitOutputHandler(QString workingDirectory, QVector return; // Layout - fVbox = new QVBoxLayout( this ); -//Qt.3x fVbox->resize(800, 500); - fOutput = new QPlainTextEdit(); + fVbox = std::make_unique( this ); + fOutput = std::make_unique(); fOutput->setMaximumBlockCount(1000); - fVbox->addWidget(fOutput); + fVbox->addWidget(fOutput.get()); fOutput->setMinimumSize(800, 455); fOutput->setReadOnly(true); - connect( fOutput, SIGNAL( destroyed() ), this, SLOT( quitButtonPressed() ) ); - fQuitButton = new QPushButton( tr("Fitting...") ); - fVbox->addWidget(fQuitButton); - connect( fQuitButton, SIGNAL( clicked() ), this, SLOT( quitButtonPressed() ) ); + connect( fOutput.get(), SIGNAL( destroyed() ), this, SLOT( quitButtonPressed() ) ); + fQuitButton = std::make_unique( tr("Fitting...") ); + fVbox->addWidget(fQuitButton.get()); + connect( fQuitButton.get(), SIGNAL( clicked() ), this, SLOT( quitButtonPressed() ) ); resize( 800, 500 ); fQuitButton->setFocus(); // QProcess related code - fProc = new QProcess( this ); + fProc = std::make_unique( this ); // make sure that the system environment variables are properly set QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); @@ -83,9 +82,9 @@ PFitOutputHandler::PFitOutputHandler(QString workingDirectory, QVector for (int i=1; istart(program, arguments); if ( !fProc->waitForStarted() ) { @@ -138,10 +137,6 @@ PFitOutputHandler::~PFitOutputHandler() #endif } } - if (fProc) { - delete fProc; - fProc = nullptr; - } } //---------------------------------------------------------------------------------------------------- diff --git a/src/musredit_qt5/musredit/PFitOutputHandler.h b/src/musredit_qt5/musredit/PFitOutputHandler.h index a6ba3896..f7bf584d 100644 --- a/src/musredit_qt5/musredit/PFitOutputHandler.h +++ b/src/musredit_qt5/musredit/PFitOutputHandler.h @@ -30,6 +30,9 @@ #ifndef _PFITOUTPUTHANDLER_H_ #define _PFITOUTPUTHANDLER_H_ +#include +#include + #include #include #include @@ -39,8 +42,6 @@ #include #include -#include - //--------------------------------------------------------------------------------------- /** *

This class is the capturing the output of musrfit and displays it in a dialog so @@ -63,11 +64,11 @@ class PFitOutputHandler : public QDialog private: Q_PID fProcPID; ///< keeps the process PID - QProcess *fProc; ///< pointer to the musrfit process + std::unique_ptr fProc; ///< pointer to the musrfit process - QVBoxLayout *fVbox; ///< pointer to the dialog layout manager - QPlainTextEdit *fOutput; ///< the captured musrfit output is written (read only) into this text edit object. - QPushButton *fQuitButton; ///< quit button, either to interrupt the fit or to close the dialog at the end of the fit. + std::unique_ptr fVbox; ///< pointer to the dialog layout manager + std::unique_ptr fOutput; ///< the captured musrfit output is written (read only) into this text edit object. + std::unique_ptr fQuitButton; ///< quit button, either to interrupt the fit or to close the dialog at the end of the fit. }; #endif // _PFITOUTPUTHANDLER_H_ diff --git a/src/musredit_qt5/musredit/PTextEdit.cpp b/src/musredit_qt5/musredit/PTextEdit.cpp index 2bf27ce9..bd0e3e54 100644 --- a/src/musredit_qt5/musredit/PTextEdit.cpp +++ b/src/musredit_qt5/musredit/PTextEdit.cpp @@ -91,7 +91,7 @@ PTextEdit::PTextEdit( QWidget *parent ) bool gotTheme = getTheme(); // reads and manages the conents of the xml-startup (musredit_startup.xml) file - fAdmin = new PAdmin(); + fAdmin = std::make_unique(); // set default setting of the fDarkMenuIconIcons only if a theme has been recognized, otherwise take the // one from the xml startup file. @@ -109,16 +109,14 @@ PTextEdit::PTextEdit( QWidget *parent ) // enable file system watcher. Needed to get notification if the msr-file is changed outside of musrfit at runtime fFileSystemWatcherActive = true; - fFileSystemWatcher = new QFileSystemWatcher(); + fFileSystemWatcher = std::make_unique(); if (fFileSystemWatcher == nullptr) { QMessageBox::information(this, "**ERROR**", "Couldn't invoke QFileSystemWatcher!"); } else { - connect( fFileSystemWatcher, SIGNAL(fileChanged(const QString&)), this, SLOT(fileChanged(const QString&))); + connect( fFileSystemWatcher.get(), SIGNAL(fileChanged(const QString&)), this, SLOT(fileChanged(const QString&))); } // initialize stuff - fMusrT0Action = nullptr; - fMsr2DataParam = nullptr; fFindReplaceData = nullptr; @@ -129,9 +127,9 @@ PTextEdit::PTextEdit( QWidget *parent ) setupMusrActions(); setupHelpActions(); - fTabWidget = new QTabWidget( this ); + fTabWidget = std::make_unique( this ); fTabWidget->setMovable(true); // allows to shuffle around tabs - setCentralWidget( fTabWidget ); + setCentralWidget( fTabWidget.get() ); textFamily(fAdmin->getFontName()); textSize(QString("%1").arg(fAdmin->getFontSize())); @@ -152,7 +150,7 @@ PTextEdit::PTextEdit( QWidget *parent ) fileNew(); } - connect( fTabWidget, SIGNAL( currentChanged(int) ), this, SLOT( applyFontSettings(int) )); + connect( fTabWidget.get(), SIGNAL( currentChanged(int) ), this, SLOT( applyFontSettings(int) )); fLastDirInUse = fAdmin->getDefaultSavePath(); } @@ -164,14 +162,6 @@ PTextEdit::PTextEdit( QWidget *parent ) */ void PTextEdit::aboutToQuit() { - if (fAdmin) { - delete fAdmin; - fAdmin = nullptr; - } - if (fMusrT0Action) { - delete fMusrT0Action; - fMusrT0Action = nullptr; - } if (fMsr2DataParam) { delete fMsr2DataParam; fMsr2DataParam = nullptr; @@ -650,33 +640,33 @@ void PTextEdit::setupTextActions() tb->setWindowTitle( "Format Actions" ); addToolBar( tb ); - fComboFont = new QComboBox(); + fComboFont = std::make_unique(); fComboFont->setEditable(true); QFontDatabase db; fComboFont->addItems( db.families() ); - connect( fComboFont, SIGNAL( activated( const QString & ) ), + connect( fComboFont.get(), SIGNAL( activated( const QString & ) ), this, SLOT( textFamily( const QString & ) ) ); QLineEdit *edit = fComboFont->lineEdit(); if (edit == nullptr) { return; } edit->setText( fAdmin->getFontName() ); - tb->addWidget(fComboFont); + tb->addWidget(fComboFont.get()); - fComboSize = new QComboBox( tb ); + fComboSize = std::make_unique( tb ); fComboSize->setEditable(true); QList sizes = db.standardSizes(); QList::Iterator it = sizes.begin(); for ( ; it != sizes.end(); ++it ) fComboSize->addItem( QString::number( *it ) ); - connect( fComboSize, SIGNAL( activated( const QString & ) ), + connect( fComboSize.get(), SIGNAL( activated( const QString & ) ), this, SLOT( textSize( const QString & ) ) ); edit = fComboSize->lineEdit(); if (edit == nullptr) { return; } edit->setText( QString("%1").arg(fAdmin->getFontSize()) ); - tb->addWidget(fComboSize); + tb->addWidget(fComboSize.get()); } //---------------------------------------------------------------------------------------------------- @@ -868,19 +858,19 @@ void PTextEdit::setupMusrActions() iconName = QString(":/icons/musrt0-dark.svg"); else iconName = QString(":/icons/musrt0-plain.svg"); - fMusrT0Action = new QAction( QIcon( QPixmap(iconName) ), tr( "&T0" ), this ); + fMusrT0Action = std::make_unique( QIcon( QPixmap(iconName) ), tr( "&T0" ), this ); fMusrT0Action->setStatusTip( tr("Start musrt0") ); - connect( fMusrT0Action, SIGNAL( triggered() ), this, SLOT( musrT0() ) ); - menu->addAction(fMusrT0Action); - fActions["musrt0"] = fMusrT0Action; + connect( fMusrT0Action.get(), SIGNAL( triggered() ), this, SLOT( musrT0() ) ); + menu->addAction(fMusrT0Action.get()); + fActions["musrt0"] = fMusrT0Action.get(); if (!fDarkToolBarIcon) { // tool bar icon is not dark, even though the theme is (ubuntu) iconName = QString(":/icons/musrt0-plain.svg"); a = new QAction( QIcon( QPixmap(iconName) ), tr( "&T0" ), this ); connect( a, SIGNAL( triggered() ), this, SLOT( musrT0() ) ); } - tb->addAction(fMusrT0Action); + tb->addAction(fMusrT0Action.get()); fMusrT0Action->setEnabled(fAdmin->getEnableMusrT0Flag()); - fActions["musrt0-tb"] = fMusrT0Action; + fActions["musrt0-tb"] = fMusrT0Action.get(); // musrFT if (fDarkMenuIcon) @@ -982,7 +972,7 @@ void PTextEdit::load( const QString &f, const int index ) return; // create a new text edit object - PSubTextEdit *edit = new PSubTextEdit( fAdmin ); + PSubTextEdit *edit = new PSubTextEdit( fAdmin.get() ); edit->setFont(QFont(fAdmin->getFontName(), fAdmin->getFontSize())); // place the text edit object at the appropriate tab position @@ -1022,6 +1012,9 @@ void PTextEdit::load( const QString &f, const int index ) */ PSubTextEdit *PTextEdit::currentEditor() const { + if (fTabWidget == nullptr) + return nullptr; + if ( fTabWidget->currentWidget() ) { if (fTabWidget->currentWidget()->inherits( "PSubTextEdit" )) { return dynamic_cast(fTabWidget->currentWidget()); @@ -1163,7 +1156,7 @@ void PTextEdit::insertStatisticBlock() */ void PTextEdit::fileNew() { - PSubTextEdit *edit = new PSubTextEdit( fAdmin ); + PSubTextEdit *edit = new PSubTextEdit( fAdmin.get() ); edit->setFont(QFont(fAdmin->getFontName(), fAdmin->getFontSize())); doConnections( edit ); fTabWidget->addTab( edit, tr( "noname" ) ); @@ -2727,7 +2720,7 @@ void PTextEdit::musrFT() */ void PTextEdit::musrPrefs() { - PPrefsDialog *dlg = new PPrefsDialog(fAdmin); + PPrefsDialog *dlg = new PPrefsDialog(fAdmin.get()); if (dlg == nullptr) { QMessageBox::critical(this, "**ERROR** musrPrefs", "Couldn't invoke Preferences Dialog."); diff --git a/src/musredit_qt5/musredit/PTextEdit.h b/src/musredit_qt5/musredit/PTextEdit.h index 89e2ae7b..9d5d0ec7 100644 --- a/src/musredit_qt5/musredit/PTextEdit.h +++ b/src/musredit_qt5/musredit/PTextEdit.h @@ -30,6 +30,9 @@ #ifndef _PTEXTEDIT_H_ #define _PTEXTEDIT_H_ +#include + +#include #include #include #include @@ -40,10 +43,12 @@ #include #include #include +#include +#include #include - +#include "PAdmin.h" #include "musredit.h" class PSubTextEdit; @@ -65,7 +70,6 @@ class PTextEdit : public QMainWindow public: PTextEdit( QWidget *parent = nullptr ); - virtual ~PTextEdit() {} int getEditW() { return fEditW; } int getEditH() { return fEditH; } @@ -167,8 +171,8 @@ private slots: private: bool fDarkMenuIcon; ///< flag indicating if a dark or plain icon shall be used in the menu pull-downs bool fDarkToolBarIcon; ///< flag indicating if a dark or plain icon shall be used in the toolbar - PAdmin *fAdmin; ///< pointer to the xml-startup file informations. Needed for different purposes like default working- and executable directories etc. - QFileSystemWatcher *fFileSystemWatcher; ///< checks if msr-files are changing on the disk while being open in musredit. + std::unique_ptr fAdmin; ///< pointer to the xml-startup file informations. Needed for different purposes like default working- and executable directories etc. + std::unique_ptr fFileSystemWatcher; ///< checks if msr-files are changing on the disk while being open in musredit. bool fFileSystemWatcherActive; ///< flag to enable/disable the file system watcher QTimer fFileSystemWatcherTimeout; ///< timer used to re-enable file system watcher. Needed to delay the re-enabling QString fLastDirInUse; ///< string holding the path from where the last file was loaded. @@ -176,16 +180,16 @@ private: int fEditW, fEditH; QMap fActions; - QAction *fMusrT0Action; + std::unique_ptr fMusrT0Action; PMsr2DataParam *fMsr2DataParam; ///< structure holding the necessary input information for msr2data PFindReplaceData *fFindReplaceData; ///< structure holding the ncessary input for find/replace - QComboBox *fComboFont; ///< combo box for the font selector - QComboBox *fComboSize; ///< combo box for the font size + std::unique_ptr fComboFont; ///< combo box for the font selector + std::unique_ptr fComboSize; ///< combo box for the font size bool fFontChanging; ///< flag needed to prevent some textChanged feature to occure when only the font changed - QTabWidget *fTabWidget; ///< tab widget in which the text editor(s) are placed + std::unique_ptr fTabWidget; ///< tab widget in which the text editor(s) are placed QMap fFilenames; ///< mapper between tab widget object and filename QMenu *fRecentFilesMenu; ///< recent file menu diff --git a/src/musredit_qt5/musredit/main.cpp b/src/musredit_qt5/musredit/main.cpp index f1b591fe..feca37a8 100644 --- a/src/musredit_qt5/musredit/main.cpp +++ b/src/musredit_qt5/musredit/main.cpp @@ -28,6 +28,7 @@ ***************************************************************************/ #include +#include #include @@ -67,12 +68,12 @@ int main( int argc, char ** argv ) QApplication a( argc, argv ); - PTextEdit *mw = new PTextEdit(); + std::unique_ptr mw = std::make_unique(); mw->setWindowTitle( "MusrFit Editor" ); mw->resize( mw->getEditW(), mw->getEditH() ); mw->show(); a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) ); - a.connect( &a, SIGNAL( aboutToQuit() ), mw, SLOT( aboutToQuit() ) ); + a.connect( &a, SIGNAL( aboutToQuit() ), mw.get(), SLOT( aboutToQuit() ) ); return a.exec(); }