Redesign measurements import.

This commit is contained in:
Roman Telezhynskyi 2020-10-10 19:31:23 +03:00
parent eaf6975331
commit 7ba9b380b7
6 changed files with 1371 additions and 30 deletions

View File

@ -0,0 +1,916 @@
/************************************************************************
**
** @file dialogmeasurementscsvcolumns.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 9 10, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "dialogmeasurementscsvcolumns.h"
#include "ui_dialogmeasurementscsvcolumns.h"
#include "../vmisc/qxtcsvmodel.h"
#include "../vtools/dialogs/dialogtoolbox.h"
#include <QPushButton>
#include <QShowEvent>
//---------------------------------------------------------------------------------------------------------------------
DialogMeasurementsCSVColumns::DialogMeasurementsCSVColumns(const QString &filename, MeasurementsType type,
QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogMeasurementsCSVColumns),
m_fileName{filename},
m_type(type)
{
ui->setupUi(this);
HackColumnControls();
}
//---------------------------------------------------------------------------------------------------------------------
DialogMeasurementsCSVColumns::DialogMeasurementsCSVColumns(const QString &filename, MeasurementsType type,
const QList<MeasurementDimension_p> &dimensions,
QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogMeasurementsCSVColumns),
m_fileName{filename},
m_type(type),
m_dimensions{dimensions}
{
ui->setupUi(this);
HackColumnControls();
}
//---------------------------------------------------------------------------------------------------------------------
DialogMeasurementsCSVColumns::~DialogMeasurementsCSVColumns()
{
qDeleteAll(m_hackedWidgets);
delete ui;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange)
{
// retranslate designer form (single inheritance approach)
ui->retranslateUi(this);
RetranslateLabels();
InitColumnsControls();
InitImportHeaders();
ShowImportPreview();
CheckStatus();
}
// remember to call base class implementation
QDialog::changeEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::showEvent(QShowEvent *event)
{
QDialog::showEvent( event );
if ( event->spontaneous() )
{
return;
}
if (m_isInitialized)
{
return;
}
// do your init stuff here
if (not m_fileName.isEmpty())
{
InitColumnsMap();
ShowInputPreview();
InitColumnsControls();
RetranslateLabels();
InitColumnsControls();
SetDefaultColumns();
InitImportHeaders();
ShowImportPreview();
connect(ui->comboBoxName, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
connect(ui->comboBoxValue, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
if (m_type == MeasurementsType::Multisize)
{
if (m_dimensions.size() > 0)
{
connect(ui->comboBoxShiftA, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
}
if (m_dimensions.size() > 1)
{
connect(ui->comboBoxShiftB, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
}
if (m_dimensions.size() > 2)
{
connect(ui->comboBoxShiftC, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
}
}
connect(ui->comboBoxFullName, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
connect(ui->comboBoxDescription, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &DialogMeasurementsCSVColumns::ColumnChanged);
CheckStatus();
}
m_isInitialized = true;//first show windows are held
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::ColumnChanged()
{
auto *control = qobject_cast<QComboBox*>(sender());
auto SaveColum = [this, control](int column)
{
m_columnsMap[column] = control->currentData().toInt();
ShowImportPreview();
CheckStatus();
};
if (control == ui->comboBoxName)
{
if (m_type == MeasurementsType::Individual)
{
SaveColum(static_cast<int>(IndividualMeasurementsColumns::Name));
}
else
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::Name));
}
}
else if (control == ui->comboBoxValue)
{
if (m_type == MeasurementsType::Individual)
{
SaveColum(static_cast<int>(IndividualMeasurementsColumns::Value));
}
else
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::BaseValue));
}
}
else if (control == ui->comboBoxShiftA)
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::ShiftA));
}
else if (control == ui->comboBoxShiftB)
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::ShiftB));
}
else if (control == ui->comboBoxShiftC)
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::ShiftC));
}
else if (control == ui->comboBoxFullName)
{
if (m_type == MeasurementsType::Individual)
{
SaveColum(static_cast<int>(IndividualMeasurementsColumns::FullName));
}
else
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::FullName));
}
}
else if (control == ui->comboBoxDescription)
{
if (m_type == MeasurementsType::Individual)
{
SaveColum(static_cast<int>(IndividualMeasurementsColumns::Description));
}
else
{
SaveColum(static_cast<int>(MultisizeMeasurementsColumns::Description));
}
}
}
//---------------------------------------------------------------------------------------------------------------------
bool DialogMeasurementsCSVColumns::ColumnMandatory(int column) const
{
if (m_type == MeasurementsType::Individual)
{
return column < static_cast<int>(IndividualMeasurementsColumns::FullName);
}
else
{
int mandatory = 3;
if (m_dimensions.size() > 1)
{
mandatory += qMin(m_dimensions.size(), 2);
}
return static_cast<int>(column) < mandatory;
}
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogMeasurementsCSVColumns::ColumnHeader(int column) const
{
if (m_type == MeasurementsType::Individual)
{
const auto individualColumn = static_cast<IndividualMeasurementsColumns>(column);
switch(individualColumn)
{
case IndividualMeasurementsColumns::Name:
return tr("Name");
case IndividualMeasurementsColumns::Value:
return tr("Value");
case IndividualMeasurementsColumns::FullName:
return tr("Full name");
case IndividualMeasurementsColumns::Description:
return tr("Description");
default:
return QString();
}
}
else
{
const auto multisizeColumn = static_cast<MultisizeMeasurementsColumns>(column);
switch(multisizeColumn)
{
case MultisizeMeasurementsColumns::Name:
return tr("Name");
case MultisizeMeasurementsColumns::BaseValue:
return tr("Base value");
case MultisizeMeasurementsColumns::ShiftA:
if (m_dimensions.size() > 0)
{
MeasurementDimension_p dimension = m_dimensions.at(0);
return tr("Shift (%1):").arg(VAbstartMeasurementDimension::DimensionName(dimension->Type()));
}
else
{
return "Shift A";
}
case MultisizeMeasurementsColumns::ShiftB:
if (m_dimensions.size() > 1)
{
MeasurementDimension_p dimension = m_dimensions.at(1);
return tr("Shift (%1):").arg(VAbstartMeasurementDimension::DimensionName(dimension->Type()));
}
else
{
return "Shift B";
}
case MultisizeMeasurementsColumns::ShiftC:
if (m_dimensions.size() > 2)
{
MeasurementDimension_p dimension = m_dimensions.at(2);
return tr("Shift (%1):").arg(VAbstartMeasurementDimension::DimensionName(dimension->Type()));
}
else
{
return "Shift C";
}
case MultisizeMeasurementsColumns::FullName:
return tr("Full name");
case MultisizeMeasurementsColumns::Description:
return tr("Description");
default:
return QString();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
int DialogMeasurementsCSVColumns::ImportColumnCount() const
{
if (m_type == MeasurementsType::Individual)
{
return static_cast<int>(IndividualMeasurementsColumns::LAST_DO_NOT_USE);
}
else
{
return static_cast<int>(MultisizeMeasurementsColumns::LAST_DO_NOT_USE);
}
}
//---------------------------------------------------------------------------------------------------------------------
int DialogMeasurementsCSVColumns::MinimumColumns() const
{
if (m_type == MeasurementsType::Individual)
{
return 2;
}
else
{
int mandatory = 3;
if (m_dimensions.size() > 1)
{
mandatory += qMin(m_dimensions.size(), 2);
}
return mandatory;
}
}
//---------------------------------------------------------------------------------------------------------------------
bool DialogMeasurementsCSVColumns::ColumnsValid()
{
ChangeColor(ui->labelName, OkColor(this));
ChangeColor(ui->labelValue, OkColor(this));
if (m_type == MeasurementsType::Multisize)
{
if (m_dimensions.size() > 0)
{
ChangeColor(ui->labelShiftA, OkColor(this));
}
if (m_dimensions.size() > 1)
{
ChangeColor(ui->labelShiftB, OkColor(this));
}
if (m_dimensions.size() > 2)
{
ChangeColor(ui->labelShiftC, OkColor(this));
}
}
ChangeColor(ui->labelFullName, OkColor(this));
ChangeColor(ui->labelDescription, OkColor(this));
auto ColumnValid = [this](int column)
{
int value = m_columnsMap.at(column);
if (value == -1 && not ColumnMandatory(column))
{
return true;
}
for (int c=0; c < m_columnsMap.size(); ++c)
{
if (c == column)
{
continue;
}
if (value == m_columnsMap.at(c))
{
return false;
}
}
return true;
};
bool columnNameFlag = true;
bool columnValueFlag = true;
bool columnShiftAFlag = true;
bool columnShiftBFlag = true;
bool columnShiftCFlag = true;
bool columnFullNameFlag = true;
bool columnDescriptionFlag = true;
const QColor errorColor = Qt::red;
if (m_type == MeasurementsType::Multisize)
{
if (not ColumnValid(static_cast<int>(MultisizeMeasurementsColumns::Name)))
{
ChangeColor(ui->labelName, errorColor);
columnNameFlag = false;
}
}
else
{
if (not ColumnValid(static_cast<int>(IndividualMeasurementsColumns::Name)))
{
ChangeColor(ui->labelName, errorColor);
columnNameFlag = false;
}
}
if (m_type == MeasurementsType::Multisize)
{
if (not ColumnValid(static_cast<int>(MultisizeMeasurementsColumns::BaseValue)))
{
ChangeColor(ui->labelValue, errorColor);
columnValueFlag = false;
}
}
else
{
if (not ColumnValid(static_cast<int>(IndividualMeasurementsColumns::Value)))
{
ChangeColor(ui->labelValue, errorColor);
columnValueFlag = false;
}
}
if (m_type == MeasurementsType::Multisize)
{
if (m_dimensions.size() > 0)
{
if (not ColumnValid(static_cast<int>(MultisizeMeasurementsColumns::ShiftA)))
{
ChangeColor(ui->labelShiftA, errorColor);
columnShiftAFlag = false;
}
}
if (m_dimensions.size() > 1)
{
if (not ColumnValid(static_cast<int>(MultisizeMeasurementsColumns::ShiftB)))
{
ChangeColor(ui->labelShiftB, errorColor);
columnShiftBFlag = false;
}
}
if (m_dimensions.size() > 2)
{
if (not ColumnValid(static_cast<int>(MultisizeMeasurementsColumns::ShiftC)))
{
ChangeColor(ui->labelShiftC, errorColor);
columnShiftCFlag = false;
}
}
}
if (not ColumnValid(m_type == MeasurementsType::Multisize
? static_cast<int>(MultisizeMeasurementsColumns::FullName)
: static_cast<int>(IndividualMeasurementsColumns::FullName)))
{
ChangeColor(ui->labelFullName, errorColor);
columnFullNameFlag = false;
}
if (not ColumnValid(m_type == MeasurementsType::Multisize
? static_cast<int>(MultisizeMeasurementsColumns::Description)
: static_cast<int>(IndividualMeasurementsColumns::Description)))
{
ChangeColor(ui->labelDescription, errorColor);
columnDescriptionFlag = false;
}
return columnNameFlag && columnValueFlag && columnShiftAFlag && columnShiftBFlag && columnShiftCFlag &&
columnFullNameFlag && columnDescriptionFlag;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::InitColumnsMap()
{
QSharedPointer<QxtCsvModel> csv = DialogMeasurementsCSVColumns::CSVModel();
m_columnsMap.clear();
auto InitColumn = [this, csv](int column, bool forceSkip=false)
{
if (forceSkip)
{
m_columnsMap[column] = -1;
}
else
{
if (ColumnMandatory(column))
{
m_columnsMap[column] = column;
}
else
{
m_columnsMap[column] = csv->columnCount() >= column ? column : -1;
}
}
};
if (m_type == MeasurementsType::Individual)
{
m_columnsMap.resize(static_cast<int>(IndividualMeasurementsColumns::LAST_DO_NOT_USE));
for(int column = 0; column < static_cast<int>(IndividualMeasurementsColumns::LAST_DO_NOT_USE); ++column)
{
InitColumn(column);
}
}
else
{
m_columnsMap.resize(static_cast<int>(MultisizeMeasurementsColumns::LAST_DO_NOT_USE));
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::Name));
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::BaseValue));
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::ShiftA));
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::ShiftB), m_dimensions.size() < 2);
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::ShiftC), m_dimensions.size() < 3);
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::FullName));
InitColumn(static_cast<int>(MultisizeMeasurementsColumns::Description));
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::InitColumnsControls()
{
const int inputColumnCount = CSVModel()->columnCount();
auto InitControl = [this, inputColumnCount](QComboBox *control, int column)
{
SCASSERT(control != nullptr)
int currentColumn = -2;
int index = control->currentIndex();
if (index != -1)
{
currentColumn = control->currentData().toInt();
}
control->blockSignals(true);
control->clear();
for(int i=0; i < inputColumnCount; ++i)
{
control->addItem(QString::number(i+1), i);
}
if (not ColumnMandatory(column))
{
control->addItem(tr("Skip"), -1);
}
control->setCurrentIndex(-1);
index = control->findData(currentColumn);
if (index != -1)
{
control->setCurrentIndex(index);
control->blockSignals(false);
}
else
{
control->blockSignals(false);
control->setCurrentIndex(0);
}
};
if (m_type == MeasurementsType::Individual)
{
InitControl(ui->comboBoxName, static_cast<int>(IndividualMeasurementsColumns::Name));
InitControl(ui->comboBoxValue, static_cast<int>(IndividualMeasurementsColumns::Value));
InitControl(ui->comboBoxFullName, static_cast<int>(IndividualMeasurementsColumns::FullName));
InitControl(ui->comboBoxDescription, static_cast<int>(IndividualMeasurementsColumns::Description));
}
else
{
InitControl(ui->comboBoxName, static_cast<int>(MultisizeMeasurementsColumns::Name));
InitControl(ui->comboBoxValue, static_cast<int>(MultisizeMeasurementsColumns::BaseValue));
if (m_dimensions.size() > 0)
{
InitControl(ui->comboBoxShiftA, static_cast<int>(MultisizeMeasurementsColumns::ShiftA));
}
if (m_dimensions.size() > 1)
{
InitControl(ui->comboBoxShiftB, static_cast<int>(MultisizeMeasurementsColumns::ShiftB));
}
if (m_dimensions.size() > 2)
{
InitControl(ui->comboBoxShiftC, static_cast<int>(MultisizeMeasurementsColumns::ShiftC));
}
InitControl(ui->comboBoxFullName, static_cast<int>(MultisizeMeasurementsColumns::FullName));
InitControl(ui->comboBoxDescription, static_cast<int>(MultisizeMeasurementsColumns::Description));
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::InitImportHeaders()
{
if (m_fileName.isEmpty())
{
return;
}
const int columns = ImportColumnCount();
ui->tableWidgetImport->clear();
ui->tableWidgetImport->setColumnCount(columns);
auto AddHeader = [this](int column, bool visible=true)
{
QTableWidgetItem *header = new QTableWidgetItem(ColumnHeader(column));
ui->tableWidgetImport->setHorizontalHeaderItem(column, header);
ui->tableWidgetImport->setColumnHidden(column, not visible);
};
if (m_type == MeasurementsType::Individual)
{
AddHeader(static_cast<int>(IndividualMeasurementsColumns::Name));
AddHeader(static_cast<int>(IndividualMeasurementsColumns::Value));
AddHeader(static_cast<int>(IndividualMeasurementsColumns::FullName));
AddHeader(static_cast<int>(IndividualMeasurementsColumns::Description));
}
else
{
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::Name));
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::BaseValue));
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::ShiftA), m_dimensions.size() > 0);
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::ShiftB), m_dimensions.size() > 1);
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::ShiftC), m_dimensions.size() > 2);
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::FullName));
AddHeader(static_cast<int>(MultisizeMeasurementsColumns::Description));
}
}
//---------------------------------------------------------------------------------------------------------------------
QSharedPointer<QxtCsvModel> DialogMeasurementsCSVColumns::CSVModel() const
{
return QSharedPointer<QxtCsvModel>::create(m_fileName, nullptr, m_withHeader, m_separator, m_codec);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::ShowInputPreview()
{
if (m_fileName.isEmpty())
{
return;
}
QSharedPointer<QxtCsvModel> csv = DialogMeasurementsCSVColumns::CSVModel();
const int columns = csv->columnCount();
const int rows = csv->rowCount();
ui->tableWidgetInput->clear();
ui->tableWidgetInput->setColumnCount(columns);
ui->tableWidgetInput->setRowCount(rows);
ui->tableWidgetInput->horizontalHeader()->setVisible(m_withHeader);
if (m_withHeader)
{
for(int column=0; column<columns; ++column)
{
QTableWidgetItem *header = new QTableWidgetItem(csv->headerText(column));
header->setToolTip(QString::number(column+1));
ui->tableWidgetInput->setHorizontalHeaderItem(column, header);
}
}
for (int row=0; row < rows; ++row)
{
for(int column=0; column<columns; ++column)
{
const QString text = csv->text(row, column);
QTableWidgetItem *item = new QTableWidgetItem(text);
item->setToolTip(text);
// set the item non-editable (view only), and non-selectable
Qt::ItemFlags flags = item->flags();
flags &= ~(Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
ui->tableWidgetInput->setItem(row, column, item);
}
}
ui->tableWidgetInput->resizeColumnsToContents();
ui->tableWidgetInput->resizeRowsToContents();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::ShowImportPreview()
{
if (m_fileName.isEmpty())
{
return;
}
QSharedPointer<QxtCsvModel> csv = DialogMeasurementsCSVColumns::CSVModel();
const int importColumns = ImportColumnCount();
const int columns = csv->columnCount();
const int rows = csv->rowCount();
ui->tableWidgetImport->clearContents();
ui->tableWidgetImport->setRowCount(rows);
for (int row=0; row < rows; ++row)
{
for(int column=0; column < importColumns; ++column)
{
const int tableColumn = m_columnsMap.at(column);
if (tableColumn >= 0 && tableColumn < columns)
{
const QString text = csv->text(row, tableColumn);
QTableWidgetItem *item = new QTableWidgetItem(text);
item->setToolTip(text);
// set the item non-editable (view only), and non-selectable
Qt::ItemFlags flags = item->flags();
flags &= ~(Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
ui->tableWidgetImport->setItem(row, column, item);
}
}
}
ui->tableWidgetImport->resizeColumnsToContents();
ui->tableWidgetImport->resizeRowsToContents();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::HackColumnControls()
{
if (m_type == MeasurementsType::Individual)
{
HackWidget(&ui->labelShiftA);
HackWidget(&ui->labelShiftB);
HackWidget(&ui->labelShiftC);
HackWidget(&ui->comboBoxShiftA);
HackWidget(&ui->comboBoxShiftB);
HackWidget(&ui->comboBoxShiftC);
}
else
{
if (m_dimensions.size() < 2)
{
HackWidget(&ui->labelShiftB);
HackWidget(&ui->comboBoxShiftB);
}
if (m_dimensions.size() < 3)
{
HackWidget(&ui->labelShiftC);
HackWidget(&ui->comboBoxShiftC);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::RetranslateLabels()
{
ui->labelName->setText(tr("Name") + "*:");
if (m_type == MeasurementsType::Individual)
{
ui->labelValue->setText(tr("Value") + "*:");
}
else
{
ui->labelValue->setText(tr("Base value") + "*:");
if (m_dimensions.size() > 0)
{
MeasurementDimension_p dimension = m_dimensions.at(0);
ui->labelShiftA->setText(tr("Shift (%1)*:")
.arg(VAbstartMeasurementDimension::DimensionName(dimension->Type())));
}
if (m_dimensions.size() > 1)
{
MeasurementDimension_p dimension = m_dimensions.at(1);
ui->labelShiftB->setText(tr("Shift (%1)*:")
.arg(VAbstartMeasurementDimension::DimensionName(dimension->Type())));
}
if (m_dimensions.size() > 2)
{
MeasurementDimension_p dimension = m_dimensions.at(2);
ui->labelShiftC->setText(tr("Shift (%1)*:")
.arg(VAbstartMeasurementDimension::DimensionName(dimension->Type())));
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::SetDefaultColumns()
{
auto SetDefault = [this](QComboBox *control, int column)
{
SCASSERT(control != nullptr)
int index = control->findData(m_columnsMap.at(column));
if (index != -1)
{
control->setCurrentIndex(index);
}
};
if (m_type == MeasurementsType::Individual)
{
SetDefault(ui->comboBoxName, static_cast<int>(IndividualMeasurementsColumns::Name));
SetDefault(ui->comboBoxValue, static_cast<int>(IndividualMeasurementsColumns::Value));
SetDefault(ui->comboBoxFullName, static_cast<int>(IndividualMeasurementsColumns::FullName));
SetDefault(ui->comboBoxDescription, static_cast<int>(IndividualMeasurementsColumns::Description));
}
else
{
SetDefault(ui->comboBoxName, static_cast<int>(MultisizeMeasurementsColumns::Name));
SetDefault(ui->comboBoxValue, static_cast<int>(MultisizeMeasurementsColumns::BaseValue));
if (m_dimensions.size() > 0)
{
SetDefault(ui->comboBoxShiftA, static_cast<int>(MultisizeMeasurementsColumns::ShiftA));
}
if (m_dimensions.size() > 1)
{
SetDefault(ui->comboBoxShiftB, static_cast<int>(MultisizeMeasurementsColumns::ShiftB));
}
if (m_dimensions.size() > 2)
{
SetDefault(ui->comboBoxShiftC, static_cast<int>(MultisizeMeasurementsColumns::ShiftC));
}
SetDefault(ui->comboBoxFullName, static_cast<int>(MultisizeMeasurementsColumns::FullName));
SetDefault(ui->comboBoxDescription, static_cast<int>(MultisizeMeasurementsColumns::Description));
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogMeasurementsCSVColumns::CheckStatus()
{
auto SetStatus = [this](bool status)
{
QPushButton *bOk = ui->buttonBox->button(QDialogButtonBox::Ok);
SCASSERT(bOk != nullptr)
bOk->setEnabled(status);
};
if (m_fileName.isEmpty())
{
SetStatus(false);
ui->labelStatus->setText(tr("File path is empty"));
return;
}
QSharedPointer<QxtCsvModel> csv = DialogMeasurementsCSVColumns::CSVModel();
const int columns = csv->columnCount();
if (columns < MinimumColumns())
{
SetStatus(false);
ui->labelStatus->setText(tr("Not enough columns"));
return;
}
const int rows = csv->rowCount();
if (rows < 1)
{
SetStatus(false);
ui->labelStatus->setText(tr("Not enough data to import"));
return;
}
if (not ColumnsValid())
{
SetStatus(false);
ui->labelStatus->setText(tr("Please, select unique number for each column"));
return;
}
SetStatus(true);
ui->labelStatus->setText(tr("Ready"));
}
//---------------------------------------------------------------------------------------------------------------------
template<class T>
void DialogMeasurementsCSVColumns::HackWidget(T **widget)
{
delete *widget;
*widget = new T();
m_hackedWidgets.append(*widget);
}

View File

@ -0,0 +1,149 @@
/************************************************************************
**
** @file dialogmeasurementscsvcolumns.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 9 10, 2020
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2020 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef DIALOGMEASUREMENTSCSVCOLUMNS_H
#define DIALOGMEASUREMENTSCSVCOLUMNS_H
#include <QDialog>
#include "../vformat/vdimensions.h"
class QxtCsvModel;
enum class IndividualMeasurementsColumns: qint8
{
Name = 0,
Value = 1,
FullName = 2, // optional
Description = 3, // optional
LAST_DO_NOT_USE = 4
};
enum class MultisizeMeasurementsColumns: qint8
{
Name = 0,
BaseValue = 1,
ShiftA = 2,
ShiftB = 3, // optional if not required
ShiftC = 4, // optional if not required
FullName = 5, // optional
Description = 6, // optional
LAST_DO_NOT_USE = 7
};
namespace Ui {
class DialogMeasurementsCSVColumns;
}
class DialogMeasurementsCSVColumns : public QDialog
{
Q_OBJECT
public:
DialogMeasurementsCSVColumns(const QString &filename, MeasurementsType type, QWidget *parent = nullptr);
DialogMeasurementsCSVColumns(const QString &filename, MeasurementsType type,
const QList<MeasurementDimension_p> &dimensions, QWidget *parent = nullptr);
virtual ~DialogMeasurementsCSVColumns();
QVector<int> ColumnsMap() const;
void SetWithHeader(bool withHeader);
void SetSeparator(const QChar &separator);
void SetCodec(QTextCodec *codec);
protected:
virtual void changeEvent(QEvent* event) override;
virtual void showEvent(QShowEvent *event) override;
private slots:
void ColumnChanged();
private:
Q_DISABLE_COPY(DialogMeasurementsCSVColumns)
Ui::DialogMeasurementsCSVColumns *ui;
bool m_isInitialized{false};
QString m_fileName;
bool m_withHeader{false};
QChar m_separator{','};
QTextCodec *m_codec{nullptr};
QVector<int> m_columnsMap{};
MeasurementsType m_type;
QList<MeasurementDimension_p> m_dimensions{};
QVector<QObject *> m_hackedWidgets{};
bool ColumnMandatory(int column) const;
QString ColumnHeader(int column) const;
int ImportColumnCount() const;
int MinimumColumns() const;
bool ColumnsValid();
void InitColumnsMap();
void InitColumnsControls();
void InitImportHeaders();
QSharedPointer<QxtCsvModel> CSVModel() const;
void ShowInputPreview();
void ShowImportPreview();
template <class T>
void HackWidget(T **widget);
void HackColumnControls();
void RetranslateLabels();
void SetDefaultColumns();
void CheckStatus();
};
//---------------------------------------------------------------------------------------------------------------------
inline QVector<int> DialogMeasurementsCSVColumns::ColumnsMap() const
{
return m_columnsMap;
}
//---------------------------------------------------------------------------------------------------------------------
inline void DialogMeasurementsCSVColumns::SetWithHeader(bool withHeader)
{
m_withHeader = withHeader;
}
//---------------------------------------------------------------------------------------------------------------------
inline void DialogMeasurementsCSVColumns::SetSeparator(const QChar &separator)
{
m_separator = separator;
}
//---------------------------------------------------------------------------------------------------------------------
inline void DialogMeasurementsCSVColumns::SetCodec(QTextCodec *codec)
{
m_codec = codec;
}
#endif // DIALOGMEASUREMENTSCSVCOLUMNS_H

View File

@ -0,0 +1,204 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogMeasurementsCSVColumns</class>
<widget class="QDialog" name="DialogMeasurementsCSVColumns">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>908</width>
<height>703</height>
</rect>
</property>
<property name="windowTitle">
<string>Setup columns</string>
</property>
<property name="windowIcon">
<iconset resource="../share/resources/tapeicon.qrc">
<normaloff>:/tapeicon/64x64/logo.png</normaloff>:/tapeicon/64x64/logo.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Preview</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Input</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTableWidget" name="tableWidgetInput">
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Import</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="tableWidgetImport">
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_4">
<property name="title">
<string>Columns</string>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="horizontalSpacing">
<number>6</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="labelName">
<property name="text">
<string notr="true">Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBoxName"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelValue">
<property name="text">
<string notr="true">Value:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBoxValue"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelShiftA">
<property name="text">
<string notr="true">ShiftA:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboBoxShiftA"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelShiftB">
<property name="text">
<string notr="true">ShiftB:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="comboBoxShiftB"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="labelShiftC">
<property name="text">
<string notr="true">ShiftC:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="comboBoxShiftC"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="labelFullName">
<property name="text">
<string>Full name:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="comboBoxFullName"/>
</item>
<item row="6" column="0">
<widget class="QLabel" name="labelDescription">
<property name="text">
<string>Description:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QComboBox" name="comboBoxDescription"/>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="labelStatus">
<property name="text">
<string>Ready</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="../share/resources/tapeicon.qrc"/>
</resources>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DialogMeasurementsCSVColumns</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DialogMeasurementsCSVColumns</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -3,6 +3,7 @@
SOURCES += \
$$PWD/dialogs/dialogdimensionlabels.cpp \
$$PWD/dialogs/dialogmeasurementscsvcolumns.cpp \
$$PWD/dialogs/dialogrestrictdimension.cpp \
$$PWD/main.cpp \
$$PWD/tmainwindow.cpp \
@ -20,6 +21,7 @@ SOURCES += \
HEADERS += \
$$PWD/dialogs/dialogdimensionlabels.h \
$$PWD/dialogs/dialogmeasurementscsvcolumns.h \
$$PWD/dialogs/dialogrestrictdimension.h \
$$PWD/tmainwindow.h \
$$PWD/stable.h \
@ -36,6 +38,7 @@ HEADERS += \
FORMS += \
$$PWD/dialogs/dialogdimensionlabels.ui \
$$PWD/dialogs/dialogmeasurementscsvcolumns.ui \
$$PWD/dialogs/dialogrestrictdimension.ui \
$$PWD/tmainwindow.ui \
$$PWD/dialogs/dialogabouttape.ui \

View File

@ -35,6 +35,7 @@
#include "dialogs/dialogsetupmultisize.h"
#include "dialogs/dialogrestrictdimension.h"
#include "dialogs/dialogdimensionlabels.h"
#include "dialogs/dialogmeasurementscsvcolumns.h"
#include "../vpatterndb/vcontainer.h"
#include "../vpatterndb/calculator.h"
#include "../vpatterndb/pmsystems.h"
@ -1020,16 +1021,34 @@ void TMainWindow::ImportDataFromCSV()
qApp->Settings()->SetCSVCodec(dialog.GetSelectedMib());
qApp->Settings()->SetCSVWithHeader(dialog.IsWithHeader());
QxtCsvModel csv(fileName, nullptr, dialog.IsWithHeader(), dialog.GetSeparator(),
QTextCodec::codecForMib(dialog.GetSelectedMib()));
if (m->Type() == MeasurementsType::Individual)
QSharedPointer<DialogMeasurementsCSVColumns> columns;
if (m->Type() == MeasurementsType::Multisize)
{
ImportIndividualMeasurements(csv);
const QList<MeasurementDimension_p> dimensions = m->Dimensions().values();
columns = QSharedPointer<DialogMeasurementsCSVColumns>::create(fileName, m->Type(), dimensions, this);
}
else
{
ImportMultisizeMeasurements(csv);
columns = QSharedPointer<DialogMeasurementsCSVColumns>::create(fileName, m->Type(), this);
}
columns->SetWithHeader(dialog.IsWithHeader());
columns->SetSeparator(dialog.GetSeparator());
columns->SetCodec(QTextCodec::codecForMib(dialog.GetSelectedMib()));
if (columns->exec() == QDialog::Accepted)
{
QxtCsvModel csv(fileName, nullptr, dialog.IsWithHeader(), dialog.GetSeparator(),
QTextCodec::codecForMib(dialog.GetSelectedMib()));
const QVector<int> map = columns->ColumnsMap();
if (m->Type() == MeasurementsType::Individual)
{
ImportIndividualMeasurements(csv, map);
}
else
{
ImportMultisizeMeasurements(csv, map);
}
}
}
}
@ -3740,7 +3759,7 @@ void TMainWindow::RefreshDataAfterImport()
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::ImportIndividualMeasurements(const QxtCsvModel &csv)
void TMainWindow::ImportIndividualMeasurements(const QxtCsvModel &csv, const QVector<int> &map)
{
const int columns = csv.columnCount();
const int rows = csv.rowCount();
@ -3773,7 +3792,8 @@ void TMainWindow::ImportIndividualMeasurements(const QxtCsvModel &csv)
{
try
{
const QString name = csv.text(i, 0).simplified();
const int nameColumn = map.at(static_cast<int>(IndividualMeasurementsColumns::Name));
const QString name = csv.text(i, nameColumn).simplified();
if (name.isEmpty())
{
ShowError(tr("Error in row %1. Measurement name is empty.").arg(i));
@ -3784,17 +3804,28 @@ void TMainWindow::ImportIndividualMeasurements(const QxtCsvModel &csv)
const QString mName = CheckMName(qApp->TrVars()->MFromUser(name), importedNames);
importedNames.insert(mName);
measurement.name = mName;
measurement.value = VTranslateVars::TryFormulaFromUser(csv.text(i, 1), qApp->Settings()->GetOsSeparator());
const bool custom = csv.text(i, 0).simplified().startsWith(CustomMSign);
const int valueColumn = map.at(static_cast<int>(IndividualMeasurementsColumns::Value));
measurement.value = VTranslateVars::TryFormulaFromUser(csv.text(i, valueColumn),
qApp->Settings()->GetOsSeparator());
const bool custom = name.startsWith(CustomMSign);
if (columns > 2 && custom)
{
measurement.fullName = csv.text(i, 2).simplified();
const int fullNameColumn = map.at(static_cast<int>(IndividualMeasurementsColumns::FullName));
if (fullNameColumn >= 0)
{
measurement.fullName = csv.text(i, fullNameColumn).simplified();
}
}
if (columns > 3 && custom)
{
measurement.description = csv.text(i, 3).simplified();
const int descriptionColumn = map.at(static_cast<int>(IndividualMeasurementsColumns::Description));
if (descriptionColumn >= 0)
{
measurement.description = csv.text(i, descriptionColumn).simplified();
}
}
measurements.append(measurement);
@ -3825,7 +3856,7 @@ void TMainWindow::ImportIndividualMeasurements(const QxtCsvModel &csv)
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv)
void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv, const QVector<int> &map)
{
const int columns = csv.columnCount();
const int rows = csv.rowCount();
@ -3838,9 +3869,10 @@ void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv)
auto ConverToDouble = [](QString text, const QString &error)
{
text.replace(" ", QString());
text = VTranslateVars::TryFormulaFromUser(text, qApp->Settings()->GetOsSeparator());
bool ok = false;
QLocale::c();
const qreal value = QLocale::c().toDouble(text, &ok);
if (not ok)
{
@ -3859,8 +3891,9 @@ void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv)
QString name;
qreal base{0};
qreal heightIncrease{0};
qreal sizeIncrease{0};
qreal shiftA{0};
qreal shiftB{0};
qreal shiftC{0};
QString fullName;
QString description;
};
@ -3868,11 +3901,14 @@ void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv)
QVector<MultisizeMeasurement> measurements;
QSet<QString> importedNames;
const QMap<MeasurementDimension, MeasurementDimension_p > dimensions = m->Dimensions();
for(int i=0; i < rows; ++i)
{
try
{
const QString name = csv.text(i, 0).simplified();
const int nameColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::Name));
const QString name = csv.text(i, nameColumn).simplified();
if (name.isEmpty())
{
ShowError(tr("Error in row %1. Measurement name is empty.").arg(i));
@ -3884,24 +3920,48 @@ void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv)
importedNames.insert(mName);
measurement.name = mName;
measurement.base = ConverToDouble(csv.text(i, 1),
tr("Cannot convert base size value to double in column 2."));
const int baseValueColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::BaseValue));
measurement.base = ConverToDouble(csv.text(i, baseValueColumn),
tr("Cannot convert base value to double in column 2."));
measurement.heightIncrease = ConverToDouble(csv.text(i, 2),
tr("Cannot convert height increase value to double in column 3."));
const int shiftAColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::ShiftA));
measurement.shiftA = ConverToDouble(csv.text(i, shiftAColumn),
tr("Cannot convert shift value to double in column %1.")
.arg(shiftAColumn));
measurement.sizeIncrease = ConverToDouble(csv.text(i, 3),
tr("Cannot convert size increase value to double in column 4."));
if (dimensions.size() > 1)
{
const int shiftBColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::ShiftB));
measurement.shiftB = ConverToDouble(csv.text(i, shiftBColumn),
tr("Cannot convert shift value to double in column %1.")
.arg(shiftBColumn));
}
const bool custom = csv.text(i, 0).simplified().startsWith(CustomMSign);
if (dimensions.size() > 2)
{
const int shiftCColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::ShiftC));
measurement.shiftC = ConverToDouble(csv.text(i, shiftCColumn),
tr("Cannot convert shift value to double in column %1.")
.arg(shiftCColumn));
}
const bool custom = name.startsWith(CustomMSign);
if (columns > 4 && custom)
{
measurement.fullName = csv.text(i, 4).simplified();
const int fullNameColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::FullName));
if (fullNameColumn >= 0)
{
measurement.fullName = csv.text(i, fullNameColumn).simplified();
}
}
if (columns > 5 && custom)
{
measurement.description = csv.text(i, 5).simplified();
const int descriptionColumn = map.at(static_cast<int>(MultisizeMeasurementsColumns::Description));
if (descriptionColumn >= 0)
{
measurement.description = csv.text(i, descriptionColumn).simplified();
}
}
measurements.append(measurement);
@ -3917,8 +3977,17 @@ void TMainWindow::ImportMultisizeMeasurements(const QxtCsvModel &csv)
{
m->AddEmpty(mm.name);
m->SetMBaseValue(mm.name, mm.base);
m->SetMShiftB(mm.name, mm.sizeIncrease);
m->SetMShiftA(mm.name, mm.heightIncrease);
m->SetMShiftA(mm.name, mm.shiftA);
if (dimensions.size() > 1)
{
m->SetMShiftB(mm.name, mm.shiftB);
}
if (dimensions.size() > 2)
{
m->SetMShiftC(mm.name, mm.shiftC);
}
if (not mm.fullName.isEmpty())
{

View File

@ -237,8 +237,8 @@ private:
void ShowError(const QString &text);
void RefreshDataAfterImport();
void ImportIndividualMeasurements(const QxtCsvModel &csv);
void ImportMultisizeMeasurements(const QxtCsvModel &csv);
void ImportIndividualMeasurements(const QxtCsvModel &csv, const QVector<int> &map);
void ImportMultisizeMeasurements(const QxtCsvModel &csv, const QVector<int> &map);
void SetCurrentPatternUnit();