From f96fd45a90eab119522ef324a76840c35e769ad7 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 5 Oct 2020 11:45:16 +0300 Subject: [PATCH] New feature Export to individual measurements. --- src/app/tape/tmainwindow.cpp | 108 ++++++++++++++++++++++++++--- src/app/tape/tmainwindow.h | 5 ++ src/app/tape/tmainwindow.ui | 21 ++++++ src/libs/vformat/vmeasurements.cpp | 2 +- 4 files changed, 126 insertions(+), 10 deletions(-) diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 9834a4715..50ee030f2 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -2110,6 +2110,85 @@ void TMainWindow::FullCircumferenceChanged(bool checked) InitDimensionControls(); } +//--------------------------------------------------------------------------------------------------------------------- +void TMainWindow::ExportToIndividual() +{ + QString dir; + if (curFile.isEmpty()) + { + dir = qApp->TapeSettings()->GetPathIndividualMeasurements(); + } + else + { + dir = QFileInfo(curFile).absolutePath(); + } + + bool usedNotExistedDir = false; + QDir directory(dir); + if (not directory.exists()) + { + usedNotExistedDir = directory.mkpath(QChar('.')); + } + + QString filters = tr("Individual measurements") + QStringLiteral(" (*.vit)"); + QString fName = tr("measurements.vit"); + QString fileName = QFileDialog::getSaveFileName(this, tr("Export to individual"), dir + QChar('/') + fName, + filters); + + auto RemoveTempDir = qScopeGuard([usedNotExistedDir, dir]() + { + if (usedNotExistedDir) + { + QDir(dir).rmpath(QChar('.')); + } + }); + + if (fileName.isEmpty()) + { + return; + } + + QString suffix = QStringLiteral("vit"); + QFileInfo f( fileName ); + if (f.suffix().isEmpty() && f.suffix() != suffix) + { + fileName += QChar('.') + suffix; + } + + QScopedPointer tmpData(new VContainer(qApp->TrVars(), &mUnit, VContainer::UniqueNamespace())); + + VMeasurements individualMeasurements(mUnit, tmpData.data()); + + const QMap > orderedTable = OrderedMeasurments(); + QMap >::const_iterator iMap; + for (iMap = orderedTable.constBegin(); iMap != orderedTable.constEnd(); ++iMap) + { + const QSharedPointer &meash = iMap.value(); + individualMeasurements.AddEmpty(meash->GetName()); + individualMeasurements.SetMValue(meash->GetName(), QString::number(*meash->GetValue())); + individualMeasurements.SetMSpecialUnits(meash->GetName(), meash->IsSpecialUnits()); + if (meash->IsCustom()) + { + individualMeasurements.SetMDescription(meash->GetName(), meash->GetDescription()); + individualMeasurements.SetMFullName(meash->GetName(), meash->GetGuiText()); + } + } + + QString error; + const bool result = individualMeasurements.SaveDocument(fileName, error); + if (not result) + { + QMessageBox messageBox; + messageBox.setIcon(QMessageBox::Warning); + messageBox.setInformativeText(tr("Could not save the file")); + messageBox.setDefaultButton(QMessageBox::Ok); + messageBox.setDetailedText(error); + messageBox.setStandardButtons(QMessageBox::Ok); + messageBox.exec(); + return; + } +} + //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::SetupMenu() { @@ -2387,6 +2466,10 @@ void TMainWindow::InitMenu() actionFullCircumference->setChecked(m->IsFullCircumference()); ui->menuMeasurements->addAction(actionFullCircumference); connect(actionFullCircumference, &QAction::triggered, this, &TMainWindow::FullCircumferenceChanged); + + ui->actionExportToIndividual->setVisible(true); + ui->actionExportToIndividual->setEnabled(true); + connect(ui->actionExportToIndividual, &QAction::triggered, this, &TMainWindow::ExportToIndividual); } } @@ -2778,15 +2861,7 @@ void TMainWindow::RefreshTable(bool freshCall) ShowUnits(); - const QMap > table = data->DataMeasurements(); - QMap > orderedTable; - QMap >::const_iterator iterMap; - for (iterMap = table.constBegin(); iterMap != table.constEnd(); ++iterMap) - { - const QSharedPointer &meash = iterMap.value(); - orderedTable.insert(meash->Index(), meash); - } - + const QMap > orderedTable = OrderedMeasurments(); qint32 currentRow = -1; QMap >::const_iterator iMap; ui->tableWidget->setRowCount ( orderedTable.size() ); @@ -3981,6 +4056,21 @@ QVector TMainWindow::DimensionRestrictedValues(int index, const Measurement return QVector(); } +//--------------------------------------------------------------------------------------------------------------------- +QMap > TMainWindow::OrderedMeasurments() const +{ + const QMap > table = data->DataMeasurements(); + QMap > orderedTable; + QMap >::const_iterator iterMap; + for (iterMap = table.constBegin(); iterMap != table.constEnd(); ++iterMap) + { + const QSharedPointer &meash = iterMap.value(); + orderedTable.insert(meash->Index(), meash); + } + + return orderedTable; +} + //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::SetDecimals() { diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h index ef81284cc..c7b26a8ad 100644 --- a/src/app/tape/tmainwindow.h +++ b/src/app/tape/tmainwindow.h @@ -44,6 +44,7 @@ namespace Ui class QLabel; class QxtCsvModel; +class VMeasurement; class TMainWindow : public VAbstractMainWindow { @@ -136,6 +137,8 @@ private slots: void FullCircumferenceChanged(bool checked); + void ExportToIndividual(); + private: Q_DISABLE_COPY(TMainWindow) Ui::TMainWindow *ui; @@ -243,6 +246,8 @@ private: void SetCurrentDimensionValues(); QVector DimensionRestrictedValues(int index, const MeasurementDimension_p &dimension); + + QMap > OrderedMeasurments() const; }; #endif // TMAINWINDOW_H diff --git a/src/app/tape/tmainwindow.ui b/src/app/tape/tmainwindow.ui index e5ce51c77..5fee81df1 100644 --- a/src/app/tape/tmainwindow.ui +++ b/src/app/tape/tmainwindow.ui @@ -963,6 +963,7 @@ + @@ -1332,6 +1333,26 @@ QAction::NoRole + + + false + + + false + + + Export to individual + + + Export to individual measurements + + + false + + + QAction::NoRole + + diff --git a/src/libs/vformat/vmeasurements.cpp b/src/libs/vformat/vmeasurements.cpp index 8a8916a1a..033f3ef63 100644 --- a/src/libs/vformat/vmeasurements.cpp +++ b/src/libs/vformat/vmeasurements.cpp @@ -132,7 +132,7 @@ VMeasurements::VMeasurements(Unit unit, VContainer *data) { SCASSERT(data != nullptr) - CreateEmptyIndividualFile(unit); + CreateEmptyIndividualFile(unit); } //---------------------------------------------------------------------------------------------------------------------