Skip to content

Commit

Permalink
Use unique_ptr for models.
Browse files Browse the repository at this point in the history
  • Loading branch information
leozide committed Jun 17, 2024
1 parent 3378359 commit b070b0a
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 118 deletions.
9 changes: 8 additions & 1 deletion common/lc_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,15 @@ PieceInfo* lcPiecesLibrary::FindPiece(const char* PieceName, Project* CurrentPro
if (PieceIt != mPieces.end())
{
PieceInfo* Info = PieceIt->second;
bool HasModel = false;

if ((!CurrentProject || !Info->IsModel() || CurrentProject->GetModels().FindIndex(Info->GetModel()) != -1) && (!ProjectPath.isEmpty() || !Info->IsProject() || Info->IsProjectPiece()))
if (lcGetActiveProject())
{
const std::vector<std::unique_ptr<lcModel>>& Models = lcGetActiveProject()->GetModels();
HasModel = std::find_if(Models.begin(), Models.end(), [Model = Info->GetModel()](const std::unique_ptr<lcModel>& CheckModel) { return CheckModel.get() == Model; }) != Models.end();
}

if ((!CurrentProject || !Info->IsModel() || HasModel) && (!ProjectPath.isEmpty() || !Info->IsProject() || Info->IsProjectPiece()))
return Info;
}

Expand Down
35 changes: 20 additions & 15 deletions common/lc_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ void lcMainWindow::ModelTabChanged(int Index)
Project* Project = lcGetActiveProject();
lcModelTabWidget* CurrentTab = (lcModelTabWidget*)mModelTabWidget->widget(Index);

Project->SetActiveModel(Project->GetModels().FindIndex(CurrentTab ? CurrentTab->GetModel() : nullptr));
Project->SetActiveModel(CurrentTab ? CurrentTab->GetModel() : nullptr);
}

void lcMainWindow::ClipboardChanged()
Expand Down Expand Up @@ -2234,32 +2234,37 @@ void lcMainWindow::UpdateSelectionMode()

void lcMainWindow::UpdateModels()
{
const lcArray<lcModel*>& Models = lcGetActiveProject()->GetModels();
const std::vector<std::unique_ptr<lcModel>>& Models = lcGetActiveProject()->GetModels();
lcModel* CurrentModel = lcGetActiveModel();

for (int ActionIdx = LC_MODEL_FIRST; ActionIdx <= LC_MODEL_LAST; ActionIdx++)
for (size_t ActionIndex = LC_MODEL_FIRST; ActionIndex <= LC_MODEL_LAST; ActionIndex++)
{
QAction* Action = mActions[ActionIdx];
int ModelIdx = ActionIdx - LC_MODEL_FIRST;
QAction* Action = mActions[ActionIndex];
size_t ModelIndex = ActionIndex - LC_MODEL_FIRST;

if (ModelIdx < Models.size())
if (ModelIndex < Models.size())
{
Action->setChecked(CurrentModel == Models[ModelIdx]);
Action->setText(QString::fromLatin1("%1%2 %3").arg(ModelIdx < 9 ? QString("&") : QString(), QString::number(ModelIdx + 1), Models[ModelIdx]->GetProperties().mFileName));
Action->setChecked(CurrentModel == Models[ModelIndex].get());
Action->setText(QString::fromLatin1("%1%2 %3").arg(ModelIndex < 9 ? QString("&") : QString(), QString::number(ModelIndex + 1), Models[ModelIndex]->GetProperties().mFileName));
Action->setVisible(true);
}
else
Action->setVisible(false);
}

auto HasModel = [&Models](lcModel* Model)
{
return std::find_if(Models.begin(), Models.end(), [Model](const std::unique_ptr<lcModel>& CheckModel) { return CheckModel.get() == Model; }) != Models.end();
};

for (int TabIdx = 0; TabIdx < mModelTabWidget->count(); )
{
lcModelTabWidget* TabWidget = (lcModelTabWidget*)mModelTabWidget->widget(TabIdx);
lcModel* Model = TabWidget->GetModel();

if (!Model)
TabIdx++;
else if (Models.FindIndex(Model) != -1)
else if (HasModel(Model))
{
mModelTabWidget->setTabText(TabIdx, Model->GetProperties().mFileName);
TabIdx++;
Expand All @@ -2271,7 +2276,7 @@ void lcMainWindow::UpdateModels()
mPartSelectionWidget->UpdateModels();

if (mCurrentPieceInfo && mCurrentPieceInfo->IsModel())
if (Models.FindIndex(mCurrentPieceInfo->GetModel()) == -1)
if (!HasModel(mCurrentPieceInfo->GetModel()))
SetCurrentPieceInfo(nullptr);
}

Expand Down Expand Up @@ -2395,14 +2400,14 @@ void lcMainWindow::MergeProject()

if (NewProject->Load(LoadFileName, true))
{
int NumModels = NewProject->GetModels().size();
size_t ModelCount = NewProject->GetModels().size();

lcGetActiveProject()->Merge(NewProject);

if (NumModels == 1)
if (ModelCount == 1)
QMessageBox::information(this, tr("LeoCAD"), tr("Merged 1 submodel."));
else
QMessageBox::information(this, tr("LeoCAD"), tr("Merged %1 submodels.").arg(NumModels));
QMessageBox::information(this, tr("LeoCAD"), tr("Merged %1 submodels.").arg(ModelCount));

UpdateModels();
}
Expand Down Expand Up @@ -2522,7 +2527,7 @@ bool lcMainWindow::SetModelFromFocus()
if (Model)
{
Project* Project = lcGetActiveProject();
Project->SetActiveModel(Project->GetModels().FindIndex(Model));
Project->SetActiveModel(Model);
return true;
}

Expand All @@ -2539,7 +2544,7 @@ void lcMainWindow::SetModelFromSelection()
if (Model)
{
Project* Project = lcGetActiveProject();
Project->SetActiveModel(Project->GetModels().FindIndex(Model));
Project->SetActiveModel(Model);
}
}

Expand Down
6 changes: 3 additions & 3 deletions common/lc_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3250,15 +3250,15 @@ lcModel* lcModel::GetFirstSelectedSubmodel() const
return nullptr;
}

