]> https://gitweb.dealii.org/ - parameter_gui.git/commitdiff
Add prm writer class
authorRene Gassmoeller <rene.gassmoeller@mailbox.org>
Tue, 28 Mar 2017 16:55:49 +0000 (10:55 -0600)
committerRene Gassmoeller <rene.gassmoeller@mailbox.org>
Tue, 28 Mar 2017 17:03:32 +0000 (11:03 -0600)
CMakeLists.txt
mainwindow.cpp
prm_parameter_writer.cpp [new file with mode: 0644]
prm_parameter_writer.h [new file with mode: 0644]

index 7804883e1d734548e1a9b3b82559665a1608a333..1211ed2b224d64670838e62f10e2c11ebac2aa30 100644 (file)
@@ -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}
   )
index 533fa8bfbea7871080e6340fa627346165a07876..4e8b9de4f14ff224a29599bf3f83914630455492 100644 (file)
@@ -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 (file)
index 0000000..0c61301
--- /dev/null
@@ -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 <QtGui>
+
+#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; i<indentation_level;++i)
+                item_string.push_back("  ");
+
+              item_string.push_back("set " + item->text(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<indentation_level;++i)
+                item_string.push_front("  ");
+
+
+              for (unsigned int i=0; i<indentation_level;++i)
+                item_string.push_back("  ");
+              item_string.push_back("end\n\n");
+            }
+        }
+
+      return item_string;
+    }
+  }
+}
diff --git a/prm_parameter_writer.h b/prm_parameter_writer.h
new file mode 100644 (file)
index 0000000..a71f36a
--- /dev/null
@@ -0,0 +1,89 @@
+// ---------------------------------------------------------------------
+//
+// 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.
+//
+// ---------------------------------------------------------------------
+
+
+#ifndef PRMPARAMETERWRITER_H
+#define PRMPARAMETERWRITER_H
+
+#include <QTreeWidget>
+#include <QTreeWidgetItem>
+
+
+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 <tt>tree_widget</tt>
+         * to @p device in the PRM format.
+         */
+        bool write_prm_file (QIODevice *device);
+
+      private:
+        /**
+         * This function writes a given @p item of <tt>tree_widget</tt>
+         * 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
+         * <code>subsection</code> is written and <tt>write_item</tt> is called
+         * recursively to write the next <tt>item</tt>.
+         * 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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.