From 568792dc005c495d13f19037ceb68d45ba0a0548 Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Tue, 3 Jul 2018 09:34:48 +0200 Subject: [PATCH] added recent files to mupp. --- src/musredit_qt5/mupp/PmuppAdmin.cpp | 114 +++++++++++++++++++++- src/musredit_qt5/mupp/PmuppAdmin.h | 11 ++- src/musredit_qt5/mupp/PmuppGui.cpp | 49 ++++++++++ src/musredit_qt5/mupp/PmuppGui.h | 6 ++ src/musredit_qt5/mupp/mupp.h | 2 + src/musredit_qt5/mupp/mupp_startup.xml.in | 4 +- 6 files changed, 182 insertions(+), 4 deletions(-) diff --git a/src/musredit_qt5/mupp/PmuppAdmin.cpp b/src/musredit_qt5/mupp/PmuppAdmin.cpp index 0e9e9493..0ca4644c 100644 --- a/src/musredit_qt5/mupp/PmuppAdmin.cpp +++ b/src/musredit_qt5/mupp/PmuppAdmin.cpp @@ -108,7 +108,9 @@ bool PmuppAdminXMLParser::startElement( const QString&, const QString&, const QString& qName, const QXmlAttributes& ) { - if (qName == "marker") { + if (qName == "path_file_name") { + fKeyWord = eRecentFile; + } else if (qName == "marker") { fKeyWord = eMarker; } else if (qName == "color") { fKeyWord = eColor; @@ -148,6 +150,9 @@ bool PmuppAdminXMLParser::characters(const QString& str) QStringList tok; switch (fKeyWord) { + case eRecentFile: + fAdmin->addRecentFile(QString(str.toLatin1()).trimmed()); + break; case eMarker: tok = str.split(",", QString::SkipEmptyParts); @@ -330,7 +335,42 @@ PmuppAdmin::PmuppAdmin() : QObject() */ PmuppAdmin::~PmuppAdmin() { - // nothing to be done for now + saveRecentFiles(); +} + +//-------------------------------------------------------------------------- +/** + *

Add recent path-file name to the internal ring-buffer. + * + * \param str recent path-file name to be added + */ +void PmuppAdmin::addRecentFile(const QString str) +{ + // check if file name is not already present + for (int i=0; i MAX_RECENT_FILES) + fRecentFile.resize(MAX_RECENT_FILES); +} + +//-------------------------------------------------------------------------- +/** + * @brief PmuppAdmin::getRecentFile + * @param idx + * @return + */ +QString PmuppAdmin::getRecentFile(int idx) +{ + QString str(""); + + if ((idx >= 0) && (idx < fRecentFile.size())) + str = fRecentFile[idx]; + + return str; } //-------------------------------------------------------------------------- @@ -439,6 +479,76 @@ void PmuppAdmin::setColor(int r, int g, int b, QString name) fColor.push_back(color); } +//-------------------------------------------------------------------------- +/** + *

Merges the recent file ring buffer into mupp_startup.xml and saves it. + * If a local copy is present it will be saved there, otherwise the master file + * will be used. + */ +void PmuppAdmin::saveRecentFiles() +{ + // check if mupp_startup.xml is present in the current directory, and if yes, use this file to + // save the recent file names otherwise use the "master" mupp_startup.xml + + QString str(""); + QString fln = QString("./mupp_startup.xml"); + if (!QFile::exists(fln)) { + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + fln = QString("%1/.musrfit/mupp/mupp_startup.xml").arg(env.value("HOME")); + } + + if (QFile::exists(fln)) { // administration file present + QVector data; + QFile file(fln); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + cerr << endl << ">> PmuppAdmin::saveRecentFile: **ERROR** Cannot open " << fln.toLatin1().data() << " for reading." << endl; + return; + } + QTextStream fin(&file); + while (!fin.atEnd()) { + data.push_back(fin.readLine()); + } + file.close(); + + // remove from data + for (QVector::iterator it = data.begin(); it != data.end(); ++it) { + if (it->contains("")) { + it = data.erase(it); + --it; + } + } + + // add recent files + int i; + for (i=0; i")) + break; + } + + if (i == data.size()) { + cerr << endl << ">> PmuppAdmin::saveRecentFile: **ERROR** " << fln.toLatin1().data() << " seems to be corrupt." << endl; + return; + } + i++; + for (int j=0; j"; + data.insert(i++, str); + } + + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + cerr << endl << ">> PmuppAdmin::saveRecentFile: **ERROR** Cannot open " << fln.toLatin1().data() << " for reading." << endl; + return; + } + fin.setDevice(&file); + for (int i=0; i #include +#include "mupp.h" + class PmuppAdmin; //--------------------------------------------------------------------------- @@ -96,7 +98,7 @@ class PmuppAdminXMLParser : public QXmlDefaultHandler virtual ~PmuppAdminXMLParser() {} private: - enum EAdminKeyWords {eEmpty, eMarker, eColor}; + enum EAdminKeyWords {eEmpty, eRecentFile, eMarker, eColor}; bool startDocument(); bool startElement( const QString&, const QString&, const QString& , @@ -127,6 +129,10 @@ class PmuppAdmin : public QObject PmuppAdmin(); virtual ~PmuppAdmin(); + void addRecentFile(const QString str); + int getNumRecentFiles() { return fRecentFile.size(); } + QString getRecentFile(int idx); + int getNoOfMarkers() { return fMarker.size(); } QVector getMarkers() { return fMarker; } PmuppMarker getMarker(int idx); @@ -142,9 +148,12 @@ class PmuppAdmin : public QObject private: friend class PmuppAdminXMLParser; + QVector fRecentFile; ///< keep vector of recent path-file names + QVector fMarker; QVector fColor; + void saveRecentFiles(); ///< save recent file list void createMuppStartupFile(); ///< create default mupp_startup.xml }; diff --git a/src/musredit_qt5/mupp/PmuppGui.cpp b/src/musredit_qt5/mupp/PmuppGui.cpp index d627c91e..b2b9b11a 100644 --- a/src/musredit_qt5/mupp/PmuppGui.cpp +++ b/src/musredit_qt5/mupp/PmuppGui.cpp @@ -470,6 +470,15 @@ void PmuppGui::setupFileActions() } tb->addAction(a); + fRecentFilesMenu = menu->addMenu( tr("Recent Files") ); + for (int i=0; isetVisible(false); + connect( fRecentFilesAction[i], SIGNAL(triggered()), this, SLOT(fileOpenRecent())); + fRecentFilesMenu->addAction(fRecentFilesAction[i]); + } + fillRecentFiles(); + a = new QAction( tr( "E&xit" ), this ); a->setShortcut( tr("Ctrl+Q") ); a->setStatusTip( tr("Exit Program") ); @@ -580,6 +589,34 @@ void PmuppGui::fileOpen() QString errorMsg(""); if (!fParamDataHandler->ReadParamFile(list, errorMsg)) { QMessageBox::critical(this, "ERROR", errorMsg); + return; + } + + // populate the recent files + if (msrPresent || dbPresent) { + for (int i=0; iaddRecentFile(list[i]); // keep it in admin + fillRecentFiles(); // update menu + } + } +} + +//---------------------------------------------------------------------------------------------------- +/** + * @brief PmuppGui::fileOpenRecent + */ +void PmuppGui::fileOpenRecent() +{ + QAction *action = qobject_cast(sender()); + + if (action) { + QStringList fln; + fln << action->text(); + QString errorMsg(""); + if (!fParamDataHandler->ReadParamFile(fln, errorMsg)) { + QMessageBox::critical(this, "ERROR", errorMsg); + return; + } } } @@ -727,6 +764,18 @@ void PmuppGui::getTheme() } } +//---------------------------------------------------------------------------------------------------- +/** + *

fill the recent file list in the menu. + */ +void PmuppGui::fillRecentFiles() +{ + for (int i=0; igetNumRecentFiles(); i++) { + fRecentFilesAction[i]->setText(fAdmin->getRecentFile(i)); + fRecentFilesAction[i]->setVisible(true); + } +} + //---------------------------------------------------------------------------------------------------- /** * @brief PmuppGui::readCmdHistory diff --git a/src/musredit_qt5/mupp/PmuppGui.h b/src/musredit_qt5/mupp/PmuppGui.h index 4056b33d..22c2dea1 100644 --- a/src/musredit_qt5/mupp/PmuppGui.h +++ b/src/musredit_qt5/mupp/PmuppGui.h @@ -109,6 +109,7 @@ public: public slots: void aboutToQuit(); void fileOpen(); + void fileOpenRecent(); void fileExit(); void toolDumpCollections(); @@ -139,6 +140,9 @@ private: QWidget *fCentralWidget; + QMenu *fRecentFilesMenu; ///< recent file menu + QAction *fRecentFilesAction[MAX_RECENT_FILES]; ///< array of the recent file actions + QBoxLayout *fBoxLayout_Main; // top->bottom (0) QBoxLayout *fBoxLayout_Top; // left->right (1) QGridLayout *fGridLayout_Left; // 2 columns, 3 rows @@ -174,6 +178,8 @@ private: void getTheme(); + void fillRecentFiles(); + void readCmdHistory(); void writeCmdHistory(); diff --git a/src/musredit_qt5/mupp/mupp.h b/src/musredit_qt5/mupp/mupp.h index a363a04e..379d9727 100644 --- a/src/musredit_qt5/mupp/mupp.h +++ b/src/musredit_qt5/mupp/mupp.h @@ -35,6 +35,8 @@ #include #include +#define MAX_RECENT_FILES 5 + #define PMUPP_MAX_MTEXT 512 struct mbuf { diff --git a/src/musredit_qt5/mupp/mupp_startup.xml.in b/src/musredit_qt5/mupp/mupp_startup.xml.in index e02c25cb..365e94d1 100644 --- a/src/musredit_qt5/mupp/mupp_startup.xml.in +++ b/src/musredit_qt5/mupp/mupp_startup.xml.in @@ -1,8 +1,10 @@ - + Defines default settings for the mupp helper program for musrfit + +