From b43675f3230bb1d1bd0bc4e1e3d13137d3e4ba50 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Fri, 10 Jan 2020 17:40:14 -0800 Subject: [PATCH] Array cleanup. --- common/lc_basewindow.h | 2 +- common/lc_category.cpp | 22 ++++++++++++---------- common/lc_category.h | 15 ++++++--------- common/lc_library.cpp | 2 +- common/lc_partselectionwidget.cpp | 2 +- qt/lc_qpreferencesdialog.cpp | 28 ++++++++++++++-------------- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/common/lc_basewindow.h b/common/lc_basewindow.h index 516c543a..7781eaf4 100644 --- a/common/lc_basewindow.h +++ b/common/lc_basewindow.h @@ -32,7 +32,7 @@ struct lcPreferencesDialogOptions int AASamples; int StudLogo; - lcArray Categories; + std::vector Categories; bool CategoriesModified; bool CategoriesDefault; diff --git a/common/lc_category.cpp b/common/lc_category.cpp index 270308d0..83de802b 100644 --- a/common/lc_category.cpp +++ b/common/lc_category.cpp @@ -3,7 +3,7 @@ #include "lc_file.h" #include "lc_profile.h" -lcArray gCategories; +std::vector gCategories; void lcResetDefaultCategories() { @@ -30,7 +30,7 @@ void lcSaveDefaultCategories() lcSetProfileBuffer(LC_PROFILE_CATEGORIES, ByteArray); } -void lcResetCategories(lcArray& Categories, bool BuiltInLibrary) +void lcResetCategories(std::vector& Categories, bool BuiltInLibrary) { const char DefaultCategories[] = { @@ -86,7 +86,7 @@ void lcResetCategories(lcArray& Categories, bool BuiltInLibra lcLoadCategories(Buffer, Categories); } -bool lcLoadCategories(const QString& FileName, lcArray& Categories) +bool lcLoadCategories(const QString& FileName, std::vector& Categories) { QFile File(FileName); @@ -98,9 +98,9 @@ bool lcLoadCategories(const QString& FileName, lcArray& Categ return lcLoadCategories(FileData, Categories); } -bool lcLoadCategories(const QByteArray& Buffer, lcArray& Categories) +bool lcLoadCategories(const QByteArray& Buffer, std::vector& Categories) { - Categories.RemoveAll(); + Categories.clear(); QTextStream Stream(Buffer); @@ -114,16 +114,18 @@ bool lcLoadCategories(const QByteArray& Buffer, lcArray& Cate QString Name = Line.left(Equals); QString Keywords = Line.mid(Equals + 1); - lcLibraryCategory& Category = Categories.Add(); + lcLibraryCategory Category; - Category.Name = Name.toLatin1().constData(); - Category.Keywords = Keywords.toLatin1().constData(); + Category.Name = Name; + Category.Keywords = Keywords.toLatin1(); + + Categories.emplace_back(std::move(Category)); } return true; } -bool lcSaveCategories(const QString& FileName, const lcArray& Categories) +bool lcSaveCategories(const QString& FileName, const std::vector& Categories) { QFile File(FileName); @@ -135,7 +137,7 @@ bool lcSaveCategories(const QString& FileName, const lcArray& return lcSaveCategories(Stream, Categories); } -bool lcSaveCategories(QTextStream& Stream, const lcArray& Categories) +bool lcSaveCategories(QTextStream& Stream, const std::vector& Categories) { QString Format("%1=%2\r\n"); diff --git a/common/lc_category.h b/common/lc_category.h index 32a087da..11c277a0 100644 --- a/common/lc_category.h +++ b/common/lc_category.h @@ -1,24 +1,21 @@ #pragma once -#include "lc_array.h" - struct lcLibraryCategory { QString Name; QByteArray Keywords; }; -extern lcArray gCategories; +extern std::vector gCategories; void lcResetDefaultCategories(); void lcLoadDefaultCategories(bool BuiltInLibrary = false); void lcSaveDefaultCategories(); -void lcResetCategories(lcArray& Categories, bool BuiltInLibrary = false); -bool lcLoadCategories(const QString& FileName, lcArray& Categories); -bool lcLoadCategories(const QByteArray& Buffer, lcArray& Categories); -bool lcSaveCategories(const QString& FileName, const lcArray& Categories); -bool lcSaveCategories(QTextStream& Stream, const lcArray& Categories); +void lcResetCategories(std::vector& Categories, bool BuiltInLibrary = false); +bool lcLoadCategories(const QString& FileName, std::vector& Categories); +bool lcLoadCategories(const QByteArray& Buffer, std::vector& Categories); +bool lcSaveCategories(const QString& FileName, const std::vector& Categories); +bool lcSaveCategories(QTextStream& Stream, const std::vector& Categories); bool lcMatchCategory(const char* PieceName, const char* Expression); - diff --git a/common/lc_library.cpp b/common/lc_library.cpp index ae3073d0..7193c01f 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -1729,7 +1729,7 @@ bool lcPiecesLibrary::PieceInCategory(PieceInfo* Info, const char* CategoryKeywo void lcPiecesLibrary::GetCategoryEntries(int CategoryIndex, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces) { - if (CategoryIndex >= 0 && CategoryIndex < gCategories.GetSize()) + if (CategoryIndex >= 0 && CategoryIndex < static_cast(gCategories.size())) GetCategoryEntries(gCategories[CategoryIndex].Keywords.constData(), GroupPieces, SinglePieces, GroupedPieces); } diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index 79e40a96..6442a21a 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -1110,7 +1110,7 @@ void lcPartSelectionWidget::UpdateCategories() CurrentItem = PaletteCategoryItem; } - for (int CategoryIdx = 0; CategoryIdx < gCategories.GetSize(); CategoryIdx++) + for (int CategoryIdx = 0; CategoryIdx < static_cast(gCategories.size()); CategoryIdx++) { QTreeWidgetItem* CategoryItem = new QTreeWidgetItem(mCategoriesWidget, QStringList(gCategories[CategoryIdx].Name)); CategoryItem->setData(0, static_cast(lcPartCategoryRole::Type), static_cast(lcPartCategoryType::Category)); diff --git a/qt/lc_qpreferencesdialog.cpp b/qt/lc_qpreferencesdialog.cpp index ea4e3532..5b8572ac 100644 --- a/qt/lc_qpreferencesdialog.cpp +++ b/qt/lc_qpreferencesdialog.cpp @@ -377,19 +377,19 @@ void lcQPreferencesDialog::on_ViewSphereSizeCombo_currentIndexChanged(int Index) void lcQPreferencesDialog::updateCategories() { - QTreeWidgetItem *categoryItem; - QTreeWidget *tree = ui->categoriesTree; + QTreeWidgetItem* CategoryItem; + QTreeWidget* CategoriesTree = ui->categoriesTree; - tree->clear(); + CategoriesTree->clear(); - for (int categoryIndex = 0; categoryIndex < mOptions->Categories.GetSize(); categoryIndex++) + for (int CategoryIndex = 0; CategoryIndex < static_cast(mOptions->Categories.size()); CategoryIndex++) { - categoryItem = new QTreeWidgetItem(tree, QStringList(mOptions->Categories[categoryIndex].Name)); - categoryItem->setData(0, CategoryRole, QVariant(categoryIndex)); + CategoryItem = new QTreeWidgetItem(CategoriesTree, QStringList(mOptions->Categories[CategoryIndex].Name)); + CategoryItem->setData(0, CategoryRole, QVariant(CategoryIndex)); } - categoryItem = new QTreeWidgetItem(tree, QStringList(tr("Unassigned"))); - categoryItem->setData(0, CategoryRole, QVariant(-1)); + CategoryItem = new QTreeWidgetItem(CategoriesTree, QStringList(tr("Unassigned"))); + CategoryItem->setData(0, CategoryRole, QVariant(-1)); } void lcQPreferencesDialog::updateParts() @@ -429,13 +429,13 @@ void lcQPreferencesDialog::updateParts() { PieceInfo* Info = PartIt.second; - for (categoryIndex = 0; categoryIndex < mOptions->Categories.GetSize(); categoryIndex++) + for (categoryIndex = 0; categoryIndex < static_cast(mOptions->Categories.size()); categoryIndex++) { if (Library->PieceInCategory(Info, mOptions->Categories[categoryIndex].Keywords.constData())) break; } - if (categoryIndex == mOptions->Categories.GetSize()) + if (categoryIndex == static_cast(mOptions->Categories.size())) { QStringList rowList(Info->m_strDescription); rowList.append(Info->mFileName); @@ -459,10 +459,10 @@ void lcQPreferencesDialog::on_newCategory_clicked() mOptions->CategoriesModified = true; mOptions->CategoriesDefault = false; - mOptions->Categories.Add(category); + mOptions->Categories.emplace_back(std::move(category)); updateCategories(); - ui->categoriesTree->setCurrentItem(ui->categoriesTree->topLevelItem(mOptions->Categories.GetSize() - 1)); + ui->categoriesTree->setCurrentItem(ui->categoriesTree->topLevelItem(static_cast(mOptions->Categories.size()) - 1)); } void lcQPreferencesDialog::on_editCategory_clicked() @@ -508,7 +508,7 @@ void lcQPreferencesDialog::on_deleteCategory_clicked() mOptions->CategoriesModified = true; mOptions->CategoriesDefault = false; - mOptions->Categories.RemoveIndex(categoryIndex); + mOptions->Categories.erase(mOptions->Categories.begin() + categoryIndex); updateCategories(); } @@ -520,7 +520,7 @@ void lcQPreferencesDialog::on_importCategories_clicked() if (FileName.isEmpty()) return; - lcArray Categories; + std::vector Categories; if (!lcLoadCategories(FileName, Categories)) { QMessageBox::warning(this, "LeoCAD", tr("Error loading categories file."));