00001
00002
00003
00004
00005
00006
00007 #include "CmdMediator.h"
00008 #include "DocumentModelExportFormat.h"
00009 #include "DocumentSerialize.h"
00010 #include "Logger.h"
00011 #include <QObject>
00012 #include <QSettings>
00013 #include <QTextStream>
00014 #include <QXmlStreamWriter>
00015 #include "Settings.h"
00016 #include "Xml.h"
00017
00018 const QStringList DEFAULT_CURVE_NAMES_NOT_EXPORTED;
00019 const double DEFAULT_POINTS_INTERVAL_FUNCTIONS = 10;
00020 const double DEFAULT_POINTS_INTERVAL_RELATIONS = 10;
00021 const QString DEFAULT_X_LABEL ("x");
00022 const ExportPointsIntervalUnits DEFAULT_POINTS_INTERVAL_UNITS_FUNCTIONS = EXPORT_POINTS_INTERVAL_UNITS_SCREEN;
00023 const ExportPointsIntervalUnits DEFAULT_POINTS_INTERVAL_UNITS_RELATIONS = EXPORT_POINTS_INTERVAL_UNITS_SCREEN;
00024 const bool DEFAULT_EXPORT_DELIMITER_OVERRIDE = false;
00025
00026 DocumentModelExportFormat::DocumentModelExportFormat()
00027 {
00028 QSettings settings (SETTINGS_ENGAUGE, SETTINGS_DIGITIZER);
00029 settings.beginGroup (SETTINGS_GROUP_EXPORT);
00030
00031 m_curveNamesNotExported = settings.value (SETTINGS_EXPORT_CURVE_NAMES_NOT_EXPORTED,
00032 QVariant (DEFAULT_CURVE_NAMES_NOT_EXPORTED)).toStringList();
00033 m_delimiter = (ExportDelimiter) settings.value (SETTINGS_EXPORT_DELIMITER,
00034 QVariant (EXPORT_DELIMITER_COMMA)).toInt();
00035 m_overrideCsvTsv = settings.value (SETTINGS_EXPORT_DELIMITER_OVERRIDE_CSV_TSV,
00036 QVariant (DEFAULT_EXPORT_DELIMITER_OVERRIDE)).toBool();
00037 m_header = (ExportHeader) settings.value (SETTINGS_EXPORT_HEADER,
00038 QVariant (EXPORT_HEADER_SIMPLE)).toInt();
00039 m_layoutFunctions = (ExportLayoutFunctions) settings.value (SETTINGS_EXPORT_LAYOUT_FUNCTIONS,
00040 QVariant (EXPORT_LAYOUT_ALL_PER_LINE)).toInt();
00041 m_pointsIntervalFunctions = settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_FUNCTIONS,
00042 QVariant (DEFAULT_POINTS_INTERVAL_FUNCTIONS)).toDouble();
00043 m_pointsIntervalRelations = settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_RELATIONS,
00044 QVariant (DEFAULT_POINTS_INTERVAL_RELATIONS)).toDouble();
00045 m_pointsIntervalUnitsFunctions = (ExportPointsIntervalUnits) settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS,
00046 QVariant (DEFAULT_POINTS_INTERVAL_UNITS_FUNCTIONS)).toInt();
00047 m_pointsIntervalUnitsRelations = (ExportPointsIntervalUnits) settings.value (SETTINGS_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS,
00048 QVariant (DEFAULT_POINTS_INTERVAL_UNITS_RELATIONS)).toInt();
00049 m_pointsSelectionFunctions = (ExportPointsSelectionFunctions) settings.value (SETTINGS_EXPORT_POINTS_SELECTION_FUNCTIONS,
00050 QVariant (EXPORT_POINTS_SELECTION_FUNCTIONS_INTERPOLATE_ALL_CURVES)).toInt();
00051 m_pointsSelectionRelations = (ExportPointsSelectionRelations) settings.value (SETTINGS_EXPORT_POINTS_SELECTION_RELATIONS,
00052 QVariant (EXPORT_POINTS_SELECTION_RELATIONS_INTERPOLATE)).toInt();
00053 m_xLabel = settings.value (SETTINGS_EXPORT_X_LABEL,
00054 QVariant (DEFAULT_X_LABEL)).toString();
00055 }
00056
00057 DocumentModelExportFormat::DocumentModelExportFormat (const Document &document) :
00058 m_curveNamesNotExported (document.modelExport().curveNamesNotExported()),
00059 m_pointsSelectionFunctions (document.modelExport().pointsSelectionFunctions()),
00060 m_pointsIntervalFunctions (document.modelExport().pointsIntervalFunctions()),
00061 m_pointsIntervalUnitsFunctions (document.modelExport().pointsIntervalUnitsFunctions()),
00062 m_pointsSelectionRelations (document.modelExport().pointsSelectionRelations()),
00063 m_pointsIntervalRelations (document.modelExport().pointsIntervalRelations()),
00064 m_pointsIntervalUnitsRelations (document.modelExport().pointsIntervalUnitsRelations()),
00065 m_layoutFunctions (document.modelExport().layoutFunctions()),
00066 m_delimiter (document.modelExport().delimiter()),
00067 m_overrideCsvTsv (document.modelExport().overrideCsvTsv()),
00068 m_header (document.modelExport().header()),
00069 m_xLabel (document.modelExport().xLabel())
00070 {
00071 }
00072
00073 DocumentModelExportFormat::DocumentModelExportFormat(const DocumentModelExportFormat &other) :
00074 m_curveNamesNotExported (other.curveNamesNotExported()),
00075 m_pointsSelectionFunctions (other.pointsSelectionFunctions()),
00076 m_pointsIntervalFunctions (other.pointsIntervalFunctions()),
00077 m_pointsIntervalUnitsFunctions (other.pointsIntervalUnitsFunctions()),
00078 m_pointsSelectionRelations (other.pointsSelectionRelations()),
00079 m_pointsIntervalRelations (other.pointsIntervalRelations()),
00080 m_pointsIntervalUnitsRelations (other.pointsIntervalUnitsRelations()),
00081 m_layoutFunctions (other.layoutFunctions()),
00082 m_delimiter (other.delimiter()),
00083 m_overrideCsvTsv (other.overrideCsvTsv()),
00084 m_header (other.header()),
00085 m_xLabel (other.xLabel ())
00086 {
00087 }
00088
00089 DocumentModelExportFormat &DocumentModelExportFormat::operator=(const DocumentModelExportFormat &other)
00090 {
00091 m_curveNamesNotExported = other.curveNamesNotExported();
00092 m_pointsSelectionFunctions = other.pointsSelectionFunctions();
00093 m_pointsIntervalFunctions = other.pointsIntervalFunctions();
00094 m_pointsIntervalUnitsFunctions = other.pointsIntervalUnitsFunctions();
00095 m_pointsSelectionRelations = other.pointsSelectionRelations();
00096 m_pointsIntervalRelations = other.pointsIntervalRelations();
00097 m_pointsIntervalUnitsRelations = other.pointsIntervalUnitsRelations();
00098 m_layoutFunctions = other.layoutFunctions();
00099 m_delimiter = other.delimiter();
00100 m_overrideCsvTsv = other.overrideCsvTsv();
00101 m_header = other.header();
00102 m_xLabel = other.xLabel();
00103
00104 return *this;
00105 }
00106
00107 QStringList DocumentModelExportFormat::curveNamesNotExported() const
00108 {
00109 return m_curveNamesNotExported;
00110 }
00111
00112 ExportDelimiter DocumentModelExportFormat::delimiter() const
00113 {
00114 return m_delimiter;
00115 }
00116
00117 ExportHeader DocumentModelExportFormat::header() const
00118 {
00119 return m_header;
00120 }
00121
00122 ExportLayoutFunctions DocumentModelExportFormat::layoutFunctions() const
00123 {
00124 return m_layoutFunctions;
00125 }
00126
00127 void DocumentModelExportFormat::loadXml(QXmlStreamReader &reader)
00128 {
00129 LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelExportFormat::loadXml";
00130
00131 bool success = true;
00132
00133 QXmlStreamAttributes attributes = reader.attributes();
00134
00135 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS) &&
00136 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS) &&
00137 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS) &&
00138 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS) &&
00139 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS) &&
00140 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS) &&
00141 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS) &&
00142 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER) &&
00143 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER) &&
00144 attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_X_LABEL)) {
00145
00146 setPointsSelectionFunctions ((ExportPointsSelectionFunctions) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS).toInt());
00147 setPointsIntervalFunctions (attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS).toDouble());
00148 setPointsIntervalUnitsFunctions ((ExportPointsIntervalUnits) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS).toInt());
00149 setPointsSelectionRelations ((ExportPointsSelectionRelations) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS).toInt());
00150 setPointsIntervalRelations (attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS).toDouble());
00151 setPointsIntervalUnitsRelations ((ExportPointsIntervalUnits) attributes.value(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS).toInt());
00152 setLayoutFunctions ((ExportLayoutFunctions) attributes.value(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS).toInt());
00153 setDelimiter ((ExportDelimiter) attributes.value (DOCUMENT_SERIALIZE_EXPORT_DELIMITER).toInt());
00154 if (attributes.hasAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV)) {
00155
00156
00157 QString stringOverrideCsvTsv = attributes.value (DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV).toString();
00158
00159 setOverrideCsvTsv(stringOverrideCsvTsv == DOCUMENT_SERIALIZE_BOOL_TRUE);
00160 }
00161 setHeader ((ExportHeader) attributes.value(DOCUMENT_SERIALIZE_EXPORT_HEADER).toInt());
00162 setXLabel (attributes.value(DOCUMENT_SERIALIZE_EXPORT_X_LABEL).toString());
00163
00164
00165 while ((loadNextFromReader (reader) != QXmlStreamReader::StartElement) ||
00166 (reader.name() != DOCUMENT_SERIALIZE_EXPORT_CURVE_NAMES_NOT_EXPORTED)) {
00167
00168 if (reader.atEnd()) {
00169 success = false;
00170 break;
00171 }
00172 }
00173
00174 if (success) {
00175
00176 QStringList curveNamesNotExported;
00177
00178 QXmlStreamReader::TokenType tokenType = loadNextFromReader(reader);
00179 while (tokenType == QXmlStreamReader::StartElement) {
00180
00181 if (reader.name() == DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED) {
00182 curveNamesNotExported << reader.text().toString();
00183 }
00184 tokenType = loadNextFromReader(reader);
00185 }
00186
00187
00188 setCurveNamesNotExported(curveNamesNotExported);
00189
00190
00191 while ((reader.tokenType() != QXmlStreamReader::EndElement) ||
00192 (reader.name() != DOCUMENT_SERIALIZE_EXPORT)){
00193 loadNextFromReader(reader);
00194 if (reader.atEnd()) {
00195 success = false;
00196 break;
00197 }
00198 }
00199 }
00200 }
00201
00202 if (!success) {
00203 reader.raiseError (QObject::tr ("Cannot read export data"));
00204 }
00205 }
00206
00207 bool DocumentModelExportFormat::overrideCsvTsv() const
00208 {
00209 return m_overrideCsvTsv;
00210 }
00211
00212 double DocumentModelExportFormat::pointsIntervalFunctions() const
00213 {
00214 return m_pointsIntervalFunctions;
00215 }
00216
00217 double DocumentModelExportFormat::pointsIntervalRelations() const
00218 {
00219 return m_pointsIntervalRelations;
00220 }
00221
00222 ExportPointsIntervalUnits DocumentModelExportFormat::pointsIntervalUnitsFunctions() const
00223 {
00224 return m_pointsIntervalUnitsFunctions;
00225 }
00226
00227 ExportPointsIntervalUnits DocumentModelExportFormat::pointsIntervalUnitsRelations() const
00228 {
00229 return m_pointsIntervalUnitsRelations;
00230 }
00231
00232 ExportPointsSelectionFunctions DocumentModelExportFormat::pointsSelectionFunctions() const
00233 {
00234 return m_pointsSelectionFunctions;
00235 }
00236
00237 ExportPointsSelectionRelations DocumentModelExportFormat::pointsSelectionRelations() const
00238 {
00239 return m_pointsSelectionRelations;
00240 }
00241
00242 void DocumentModelExportFormat::printStream(QString indentation,
00243 QTextStream &str) const
00244 {
00245 str << indentation << "DocumentModelExportFormat\n";
00246
00247 indentation += INDENTATION_DELTA;
00248
00249 str << indentation << "curveNamesNotExported=";
00250 QStringList::const_iterator itr;
00251 for (itr = m_curveNamesNotExported.begin (); itr != m_curveNamesNotExported.end(); itr++) {
00252 QString curveName = *itr;
00253 str << indentation << curveName << " ";
00254 }
00255 str << "\n";
00256
00257 str << indentation << "exportPointsSelectionFunctions="
00258 << exportPointsSelectionFunctionsToString (m_pointsSelectionFunctions) << "\n";
00259 str << indentation << "pointsIntervalFunctions=" << m_pointsIntervalFunctions << "\n";
00260 str << indentation << "pointsIntervalUnitsFunctions="
00261 << exportPointsIntervalUnitsToString (m_pointsIntervalUnitsFunctions) << "\n";
00262 str << indentation << "exportPointsSelectionRelations="
00263 << exportPointsSelectionRelationsToString (m_pointsSelectionRelations) << "\n";
00264 str << indentation << "pointsIntervalRelations=" << m_pointsIntervalRelations << "\n";
00265 str << indentation << "pointsIntervalUnitsRelations="
00266 << exportPointsIntervalUnitsToString (m_pointsIntervalUnitsRelations) << "\n";
00267 str << indentation << "exportLayoutFunctions=" << exportLayoutFunctionsToString (m_layoutFunctions) << "\n";
00268 str << indentation << "exportDelimiter=" << exportDelimiterToString (m_delimiter) << "\n";
00269 str << indentation << "overrideCsvTsv=" << (m_overrideCsvTsv ? "true" : "false") << "\n";
00270 str << indentation << "exportHeader=" << exportHeaderToString (m_header) << "\n";
00271 str << indentation << "xLabel=" << m_xLabel << "\n";
00272 }
00273
00274 void DocumentModelExportFormat::saveXml(QXmlStreamWriter &writer) const
00275 {
00276 LOG4CPP_INFO_S ((*mainCat)) << "DocumentModelExportFormat::saveXml";
00277
00278 writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT);
00279 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS, QString::number (m_pointsSelectionFunctions));
00280 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_FUNCTIONS_STRING, exportPointsSelectionFunctionsToString (m_pointsSelectionFunctions));
00281 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_FUNCTIONS, QString::number (m_pointsIntervalFunctions));
00282 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_FUNCTIONS, QString::number (m_pointsIntervalUnitsFunctions));
00283 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS, QString::number (m_pointsSelectionRelations));
00284 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_SELECTION_RELATIONS_STRING, exportPointsSelectionRelationsToString (m_pointsSelectionRelations));
00285 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_UNITS_RELATIONS, QString::number (m_pointsIntervalUnitsRelations));
00286 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_POINTS_INTERVAL_RELATIONS, QString::number (m_pointsIntervalRelations));
00287 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS, QString::number (m_layoutFunctions));
00288 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_LAYOUT_FUNCTIONS_STRING, exportLayoutFunctionsToString (m_layoutFunctions));
00289 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER, QString::number (m_delimiter));
00290 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_OVERRIDE_CSV_TSV, m_overrideCsvTsv ?
00291 DOCUMENT_SERIALIZE_BOOL_TRUE :
00292 DOCUMENT_SERIALIZE_BOOL_FALSE);
00293 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_DELIMITER_STRING, exportDelimiterToString (m_delimiter));
00294 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER, QString::number (m_header));
00295 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_HEADER_STRING, exportHeaderToString (m_header));
00296 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_X_LABEL, m_xLabel);
00297
00298
00299 writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAMES_NOT_EXPORTED);
00300 QStringList::const_iterator itr;
00301 for (itr = m_curveNamesNotExported.begin (); itr != m_curveNamesNotExported.end (); itr++) {
00302 QString curveNameNotExported = *itr;
00303 writer.writeStartElement(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED);
00304 writer.writeAttribute(DOCUMENT_SERIALIZE_EXPORT_CURVE_NAME_NOT_EXPORTED_NAME, curveNameNotExported);
00305 writer.writeEndElement();
00306 }
00307 writer.writeEndElement();
00308
00309 writer.writeEndElement();
00310 }
00311
00312 void DocumentModelExportFormat::setCurveNamesNotExported(const QStringList &curveNamesNotExported)
00313 {
00314 m_curveNamesNotExported = curveNamesNotExported;
00315 }
00316
00317 void DocumentModelExportFormat::setDelimiter(ExportDelimiter delimiter)
00318 {
00319 m_delimiter = delimiter;
00320 }
00321
00322 void DocumentModelExportFormat::setHeader(ExportHeader header)
00323 {
00324 m_header = header;
00325 }
00326
00327 void DocumentModelExportFormat::setLayoutFunctions(ExportLayoutFunctions layoutFunctions)
00328 {
00329 m_layoutFunctions = layoutFunctions;
00330 }
00331
00332 void DocumentModelExportFormat::setOverrideCsvTsv(bool overrideCsvTsv)
00333 {
00334 m_overrideCsvTsv = overrideCsvTsv;
00335 }
00336
00337 void DocumentModelExportFormat::setPointsIntervalFunctions(double pointsIntervalFunctions)
00338 {
00339 m_pointsIntervalFunctions = pointsIntervalFunctions;
00340 }
00341
00342 void DocumentModelExportFormat::setPointsIntervalRelations(double pointsIntervalRelations)
00343 {
00344 m_pointsIntervalRelations = pointsIntervalRelations;
00345 }
00346
00347 void DocumentModelExportFormat::setPointsIntervalUnitsFunctions(ExportPointsIntervalUnits pointsIntervalUnitsFunctions)
00348 {
00349 m_pointsIntervalUnitsFunctions = pointsIntervalUnitsFunctions;
00350 }
00351
00352 void DocumentModelExportFormat::setPointsIntervalUnitsRelations(ExportPointsIntervalUnits pointsIntervalUnitsRelations)
00353 {
00354 m_pointsIntervalUnitsRelations = pointsIntervalUnitsRelations;
00355 }
00356
00357 void DocumentModelExportFormat::setPointsSelectionFunctions(ExportPointsSelectionFunctions pointsSelectionFunctions)
00358 {
00359 m_pointsSelectionFunctions = pointsSelectionFunctions;
00360 }
00361
00362 void DocumentModelExportFormat::setPointsSelectionRelations(ExportPointsSelectionRelations pointsSelectionRelations)
00363 {
00364 m_pointsSelectionRelations = pointsSelectionRelations;
00365 }
00366
00367 void DocumentModelExportFormat::setXLabel (const QString &xLabel)
00368 {
00369 m_xLabel = xLabel;
00370 }
00371
00372 QString DocumentModelExportFormat::xLabel () const
00373 {
00374 return m_xLabel;
00375 }