From a0889b9085905d9b4fed18d46d00aa15d63a53e4 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Tue, 28 Mar 2017 10:55:49 -0600 Subject: [PATCH] Add prm writer class --- CMakeLists.txt | 1 + mainwindow.cpp | 28 ++++++++++--- prm_parameter_writer.cpp | 91 ++++++++++++++++++++++++++++++++++++++++ prm_parameter_writer.h | 89 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 prm_parameter_writer.cpp create mode 100644 prm_parameter_writer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7804883..1211ed2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ ADD_EXECUTABLE(parameter_gui_exe parameter_delegate.cpp xml_parameter_reader.cpp xml_parameter_writer.cpp + prm_parameter_writer.cpp ${SOURCE_MOC} ${SOURCE_RCC} ) diff --git a/mainwindow.cpp b/mainwindow.cpp index 533fa8b..4e8b9de 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,6 +20,7 @@ #include "parameter_delegate.h" #include "xml_parameter_reader.h" #include "xml_parameter_writer.h" +#include "prm_parameter_writer.h" namespace dealii @@ -101,9 +102,9 @@ namespace dealii bool MainWindow::save_as() { QString file_name = // open a file dialog - QFileDialog::getSaveFileName(this, tr("Save XML Parameter File"), + QFileDialog::getSaveFileName(this, tr("Save Parameter File"), QDir::currentPath(), - tr("XML Files (*.xml)")); + tr("XML Files (*.xml);;PRM Files (*.prm)")); if (file_name.isEmpty()) // if no file was selected return false; // return false @@ -309,10 +310,27 @@ namespace dealii return false; }; - XMLParameterWriter xml_writer(tree_widget); // create a xml_writer + if (filename.contains(".xml")) + { + XMLParameterWriter writer(tree_widget); // create a xml writer + if (!writer.write_xml_file(&file)) // and write the xml file + return false; + } + else if (filename.contains(".prm")) + { + PRMParameterWriter writer(tree_widget); // create a prm writer + if (!writer.write_prm_file(&file)) // and write the prm file + return false; + } + else + { + QMessageBox::warning(this, tr("parameterGUI"), + tr("Unknown output format: %1.") + .arg(filename)); + file.remove(); - if (!xml_writer.write_xml_file(&file)) // and read the xml file - return false; + return false; + } statusBar()->showMessage(tr("File saved"), 2000); // if we succeed, show a message set_current_file(filename); // and reset the window diff --git a/prm_parameter_writer.cpp b/prm_parameter_writer.cpp new file mode 100644 index 0000000..0c61301 --- /dev/null +++ b/prm_parameter_writer.cpp @@ -0,0 +1,91 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 by Rene Gassmoeller +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +#include + +#include "prm_parameter_writer.h" + + +namespace dealii +{ + namespace ParameterGui + { + PRMParameterWriter::PRMParameterWriter(QTreeWidget *tree_widget) + : tree_widget(tree_widget) + { + } + + + + bool PRMParameterWriter::write_prm_file(QIODevice *device) + { + // loop over the elements + for (int i = 0; i < tree_widget->topLevelItemCount(); ++i) + { + const QString item_string = write_item(tree_widget->topLevelItem(i),0); + device->write(item_string.toAscii()); + } + + return true; + } + + + + QString PRMParameterWriter::write_item(const QTreeWidgetItem *item, + const unsigned int indentation_level) + { + QString item_string; + + if (item->childCount() == 0) // if the "value"-entry of this item is not empty + { // we have a parameter + bool non_default_value; + + if (item->text(5).contains("Double")) + non_default_value = item->data(1,Qt::DisplayRole).toReal() != item->data(2,Qt::DisplayRole).toReal(); + else + non_default_value = item->data(1,Qt::DisplayRole).toString() != item->data(2,Qt::DisplayRole).toString(); + + + if (non_default_value) + { + for (unsigned int i=0; itext(0) + " = " + item->data(1,Qt::EditRole).toString() + "\n"); + } + } + else + { + for (int i = 0; i < item->childCount(); ++i) + item_string.push_back(write_item(item->child(i),indentation_level+1)); + + if (!item_string.isEmpty()) + { + item_string.push_front("subsection " + item->text(0).toAscii() + "\n"); + for (unsigned int i=0; i +#include + + +namespace dealii +{ +/*! @addtogroup ParameterGui + *@{ + */ + namespace ParameterGui + { +/** + * The PRMParameterWriter class provides an interface to write parameters + * stored in a QTreeWidget to a file in deal.II's PRM format. + * This class only writes parameters that deviate from their default values to + * improve the readability of the created file. + * + * @note This class is used in the graphical user interface for the + * @ref ParameterHandler class. + * It is not compiled into the deal.II libraries and can not be used by + * applications using deal.II. + * + * @ingroup ParameterGui + * @author Rene Gassmoeller, 2017 + */ + class PRMParameterWriter + { + public: + /** + * Constructor. + * Parameter values from @p tree_widget will be written. + */ + PRMParameterWriter (QTreeWidget *tree_widget); + + /** + * This function writes the parameter values stored in tree_widget + * to @p device in the PRM format. + */ + bool write_prm_file (QIODevice *device); + + private: + /** + * This function writes a given @p item of tree_widget + * to a file in prm format. + * If the @p item is a parameter it is written if its value differs + * from its default value. If the @p item is a subsection, a start element + * subsection is written and write_item is called + * recursively to write the next item. + * If not items in this subsection differ from their default value then + * the subsection is not written. + * @p indentation_level describes the level the current item belongs to. + * 0 describes a top level item and each subsection increases the level + * by one. + * + */ + QString write_item (const QTreeWidgetItem *item, + const unsigned int indentation_level); + + /** + * A pointer to the QTreeWidget structure + * which stores the parameters. + */ + QTreeWidget * tree_widget; + }; + } +/**@}*/ +} + + +#endif -- 2.39.5