Refactor set inventory parsing.

This commit is contained in:
Leonardo Zide
2026-05-03 15:30:11 -07:00
parent 2171a3cfba
commit 1ea54149c5
8 changed files with 63 additions and 42 deletions
+3 -2
View File
@@ -2531,8 +2531,9 @@ void lcMainWindow::ImportInventory()
return;
Project* NewProject = new Project();
if (NewProject->ImportInventory(Dialog.GetSetInventory(), Dialog.GetSetName(), Dialog.GetSetDescription()))
std::vector<lcSetInventoryItem> SetInventory = Dialog.GetSetInventory();
if (NewProject->ImportInventory(SetInventory, Dialog.GetSetName(), Dialog.GetSetDescription()))
{
gApplication->SetProject(NewProject);
lcView::UpdateProjectViews(NewProject);
+6 -18
View File
@@ -5,6 +5,7 @@
#include "camera.h"
#include "light.h"
#include "group.h"
#include "project.h"
#include "lc_mainwindow.h"
#include "lc_profile.h"
#include "lc_library.h"
@@ -1045,34 +1046,21 @@ bool lcModel::LoadLDD(const QString& FileData)
return true;
}
bool lcModel::LoadInventory(const QByteArray& Inventory)
bool lcModel::LoadInventory(const std::vector<lcSetInventoryItem>& SetInventory)
{
QJsonDocument Document = QJsonDocument::fromJson(Inventory);
QJsonObject Root = Document.object();
const QJsonArray Parts = Root["results"].toArray();
lcPiecesLibrary* Library = lcGetPiecesLibrary();
for (const QJsonValue& Part : Parts)
for (const lcSetInventoryItem& SetInventoryItem : SetInventory)
{
QJsonObject PartObject = Part.toObject();
QByteArray PartID = PartObject["part"].toObject()["part_num"].toString().toLatin1();
QJsonArray PartIDArray = PartObject["part"].toObject()["external_ids"].toObject()["LDraw"].toArray();
if (!PartIDArray.isEmpty())
PartID = PartIDArray.first().toString().toLatin1();
int Quantity = PartObject["quantity"].toInt();
int ColorCode = 16;
QJsonArray ColorArray = PartObject["color"].toObject()["external_ids"].toObject()["LDraw"].toObject()["ext_ids"].toArray();
if (!ColorArray.isEmpty())
ColorCode = ColorArray.first().toInt();
PieceInfo* Info = Library->FindPiece(PartID + ".dat", nullptr, true, false);
PieceInfo* Info = Library->FindPiece(SetInventoryItem.PartID + ".dat", nullptr, true, false);
int Quantity = SetInventoryItem.Quantity;
while (Quantity--)
{
lcPiece* Piece = new lcPiece(nullptr);
Piece->SetPieceInfo(Info, QString(), false, true);
Piece->Initialize(lcMatrix44Identity(), 1);
Piece->SetColorCode(ColorCode);
Piece->SetColorCode(SetInventoryItem.ColorCode);
AddPiece(Piece);
}
}
+2 -1
View File
@@ -5,6 +5,7 @@
enum class lcObjectPropertyId;
enum class lcCameraProjection;
struct lcSetInventoryItem;
struct lcModelHistoryEditState;
class lcModelHistory;
class lcModelHistorySelect;
@@ -263,7 +264,7 @@ public:
void LoadLDraw(QIODevice& Device, Project* Project);
bool LoadBinary(lcFile* File);
bool LoadLDD(const QString& FileData);
bool LoadInventory(const QByteArray& Inventory);
bool LoadInventory(const std::vector<lcSetInventoryItem>& SetInventory);
int SplitMPD(QIODevice& Device);
void Merge(std::unique_ptr<lcModel> Other);
+4 -12
View File
@@ -3,6 +3,7 @@
#include "ui_lc_partpalettedialog.h"
#include "lc_partselectionwidget.h"
#include "lc_setsdatabasedialog.h"
#include "project.h"
lcPartPaletteDialog::lcPartPaletteDialog(QWidget* Parent, std::vector<lcPartPalette>& PartPalettes)
: QDialog(Parent), ui(new Ui::lcPartPaletteDialog), mPartPalettes(PartPalettes)
@@ -132,20 +133,11 @@ void lcPartPaletteDialog::ImportButtonClicked()
Palette->Name = Dialog.GetSetDescription();
mImportedPalettes.push_back(Palette);
QByteArray Inventory = Dialog.GetSetInventory();
QJsonDocument Document = QJsonDocument::fromJson(Inventory);
QJsonObject Root = Document.object();
const QJsonArray Parts = Root["results"].toArray();
std::vector<lcSetInventoryItem> Inventory = Dialog.GetSetInventory();
for (const QJsonValue& Part : Parts)
for (const lcSetInventoryItem& Item : Inventory)
{
QJsonObject PartObject = Part.toObject();
QByteArray PartID = PartObject["part"].toObject()["part_num"].toString().toLatin1();
QJsonArray PartIDArray = PartObject["part"].toObject()["external_ids"].toObject()["LDraw"].toArray();
if (!PartIDArray.isEmpty())
PartID = PartIDArray.first().toString().toLatin1();
Palette->Parts.push_back(PartID.toUpper().toStdString() + ".DAT");
Palette->Parts.push_back(Item.PartID.toUpper().toStdString() + ".DAT");
}
QListWidgetItem* Item = new QListWidgetItem(Palette->Name);
+35
View File
@@ -2,6 +2,7 @@
#include "lc_setsdatabasedialog.h"
#include "ui_lc_setsdatabasedialog.h"
#include "lc_http.h"
#include "project.h"
lcSetsDatabaseDialog::lcSetsDatabaseDialog(QWidget* Parent)
: QDialog(Parent),
@@ -37,6 +38,40 @@ QString lcSetsDatabaseDialog::GetSetDescription() const
return Current ? Current->text(1) : QString();
}
std::vector<lcSetInventoryItem> lcSetsDatabaseDialog::GetSetInventory() const
{
std::vector<lcSetInventoryItem> Inventory;
if (mInventory.isEmpty())
return Inventory;
QJsonDocument Document = QJsonDocument::fromJson(mInventory);
QJsonObject Root = Document.object();
const QJsonArray Parts = Root["results"].toArray();
for (const QJsonValue& Part : Parts)
{
QJsonObject PartObject = Part.toObject();
QByteArray PartID = PartObject["part"].toObject()["part_num"].toString().toLatin1();
QJsonArray PartIDArray = PartObject["part"].toObject()["external_ids"].toObject()["LDraw"].toArray();
if (!PartIDArray.isEmpty())
PartID = PartIDArray.first().toString().toLatin1();
int Quantity = PartObject["quantity"].toInt();
int ColorCode = 16;
QJsonArray ColorArray = PartObject["color"].toObject()["external_ids"].toObject()["LDraw"].toObject()["ext_ids"].toArray();
if (!ColorArray.isEmpty())
ColorCode = ColorArray.first().toInt();
Inventory.push_back({ PartID, Quantity, ColorCode });
}
return Inventory;
}
bool lcSetsDatabaseDialog::eventFilter(QObject* Object, QEvent* Event)
{
if (Event->type() == QEvent::KeyPress)
+2 -5
View File
@@ -2,6 +2,7 @@
class lcHttpReply;
class lcHttpManager;
struct lcSetInventoryItem;
namespace Ui {
class lcSetsDatabaseDialog;
@@ -17,11 +18,7 @@ public:
QString GetSetName() const;
QString GetSetDescription() const;
QByteArray GetSetInventory() const
{
return mInventory;
}
std::vector<lcSetInventoryItem> GetSetInventory() const;
bool eventFilter(QObject* Object, QEvent* Event) override;
+3 -3
View File
@@ -603,14 +603,14 @@ bool Project::ImportLDD(const QString& FileName)
return true;
}
bool Project::ImportInventory(const QByteArray& Inventory, const QString& Name, const QString& Description)
bool Project::ImportInventory(const std::vector<lcSetInventoryItem>& SetInventory, const QString& Name, const QString& Description)
{
if (Inventory.isEmpty())
if (SetInventory.empty())
return false;
std::unique_ptr<lcModel> NewModel(new lcModel(Name, this, false));
if (!NewModel->LoadInventory(Inventory))
if (!NewModel->LoadInventory(SetInventory))
return false;
NewModel->SetSaved();
+8 -1
View File
@@ -36,6 +36,13 @@ struct lcImageDialogOptions
int End;
};
struct lcSetInventoryItem
{
QByteArray PartID;
int Quantity;
int ColorCode;
};
class Project
{
public:
@@ -90,7 +97,7 @@ public:
bool Save(QTextStream& Stream);
void Merge(Project* Other);
bool ImportLDD(const QString& FileName);
bool ImportInventory(const QByteArray& Inventory, const QString& Name, const QString& Description);
bool ImportInventory(const std::vector<lcSetInventoryItem>& SetInventory, const QString& Name, const QString& Description);
void SaveImage(const lcImageDialogOptions& Options);
bool ExportCurrentStep(const QString& FileName);