Skip to content

Commit

Permalink
Calculate the stop condition value outside the loops and set it to a …
Browse files Browse the repository at this point in the history
…variable.
  • Loading branch information
przemek83 committed Dec 30, 2024
1 parent 367c89d commit 30bd78c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ExportData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ bool ExportData::exportView(const QAbstractItemView& view, QIODevice& ioDevice)
QByteArray content{generateHeaderContent(*model)};
int skippedRows{0};
unsigned int lastEmittedPercent{0};
for (int row{0}; row < model->rowCount(); ++row)
const int rowCount{model->rowCount()};
for (int row{0}; row < rowCount; ++row)
{
if (rowShouldBeSkipped(view, row))
{
Expand All @@ -25,8 +26,7 @@ bool ExportData::exportView(const QAbstractItemView& view, QIODevice& ioDevice)
}
content.append(generateRowContent(*model, row, skippedRows));
updateProgress(static_cast<unsigned int>(row),
static_cast<unsigned int>(model->rowCount()),
lastEmittedPercent);
static_cast<unsigned int>(rowCount), lastEmittedPercent);
}
content.append(getContentEnding());
return writeContent(content, ioDevice);
Expand Down
10 changes: 6 additions & 4 deletions src/ExportDsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ QByteArray ExportDsv::getEmptyContent() { return {}; }
QByteArray ExportDsv::generateHeaderContent(const QAbstractItemModel& model)
{
QByteArray headersContent;
for (int j{0}; j < model.columnCount(); ++j)
const int columnCount{model.columnCount()};
for (int j{0}; j < columnCount; ++j)
{
headersContent.append(
model.headerData(j, Qt::Horizontal).toString().toUtf8());
if (j != (model.columnCount() - 1))
if (j != (columnCount - 1))
headersContent.append(separator_);
}
return headersContent;
Expand All @@ -31,12 +32,13 @@ QByteArray ExportDsv::generateRowContent(const QAbstractItemModel& model,
{
QByteArray rowContent;
rowContent.append("\n");
for (int j{0}; j < model.columnCount(); ++j)
const int columnCount{model.columnCount()};
for (int j{0}; j < columnCount; ++j)
{
const QVariant actualField{model.index(row, j).data()};
if (!actualField.isNull())
variantToString(actualField, rowContent, separator_);
if (j != (model.columnCount() - 1))
if (j != (columnCount - 1))
rowContent.append(separator_);
}

Expand Down
19 changes: 12 additions & 7 deletions src/ExportXlsx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ QByteArray ExportXlsx::getEmptyContent()

QByteArray ExportXlsx::generateHeaderContent(const QAbstractItemModel& model)
{
if (columnNames_.size() != model.columnCount())
initColumnNames(model.columnCount());
const int columnCount{model.columnCount()};
if (columnNames_.size() != columnCount)
initColumnNames(columnCount);

QByteArray headersContent{QByteArrayLiteral("<sheetData>")};
headersContent.append(R"(<row r="1" spans="1:1" x14ac:dyDescent="0.25">)");
for (int j{0}; j < model.columnCount(); ++j)

for (int j{0}; j < columnCount; ++j)
{
QString header{model.headerData(j, Qt::Horizontal).toString()};
const QString clearedHeader(
Expand All @@ -91,16 +93,17 @@ QByteArray ExportXlsx::generateHeaderContent(const QAbstractItemModel& model)
QByteArray ExportXlsx::generateRowContent(const QAbstractItemModel& model,
int row, int skippedRowsCount)
{
if (columnNames_.size() != model.columnCount())
initColumnNames(model.columnCount());
const int columnCount{model.columnCount()};
if (columnNames_.size() != columnCount)
initColumnNames(columnCount);

QByteArray rowContent;
const QByteArray rowNumber{
QByteArray::number((row - skippedRowsCount) + 2)};
rowContent.append(QByteArrayLiteral("<row r=\""));
rowContent.append(rowNumber);
rowContent.append(R"(" spans="1:1" x14ac:dyDescent="0.25">)");
for (int column{0}; column < model.columnCount(); ++column)
for (int column{0}; column < columnCount; ++column)
{
const QVariant& cell{model.index(row, column).data()};
if (cell.isNull())
Expand Down Expand Up @@ -152,6 +155,8 @@ void ExportXlsx::initColumnNames(int modelColumnCount)
QHash<QByteArray, int> nameToIndexMap{
utilities::generateExcelColumnNames(modelColumnCount)};
columnNames_.resize(nameToIndexMap.size());
for (auto it{nameToIndexMap.begin()}; it != nameToIndexMap.end(); ++it)

const auto mapEnd{nameToIndexMap.end()};
for (auto it{nameToIndexMap.begin()}; it != mapEnd; ++it)
columnNames_[it.value()] = it.key();
}

0 comments on commit 30bd78c

Please sign in to comment.