Use the init-statement to declare a variable inside the if statement.

This commit is contained in:
Roman Telezhynskyi 2024-04-04 10:27:15 +03:00
parent f50aec80c3
commit b3e6365e92
10 changed files with 76 additions and 110 deletions

View File

@ -4612,8 +4612,7 @@ void TMainWindow::SetDimensionBases()
{
SCASSERT(control != nullptr)
const qint32 i = control->findData(value);
if (i != -1)
if (const qint32 i = control->findData(value); i != -1)
{
control->setCurrentIndex(i);
}

View File

@ -379,8 +379,7 @@ auto DialogIncrements::GetCustomName() const -> QString
auto DialogIncrements::ClearIncrementName(const QString &name) -> QString
{
QString clear = name;
const auto index = clear.indexOf(CustomIncrSign);
if (index == 0)
if (const auto index = clear.indexOf(CustomIncrSign); index == 0)
{
clear.remove(0, 1);
}
@ -1296,8 +1295,7 @@ void DialogIncrements::InitIncrementUnits(QComboBox *combo)
combo->addItem(units, QVariant(static_cast<int>(IncrUnits::Pattern)));
combo->addItem(tr("Degrees"), QVariant(static_cast<int>(IncrUnits::Degrees)));
int const i = combo->findData(current);
if (i != -1)
if (int const i = combo->findData(current); i != -1)
{
combo->setCurrentIndex(i);
}
@ -1787,8 +1785,7 @@ void DialogIncrements::SaveIncrFormula()
QString const text = textEdit->toPlainText();
QSharedPointer<VIncrement> const incr = m_data->GetVariable<VIncrement>(nameField->text());
QTableWidgetItem *formulaField = table->item(row, 2);
if (formulaField->text() == text)
if (QTableWidgetItem *formulaField = table->item(row, 2); formulaField->text() == text)
{
QTableWidgetItem *result = table->item(row, 1);
if (incr->IsSpecialUnits())
@ -2065,8 +2062,7 @@ void DialogIncrements::showEvent(QShowEvent *event)
}
// do your init stuff here
const QSize sz = VAbstractApplication::VApp()->Settings()->GetIncrementsDialogSize();
if (not sz.isEmpty())
if (const QSize sz = VAbstractApplication::VApp()->Settings()->GetIncrementsDialogSize(); not sz.isEmpty())
{
resize(sz);
}

View File

@ -457,8 +457,7 @@ void DialogLayoutSettings::FindTemplate()
if (QSizeF(width, height) == tmplSize || QSizeF(height, width) == tmplSize)
{
ui->comboBoxTemplates->blockSignals(true);
const int index = ui->comboBoxTemplates->findData(i);
if (index != -1)
if (const int index = ui->comboBoxTemplates->findData(i); index != -1)
{
ui->comboBoxTemplates->setCurrentIndex(index);
}
@ -468,8 +467,7 @@ void DialogLayoutSettings::FindTemplate()
}
ui->comboBoxTemplates->blockSignals(true);
const int index = ui->comboBoxTemplates->findData(max);
if (index != -1)
if (const int index = ui->comboBoxTemplates->findData(max); index != -1)
{
ui->comboBoxTemplates->setCurrentIndex(index);
}
@ -573,8 +571,7 @@ void DialogLayoutSettings::showEvent(QShowEvent *event)
}
// do your init stuff here
const QSize sz = VAbstractApplication::VApp()->Settings()->GetLayoutSettingsDialogSize();
if (not sz.isEmpty())
if (const QSize sz = VAbstractApplication::VApp()->Settings()->GetLayoutSettingsDialogSize(); not sz.isEmpty())
{
resize(sz);
}
@ -689,8 +686,8 @@ void DialogLayoutSettings::DialogAccepted()
}
else
{
QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
if (printer.isNull())
if (QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
printer.isNull())
{
m_generator->SetPrinterFields(true, GetFields());
}
@ -701,11 +698,10 @@ void DialogLayoutSettings::DialogAccepted()
if (fields.left() < minFields.left() || fields.right() < minFields.right() ||
fields.top() < minFields.top() || fields.bottom() < minFields.bottom())
{
QMessageBox::StandardButton answer;
answer = QMessageBox::question(this, tr("Wrong fields."),
tr("Margins go beyond printing. \n\nApply settings anyway?"),
if (QMessageBox::StandardButton answer = QMessageBox::question(
this, tr("Wrong fields."), tr("Margins go beyond printing. \n\nApply settings anyway?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (answer == QMessageBox::No)
answer == QMessageBox::No)
{
SetMinMargins(fields, minFields);
m_generator->SetPrinterFields(true, GetFields());
@ -764,8 +760,8 @@ void DialogLayoutSettings::RestoreDefaults()
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutSettings::PrinterMargins()
{
QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
if (not printer.isNull())
if (QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
not printer.isNull())
{
SetFields(GetPrinterFields(QSharedPointer<QPrinter>(new QPrinter(printer))));
}
@ -958,8 +954,8 @@ auto DialogLayoutSettings::GetTemplateSize(const PaperSizeTemplate &tmpl, const
//---------------------------------------------------------------------------------------------------------------------
auto DialogLayoutSettings::MinPrinterFields() const -> QMarginsF
{
QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
if (not printer.isNull())
if (QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
not printer.isNull())
{
return GetMinPrinterFields(QSharedPointer<QPrinter>(new QPrinter(printer)));
}
@ -970,8 +966,8 @@ auto DialogLayoutSettings::MinPrinterFields() const -> QMarginsF
//---------------------------------------------------------------------------------------------------------------------
auto DialogLayoutSettings::GetDefPrinterFields() const -> QMarginsF
{
QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
if (not printer.isNull())
if (QPrinterInfo const printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
not printer.isNull())
{
return GetPrinterFields(QSharedPointer<QPrinter>(new QPrinter(printer)));
}

View File

@ -101,9 +101,9 @@ DialogPatternProperties::DialogPatternProperties(VPattern *doc, VContainer *patt
ui->comboBoxLabelLanguage->addItem(QLocale(name).nativeLanguageName(), name);
}
int const index =
ui->comboBoxLabelLanguage->findData(VAbstractValApplication::VApp()->ValentinaSettings()->GetLabelLanguage());
if (index != -1)
if (int const index = ui->comboBoxLabelLanguage->findData(
VAbstractValApplication::VApp()->ValentinaSettings()->GetLabelLanguage());
index != -1)
{
ui->comboBoxLabelLanguage->setCurrentIndex(index);
}
@ -288,10 +288,10 @@ void DialogPatternProperties::SaveDescription()
m_doc->SetPassmarkWidthVariable(ui->lineEditPassmarkWidth->text());
m_doc->SetDefaultPieceLabelPath(ui->lineEditPieceLabelPath->text());
const bool lengthChanged = m_oldPassmarkLength != ui->lineEditPassmarkLength->text();
const bool widthChanged = m_oldPassmarkWidth != ui->lineEditPassmarkWidth->text();
if (lengthChanged || widthChanged)
if (const bool lengthChanged = m_oldPassmarkLength != ui->lineEditPassmarkLength->text();
lengthChanged || widthChanged)
{
emit UpddatePieces();

View File

@ -608,8 +608,8 @@ void VWidgetBackgroundImages::ResetImageTransformationSettings()
}
else if (ui->tabWidgetImageTransformation->indexOf(ui->tabScale) == index)
{ // scale
int const unitIndex = ui->comboBoxScaleUnit->findData(QVariant(static_cast<int>(ScaleUnit::Percent)));
if (unitIndex != -1)
if (int const unitIndex = ui->comboBoxScaleUnit->findData(QVariant(static_cast<int>(ScaleUnit::Percent)));
unitIndex != -1)
{
ui->comboBoxScaleUnit->setCurrentIndex(unitIndex);
}
@ -725,8 +725,7 @@ void VWidgetBackgroundImages::ImagePositionChanged(const QUuid &id)
QTableWidgetItem *item = ui->tableWidget->item(row, 0);
if (item != nullptr)
{
QUuid const curentId = item->data(Qt::UserRole).toUuid();
if (curentId != id)
if (QUuid const curentId = item->data(Qt::UserRole).toUuid(); curentId != id)
{
return;
}
@ -972,8 +971,7 @@ auto VWidgetBackgroundImages::ImageWidth() const -> qreal
{
qreal width = 0;
int const row = ui->tableWidget->currentRow();
if (row != -1)
if (int const row = ui->tableWidget->currentRow(); row != -1)
{
QTableWidgetItem *item = ui->tableWidget->item(row, 0);
if (item != nullptr)
@ -992,8 +990,7 @@ auto VWidgetBackgroundImages::ImageHeight() const -> qreal
{
qreal height = 0;
int const row = ui->tableWidget->currentRow();
if (row != -1)
if (int const row = ui->tableWidget->currentRow(); row != -1)
{
QTableWidgetItem *item = ui->tableWidget->item(row, 0);
if (item != nullptr)

View File

@ -305,8 +305,7 @@ void VWidgetDetails::ShowContextMenu(const QPoint &pos)
QAction *actionDeletePiece = nullptr;
VToolSeamAllowance *toolPiece = nullptr;
QTableWidgetItem *selectedItem = ui->tableWidget->itemAt(pos);
if (selectedItem)
if (QTableWidgetItem *selectedItem = ui->tableWidget->itemAt(pos); selectedItem)
{
QTableWidgetItem *item = ui->tableWidget->item(selectedItem->row(), PieceColumn::InLayout);
const quint32 id = item->data(Qt::UserRole).toUInt();

View File

@ -104,8 +104,7 @@ void VWidgetGroups::SetMultipleGroupsVisibility(const QVector<vidtype> &groups,
continue;
}
QTableWidgetItem *item = ui->tableWidget->item(row, 0);
if (item)
if (QTableWidgetItem *item = ui->tableWidget->item(row, 0); item)
{
const QString resource = QStringLiteral("icon");
item->setIcon(i.value()
@ -134,8 +133,8 @@ auto VWidgetGroups::FilterGroups(const QMap<quint32, VGroupData> &groups) -> QMa
while (i != groups.constEnd())
{
const VGroupData &data = i.value();
QSet<QString> const groupCategories = ConvertToSet<QString>(data.tags);
if (filterCategories.intersects(groupCategories))
if (QSet<QString> const groupCategories = ConvertToSet<QString>(data.tags);
filterCategories.intersects(groupCategories))
{
filtered.insert(i.key(), data);
}

View File

@ -310,8 +310,7 @@ void WarningNotUniquePieceName(const QHash<quint32, VPiece> *allDetails)
while (i != allDetails->constEnd())
{
const QString pieceName = i.value().GetName();
if (not uniqueNames.contains(pieceName))
if (const QString pieceName = i.value().GetName(); not uniqueNames.contains(pieceName))
{
uniqueNames.insert(pieceName);
}
@ -419,11 +418,11 @@ MainWindow::MainWindow(QWidget *parent)
asking = true;
m_mChangesAsked = true;
m_measurementsSyncTimer->stop();
const auto answer = QMessageBox::question(
if (const auto answer = QMessageBox::question(
this, tr("Measurements"),
tr("Measurements were changed. Do you want to sync measurements now?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (answer == QMessageBox::Yes)
answer == QMessageBox::Yes)
{
SyncMeasurements();
}
@ -564,8 +563,7 @@ void MainWindow::AddPP(const QString &PPName)
SetEnableTool(true);
SetEnableWidgets(true);
const qint32 index = m_comboBoxDraws->findText(PPName);
if (index != -1)
if (const qint32 index = m_comboBoxDraws->findText(PPName); index != -1)
{ // -1 for not found
m_comboBoxDraws->setCurrentIndex(index);
}
@ -836,8 +834,8 @@ void MainWindow::SetToolButton(bool checked, Tool t, const QString &cursor, cons
emit EnableItemMove(false);
m_currentTool = m_lastUsedTool = t;
VValentinaSettings *settings = VAbstractValApplication::VApp()->ValentinaSettings();
if (settings->GetPointerMode() == VToolPointerMode::ToolIcon)
if (VValentinaSettings *settings = VAbstractValApplication::VApp()->ValentinaSettings();
settings->GetPointerMode() == VToolPointerMode::ToolIcon)
{
const QString resource = QStringLiteral("toolcursor");
auto cursorResource = VTheme::GetResourceName(resource, cursor);
@ -2203,8 +2201,7 @@ void MainWindow::ShowMeasurements()
void MainWindow::MeasurementsChanged(const QString &path)
{
m_mChanges = false;
QFileInfo const checkFile(path);
if (checkFile.exists())
if (QFileInfo const checkFile(path); checkFile.exists())
{
m_mChanges = true;
m_mChangesAsked = false;
@ -2498,8 +2495,7 @@ void MainWindow::SetDimensionBases()
control->blockSignals(true);
const qint32 i = control->findData(value);
if (i != -1)
if (const qint32 i = control->findData(value); i != -1)
{
control->setCurrentIndex(i);
}
@ -2684,8 +2680,7 @@ auto MainWindow::SavePatternAs(const QString &fileName) -> bool
return result;
}
const QString oldFilePath = VAbstractValApplication::VApp()->GetPatternPath();
if (not oldFilePath.isEmpty())
if (const QString oldFilePath = VAbstractValApplication::VApp()->GetPatternPath(); not oldFilePath.isEmpty())
{
qCDebug(vMainWindow, "Updating restore file list.");
QStringList restoreFiles = VAbstractValApplication::VApp()->ValentinaSettings()->GetRestoreFileList();
@ -2941,8 +2936,7 @@ void MainWindow::ToolBarDraws()
[this]()
{
QString draw = doc->GetNameActivPP();
bool const ok = PatternPieceName(draw);
if (not ok)
if (bool const ok = PatternPieceName(draw); not ok)
{
return;
}
@ -4251,9 +4245,8 @@ void MainWindow::on_actionCreateManualLayout_triggered()
DialogLayoutScale layoutScale(false, this);
layoutScale.SetXScale(1);
layoutScale.SetYScale(1);
int const res = layoutScale.exec();
if (res == QDialog::Rejected)
if (int const res = layoutScale.exec(); res == QDialog::Rejected)
{
return;
}
@ -4378,8 +4371,7 @@ void MainWindow::ActionExportFontCorrections()
const QString dirPath = settings->GetPathFontCorrections();
bool usedNotExistedDir = false;
QDir const directory(dirPath);
if (not directory.exists())
if (QDir const directory(dirPath); not directory.exists())
{
usedNotExistedDir = directory.mkpath(QChar('.'));
}
@ -4503,8 +4495,7 @@ void MainWindow::FileClosedCorrect()
VAbstractValApplication::VApp()->ValentinaSettings()->SetRestoreFileList(restoreFiles);
// Remove autosave file
QFile autofile(VAbstractValApplication::VApp()->GetPatternPath() + *autosavePrefix);
if (autofile.exists())
if (QFile autofile(VAbstractValApplication::VApp()->GetPatternPath() + *autosavePrefix); autofile.exists())
{
autofile.remove();
}
@ -4752,8 +4743,7 @@ void MainWindow::on_actionNew_triggered()
qCDebug(vMainWindow, "Generated PP name: %s", qUtf8Printable(patternPieceName));
qCDebug(vMainWindow, "First PP");
DialogNewPattern newPattern(pattern, patternPieceName, this);
if (newPattern.exec() == QDialog::Accepted)
if (DialogNewPattern newPattern(pattern, patternPieceName, this); newPattern.exec() == QDialog::Accepted)
{
patternPieceName = newPattern.name();
VAbstractValApplication::VApp()->SetPatternUnits(newPattern.PatternUnit());
@ -4813,8 +4803,7 @@ void MainWindow::DimensionABaseChanged()
if (not VFuzzyComparePossibleNulls(oldValue, m_currentDimensionA))
{
const QList<MeasurementDimension_p> dimensions = m_m->Dimensions().values();
if (dimensions.size() > 1)
if (const QList<MeasurementDimension_p> dimensions = m_m->Dimensions().values(); dimensions.size() > 1)
{
MeasurementDimension_p dimension = dimensions.at(1);
InitDimensionGradation(1, dimension, m_dimensionB);
@ -4839,9 +4828,7 @@ void MainWindow::DimensionBBaseChanged()
if (not VFuzzyComparePossibleNulls(oldValue, m_currentDimensionB))
{
const QList<MeasurementDimension_p> dimensions = m_m->Dimensions().values();
if (dimensions.size() > 2)
if (const QList<MeasurementDimension_p> dimensions = m_m->Dimensions().values(); dimensions.size() > 2)
{
const MeasurementDimension_p &dimension = dimensions.at(2);
InitDimensionGradation(2, dimension, m_dimensionC);
@ -6635,8 +6622,8 @@ auto MainWindow::GetUnlokedRestoreFileList() -> QStringList
{
QStringList restoreFiles;
// Take all files that need to be restored
QStringList files = VAbstractValApplication::VApp()->ValentinaSettings()->GetRestoreFileList();
if (not files.empty())
if (QStringList files = VAbstractValApplication::VApp()->ValentinaSettings()->GetRestoreFileList();
not files.empty())
{
restoreFiles.reserve(files.size());
for (auto &file : files)
@ -6779,8 +6766,7 @@ void MainWindow::ExportDrawAs(bool checked)
return;
}
QFileInfo const f(fileName);
if (f.suffix().isEmpty() || f.suffix() != "svg"_L1)
if (QFileInfo const f(fileName); f.suffix().isEmpty() || f.suffix() != "svg"_L1)
{
fileName += ".svg"_L1;
}
@ -6977,9 +6963,10 @@ auto MainWindow::CheckPathToMeasurements(const QString &patternPath, const QStri
const QString text = tr("The measurements file <br/><br/> <b>%1</b> <br/><br/> could not be found. Do you "
"want to update the file location?")
.arg(path);
QMessageBox::StandardButton const res = QMessageBox::question(this, tr("Loading measurements file"), text,
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (res == QMessageBox::No)
if (QMessageBox::StandardButton const res = QMessageBox::question(
this, tr("Loading measurements file"), text, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
res == QMessageBox::No)
{
return {};
}
@ -6998,8 +6985,7 @@ auto MainWindow::CheckPathToMeasurements(const QString &patternPath, const QStri
{
QString dirPath;
const QDir patternDir = QFileInfo(patternPath).absoluteDir();
QString measurements = table.fileName();
if (patternDir.exists(measurements))
if (QString measurements = table.fileName(); patternDir.exists(measurements))
{
selectedName = measurements;
dirPath = patternDir.absolutePath();
@ -7209,8 +7195,7 @@ auto MainWindow::DoExport(const VCommandLinePtr &expParams) -> bool
}
listDetails = PrepareDetailsForLayout(details);
const bool exportOnlyDetails = expParams->IsExportOnlyDetails();
if (exportOnlyDetails)
if (const bool exportOnlyDetails = expParams->IsExportOnlyDetails(); exportOnlyDetails)
{
try
{
@ -7314,8 +7299,7 @@ auto MainWindow::DoFMExport(const VCommandLinePtr &expParams) -> bool
return false;
}
QFileInfo const info(filePath);
if (info.isRelative())
if (QFileInfo const info(filePath); info.isRelative())
{
filePath = QDir::currentPath() + '/'_L1 + filePath;
}
@ -7496,9 +7480,7 @@ void MainWindow::ProcessCMD()
VAbstractValApplication::VApp()->SetUserMaterials(cmd->OptUserMaterials());
const bool loaded = LoadPattern(args.constFirst(), cmd->OptMeasurePath());
if (not loaded)
if (const bool loaded = LoadPattern(args.constFirst(), cmd->OptMeasurePath()); not loaded)
{
return; // process only one input file
}

View File

@ -499,9 +499,8 @@ void MainWindowsNoGUI::ExportFMeasurementsToCSV()
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::ExportData(const QVector<VLayoutPiece> &listDetails)
{
const LayoutExportFormats format = m_dialogSaveLayout->Format();
if (format == LayoutExportFormats::DXF_AAMA || format == LayoutExportFormats::DXF_ASTM ||
if (const LayoutExportFormats format = m_dialogSaveLayout->Format();
format == LayoutExportFormats::DXF_AAMA || format == LayoutExportFormats::DXF_ASTM ||
format == LayoutExportFormats::RLD || format == LayoutExportFormats::HPGL ||
format == LayoutExportFormats::HPGL2)
{
@ -554,9 +553,9 @@ void MainWindowsNoGUI::ExportFlatLayout(const QList<QGraphicsScene *> &scenes, c
}
VAbstractValApplication::VApp()->ValentinaSettings()->SetPathLayout(path);
const LayoutExportFormats format = m_dialogSaveLayout->Format();
if (format == LayoutExportFormats::PDFTiled && m_dialogSaveLayout->Mode() == Draw::Layout)
if (const LayoutExportFormats format = m_dialogSaveLayout->Format();
format == LayoutExportFormats::PDFTiled && m_dialogSaveLayout->Mode() == Draw::Layout)
{
const QString name = path + '/'_L1 + m_dialogSaveLayout->FileName() + '1'_L1;
PdfTiledFile(name);
@ -927,8 +926,8 @@ auto MainWindowsNoGUI::RecentFileList() const -> QStringList
auto MainWindowsNoGUI::ScenePreview(int i, QSize iconSize, PreviewQuatilty quality) const -> QIcon
{
QImage image;
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutSettings->LayoutPapers().at(i));
if (paper)
if (auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutSettings->LayoutPapers().at(i)); paper)
{
if (quality == PreviewQuatilty::Fast)
{

View File

@ -5428,12 +5428,11 @@ void DialogSeamAllowance::EditPatternLabel()
{
if (m_patternLabelDataChanged && m_askSavePatternLabelData)
{
QMessageBox::StandardButton const answer = QMessageBox::question(
if (QMessageBox::StandardButton const answer = QMessageBox::question(
this, tr("Save label data."),
tr("Label data were changed. Do you want to save them before editing label template?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (answer == QMessageBox::Yes)
answer == QMessageBox::Yes)
{
SavePatternLabelData();
}