Skip to content

Commit

Permalink
Rename constant fields in ImportOds for consistency and clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Jan 13, 2025
1 parent cb3c690 commit fa770f5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 56 deletions.
46 changes: 23 additions & 23 deletions include/eible/ImportOds.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,28 @@ class EIBLE_EXPORT ImportOds : public ImportSpreadsheet

std::optional<QStringList> sheetNames_{std::nullopt};

const QString TABLE_TAG{QStringLiteral("table")};
const QString TABLE_ROW_TAG{QStringLiteral("table-row")};
const QString TABLE_CELL_TAG{QStringLiteral("table-cell")};
const QString OFFICE_VALUE_TYPE_TAG{QStringLiteral("office:value-type")};
const QString COLUMNS_REPEATED_TAG{
const QString tableTag_{QStringLiteral("table")};
const QString tableRowTag_{QStringLiteral("table-row")};
const QString tableCellTag_{QStringLiteral("table-cell")};
const QString officeValueTypeTag_{QStringLiteral("office:value-type")};
const QString columnsRepeatedTag_{
QStringLiteral("table:number-columns-repeated")};
const QString STRING_TAG{QStringLiteral("string")};
const QString DATE_TAG{QStringLiteral("date")};
const QString FLOAT_TAG{QStringLiteral("float")};
const QString PERCENTAGE_TAG{QStringLiteral("percentage")};
const QString CURRENCY_TAG{QStringLiteral("currency")};
const QString TIME_TAG{QStringLiteral("time")};
const QString P_TAG{QStringLiteral("p")};
const QString OFFICE_DATE_VALUE_TAG{QStringLiteral("office:date-value")};
const QString OFFICE_VALUE_TAG{QStringLiteral("office:value")};
const QString DATE_FORMAT{QStringLiteral("yyyy-MM-dd")};
const QString TABLE_QUALIFIED_NAME{QStringLiteral("table:table")};
const QString TABLE_NAME_TAG{QStringLiteral("table:name")};

const QStringList RECOCNIZED_COLUMN_TYPES{STRING_TAG, DATE_TAG,
FLOAT_TAG, PERCENTAGE_TAG,
CURRENCY_TAG, TIME_TAG};

const int ODS_STRING_DATE_LENGTH{10};
const QString stringTag_{QStringLiteral("string")};
const QString dateTag_{QStringLiteral("date")};
const QString floatTag_{QStringLiteral("float")};
const QString percentageTag_{QStringLiteral("percentage")};
const QString currencyTag_{QStringLiteral("currency")};
const QString timeTag_{QStringLiteral("time")};
const QString pTag_{QStringLiteral("p")};
const QString officeDateValueTag_{QStringLiteral("office:date-value")};
const QString officeValueTag_{QStringLiteral("office:value")};
const QString dateFormat_{QStringLiteral("yyyy-MM-dd")};
const QString tableQualifiedName_{QStringLiteral("table:table")};
const QString tableNameTag_{QStringLiteral("table:name")};

const QStringList recognizedColumnTypes_{stringTag_, dateTag_,
floatTag_, percentageTag_,
currencyTag_, timeTag_};

const int odsStringDateLength_{10};
};
63 changes: 30 additions & 33 deletions src/ImportOds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ std::pair<bool, QVector<QVector<QVariant>>> ImportOds::getLimitedData(
int rowCounter{0};
bool rowEmpty{true};
int lastEmittedPercent{0};
while ((!reader.atEnd()) && (reader.name() != TABLE_TAG) &&
while ((!reader.atEnd()) && (reader.name() != tableTag_) &&
(rowCounter < rowLimit))
{
if (isRowStart(reader))
Expand Down Expand Up @@ -201,20 +201,20 @@ std::pair<bool, QStringList> ImportOds::retrieveColumnNames(
reader.setDevice(&quaZipFile);
skipToSheet(reader, sheetName);

while ((!reader.atEnd()) && (reader.name() != TABLE_ROW_TAG))
while ((!reader.atEnd()) && (reader.name() != tableRowTag_))
reader.readNext();
reader.readNext();

QXmlStreamReader::TokenType lastToken{reader.tokenType()};
QStringList columnNames;

while ((!reader.atEnd()) && (reader.name() != TABLE_ROW_TAG))
while ((!reader.atEnd()) && (reader.name() != tableRowTag_))
{
if (isCellStart(reader) &&
(getColumnRepeatCount(reader.attributes()) > 1))
return {true, columnNames};

if ((reader.name().toString() == P_TAG) && reader.isStartElement())
if ((reader.name().toString() == pTag_) && reader.isStartElement())
{
while (reader.tokenType() != QXmlStreamReader::Characters)
reader.readNext();
Expand Down Expand Up @@ -260,7 +260,7 @@ ImportOds::retrieveRowCountAndColumnTypes(const QString& sheetName)
int rowCounter{0};
bool rowEmpty{true};

while ((!reader.atEnd()) && (reader.name() != TABLE_TAG))
while ((!reader.atEnd()) && (reader.name() != tableTag_))
{
if (isRowStart(reader))
column = NOT_SET_COLUMN;
Expand All @@ -270,7 +270,7 @@ ImportOds::retrieveRowCountAndColumnTypes(const QString& sheetName)
++column;
const QXmlStreamAttributes attributes{reader.attributes()};
const QString xmlColTypeValue{
attributes.value(OFFICE_VALUE_TYPE_TAG).toString()};
attributes.value(officeValueTypeTag_).toString()};
const int repeats{getColumnRepeatCount(attributes)};
if (isRecognizedColumnType(attributes))
{
Expand Down Expand Up @@ -305,7 +305,7 @@ void ImportOds::moveToSecondRow(const QString& sheetName,

bool secondRow{false};
while ((!reader.atEnd()) &&
(!((reader.qualifiedName() == TABLE_QUALIFIED_NAME) &&
(!((reader.qualifiedName() == tableQualifiedName_) &&
reader.isEndElement())))
{
if (isRowStart(reader))
Expand All @@ -324,9 +324,9 @@ void ImportOds::skipToSheet(QXmlStreamReader& reader,
{
while (!reader.atEnd())
{
if (reader.qualifiedName() == TABLE_QUALIFIED_NAME)
if (reader.qualifiedName() == tableQualifiedName_)
{
if (reader.attributes().value(TABLE_NAME_TAG) != sheetName)
if (reader.attributes().value(tableNameTag_) != sheetName)
reader.skipCurrentElement();
else
return;
Expand All @@ -338,61 +338,58 @@ void ImportOds::skipToSheet(QXmlStreamReader& reader,
bool ImportOds::isRecognizedColumnType(
const QXmlStreamAttributes& attributes) const
{
const QString columnType{
attributes.value(OFFICE_VALUE_TYPE_TAG).toString()};
return RECOCNIZED_COLUMN_TYPES.contains(columnType);
const QString columnType{attributes.value(officeValueTypeTag_).toString()};
return recognizedColumnTypes_.contains(columnType);
}

int ImportOds::getColumnRepeatCount(
const QXmlStreamAttributes& attributes) const
{
const int repeats{
attributes.value(COLUMNS_REPEATED_TAG).toString().toInt()};
const int repeats{attributes.value(columnsRepeatedTag_).toString().toInt()};
return std::max(1, repeats);
}

bool ImportOds::isRowStart(const QXmlStreamReader& reader) const
{
return (reader.name() == TABLE_ROW_TAG) && reader.isStartElement();
return (reader.name() == tableRowTag_) && reader.isStartElement();
}

bool ImportOds::isRowEnd(const QXmlStreamReader& reader) const
{
return (reader.name() == TABLE_ROW_TAG) && reader.isEndElement();
return (reader.name() == tableRowTag_) && reader.isEndElement();
}

bool ImportOds::isCellStart(const QXmlStreamReader& reader) const
{
return (reader.name() == TABLE_CELL_TAG) && reader.isStartElement();
return (reader.name() == tableCellTag_) && reader.isStartElement();
}

bool ImportOds::isCellEnd(const QXmlStreamReader& reader) const
{
return (reader.name().toString() == TABLE_CELL_TAG) &&
reader.isEndElement();
return (reader.name().toString() == tableCellTag_) && reader.isEndElement();
}

ColumnType ImportOds::recognizeColumnType(ColumnType currentType,
const QString& xmlColTypeValue) const
{
if (xmlColTypeValue == STRING_TAG)
if (xmlColTypeValue == stringTag_)
{
if (currentType == ColumnType::UNKNOWN)
return ColumnType::STRING;
if (currentType != ColumnType::STRING)
return ColumnType::STRING;
}

if (xmlColTypeValue == DATE_TAG)
if (xmlColTypeValue == dateTag_)
{
if (currentType == ColumnType::UNKNOWN)
return ColumnType::DATE;
if (currentType != ColumnType::DATE)
return ColumnType::STRING;
}

if ((xmlColTypeValue == FLOAT_TAG) || (xmlColTypeValue == PERCENTAGE_TAG) ||
(xmlColTypeValue == CURRENCY_TAG) || (xmlColTypeValue == TIME_TAG))
if ((xmlColTypeValue == floatTag_) || (xmlColTypeValue == percentageTag_) ||
(xmlColTypeValue == currencyTag_) || (xmlColTypeValue == timeTag_))
{
if (currentType == ColumnType::UNKNOWN)
return ColumnType::NUMBER;
Expand All @@ -407,19 +404,19 @@ QVariant ImportOds::retrieveValueFromStringColumn(
QXmlStreamReader& reader) const
{
const QString xmlColTypeValue{
reader.attributes().value(OFFICE_VALUE_TYPE_TAG).toString()};
reader.attributes().value(officeValueTypeTag_).toString()};
const QString emptyString(QString::fromLatin1(""));
const QString currentDateValue{
reader.attributes().value(OFFICE_DATE_VALUE_TAG).toString()};
reader.attributes().value(officeDateValueTag_).toString()};

while ((!reader.atEnd()) && (reader.name() != P_TAG))
while ((!reader.atEnd()) && (reader.name() != pTag_))
reader.readNext();

while ((reader.tokenType() != QXmlStreamReader::Characters) &&
(reader.name() != TABLE_CELL_TAG))
(reader.name() != tableCellTag_))
reader.readNext();

if (xmlColTypeValue == DATE_TAG)
if (xmlColTypeValue == dateTag_)
return {currentDateValue};

const QStringView stringView{reader.text()};
Expand All @@ -437,18 +434,18 @@ QVariant ImportOds::retrieveValueFromField(QXmlStreamReader& reader,
return retrieveValueFromStringColumn(reader);

if (columnType == ColumnType::NUMBER)
return {reader.attributes().value(OFFICE_VALUE_TAG).toDouble()};
return {reader.attributes().value(officeValueTag_).toDouble()};

QString dateValue{
reader.attributes().value(OFFICE_DATE_VALUE_TAG).toString()};
dateValue.chop(dateValue.size() - ODS_STRING_DATE_LENGTH);
return QDate::fromString(dateValue, DATE_FORMAT);
reader.attributes().value(officeDateValueTag_).toString()};
dateValue.chop(dateValue.size() - odsStringDateLength_);
return QDate::fromString(dateValue, dateFormat_);
}

bool ImportOds::isOfficeValueTagEmpty(const QXmlStreamReader& reader) const
{
const QString xmlColTypeValue{
reader.attributes().value(OFFICE_VALUE_TYPE_TAG).toString()};
reader.attributes().value(officeValueTypeTag_).toString()};
return xmlColTypeValue.isEmpty();
}

Expand Down

0 comments on commit fa770f5

Please sign in to comment.