Reference documentation for deal.II version 8.4.2
xml_parameter_writer.cpp
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2013 by Martin Steigemann and Wolfgang Bangerth
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <QtGui>
18 
19 #include "xml_parameter_writer.h"
20 
21 
22 namespace dealii
23 {
24  namespace ParameterGui
25  {
26  XMLParameterWriter::XMLParameterWriter(QTreeWidget *tree_widget)
27  : tree_widget(tree_widget)
28  {
29  xml.setAutoFormatting(true); // enable auto-formatting
30  }
31 
32 
33 
34  bool XMLParameterWriter::write_xml_file(QIODevice *device)
35  {
36  xml.setDevice(device); // setup the output device
37  xml.writeStartDocument(); // write the head <?xml ... ?>
38  xml.writeStartElement("ParameterHandler"); // write the root element <ParameterHandler>
39  // loop over the elements
40  for (int i = 0; i < tree_widget->topLevelItemCount(); ++i)
41  write_item(tree_widget->topLevelItem(i)); // and write the items
42 
43  xml.writeEndDocument() ; // close the first element
44 
45  return true;
46  }
47 
48 
49 
50  void XMLParameterWriter::write_item(QTreeWidgetItem *item)
51  {
52  QString tag_name = mangle(item->text(0)); // store the element name
53 
54  xml.writeStartElement(tag_name); // and write <tag_name> to the file
55 
56  if (!item->text(1).isEmpty()) // if the "value"-entry of this item is not empty
57  { // we have a parameter
58  xml.writeTextElement("value", item->data(1,Qt::EditRole).toString());
59  xml.writeTextElement("default_value", item->text(2)); // and we write its values
60  xml.writeTextElement("documentation", item->text(3));
61  xml.writeTextElement("pattern", item->text(4));
62  xml.writeTextElement("pattern_description", item->text(5));
63  };
64 
65  for (int i = 0; i < item->childCount(); ++i) // go over the childrens recursively
66  write_item(item->child(i));
67 
68  xml.writeEndElement(); // write closing </tag_name>
69  }
70 
71 
72 
73  QString XMLParameterWriter::mangle (const QString &s)
74  {
75  std::string s_temp (s.toStdString()); // this function is copied from
76  // the ParameterHandler class
77  std::string u; // and adapted to mangle QString
78  u.reserve (s_temp.size());
79 
80  static const std::string allowed_characters
81  ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
82 
83  // for all parts of the string, see
84  // if it is an allowed character or
85  // not
86  for (unsigned int i=0; i<s_temp.size(); ++i)
87  if (allowed_characters.find (s_temp[i]) != std::string::npos)
88  u.push_back (s_temp[i]);
89  else
90  {
91  u.push_back ('_');
92  static const char hex[16]
93  = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
94  u.push_back (hex[static_cast<unsigned char>(s_temp[i])/16]);
95  u.push_back (hex[static_cast<unsigned char>(s_temp[i])%16]);
96  }
97 
98  QString v (u.c_str());
99 
100  return v;
101  }
102  }
103 }