allow the user to change the default search data file paths from within musredit > preferences.

This commit is contained in:
2017-01-14 18:19:36 +01:00
parent 30708ff4bc
commit 77636efbb1
7 changed files with 588 additions and 3 deletions

View File

@ -0,0 +1,331 @@
/****************************************************************************
PChangeDefaultPathsDialog.cpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
*****************************************************************************/
/***************************************************************************
* Copyright (C) 2010-2017 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QMessageBox>
#include <QFileDialog>
#include <QListWidgetItem>
#include "PChangeDefaultPathsDialog.h"
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* @brief PDefaultPathsXMLParser::PDefaultPathsXMLParser
* @param defaultPaths
*/
PDefaultPathsXMLParser::PDefaultPathsXMLParser(PDefaultPaths *defaultPaths) :
fDefaultPaths(defaultPaths)
{
fKeyWord = eEmpty;
}
//--------------------------------------------------------------------------
/**
* <p>Routine called at the beginning of the XML parsing process.
*/
bool PDefaultPathsXMLParser::startDocument()
{
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Routine called when a new XML tag is found. Here it is used
* to set a tag for filtering afterwards the content.
*
* \param qName name of the XML tag.
*/
bool PDefaultPathsXMLParser::startElement( const QString&, const QString&,
const QString& qName,
const QXmlAttributes& )
{
if (qName == "data_path") {
fKeyWord = eDataPath;
}
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Routine called when the end XML tag is found. It is used to
* put the filtering tag to 'empty'.
*
* \param qName name of the element.
*/
bool PDefaultPathsXMLParser::endElement( const QString&, const QString&, const QString & )
{
fKeyWord = eEmpty;
return true;
}
//--------------------------------------------------------------------------
/**
* <p>This routine delivers the content of an XML tag. It fills the
* content into the load data structure.
*
* \param str keeps the content of the XML tag.
*/
bool PDefaultPathsXMLParser::characters(const QString& str)
{
switch (fKeyWord) {
case eDataPath:
fDefaultPaths->appendDefaultPath(str);
break;
default:
break;
}
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Called at the end of the XML parse process. It checks if default paths
* contain system variables, and if so expand them for the further use.
*/
bool PDefaultPathsXMLParser::endDocument()
{
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Report XML warnings.
*
* \param exception holds the information of the XML warning
*/
bool PDefaultPathsXMLParser::warning( const QXmlParseException & exception )
{
QString msg;
msg = QString("**WARNING** while parsing musrfit_startup.xml in line no %1\n").arg(exception.lineNumber());
msg += QString("**WARNING MESSAGE** ") + exception.message();
qWarning() << endl << msg << endl;
QMessageBox::warning(0, "WARNING", msg, QMessageBox::Ok, QMessageBox::NoButton);
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Report recoverable XML errors.
*
* \param exception holds the information of the XML recoverable errors.
*/
bool PDefaultPathsXMLParser::error( const QXmlParseException & exception )
{
QString msg;
msg = QString("**ERROR** while parsing musrfit_startup.xml in line no %1\n").arg(exception.lineNumber());
msg += QString("**ERROR MESSAGE** ") + exception.message();
qWarning() << endl << msg << endl;
QMessageBox::critical(0, "ERROR", msg, QMessageBox::Ok, QMessageBox::NoButton);
return true;
}
//--------------------------------------------------------------------------
/**
* <p>Report fatal XML errors.
*
* \param exception holds the information of the XML fatal errors.
*/
bool PDefaultPathsXMLParser::fatalError( const QXmlParseException & exception )
{
QString msg;
msg = QString("**FATAL ERROR** while parsing musrfit_startup.xml in line no %1\n").arg(exception.lineNumber());
msg += QString("**FATAL ERROR MESSAGE** ") + exception.message();
qWarning() << endl << msg << endl;
QMessageBox::critical(0, "FATAL ERROR", msg, QMessageBox::Ok, QMessageBox::NoButton);
return true;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PDefaultPaths::PDefaultPaths() : QObject()
{
fValid = true;
// XML Parser part
// 1st: check local directory
QString path = QString("./");
QString fln = QString("musrfit_startup.xml");
QString pathFln = path + fln;
QProcessEnvironment procEnv = QProcessEnvironment::systemEnvironment();
if (!QFile::exists(pathFln)) {
// 2nd: check $HOME/.musrfit/musrfit_startup.xml
path = procEnv.value("HOME", "");
pathFln = path + "/.musrfit/" + fln;
if (!QFile::exists(pathFln)) {
// 3rd: check $MUSRFITPATH/musrfit_startup.xml
path = procEnv.value("MUSRFITPATH", "");
pathFln = path + "/" + fln;
if (!QFile::exists(pathFln)) {
// 4th: check $ROOTSYS/bin/musrfit_startup.xml
path = procEnv.value("ROOTSYS", "");
pathFln = path + "/bin/" + fln;
}
}
}
fPrefPathName = pathFln;
// read musrfit_startup.xml and extract default data file search paths
PDefaultPathsXMLParser handler(this);
QFile xmlFile(fPrefPathName);
QXmlInputSource source( &xmlFile );
QXmlSimpleReader reader;
reader.setContentHandler( &handler );
reader.setErrorHandler( &handler );
if (!reader.parse( source )) {
fValid = false;
QMessageBox::critical(0, "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;
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* <p>Constructor.
*
* \param fAdmin keeps all the needed flags
*/
PChangeDefaultPathsDialog::PChangeDefaultPathsDialog()
{
fDefaultPath = 0;
fDefaultPath = new PDefaultPaths();
if (!fDefaultPath->isValid())
return;
QStringList *strList = fDefaultPath->getDefaultPathList();
if (strList == 0)
return;
setupUi(this);
setModal(true);
// populate default search paths
for (int i=0; i<strList->count(); i++) {
fSearchPath_listWidget->addItem(strList->at(i));
}
QObject::connect(fDelete_pushButton, SIGNAL(clicked()), this, SLOT(deleteItem()));
QObject::connect(fAdd_pushButton, SIGNAL(clicked()), this, SLOT(addItem()));
QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(saveDefaultPathList()));
}
//----------------------------------------------------------------------------------------------------
/**
* @brief PChangeDefaultPathsDialog::addItem
*/
void PChangeDefaultPathsDialog::addItem()
{
// call QFileDialog in order to get the proper directory
QString dirName = QFileDialog::getExistingDirectory(this, tr("Data File Directory"), "",
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (dirName == "")
return;
// insert the new directory in fSearchPath_listWidget
QListWidgetItem *item = new QListWidgetItem;
if (item == 0) {
QMessageBox::critical(this, "ERROR", "Couldn't invoke QListWidgetItem! Won't do anything.");
return;
}
item->setText(dirName);
fSearchPath_listWidget->insertItem(fSearchPath_listWidget->currentRow()+1, item);
}
//----------------------------------------------------------------------------------------------------
/**
* @brief PChangeDefaultPathsDialog::deleteItem
*/
void PChangeDefaultPathsDialog::deleteItem()
{
QListWidgetItem *item = fSearchPath_listWidget->takeItem(fSearchPath_listWidget->currentRow());
if (item != 0)
delete item;
}
//----------------------------------------------------------------------------------------------------
/**
* @brief PChangeDefaultPathsDialog::saveDefaultPathList
*/
void PChangeDefaultPathsDialog::saveDefaultPathList()
{
// read the used musrfit_startup.xml
QFile fileIn(fDefaultPath->getPrefPathName());
if (!fileIn.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this, "ERROR", QString("Cannot read '%1'. Won't do anything.").arg(fDefaultPath->getPrefPathName()));
return;
}
QTextStream in(&fileIn);
QStringList fileContent;
while (!in.atEnd()) {
fileContent << in.readLine();
}
fileIn.close();
// write the new musrfit_startup.xml
QFile fileOut(fDefaultPath->getPrefPathName());
if (!fileOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, "ERROR", QString("Cannot write to '%1'.").arg(fDefaultPath->getPrefPathName()));
return;
}
QTextStream out(&fileOut);
QString str;
bool first = true;
for (int i=0; i<fileContent.count(); i++) {
str = fileContent[i];
if (!str.trimmed().startsWith("<data_path>")) {
out << fileContent[i] << endl;
} else {
if (first) {
first = false;
for (int j=0; j<fSearchPath_listWidget->count(); j++)
out << " <data_path>" << fSearchPath_listWidget->item(j)->text() << "</data_path>" << endl;
}
}
}
fileOut.close();
}
//----------------------------------------------------------------------------------------------------
// END
//----------------------------------------------------------------------------------------------------

View File

@ -0,0 +1,104 @@
/****************************************************************************
PChangeDefaultPathsDialog.h
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
*****************************************************************************/
/***************************************************************************
* Copyright (C) 2010-2017 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _PCHANGEDEFAULTPATHSDIALOG_H_
#define _PCHANGEDEFAULTPATHSDIALOG_H_
#include <QDialog>
#include <QMessageBox>
#include <QtXml>
#include "ui_PChangeDefaultPathsDialog.h"
class PDefaultPaths;
//---------------------------------------------------------------------------
class PDefaultPathsXMLParser : public QXmlDefaultHandler
{
public:
PDefaultPathsXMLParser(PDefaultPaths *defaultPaths);
virtual ~PDefaultPathsXMLParser() {}
private:
enum EAdminKeyWords {eEmpty, eDataPath};
bool startDocument();
bool startElement( const QString&, const QString&, const QString& ,
const QXmlAttributes& );
bool endElement( const QString&, const QString&, const QString& );
bool characters(const QString&);
bool endDocument();
bool warning( const QXmlParseException & exception );
bool error( const QXmlParseException & exception );
bool fatalError( const QXmlParseException & exception );
EAdminKeyWords fKeyWord; ///< key word tag to know how to handle the content
PDefaultPaths *fDefaultPaths; ///< keeps the default search paths for the data files
};
//---------------------------------------------------------------------------
class PDefaultPaths : public QObject
{
public:
PDefaultPaths();
virtual ~PDefaultPaths() {}
virtual bool isValid() { return fValid; }
virtual void appendDefaultPath(QString str) { fDefaultPath << str; }
virtual QStringList *getDefaultPathList() { return &fDefaultPath; }
virtual QString getPrefPathName() { return fPrefPathName; }
private:
friend class PDefaultPathsXMLParser;
bool fValid;
QString fPrefPathName;
QStringList fDefaultPath;
};
//---------------------------------------------------------------------------
class PChangeDefaultPathsDialog : public QDialog, private Ui::PChangeDefaultPathsDialog
{
Q_OBJECT
public:
PChangeDefaultPathsDialog();
private slots:
void deleteItem();
void addItem();
void saveDefaultPathList();
private:
PDefaultPaths *fDefaultPath;
};
#endif // _PCHANGEDEFAULTPATHSDIALOG_H_

View File

@ -27,6 +27,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "PChangeDefaultPathsDialog.h"
#include "PPrefsDialog.h"
//----------------------------------------------------------------------------------------------------
@ -66,6 +67,8 @@ PPrefsDialog::PPrefsDialog(PAdmin *admin) : fAdmin(admin)
fTimeout_lineEdit->setText(QString("%1").arg(fAdmin->getTimeout()));
fTimeout_lineEdit->setValidator(new QIntValidator(fTimeout_lineEdit));
QObject::connect(fDefaultPath_pushButton, SIGNAL(clicked()), this, SLOT(handleDefaultPaths()));
}
//----------------------------------------------------------------------------------------------------
@ -106,6 +109,22 @@ void PPrefsDialog::dumpRoot()
fDumpAscii_checkBox->setChecked(false);
}
//----------------------------------------------------------------------------------------------------
/**
*<p>SLOT: called when the QPushButton 'Change Default Search Paths' is clicked. Will call a
* dialog which allows to deal with the default search paths to look for data files.
*/
void PPrefsDialog::handleDefaultPaths()
{
PChangeDefaultPathsDialog *dlg = new PChangeDefaultPathsDialog();
if (dlg->exec() == QDialog::Accepted) {
}
delete dlg;
}
//----------------------------------------------------------------------------------------------------
// END
//----------------------------------------------------------------------------------------------------

View File

@ -8,7 +8,7 @@
*****************************************************************************/
/***************************************************************************
* Copyright (C) 2010-2016 by Andreas Suter *
* Copyright (C) 2010-2017 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
@ -59,6 +59,7 @@ class PPrefsDialog : public QDialog, private Ui::PPrefsDialog
public slots:
void dumpAscii();
void dumpRoot();
void handleDefaultPaths();
private:
PAdmin *fAdmin;

View File

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PChangeDefaultPathsDialog</class>
<widget class="QDialog" name="PChangeDefaultPathsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>528</width>
<height>363</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>9</x>
<y>10</y>
<width>512</width>
<height>341</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="fSearchPath_listWidget"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>328</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="fAdd_pushButton">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="fDelete_pushButton">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>PChangeDefaultPathsDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>228</x>
<y>332</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>PChangeDefaultPathsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>296</x>
<y>338</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -33,7 +33,7 @@
<item>
<widget class="QTabWidget" name="fTabWidget">
<property name="currentIndex">
<number>3</number>
<number>0</number>
</property>
<widget class="QWidget" name="fGeneral_tab">
<attribute name="title">
@ -74,6 +74,19 @@
</item>
</layout>
</widget>
<widget class="QPushButton" name="fDefaultPath_pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>421</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Change Default Search Paths</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="fMusrfit_tab">
<attribute name="title">

View File

@ -104,6 +104,7 @@ HEADERS = musredit.h \
PGetFourierBlockDialog.h \
PGetPlotBlockDialog.h \
PMsr2DataDialog.h \
PChangeDefaultPathsDialog.h \
PMusrEditAbout.h
SOURCES = PHelp.cpp \
@ -128,6 +129,7 @@ SOURCES = PHelp.cpp \
PGetFourierBlockDialog.cpp \
PGetPlotBlockDialog.cpp \
PMsr2DataDialog.cpp \
PChangeDefaultPathsDialog.cpp \
PMusrEditAbout.cpp \
main.cpp
@ -147,7 +149,8 @@ FORMS = forms/PFindDialog.ui \
forms/PGetNonMusrRunBlockDialog.ui \
forms/PGetFourierBlockDialog.ui \
forms/PGetPlotBlockDialog.ui \
forms/PMsr2DataDialog.ui
forms/PMsr2DataDialog.ui \
forms/PChangeDefaultPathsDialog.ui
RESOURCES = musredit.qrc