added recent files to mupp.

This commit is contained in:
suter_a 2018-07-03 09:34:48 +02:00
parent 294eaa8fb1
commit 568792dc00
6 changed files with 182 additions and 4 deletions

View File

@ -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();
}
//--------------------------------------------------------------------------
/**
* <p>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<fRecentFile.size(); i++) {
if (str == fRecentFile[i])
return;
}
fRecentFile.push_front(str);
if (fRecentFile.size() > 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);
}
//--------------------------------------------------------------------------
/**
* <p>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<QString> 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 <path_file_name> from data
for (QVector<QString>::iterator it = data.begin(); it != data.end(); ++it) {
if (it->contains("<path_file_name>")) {
it = data.erase(it);
--it;
}
}
// add recent files
int i;
for (i=0; i<data.size(); i++) {
if (data[i].contains("<recent_files>"))
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<fRecentFile.size(); j++) {
str = " <path_file_name>" + fRecentFile[j] + "</path_file_name>";
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<data.size(); i++)
fin << data[i] << endl;
file.close();
} else {
QString msg("Failed to write mupp_startup.xml. Neither a local nor a global copy found.");
QMessageBox::warning(0, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton);
}
}
//--------------------------------------------------------------------------
/**
* @brief PmuppAdmin::createMuppStartupFile

View File

@ -36,6 +36,8 @@
#include <QPixmap>
#include <QtXml>
#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<PmuppMarker> getMarkers() { return fMarker; }
PmuppMarker getMarker(int idx);
@ -142,9 +148,12 @@ class PmuppAdmin : public QObject
private:
friend class PmuppAdminXMLParser;
QVector<QString> fRecentFile; ///< keep vector of recent path-file names
QVector<PmuppMarker> fMarker;
QVector<PmuppColor> fColor;
void saveRecentFiles(); ///< save recent file list
void createMuppStartupFile(); ///< create default mupp_startup.xml
};

View File

@ -470,6 +470,15 @@ void PmuppGui::setupFileActions()
}
tb->addAction(a);
fRecentFilesMenu = menu->addMenu( tr("Recent Files") );
for (int i=0; i<MAX_RECENT_FILES; i++) {
fRecentFilesAction[i] = new QAction(fRecentFilesMenu);
fRecentFilesAction[i]->setVisible(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; i<list.size(); i++) {
fAdmin->addRecentFile(list[i]); // keep it in admin
fillRecentFiles(); // update menu
}
}
}
//----------------------------------------------------------------------------------------------------
/**
* @brief PmuppGui::fileOpenRecent
*/
void PmuppGui::fileOpenRecent()
{
QAction *action = qobject_cast<QAction *>(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()
}
}
//----------------------------------------------------------------------------------------------------
/**
* <p>fill the recent file list in the menu.
*/
void PmuppGui::fillRecentFiles()
{
for (int i=0; i<fAdmin->getNumRecentFiles(); i++) {
fRecentFilesAction[i]->setText(fAdmin->getRecentFile(i));
fRecentFilesAction[i]->setVisible(true);
}
}
//----------------------------------------------------------------------------------------------------
/**
* @brief PmuppGui::readCmdHistory

View File

@ -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();

View File

@ -35,6 +35,8 @@
#include <sys/msg.h>
#include <sys/stat.h>
#define MAX_RECENT_FILES 5
#define PMUPP_MAX_MTEXT 512
struct mbuf {

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<mupp xmlns="http://lmu.web.psi.ch/musrfit/user/MUSR/WebHome.html">
<comment>
Defines default settings for the mupp helper program for musrfit
</comment>
<recent_files>
</recent_files>
<root_settings>
<marker_list>
<!-- Root marker numbers -->