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 const bool DEFAULT_EXPORT_DELIMITER_OVERRIDE = false; // Target beginner users who expect simplest behavior. Issue #169
25 
27 {
28  QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
29  settings.beginGroup (SETTINGS_GROUP_EXPORT);
30 
31  m_curveNamesNotExported = settings.value (SETTINGS_EXPORT_CURVE_NAMES_NOT_EXPORTED,
32  QVariant (DEFAULT_CURVE_NAMES_NOT_EXPORTED)).toStringList();
33  m_delimiter = (ExportDelimiter) settings.value (SETTINGS_EXPORT_DELIMITER,
34  QVariant (EXPORT_DELIMITER_COMMA)).toInt();
35  m_overrideCsvTsv = settings.value (SETTINGS_EXPORT_DELIMITER_OVERRIDE_CSV_TSV,
36  QVariant (DEFAULT_EXPORT_DELIMITER_OVERRIDE)).toBool();
37  m_header = (ExportHeader) settings.value (SETTINGS_EXPORT_HEADER,
38  QVariant (EXPORT_HEADER_SIMPLE)).toInt();
39  m_layoutFunctions = (ExportLayoutFunctions) settings.value (SETTINGS_EXPORT_LAYOUT_FUNCTIONS,
40  QVariant (EXPORT_LAYOUT_ALL_PER_LINE)).toInt();
41  m_pointsIntervalFunctions = settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_FUNCTIONS,
42  QVariant (DEFAULT_POINTS_INTERVAL_FUNCTIONS)).toDouble();
43  m_pointsIntervalRelations = settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_RELATIONS,
44  QVariant (DEFAULT_POINTS_INTERVAL_RELATIONS)).toDouble();
45  m_pointsIntervalUnitsFunctions = (ExportPointsIntervalUnits) settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS,
46  QVariant (DEFAULT_POINTS_INTERVAL_UNITS_FUNCTIONS)).toInt();
47  m_pointsIntervalUnitsRelations = (ExportPointsIntervalUnits) settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS,
48  QVariant (DEFAULT_POINTS_INTERVAL_UNITS_RELATIONS)).toInt();
49  m_pointsSelectionFunctions = (ExportPointsSelectionFunctions) settings.value (SETTINGS_EXPORT_POINTS_SELECTION_FUNCTIONS,
50  QVariant (EXPORT_POINTS_SELECTION_FUNCTIONS_INTERPOLATE_ALL_CURVES)).toInt();
51  m_pointsSelectionRelations = (ExportPointsSelectionRelations) settings.value (SETTINGS_EXPORT_POINTS_SELECTION_RELATIONS,
52  QVariant (EXPORT_POINTS_SELECTION_RELATIONS_INTERPOLATE)).toInt();
53  m_xLabel = settings.value (SETTINGS_EXPORT_X_LABEL,
54  QVariant (DEFAULT_X_LABEL)).toString();
55 }
56 
58  m_curveNamesNotExported (document.modelExport().curveNamesNotExported()),
59  m_pointsSelectionFunctions (document.modelExport().pointsSelectionFunctions()),
60  m_pointsIntervalFunctions (document.modelExport().pointsIntervalFunctions()),
61  m_pointsIntervalUnitsFunctions (document.modelExport().pointsIntervalUnitsFunctions()),
62  m_pointsSelectionRelations (document.modelExport().pointsSelectionRelations()),
63  m_pointsIntervalRelations (document.modelExport().pointsIntervalRelations()),
64  m_pointsIntervalUnitsRelations (document.modelExport().pointsIntervalUnitsRelations()),
65  m_layoutFunctions (document.modelExport().layoutFunctions()),
66  m_delimiter (document.modelExport().delimiter()),
67  m_overrideCsvTsv (document.modelExport().overrideCsvTsv()),
68  m_header (document.modelExport().header()),
69  m_xLabel (document.modelExport().xLabel())
70 {
71 }
72 
74  m_curveNamesNotExported (other.curveNamesNotExported()),
75  m_pointsSelectionFunctions (other.pointsSelectionFunctions()),
76  m_pointsIntervalFunctions (other.pointsIntervalFunctions()),
77  m_pointsIntervalUnitsFunctions (other.pointsIntervalUnitsFunctions()),
78  m_pointsSelectionRelations (other.pointsSelectionRelations()),
79  m_pointsIntervalRelations (other.pointsIntervalRelations()),
80  m_pointsIntervalUnitsRelations (other.pointsIntervalUnitsRelations()),
81  m_layoutFunctions (other.layoutFunctions()),
82  m_delimiter (other.delimiter()),
83  m_overrideCsvTsv (other.overrideCsvTsv()),
84  m_header (other.header()),
85  m_xLabel (other.xLabel ())
86 {
87 }
88 
90 {
91  m_curveNamesNotExported = other.curveNamesNotExported();
92  m_pointsSelectionFunctions = other.pointsSelectionFunctions();
93  m_pointsIntervalFunctions = other.pointsIntervalFunctions();
94  m_pointsIntervalUnitsFunctions = other.pointsIntervalUnitsFunctions();
95  m_pointsSelectionRelations = other.pointsSelectionRelations();
96  m_pointsIntervalRelations = other.pointsIntervalRelations();
97  m_pointsIntervalUnitsRelations = other.pointsIntervalUnitsRelations();
98  m_layoutFunctions = other.layoutFunctions();
99  m_delimiter = other.delimiter();
100  m_overrideCsvTsv = other.overrideCsvTsv();
101  m_header = other.header();
102  m_xLabel = other.xLabel();
103 
104  return *this;
105 }
106 
108 {
109  return m_curveNamesNotExported;
110 }
111 
113 {
114  return m_delimiter;
115 }
116 
118 {
119  return m_header;
120 }
121 
122 ExportLayoutFunctions DocumentModelExportFormat::layoutFunctions() const
123 {
124  return m_layoutFunctions;
125 }
126 
127 void DocumentModelExportFormat::loadXml(QXmlStreamReader &reader)
128 {
129  LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelExportFormat::loadXml";
130 
131  bool success = true;
132 
133  QXmlStreamAttributes attributes = reader.attributes();
134 
135  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS) &&
136  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS) &&
137  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS) &&
138  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS) &&
139  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS) &&
140  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS) &&
141  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS) &&
142  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER) &&
143  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER) &&
144  attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_X_LABEL)) {
145 
146  setPointsSelectionFunctions ((ExportPointsSelectionFunctions) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS).toInt());
147  setPointsIntervalFunctions (attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS).toDouble());
148  setPointsIntervalUnitsFunctions ((ExportPointsIntervalUnits) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS).toInt());
149  setPointsSelectionRelations ((ExportPointsSelectionRelations) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS).toInt());
150  setPointsIntervalRelations (attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS).toDouble());
151  setPointsIntervalUnitsRelations ((ExportPointsIntervalUnits) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS).toInt());
152  setLayoutFunctions ((ExportLayoutFunctions) attributes.value(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS).toInt());
153  setDelimiter ((ExportDelimiter) attributes.value (DOCUMENT_SERIALIZE_EXPORT_DELIMITER).toInt());
154  if (attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV)) {
155 
156  // Boolean value
157  QString stringOverrideCsvTsv = attributes.value (DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV).toString();
158 
159  setOverrideCsvTsv(stringOverrideCsvTsv == DOCUMENT_SERIALIZE_BOOL_TRUE);
160  }
161  setHeader ((ExportHeader) attributes.value(DOCUMENT_SERIALIZE_EXPORT_HEADER).toInt());
162  setXLabel (attributes.value(DOCUMENT_SERIALIZE_EXPORT_X_LABEL).toString());
163 
164  // Read element containing excluded curve names
165  while ((loadNextFromReader (reader) != QXmlStreamReader::StartElement) ||
166  (reader.name() != DOCUMENT_SERIALIZE_EXPORT_CURVE_NAMES_NOT_EXPORTED)) {
167 
168  if (reader.atEnd()) {
169  success = false;
170  break;
171  }
172  }
173 
174  if (success) {
175 
176  QStringList curveNamesNotExported;
177 
178  QXmlStreamReader::TokenType tokenType = loadNextFromReader(reader);
179  while (tokenType == QXmlStreamReader::StartElement) {
180 
181  if (reader.name() == DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED) {
182  curveNamesNotExported << reader.text().toString();
183  }
184  tokenType = loadNextFromReader(reader);
185  }
186 
187  // Save curve names
188  setCurveNamesNotExported(curveNamesNotExported);
189 
190  // Read until end of this subtree
191  while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
192  (reader.name() != DOCUMENT_SERIALIZE_EXPORT)){
193  loadNextFromReader(reader);
194  if (reader.atEnd()) {
195  success = false;
196  break;
197  }
198  }
199  }
200  }
201 
202  if (!success) {
203  reader.raiseError (QObject::tr ("Cannot read export data"));
204  }
205 }
206 
208 {
209  return m_overrideCsvTsv;
210 }
211 
213 {
214  return m_pointsIntervalFunctions;
215 }
216 
218 {
219  return m_pointsIntervalRelations;
220 }
221 
223 {
224  return m_pointsIntervalUnitsFunctions;
225 }
226 
228 {
229  return m_pointsIntervalUnitsRelations;
230 }
231 
232 ExportPointsSelectionFunctions DocumentModelExportFormat::pointsSelectionFunctions() const
233 {
234  return m_pointsSelectionFunctions;
235 }
236 
237 ExportPointsSelectionRelations DocumentModelExportFormat::pointsSelectionRelations() const
238 {
239  return m_pointsSelectionRelations;
240 }
241 
242 void DocumentModelExportFormat::printStream(QString indentation,
243  QTextStream &str) const
244 {
245  str << indentation << "DocumentModelExportFormat\n";
246 
247  indentation += INDENTATION_DELTA;
248 
249  str << indentation << "curveNamesNotExported=";
250  QStringList::const_iterator itr;
251  for (itr = m_curveNamesNotExported.begin (); itr != m_curveNamesNotExported.end(); itr++) {
252  QString curveName = *itr;
253  str << indentation << curveName << " ";
254  }
255  str << "\n";
256 
257  str << indentation << "exportPointsSelectionFunctions="
258  << exportPointsSelectionFunctionsToString (m_pointsSelectionFunctions) << "\n";
259  str << indentation << "pointsIntervalFunctions=" << m_pointsIntervalFunctions << "\n";
260  str << indentation << "pointsIntervalUnitsFunctions="
261  << exportPointsIntervalUnitsToString (m_pointsIntervalUnitsFunctions) << "\n";
262  str << indentation << "exportPointsSelectionRelations="
263  << exportPointsSelectionRelationsToString (m_pointsSelectionRelations) << "\n";
264  str << indentation << "pointsIntervalRelations=" << m_pointsIntervalRelations << "\n";
265  str << indentation << "pointsIntervalUnitsRelations="
266  << exportPointsIntervalUnitsToString (m_pointsIntervalUnitsRelations) << "\n";
267  str << indentation << "exportLayoutFunctions=" << exportLayoutFunctionsToString (m_layoutFunctions) << "\n";
268  str << indentation << "exportDelimiter=" << exportDelimiterToString (m_delimiter) << "\n";
269  str << indentation << "overrideCsvTsv=" << (m_overrideCsvTsv ? "true" : "false") << "\n";
270  str << indentation << "exportHeader=" << exportHeaderToString (m_header) << "\n";
271  str << indentation << "xLabel=" << m_xLabel << "\n";
272 }
273 
274 void DocumentModelExportFormat::saveXml(QXmlStreamWriter &writer) const
275 {
276  LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelExportFormat::saveXml";
277 
278  writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT);
279  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS, QString::number (m_pointsSelectionFunctions));
280  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS_STRING, exportPointsSelectionFunctionsToString (m_pointsSelectionFunctions));
281  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS, QString::number (m_pointsIntervalFunctions));
282  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS, QString::number (m_pointsIntervalUnitsFunctions));
283  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS, QString::number (m_pointsSelectionRelations));
284  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS_STRING, exportPointsSelectionRelationsToString (m_pointsSelectionRelations));
285  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS, QString::number (m_pointsIntervalUnitsRelations));
286  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS, QString::number (m_pointsIntervalRelations));
287  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS, QString::number (m_layoutFunctions));
288  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS_STRING, exportLayoutFunctionsToString (m_layoutFunctions));
289  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER, QString::number (m_delimiter));
290  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV, m_overrideCsvTsv ?
291  DOCUMENT_SERIALIZE_BOOL_TRUE :
292  DOCUMENT_SERIALIZE_BOOL_FALSE);
293  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_STRING, exportDelimiterToString (m_delimiter));
294  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER, QString::number (m_header));
295  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER_STRING, exportHeaderToString (m_header));
296  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_X_LABEL, m_xLabel);
297 
298  // Loop through curve names that are not to be exported
299  writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAMES_NOT_EXPORTED);
300  QStringList::const_iterator itr;
301  for (itr = m_curveNamesNotExported.begin (); itr != m_curveNamesNotExported.end (); itr++) {
302  QString curveNameNotExported = *itr;
303  writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED);
304  writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED_NAME, curveNameNotExported);
305  writer.writeEndElement();
306  }
307  writer.writeEndElement();
308 
309  writer.writeEndElement();
310 }
311 
313 {
314  m_curveNamesNotExported = curveNamesNotExported;
315 }
316 
318 {
319  m_delimiter = delimiter;
320 }
321 
323 {
324  m_header = header;
325 }
326 
328 {
329  m_layoutFunctions = layoutFunctions;
330 }
331 
333 {
334  m_overrideCsvTsv = overrideCsvTsv;
335 }
336 
338 {
339  m_pointsIntervalFunctions = pointsIntervalFunctions;
340 }
341 
343 {
344  m_pointsIntervalRelations = pointsIntervalRelations;
345 }
346 
348 {
349  m_pointsIntervalUnitsFunctions = pointsIntervalUnitsFunctions;
350 }
351 
353 {
354  m_pointsIntervalUnitsRelations = pointsIntervalUnitsRelations;
355 }
356 
358 {
359  m_pointsSelectionFunctions = pointsSelectionFunctions;
360 }
361 
363 {
364  m_pointsSelectionRelations = pointsSelectionRelations;
365 }
366 
368 {
369  m_xLabel = xLabel;
370 }
371 
373 {
374  return m_xLabel;
375 }
void setPointsSelectionFunctions(ExportPointsSelectionFunctions exportPointsSelectionFunctions)
Set method for point selection for functions.
bool overrideCsvTsv() const
Get method for csv/tsv format override.
QStringList curveNamesNotExported() const
Get method for curve names not exported.
void printStream(QString indentation, QTextStream &str) const
Debugging method that supports print method of this class and printStream method of some other class(...
DocumentModelExportFormat()
Default constructor.
virtual void saveXml(QXmlStreamWriter &writer) const
Save entire model as xml into stream.
Model for DlgSettingsExportFormat and CmdSettingsExportFormat.
void setPointsSelectionRelations(ExportPointsSelectionRelations exportPointsSelectionRelations)
Set method for point selection for relations.
void setCurveNamesNotExported(const QStringList &curveNamesNotExported)
Set method for curve names not exported.
ExportPointsSelectionRelations pointsSelectionRelations() const
Get method for point selection for relations.
double pointsIntervalFunctions() const
Get method for points interval for functions.
void setDelimiter(ExportDelimiter exportDelimiter)
Set method for delimiter.
void setPointsIntervalFunctions(double pointsIntervalFunctions)
Set method for points interval for functions.
void setLayoutFunctions(ExportLayoutFunctions exportLayoutFunctions)
Set method for functions layout.
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.
ExportPointsIntervalUnits pointsIntervalUnitsRelations() const
Get method for points interval units for relations.
ExportDelimiter delimiter() const
Get method for delimiter.
void setPointsIntervalRelations(double pointsIntervalRelations)
Set method for relations interval for relations.
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
ExportPointsIntervalUnits pointsIntervalUnitsFunctions() const
Get method for points interval units for functions.
ExportHeader header() const
Get method for header.
double pointsIntervalRelations() const
Get method for relations interval for relations.
ExportLayoutFunctions layoutFunctions() const
Get method for functions layout.
QString xLabel() const
Get method for x label.
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.
ExportPointsSelectionFunctions pointsSelectionFunctions() const
Get method for point selection for functions.
void setXLabel(const QString &xLabel)
Set method for x label.