From 56487573895e4e8ac46078ab100f7ae30fd20e3d Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Sun, 30 Mar 2025 18:14:04 +0200 Subject: [PATCH] back ported some qt6 recent file handling. --- src/musredit_qt5/musredit/PAdmin.cpp | 45 +++++++++++-------------- src/musredit_qt5/musredit/PAdmin.h | 4 +-- src/musredit_qt5/musredit/PTextEdit.cpp | 10 +++--- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/musredit_qt5/musredit/PAdmin.cpp b/src/musredit_qt5/musredit/PAdmin.cpp index 1821a71f..1ce5840e 100644 --- a/src/musredit_qt5/musredit/PAdmin.cpp +++ b/src/musredit_qt5/musredit/PAdmin.cpp @@ -59,8 +59,12 @@ PAdminXMLParser::PAdminXMLParser(const QString& fln, PAdmin *admin) : fAdmin(adm fFunc = false; QFile file(fln); - if (!file.open(QFile::ReadOnly | QFile::Text)) { - // warning and create default - STILL MISSING + if (!file.open(QFile::ReadOnly | QFile::Text) || (file.size()==0)) { + // warning and create default + QMessageBox::StandardButton ret = QMessageBox::warning(nullptr, "WARNING", "The musredit_startup.xml is corrupted. Create a default one?", QMessageBox::Yes | QMessageBox::No); + if (ret == QMessageBox::Yes) { + fAdmin->createMusreditStartupFile(); + } } fValid = parse(&file); @@ -98,7 +102,7 @@ bool PAdminXMLParser::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; } @@ -717,14 +721,14 @@ QString PAdminXMLParser::expandPath(const QString &str) token.remove('$'); if (!procEnv.contains(token)) { msg = QString("Couldn't find '%1'. Some things might not work properly").arg(token); - QMessageBox::warning(0, "**WARNING**", msg, QMessageBox::Ok, QMessageBox::NoButton); + QMessageBox::warning(nullptr, "**WARNING**", msg, QMessageBox::Ok, QMessageBox::NoButton); newStr = ""; break; } path = procEnv.value(token, ""); if (path.isEmpty()) { msg = QString("Couldn't expand '%1'. Some things might not work properly").arg(token); - QMessageBox::warning(0, "**WARNING**", msg, QMessageBox::Ok, QMessageBox::NoButton); + QMessageBox::warning(nullptr, "**WARNING**", msg, QMessageBox::Ok, QMessageBox::NoButton); newStr = ""; break; } @@ -769,13 +773,13 @@ PAdmin::PAdmin() : QObject() // XML Parser part // 1st: check local directory - QString path = QString("./"); + QString path = QDir::currentPath(); QString fln = QString("musredit_startup.xml"); QString pathFln = path + fln; QProcessEnvironment procEnv = QProcessEnvironment::systemEnvironment(); if (!QFile::exists(pathFln)) { // 2nd: check $HOME/.musrfit/musredit/musredit_startup.xml - path = procEnv.value("HOME", ""); + path = QDir::homePath(); pathFln = path + "/.musrfit/musredit/" + fln; if (!QFile::exists(pathFln)) { // 3rd: check $MUSRFITPATH/musredit_startup.xml @@ -803,9 +807,9 @@ PAdmin::PAdmin() : QObject() QFileInfo info(str); if (info.exists()) { if (!info.isExecutable()) - QMessageBox::critical(0, "ERROR", "musrfit found but not recognized as executable.\nPlease check!"); + QMessageBox::critical(nullptr, "ERROR", "musrfit found but not recognized as executable.\nPlease check!"); } else { - QMessageBox::critical(0, "ERROR", "musrfit not found.\nHave you set the necessary system variables properly?\nPlease check the manual.\nBefore you can use musrfit, this needs to be fixed."); + QMessageBox::critical(nullptr, "ERROR", "musrfit not found.\nHave you set the necessary system variables properly?\nPlease check the manual.\nBefore you can use musrfit, this needs to be fixed."); } // check if system variables are set properly @@ -823,19 +827,10 @@ PAdmin::PAdmin() : QObject() } if (sysVarMissing) { msg += "Please set this/these system variables."; - QMessageBox::warning(0, "WARNING", msg); + QMessageBox::warning(nullptr, "WARNING", msg); } } -//-------------------------------------------------------------------------- -/** - *

Destructor - */ -PAdmin::~PAdmin() -{ - saveRecentFiles(); -} - //-------------------------------------------------------------------------- /** *

returns the help url corresponding the the tag. @@ -903,13 +898,13 @@ int PAdmin::loadPrefs(QString fln) if (QFile::exists(fln)) { // administration file present PAdminXMLParser handler(fln, this); if (!handler.isValid()) { - QMessageBox::critical(0, "**ERROR**", + QMessageBox::critical(nullptr, "**ERROR**", "Error parsing musredit_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.", QMessageBox::Ok, QMessageBox::NoButton); return 0; } } else { - QMessageBox::critical(0, "**ERROR**", + QMessageBox::critical(nullptr, "**ERROR**", "Couldn't find the musredit_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.", QMessageBox::Ok, QMessageBox::NoButton); return 0; @@ -1054,7 +1049,7 @@ int PAdmin::savePrefs(QString pref_fln) file.close(); } else { QString msg("Failed to write musredit_startup.xml. Neither a local nor a global copy found."); - QMessageBox::warning(0, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton); + QMessageBox::warning(nullptr, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton); } return 1; @@ -1148,7 +1143,7 @@ void PAdmin::saveRecentFiles() file.close(); } else { QString msg("Failed to write musredit_startup.xml. Neither a local nor a global copy found."); - QMessageBox::warning(0, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton); + QMessageBox::warning(nullptr, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton); } } @@ -1177,13 +1172,13 @@ void PAdmin::createMusreditStartupFile() QFile fres(":/musredit_startup.xml.in"); if (!fres.exists()) { QString msg = QString("Neither couldn't find nor create musredit_startup.xml. Things are likely not to work."); - QMessageBox::critical(0, "ERROR", msg); + QMessageBox::critical(nullptr, "ERROR", msg); return; } if (!fres.open(QIODevice::ReadOnly | QIODevice::Text)) { QString msg = QString("Couldn't open internal resource file musredit_startup.xml.in. Things are likely not to work."); - QMessageBox::critical(0, "ERROR", msg); + QMessageBox::critical(nullptr, "ERROR", msg); return; } // text stream for fres diff --git a/src/musredit_qt5/musredit/PAdmin.h b/src/musredit_qt5/musredit/PAdmin.h index 1e118355..99344d8f 100644 --- a/src/musredit_qt5/musredit/PAdmin.h +++ b/src/musredit_qt5/musredit/PAdmin.h @@ -65,7 +65,6 @@ class PAdminXMLParser { public: PAdminXMLParser(const QString &fln, PAdmin*); - virtual ~PAdminXMLParser() {} virtual bool isValid() { return fValid; } @@ -113,7 +112,6 @@ class PAdmin : public QObject { public: PAdmin(); - virtual ~PAdmin(); int getTimeout() { return fTimeout; } QString getFontName() { return fFontName; } @@ -173,6 +171,7 @@ class PAdmin : public QObject int loadPrefs(QString fln); int savePrefs(QString pref_fln); + void saveRecentFiles(); ///< save recent file list protected: void setExecPath(const QString str) { fExecPath = str; } @@ -228,7 +227,6 @@ class PAdmin : public QObject QVector fTheory; ///< stores all known theories. Needed when generating theory blocks from within musredit. - void saveRecentFiles(); ///< save recent file list void createMusreditStartupFile(); ///< create default musredit_startup.xml }; diff --git a/src/musredit_qt5/musredit/PTextEdit.cpp b/src/musredit_qt5/musredit/PTextEdit.cpp index 28d595c4..816808d8 100644 --- a/src/musredit_qt5/musredit/PTextEdit.cpp +++ b/src/musredit_qt5/musredit/PTextEdit.cpp @@ -1558,7 +1558,7 @@ void PTextEdit::fileClose(const bool check) int result = QMessageBox::warning(this, "**WARNING**", "Do you really want to close this file.\nChanges will be lost", "Close", "Cancel"); - if (result == 1) // Cancel + if (result == QMessageBox::Cancel) // Cancel return; } @@ -1588,7 +1588,7 @@ void PTextEdit::fileCloseAll() int result = QMessageBox::warning(this, "**WARNING**", "Do you really want to close all files.\nChanges of unsaved files will be lost", "Close", "Cancel"); - if (result == 1) // Cancel + if (result == QMessageBox::Cancel) // Cancel return; break; } @@ -1625,7 +1625,7 @@ void PTextEdit::fileCloseAllOthers() int result = QMessageBox::warning(this, "**WARNING**", "Do you really want to close all files.\nChanges of unsaved files will be lost", "Close", "Cancel"); - if (result == 1) // Cancel + if (result == QMessageBox::Cancel) // Cancel return; break; } @@ -1668,12 +1668,14 @@ void PTextEdit::fileExit() int result = QMessageBox::warning(this, "**WARNING**", "Do you really want to exit from the applcation.\nChanges will be lost", "Exit", "Cancel"); - if (result == 1) // Cancel + if (result == QMessageBox::Cancel) // Cancel return; break; } } + fAdmin->saveRecentFiles(); + qApp->quit(); }