void lcModel::GetSubModels(lcArray<lcModel*>& SubModels) const
void lcModel::GetSubModels(std::set<lcModel*>& SubModels) const
{
for (const lcPiece* Piece : mPieces)
{
if (Piece->mPieceInfo->IsModel())
{
lcModel* SubModel = Piece->mPieceInfo->GetModel();
if (SubModels.FindIndex(SubModel) == -1)
SubModels.emplace_back(SubModel);

SubModels.insert(SubModel);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion common/lc_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class lcModel
bool AnyPiecesSelected() const;
bool AnyObjectsSelected() const;
lcModel* GetFirstSelectedSubmodel() const;
void GetSubModels(lcArray<lcModel*>& SubModels) const;
void GetSubModels(std::set<lcModel*>& SubModels) const;
bool GetMoveRotateTransform(lcVector3& Center, lcMatrix33& RelativeRotation) const;
bool CanRotateSelection() const;
bool GetPieceFocusOrSelectionCenter(lcVector3& Center) const;
Expand Down
15 changes: 11 additions & 4 deletions common/lc_modellistdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@ enum class lcModelListRole
DuplicateModel
};

lcModelListDialog::lcModelListDialog(QWidget* Parent, const lcArray<lcModel*> Models)
lcModelListDialog::lcModelListDialog(QWidget* Parent, const std::vector<std::unique_ptr<lcModel>>& Models)
: QDialog(Parent), ui(new Ui::lcModelListDialog)
{
mActiveModelItem = nullptr;

ui->setupUi(this);

for (const lcModel* Model : Models)
lcModel* ActiveModel = lcGetActiveProject()->GetActiveModel();
int ActiveModelIndex = -1;

for (const std::unique_ptr<lcModel>& Model : Models)
{
QListWidgetItem* Item = new QListWidgetItem(Model->GetProperties().mFileName);
Item->setData(static_cast<int>(lcModelListRole::ExistingModel), QVariant::fromValue<uintptr_t>((uintptr_t)Model));
Item->setData(static_cast<int>(lcModelListRole::ExistingModel), QVariant::fromValue<uintptr_t>((uintptr_t)Model.get()));
ui->ModelList->addItem(Item);

if (Model.get() == ActiveModel )
ActiveModelIndex = ui->ModelList->count() - 1;
}

ui->ModelList->setCurrentRow(lcGetActiveProject()->GetActiveModelIndex());
if (ActiveModelIndex != -1)
ui->ModelList->setCurrentRow(ActiveModelIndex);

QSettings Settings;
ui->SetActiveModel->setChecked(Settings.value("Settings/ModelListSetActive", true).toBool());
Expand Down
2 changes: 1 addition & 1 deletion common/lc_modellistdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class lcModelListDialog : public QDialog
Q_OBJECT

public:
lcModelListDialog(QWidget* Parent, const lcArray<lcModel*> Models);
lcModelListDialog(QWidget* Parent, const std::vector<std::unique_ptr<lcModel>>& Models);
~lcModelListDialog();

int GetActiveModelIndex() const;
Expand Down
8 changes: 2 additions & 6 deletions common/lc_partselectionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,12 @@ void lcPartSelectionListModel::SetModelsCategory()

mParts.clear();

const lcArray<lcModel*>& Models = lcGetActiveProject()->GetModels();
const std::vector<std::unique_ptr<lcModel>>& Models = lcGetActiveProject()->GetModels();
lcModel* ActiveModel = gMainWindow->GetActiveModel();

for (int ModelIdx = 0; ModelIdx < Models.size(); ModelIdx++)
{
lcModel* Model = Models[ModelIdx];

for (const std::unique_ptr<lcModel>& Model : Models)
if (!Model->IncludesModel(ActiveModel))
mParts.emplace_back(std::pair<PieceInfo*, QPixmap>(Model->GetPieceInfo(), QPixmap()));
}

auto lcPartSortFunc = [](const std::pair<PieceInfo*, QPixmap>& a, const std::pair<PieceInfo*, QPixmap>& b)
{
Expand Down
Loading

0 comments on commit b070b0a

Please sign in to comment.