Added button to import inventories to a palette.

This commit is contained in:
Leonardo Zide 2020-01-09 12:06:42 -08:00
parent 7a78f86cd5
commit 220fb26a3a
3 changed files with 56 additions and 1 deletions

View file

@ -2,25 +2,34 @@
#include "lc_partpalettedialog.h"
#include "ui_lc_partpalettedialog.h"
#include "lc_partselectionwidget.h"
#include "lc_setsdatabasedialog.h"
lcPartPaletteDialog::lcPartPaletteDialog(QWidget* Parent, std::vector<lcPartPalette>& PartPalettes)
: QDialog(Parent), ui(new Ui::lcPartPaletteDialog), mPartPalettes(PartPalettes)
{
ui->setupUi(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
ui->ImportButton->hide();
#endif
for (const lcPartPalette& Palette : PartPalettes)
{
QListWidgetItem* Item = new QListWidgetItem(Palette.Name);
Item->setData(Qt::UserRole, qVariantFromValue<uintptr_t>(reinterpret_cast<uintptr_t>(&Palette)));
ui->PaletteList->addItem(Item);
}
ui->PaletteList->setCurrentRow(0);
UpdateButtons();
}
lcPartPaletteDialog::~lcPartPaletteDialog()
{
delete ui;
for (lcPartPalette* Palette : mImportedPalettes)
delete Palette;
delete ui;
}
void lcPartPaletteDialog::UpdateButtons()
@ -108,6 +117,42 @@ void lcPartPaletteDialog::on_RenameButton_clicked()
SelectedItems[0]->setText(Name);
}
void lcPartPaletteDialog::on_ImportButton_clicked()
{
lcSetsDatabaseDialog Dialog(this);
if (Dialog.exec() != QDialog::Accepted)
return;
lcPartPalette* Palette = new lcPartPalette;
Palette->Name = Dialog.GetSetDescription();
mImportedPalettes.push_back(Palette);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
QByteArray Inventory = Dialog.GetSetInventory();
QJsonDocument Document = QJsonDocument::fromJson(Inventory);
QJsonObject Root = Document.object();
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();
Palette->Parts.push_back(PartID.toUpper().toStdString() + ".DAT");
}
#endif
QListWidgetItem* Item = new QListWidgetItem(Palette->Name);
Item->setData(Qt::UserRole, qVariantFromValue<uintptr_t>(reinterpret_cast<uintptr_t>(Palette)));
ui->PaletteList->addItem(Item);
ui->PaletteList->setCurrentRow(ui->PaletteList->count() - 1);
UpdateButtons();
}
void lcPartPaletteDialog::on_MoveUpButton_clicked()
{
QList<QListWidgetItem*> SelectedItems = ui->PaletteList->selectedItems();

View file

@ -19,6 +19,7 @@ protected slots:
void on_NewButton_clicked();
void on_DeleteButton_clicked();
void on_RenameButton_clicked();
void on_ImportButton_clicked();
void on_MoveUpButton_clicked();
void on_MoveDownButton_clicked();
void on_PaletteList_currentRowChanged(int CurrentRow);
@ -28,4 +29,5 @@ private:
Ui::lcPartPaletteDialog* ui;
std::vector<lcPartPalette>& mPartPalettes;
std::vector<lcPartPalette*> mImportedPalettes;
};

View file

@ -42,6 +42,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="ImportButton">
<property name="text">
<string>Import...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="MoveUpButton">
<property name="text">
@ -90,6 +97,7 @@
<tabstop>NewButton</tabstop>
<tabstop>DeleteButton</tabstop>
<tabstop>RenameButton</tabstop>
<tabstop>ImportButton</tabstop>
<tabstop>MoveUpButton</tabstop>
<tabstop>MoveDownButton</tabstop>
</tabstops>