musredit 1.0.0
Loading...
Searching...
No Matches
PChangeDefaultPathsDialog.cpp
Go to the documentation of this file.
1/****************************************************************************
2
3 PChangeDefaultPathsDialog.cpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8*****************************************************************************/
9
10/***************************************************************************
11 * Copyright (C) 2010-2026 by Andreas Suter *
12 * andreas.suter@psi.ch *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29
43
44#include <QString>
45#include <QStringView>
46#include <QMessageBox>
47#include <QFileDialog>
48#include <QListWidgetItem>
49#include <QIODevice>
50#include <QFile>
51#include <QProcessEnvironment>
52#include <QTextStream>
53
55
56//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57// Implementation of PDefaultPathsXMLParser
58//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
71 fDefaultPaths(defaultPaths)
72{
73 fValid = false;
75
76 QFile file(fln);
77 if (!file.open(QFile::ReadOnly | QFile::Text)) {
78 // warning and create default - STILL MISSING
79 }
80
81 fValid = parse(&file);
82}
83
84//--------------------------------------------------------------------------
98bool PDefaultPathsXMLParser::parse(QIODevice *device)
99{
100 fXml.setDevice(device);
101
102 bool expectChars = false;
103 while (!fXml.atEnd()) {
104 fXml.readNext();
105 if (fXml.isStartDocument()) {
107 } else if (fXml.isStartElement()) {
108 startElement();
109 expectChars = true;
110 } else if (fXml.isCharacters() && expectChars) {
111 characters();
112 } else if (fXml.isEndElement()) {
113 endElement();
114 expectChars = false;
115 } else if (fXml.isEndDocument()) {
116 endDocument();
117 }
118 }
119 if (fXml.hasError()) {
120 QString msg;
121 msg = QString("%1 Line %2, column %3").arg(fXml.errorString()).arg(fXml.lineNumber()).arg(fXml.columnNumber());
122 QMessageBox::critical(nullptr, "ERROR", msg, QMessageBox::Ok, QMessageBox::NoButton);
123 return false;
124 }
125
126 return true;
127}
128
129//--------------------------------------------------------------------------
139{
140 // nothing to be done here for now
141 return true;
142}
143
144//--------------------------------------------------------------------------
155{
156 QString qName = fXml.name().toString();
157
158 if (qName == "data_path") {
160 }
161
162 return true;
163}
164
165//--------------------------------------------------------------------------
175{
177
178 return true;
179}
180
181//--------------------------------------------------------------------------
192{
193 QString str = fXml.text().toString();
194 if (str.isEmpty())
195 return true;
196
197 switch (fKeyWord) {
198 case eDataPath:
199 fDefaultPaths->appendDefaultPath(str);
200 break;
201 default:
202 break;
203 }
204
205 return true;
206}
207
208//--------------------------------------------------------------------------
218{
219 return true;
220}
221
222//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
223// Implementation of PDefaultPaths
224//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
243{
244 fValid = true;
245
246 // XML Parser part
247 // 1st: check local directory
248 QString path = QString("./");
249 QString fln = QString("musrfit_startup.xml");
250 QString pathFln = path + fln;
251 QProcessEnvironment procEnv = QProcessEnvironment::systemEnvironment();
252 if (!QFile::exists(pathFln)) {
253 // 2nd: check $HOME/.musrfit/musrfit_startup.xml
254 path = procEnv.value("HOME", "");
255 pathFln = path + "/.musrfit/" + fln;
256 if (!QFile::exists(pathFln)) {
257 // 3rd: check $MUSRFITPATH/musrfit_startup.xml
258 path = procEnv.value("MUSRFITPATH", "");
259 pathFln = path + "/" + fln;
260 if (!QFile::exists(pathFln)) {
261 // 4th: check $ROOTSYS/bin/musrfit_startup.xml
262 path = procEnv.value("ROOTSYS", "");
263 pathFln = path + "/bin/" + fln;
264 }
265 }
266 }
267 fPrefPathName = pathFln;
268
269 // read musrfit_startup.xml and extract default data file search paths
271 if (!handler.isValid()) {
272 fValid = false;
273 QMessageBox::critical(nullptr, "ERROR",
274 "Error parsing musrfit_startup.xml settings file.\nProbably a few things will not work porperly.\nPlease fix this first.",
275 QMessageBox::Ok, QMessageBox::NoButton);
276 return;
277 }
278}
279
280//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
281// Implementation of PChangeDefaultPathsDialog
282//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
299{
300 fDefaultPath = std::make_unique<PDefaultPaths>();
301 if (!fDefaultPath->isValid())
302 return;
303
304 QStringList *strList = fDefaultPath->getDefaultPathList();
305 if (strList == 0)
306 return;
307
308 setupUi(this);
309
310 setModal(true);
311
312 // populate default search paths
313 for (int i=0; i<strList->count(); i++) {
314 fSearchPath_listWidget->addItem(strList->at(i));
315 }
316
317 QObject::connect(fDelete_pushButton, SIGNAL(clicked()), this, SLOT(deleteItem()));
318 QObject::connect(fAdd_pushButton, SIGNAL(clicked()), this, SLOT(addItem()));
319 QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(saveDefaultPathList()));
320}
321
322//----------------------------------------------------------------------------------------------------
335{
336 // call QFileDialog in order to get the proper directory
337 QString dirName = QFileDialog::getExistingDirectory(this, tr("Data File Directory"), "",
338 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
339 if (dirName == "")
340 return;
341
342 // insert the new directory in fSearchPath_listWidget
343 QListWidgetItem *item = new QListWidgetItem;
344 if (item == 0) {
345 QMessageBox::critical(this, "ERROR", "Couldn't invoke QListWidgetItem! Won't do anything.");
346 return;
347 }
348 item->setText(dirName);
349 fSearchPath_listWidget->insertItem(fSearchPath_listWidget->currentRow()+1, item);
350}
351
352//----------------------------------------------------------------------------------------------------
360{
361 QListWidgetItem *item = fSearchPath_listWidget->takeItem(fSearchPath_listWidget->currentRow());
362 if (item != 0)
363 delete item;
364}
365
366//----------------------------------------------------------------------------------------------------
384{
385 // read the used musrfit_startup.xml
386 QFile fileIn(fDefaultPath->getPrefPathName());
387 if (!fileIn.open(QIODevice::ReadOnly | QIODevice::Text)) {
388 QMessageBox::critical(this, "ERROR", QString("Cannot read '%1'. Won't do anything.").arg(fDefaultPath->getPrefPathName()));
389 return;
390 }
391 QTextStream in(&fileIn);
392 QStringList fileContent;
393 while (!in.atEnd()) {
394 fileContent << in.readLine();
395 }
396 fileIn.close();
397
398 // check if there is any data_path is present in the musrfit_startup.xml
399 bool dataPathPresent = false;
400 QString str;
401 for (int i=0; i<fileContent.count(); i++) {
402 str = fileContent[i];
403 if (str.trimmed().startsWith("<data_path>")) {
404 dataPathPresent = true;
405 break;
406 }
407 }
408
409 // write the new musrfit_startup.xml
410 QFile fileOut(fDefaultPath->getPrefPathName());
411 if (!fileOut.open(QIODevice::WriteOnly | QIODevice::Text)) {
412 QMessageBox::critical(this, "ERROR", QString("Cannot write to '%1'.").arg(fDefaultPath->getPrefPathName()));
413 return;
414 }
415 QTextStream out(&fileOut);
416 bool first = true;
417 for (int i=0; i<fileContent.count(); i++) {
418 str = fileContent[i];
419 if (!str.trimmed().startsWith("<data_path>")) {
420 // if not data_path was present, add the new data_paths just before the end of the musrfit_start.xml close tag
421 if ((dataPathPresent == false) && (str.trimmed().startsWith("</musrfit>"))) {
422 for (int j=0; j<fSearchPath_listWidget->count(); j++) {
423 out << " <data_path>" << fSearchPath_listWidget->item(j)->text() << "</data_path>" << Qt::endl;
424 }
425 }
426 out << fileContent[i] << Qt::endl;
427 } else {
428 if (first) {
429 first = false;
430 for (int j=0; j<fSearchPath_listWidget->count(); j++)
431 out << " <data_path>" << fSearchPath_listWidget->item(j)->text() << "</data_path>" << Qt::endl;
432 }
433 }
434 }
435 fileOut.close();
436}
437
438//----------------------------------------------------------------------------------------------------
439// END
440//----------------------------------------------------------------------------------------------------
Dialog for managing default data file search paths in musredit.
void addItem()
Add a new directory path via file browser dialog.
PChangeDefaultPathsDialog()
Constructs the default paths dialog.
std::unique_ptr< PDefaultPaths > fDefaultPath
Manages the default paths data.
void saveDefaultPathList()
Save the modified path list to the configuration file.
void deleteItem()
Remove the currently selected path from the list.
virtual bool isValid()
Check if the XML file was parsed successfully.
QXmlStreamReader fXml
Qt XML stream reader for parsing.
PDefaultPathsXMLParser(const QString &fln, PDefaultPaths *defaultPaths)
Constructs an XML parser for reading default data paths.
bool startElement()
Handler called when an XML start element is encountered.
bool characters()
Handler for XML element character content.
bool endDocument()
Handler called at the end of XML document parsing.
@ eDataPath
Processing a <data_path> element.
@ eEmpty
No element currently being processed.
bool startDocument()
Handler called at the start of XML document parsing.
bool fValid
Flag indicating successful parsing.
PDefaultPaths * fDefaultPaths
Pointer to the object storing parsed paths.
bool parse(QIODevice *device)
Parse the XML configuration file for data paths.
bool endElement()
Handler called when an XML end element is encountered.
EAdminKeyWords fKeyWord
Current element type being processed.
Container class for managing default data file search paths.
friend class PDefaultPathsXMLParser
Allow parser to access private members.
QString fPrefPathName
Full path to the active configuration file.
PDefaultPaths()
Constructs a PDefaultPaths object and loads paths from configuration.
bool fValid
Flag indicating if configuration loaded successfully.