Engauge Digitizer  2
DocumentModelExportFormat.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "CmdMediator.h"
8 #include "DocumentModelExportFormat.h"
9 #include "DocumentSerialize.h"
10 #include "Logger.h"
11 #include <QObject>
12 #include <QSettings>
13 #include <QTextStream>
14 #include <QXmlStreamWriter>
15 #include "Settings.h"
16 #include "Xml.h"
17 
18 const QStringList DEFAULT_CURVE_NAMES_NOT_EXPORTED;
19 const double DEFAULT_POINTS_INTERVAL_FUNCTIONS = 10; // Consistent with DEFAULT_POINTS_INTERVAL_UNITS_FUNCTIONS
20 const double DEFAULT_POINTS_INTERVAL_RELATIONS = 10; // Consistent with DEFAULT_POINTS_INTERVAL_UNITS_RELATIONS
21 const QString DEFAULT_X_LABEL ("x");
22 const ExportPointsIntervalUnits DEFAULT_POINTS_INTERVAL_UNITS_FUNCTIONS = EXPORT_POINTS_INTERVAL_UNITS_SCREEN; // Consistent with DEFAULT_POINTS_INTERVAL_FUNCTIONS
23 const ExportPointsIntervalUnits DEFAULT_POINTS_INTERVAL_UNITS_RELATIONS = EXPORT_POINTS_INTERVAL_UNITS_SCREEN; // Consistent with DEFAULT_POINTS_INTERVAL_RELATIONS
24 
26 {
27  QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
28  settings.beginGroup (SETTINGS_GROUP_EXPORT);
29 
30  m_curveNamesNotExported = settings.value (SETTINGS_EXPORT_CURVE_NAMES_NOT_EXPORTED,
31  QVariant (DEFAULT_CURVE_NAMES_NOT_EXPORTED)).toStringList();
32  m_delimiter = (ExportDelimiter) settings.value (SETTINGS_EXPORT_DELIMITER,
33  QVariant (EXPORT_DELIMITER_COMMA)).toInt();
34  m_overrideCsvTsv = settings.value (SETTINGS_EXPORT_DELIMITER_OVERRIDE_CSV_TSV,
35  QVariant (true)).toBool();
36  m_header = (ExportHeader) settings.value (SETTINGS_EXPORT_HEADER,
37  QVariant (EXPORT_HEADER_SIMPLE)).toInt();
38  m_layoutFunctions = (ExportLayoutFunctions) settings.value (SETTINGS_EXPORT_LAYOUT_FUNCTIONS,
39  QVariant (EXPORT_LAYOUT_ALL_PER_LINE)).toInt();
40  m_pointsIntervalFunctions = settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_FUNCTIONS,
41  QVariant (DEFAULT_POINTS_INTERVAL_FUNCTIONS)).toDouble();
42  m_pointsIntervalRelations = settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_RELATIONS,
43  QVariant (DEFAULT_POINTS_INTERVAL_RELATIONS)).toDouble();
44  m_pointsIntervalUnitsFunctions = (ExportPointsIntervalUnits) settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS,
45  QVariant (DEFAULT_POINTS_INTERVAL_UNITS_FUNCTIONS)).toInt();
46  m_pointsIntervalUnitsRelations = (ExportPointsIntervalUnits) settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS,
47  QVariant (DEFAULT_POINTS_INTERVAL_UNITS_RELATIONS)).toInt();
48  m_pointsSelectionFunctions = (ExportPointsSelectionFunctions) settings.value (SETTINGS_EXPORT_POINTS_SELECTION_FUNCTIONS,
49  QVariant (EXPORT_POINTS_SELECTION_FUNCTIONS_INTERPOLATE_ALL_CURVES)).toInt();
50  m_pointsSelectionRelations = (ExportPointsSelectionRelations) settings.value (SETTINGS_EXPORT_POINTS_SELECTION_RELATIONS,
51  QVariant (EXPORT_POINTS_SELECTION_RELATIONS_INTERPOLATE)).toInt();
52  m_xLabel = settings.value (SETTINGS_EXPORT_X_LABEL,
53  QVariant (DEFAULT_X_LABEL)).toString();
54 }
55 
57  m_curveNamesNotExported (document.modelExport().curveNamesNotExported()),
58  m_pointsSelectionFunctions (document.modelExport().pointsSelectionFunctions()),
59  m_pointsIntervalFunctions (document.modelExport().pointsIntervalFunctions()),
60  m_pointsIntervalUnitsFunctions (document.modelExport().pointsIntervalUnitsFunctions()),
61  m_pointsSelectionRelations (document.modelExport().pointsSelectionRelations()),
62  m_pointsIntervalRelations (document.modelExport().pointsIntervalRelations()),
63  m_pointsIntervalUnitsRelations (document.modelExport().pointsIntervalUnitsRelations()),
64  m_layoutFunctions (document.modelExport().layoutFunctions()),
65  m_delimiter (document.modelExport().delimiter()),
66  m_overrideCsvTsv (document.modelExport().overrideCsvTsv()),
67  m_header (document.modelExport().header()),
68  m_xLabel (document.modelExport().xLabel())
69 {
70 }
71 
73  m_curveNamesNotExported (other.curveNamesNotExported()),
74  m_pointsSelectionFunctions (other.pointsSelectionFunctions()),
75  m_pointsIntervalFunctions (other.pointsIntervalFunctions()),
76  m_pointsIntervalUnitsFunctions (other.pointsIntervalUnitsFunctions()),
77  m_pointsSelectionRelations (other.pointsSelectionRelations()),
78  m_pointsIntervalRelations (other.pointsIntervalRelations()),
79  m_pointsIntervalUnitsRelations (other.pointsIntervalUnitsRelations()),
80  m_layoutFunctions (other.layoutFunctions()),
81  m_delimiter (other.delimiter()),
82  m_overrideCsvTsv (other.overrideCsvTsv()),
83  m_header (other.header()),
84  m_xLabel (other.xLabel ())
85 {
86 }
87 
89 {
90  m_curveNamesNotExported = other.curveNamesNotExported();
91  m_pointsSelectionFunctions = other.pointsSelectionFunctions();
92  m_pointsIntervalFunctions = other.pointsIntervalFunctions();
93  m_pointsIntervalUnitsFunctions = other.pointsIntervalUnitsFunctions();
94  m_pointsSelectionRelations = other.pointsSelectionRelations();
95  m_pointsIntervalRelations = other.pointsIntervalRelations();
96  m_pointsIntervalUnitsRelations = other.pointsIntervalUnitsRelations();
97  m_layoutFunctions = other.layoutFunctions();
98  m_delimiter = other.delimiter();
99  m_overrideCsvTsv = other.overrideCsvTsv();
100  m_header = other.header();
101  m_xLabel = other.xLabel();
102 
103  return *this;
104 }
105 
107 {
108  return m_curveNamesNotExported;
109 }
110 
112 {
113  return m_delimiter;
114 }
115 
117 {
118  return m_header;
119 }
120 
121 ExportLayoutFunctions DocumentModelExportFormat::layoutFunctions() const
122 {
123  return m_layoutFunctions;
124 }
125 
126 void DocumentModelExportFormat::loadXml(QXmlStreamReader &reader)
127 {
128  LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelExportFormat::loadXml";
129 
130  bool success = true;
131 
132  QXmlStreamAttributes attributes = reader.attributes();
133 
134  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS) &&
135  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS) &&
136  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS) &&
137  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS) &&
138  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS) &&
139  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS) &&
140  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS) &&
141  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER) &&
142  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER) &&
143  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_X_LABEL)) {
144 
145  setPointsSelectionFunctions ((ExportPointsSelectionFunctions) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS).toInt());
146  setPointsIntervalFunctions (attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS).toDouble());
147  setPointsIntervalUnitsFunctions ((ExportPointsIntervalUnits) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS).toInt());
148  setPointsSelectionRelations ((ExportPointsSelectionRelations) attributes.value(DOCUMENT_SERIALIZE_COORDS_SCALE_Y_RADIUS).toInt());
149  setPointsIntervalRelations (attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS).toDouble());
150  setPointsIntervalUnitsRelations ((ExportPointsIntervalUnits) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS).toInt());
151  setLayoutFunctions ((ExportLayoutFunctions) attributes.value(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS).toInt());
152  setDelimiter ((ExportDelimiter) attributes.value (DOCUMENT_SERIALIZE_EXPORT_DELIMITER).toInt());
153  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV)) {
154 
155  // Boolean value
156  QString stringOverrideCsvTsv = attributes.value (DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV).toString();
157 
158  setOverrideCsvTsv(stringOverrideCsvTsv == DOCUMENT_SERIALIZE_BOOL_TRUE);
159  }
160  setHeader ((ExportHeader) attributes.value(DOCUMENT_SERIALIZE_EXPORT_HEADER).toInt());
161  setXLabel (attributes.value(DOCUMENT_SERIALIZE_EXPORT_X_LABEL).toString());
162 
163  // Read element containing excluded curve names
164  while ((loadNextFromReader (reader) != QXmlStreamReader::StartElement) ||
165  (reader.name() != DOCUMENT_SERIALIZE_EXPORT_CURVE_NAMES_NOT_EXPORTED)) {
166 
167  if (reader.atEnd()) {
168  success = false;
169  break;
170  }
171  }
172 
173  if (success) {
174 
175  QStringList curveNamesNotExported;
176 
177  QXmlStreamReader::TokenType tokenType = loadNextFromReader(reader);
178  while (tokenType == QXmlStreamReader::StartElement) {
179 
180  if (reader.name() == DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED) {
181  curveNamesNotExported << reader.text().toString();
182  }
183  tokenType = loadNextFromReader(reader);
184  }
185 
186  // Save curve names
187  setCurveNamesNotExported(curveNamesNotExported);
188 
189  // Read until end of this subtree
190  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
191  (reader.name() != DOCUMENT_SERIALIZE_EXPORT)){
192  loadNextFromReader(reader);
193  if (reader.atEnd()) {
194  success = false;
195  break;
196  }
197  }
198  }
199  }
200 
201  if (!success) {
202  reader.raiseError (QObject::tr ("Cannot read export data"));
203  }
204 }
205 
207 {
208  return m_overrideCsvTsv;
209 }
210 
212 {
213  return m_pointsIntervalFunctions;
214 }
215 
217 {
218  return m_pointsIntervalRelations;
219 }
220 
222 {
223  return m_pointsIntervalUnitsFunctions;
224 }
225 
227 {
228  return m_pointsIntervalUnitsRelations;
229 }
230 
231 ExportPointsSelectionFunctions DocumentModelExportFormat::pointsSelectionFunctions() const
232 {
233  return m_pointsSelectionFunctions;
234 }
235 
236 ExportPointsSelectionRelations DocumentModelExportFormat::pointsSelectionRelations() const
237 {
238  return m_pointsSelectionRelations;
239 }
240 
241 void DocumentModelExportFormat::printStream(QString indentation,
242  QTextStream &str) const
243 {
244  str << indentation << "DocumentModelExportFormat\n";
245 
246  indentation += INDENTATION_DELTA;
247 
248  str << indentation << "curveNamesNotExported=";
249  QStringList::const_iterator itr;
250  for (itr = m_curveNamesNotExported.begin (); itr != m_curveNamesNotExported.end(); itr++) {
251  QString curveName = *itr;
252  str << indentation << curveName << " ";
253  }
254  str << "\n";
255 
256  str << indentation << "exportPointsSelectionFunctions="
257  << exportPointsSelectionFunctionsToString (m_pointsSelectionFunctions) << "\n";
258  str << indentation << "pointsIntervalFunctions=" << m_pointsIntervalFunctions << "\n";
259  str << indentation << "pointsIntervalUnitsFunctions="
260  << exportPointsIntervalUnitsToString (m_pointsIntervalUnitsFunctions) << "\n";
261  str << indentation << "exportPointsSelectionRelations="
262  << exportPointsSelectionRelationsToString (m_pointsSelectionRelations) << "\n";
263  str << indentation << "pointsIntervalRelations=" << m_pointsIntervalRelations << "\n";
264  str << indentation << "pointsIntervalUnitsRelations="
265  << exportPointsIntervalUnitsToString (m_pointsIntervalUnitsRelations) << "\n";
266  str << indentation << "exportLayoutFunctions=" << exportLayoutFunctionsToString (m_layoutFunctions) << "\n";
267  str << indentation << "exportDelimiter=" << exportDelimiterToString (m_delimiter) << "\n";
268  str << indentation << "overrideCsvTsv=" << (m_overrideCsvTsv ? "true" : "false") << "\n";
269  str << indentation << "exportHeader=" << exportHeaderToString (m_header) << "\n";
270  str << indentation << "xLabel=" << m_xLabel << "\n";
271 }
272 
273 void DocumentModelExportFormat::saveXml(QXmlStreamWriter &writer) const
274 {
275  LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelExportFormat::saveXml";
276 
277  writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT);
278  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS, QString::number (m_pointsSelectionFunctions));
279  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS_STRING, exportPointsSelectionFunctionsToString (m_pointsSelectionFunctions));
280  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS, QString::number (m_pointsIntervalFunctions));
281  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS, QString::number (m_pointsIntervalUnitsFunctions));
282  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS, QString::number (m_pointsSelectionRelations));
283  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS_STRING, exportPointsSelectionRelationsToString (m_pointsSelectionRelations));
284  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS, QString::number (m_pointsIntervalUnitsRelations));
285  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS, QString::number (m_pointsIntervalRelations));
286  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS, QString::number (m_layoutFunctions));
287  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS_STRING, exportLayoutFunctionsToString (m_layoutFunctions));
288  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER, QString::number (m_delimiter));
289  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV, m_overrideCsvTsv ?
290  DOCUMENT_SERIALIZE_BOOL_TRUE :
291  DOCUMENT_SERIALIZE_BOOL_FALSE);
292  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_STRING, exportDelimiterToString (m_delimiter));
293  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER, QString::number (m_header));
294  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER_STRING, exportHeaderToString (m_header));
295  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_X_LABEL, m_xLabel);
296 
297  // Loop through curve names that are not to be exported
298  writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAMES_NOT_EXPORTED);
299  QStringList::const_iterator itr;
300  for (itr = m_curveNamesNotExported.begin (); itr != m_curveNamesNotExported.end (); itr++) {
301  QString curveNameNotExported = *itr;
302  writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED);
303  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED_NAME, curveNameNotExported);
304  writer.writeEndElement();
305  }
306  writer.writeEndElement();
307 
308  writer.writeEndElement();
309 }
310 
312 {
313  m_curveNamesNotExported = curveNamesNotExported;
314 }
315 
317 {
318  m_delimiter = delimiter;
319 }
320 
322 {
323  m_header = header;
324 }
325 
327 {
328  m_layoutFunctions = layoutFunctions;
329 }
330 
332 {
333  m_overrideCsvTsv = overrideCsvTsv;
334 }
335 
337 {
338  m_pointsIntervalFunctions = pointsIntervalFunctions;
339 }
340 
342 {
343  m_pointsIntervalRelations = pointsIntervalRelations;
344 }
345 
347 {
348  m_pointsIntervalUnitsFunctions = pointsIntervalUnitsFunctions;
349 }
350 
352 {
353  m_pointsIntervalUnitsRelations = pointsIntervalUnitsRelations;
354 }
355 
357 {
358  m_pointsSelectionFunctions = pointsSelectionFunctions;
359 }
360 
362 {
363  m_pointsSelectionRelations = pointsSelectionRelations;
364 }
365 
367 {
368  m_xLabel = xLabel;
369 }
370 
372 {
373  return m_xLabel;
374 }
void setPointsSelectionFunctions(ExportPointsSelectionFunctions exportPointsSelectionFunctions)
Set method for point selection for functions.
ExportPointsSelectionFunctions pointsSelectionFunctions() const
Get method for point selection for functions.
ExportLayoutFunctions layoutFunctions() const
Get method for functions layout.
DocumentModelExportFormat()
Default constructor.
ExportPointsIntervalUnits pointsIntervalUnitsRelations() const
Get method for points interval units for relations.
ExportPointsIntervalUnits pointsIntervalUnitsFunctions() const
Get method for points interval units for functions.
Model for DlgSettingsExportFormat and CmdSettingsExportFormat.
void setPointsSelectionRelations(ExportPointsSelectionRelations exportPointsSelectionRelations)
Set method for point selection for relations.
virtual void saveXml(QXmlStreamWriter &writer) const
Save entire model as xml into stream.
void setCurveNamesNotExported(const QStringList &curveNamesNotExported)
Set method for curve names not exported.
void setDelimiter(ExportDelimiter exportDelimiter)
Set method for delimiter.
void setPointsIntervalFunctions(double pointsIntervalFunctions)
Set method for points interval for functions.
double pointsIntervalFunctions() const
Get method for points interval for functions.
ExportHeader header() const
Get method for header.
void setLayoutFunctions(ExportLayoutFunctions exportLayoutFunctions)
Set method for functions layout.
QString xLabel() const
Get method for x label.
void setPointsIntervalUnitsFunctions(ExportPointsIntervalUnits pointsIntervalUnitsFunctions)
Set method for points interval units for functions.
virtual void loadXml(QXmlStreamReader &reader)
Load model from serialized xml.
DocumentModelExportFormat & operator=(const DocumentModelExportFormat &other)
Assignment constructor.
ExportDelimiter delimiter() const
Get method for delimiter.
QStringList curveNamesNotExported() const
Get method for curve names not exported.
void setPointsIntervalRelations(double pointsIntervalRelations)
Set method for relations interval for relations.
double pointsIntervalRelations() const
Get method for relations interval for relations.
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
void setPointsIntervalUnitsRelations(ExportPointsIntervalUnits pointsIntervalUnitsRelations)
Set method for points interval units for relations.
void setHeader(ExportHeader exportHeader)
Set method for header.
void setOverrideCsvTsv(bool overrideCsvTsv)
Set method for csv/tsv format override.
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
ExportPointsSelectionRelations pointsSelectionRelations() const
Get method for point selection for relations.
bool overrideCsvTsv() const
Get method for csv/tsv format override.
void setXLabel(const QString &xLabel)
Set method for x label.