leocad/common/lc_minifigdialog.cpp

281 lines
8.3 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
2020-12-20 20:24:50 +01:00
#include "lc_minifigdialog.h"
#include "ui_lc_minifigdialog.h"
2020-12-18 02:59:11 +01:00
#include "lc_viewwidget.h"
2023-05-16 06:12:08 +02:00
#include "lc_colorpicker.h"
2013-08-09 06:57:18 +02:00
#include "minifig.h"
2020-12-19 20:50:44 +01:00
#include "lc_application.h"
#include "pieceinf.h"
#include "lc_library.h"
2020-12-25 19:43:22 +01:00
#include "lc_view.h"
#include "camera.h"
2013-08-09 06:57:18 +02:00
2020-12-20 20:24:50 +01:00
lcMinifigDialog::lcMinifigDialog(QWidget* Parent)
: QDialog(Parent), ui(new Ui::lcMinifigDialog)
2013-08-09 06:57:18 +02:00
{
ui->setupUi(this);
2020-12-19 20:50:44 +01:00
mComboBoxes =
{
ui->hatsType, ui->hats2Type, ui->headType, ui->neckType, ui->bodyType, ui->body2Type, ui->body3Type, ui->rarmType, ui->larmType,
ui->rhandType, ui->lhandType, ui->rhandaType, ui->lhandaType, ui->rlegType, ui->llegType, ui->rlegaType, ui->llegaType
};
mColorPickers =
{
ui->hatsColor, ui->hats2Color, ui->headColor, ui->neckColor, ui->bodyColor, ui->body2Color, ui->body3Color, ui->rarmColor, ui->larmColor,
ui->rhandColor, ui->lhandColor, ui->rhandaColor, ui->lhandaColor, ui->rlegColor, ui->llegColor, ui->rlegaColor, ui->llegaColor
};
mSpinBoxes =
{
ui->hatsAngle, ui->hats2Angle, ui->headAngle, nullptr, nullptr, nullptr, nullptr, ui->rarmAngle, ui->larmAngle,
ui->rhandAngle, ui->lhandAngle, ui->rhandaAngle, ui->lhandaAngle, ui->rlegAngle, ui->llegAngle, ui->rlegaAngle, ui->llegaAngle
};
for (QComboBox* ComboBox : mComboBoxes)
connect(ComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(TypeChanged(int)));
2023-05-16 06:02:17 +02:00
for (lcColorPicker* ColorPicker : mColorPickers)
connect(ColorPicker, &lcColorPicker::ColorChanged, this, &lcMinifigDialog::ColorChanged);
2020-12-19 20:50:44 +01:00
for (QDoubleSpinBox* SpinBox : mSpinBoxes)
if (SpinBox)
connect(SpinBox, SIGNAL(valueChanged(double)), this, SLOT(AngleChanged(double)));
QGridLayout* PreviewLayout = new QGridLayout(ui->minifigFrame);
PreviewLayout->setContentsMargins(0, 0, 0, 0);
2013-08-09 06:57:18 +02:00
mMinifigWizard = new MinifigWizard();
2020-12-25 19:54:33 +01:00
mView = new lcView(lcViewType::Minifig, mMinifigWizard->GetModel());
2013-08-09 06:57:18 +02:00
lcViewWidget* ViewWidget = new lcViewWidget(nullptr, mView);
2020-12-19 20:50:44 +01:00
ViewWidget->setMinimumWidth(100);
PreviewLayout->addWidget(ViewWidget);
2013-08-09 06:57:18 +02:00
mView->MakeCurrent();
mMinifigWizard->LoadDefault();
for (int ItemIndex = 0; ItemIndex < LC_MFW_NUMITEMS; ItemIndex++)
2013-08-09 06:57:18 +02:00
{
2020-12-20 01:13:25 +01:00
const std::vector<lcMinifigPieceInfo>& PartList = mMinifigWizard->mSettings[ItemIndex];
QStringList ItemStrings;
QVector<int> Separators;
2013-08-09 06:57:18 +02:00
2019-06-05 03:45:22 +02:00
for (const lcMinifigPieceInfo& MinifigPieceInfo : PartList)
{
2019-06-05 03:45:22 +02:00
const char* Description = MinifigPieceInfo.Description;
2013-08-09 06:57:18 +02:00
if (Description[0] != '-' || Description[1] != '-')
ItemStrings.append(Description);
else
Separators.append(ItemStrings.size());
}
2013-08-09 06:57:18 +02:00
2020-12-19 20:50:44 +01:00
QComboBox* ItemCombo = mComboBoxes[ItemIndex];
2013-08-09 06:57:18 +02:00
ItemCombo->blockSignals(true);
ItemCombo->addItems(ItemStrings);
for (int SeparatorIndex = Separators.size() - 1; SeparatorIndex >= 0; SeparatorIndex--)
ItemCombo->insertSeparator(Separators[SeparatorIndex]);
ItemCombo->setCurrentIndex(mMinifigWizard->GetSelectionIndex(ItemIndex));
ItemCombo->blockSignals(false);
2023-05-16 06:02:17 +02:00
lcColorPicker* ColorPicker = mColorPickers[ItemIndex];
2020-12-20 01:13:25 +01:00
ColorPicker->blockSignals(true);
2023-05-16 06:02:17 +02:00
ColorPicker->SetCurrentColor(mMinifigWizard->mMinifig.Colors[ItemIndex]);
2020-12-20 01:13:25 +01:00
ColorPicker->blockSignals(false);
2013-08-09 06:57:18 +02:00
}
2014-04-20 03:50:41 +02:00
UpdateTemplateCombo();
mMinifigWizard->Calculate();
mView->GetCamera()->SetViewpoint(lcVector3(0.0f, -270.0f, 90.0f));
mView->ZoomExtents();
2013-08-09 06:57:18 +02:00
}
2020-12-20 20:24:50 +01:00
lcMinifigDialog::~lcMinifigDialog()
2013-08-09 06:57:18 +02:00
{
delete mMinifigWizard;
2013-08-09 06:57:18 +02:00
delete ui;
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::UpdateTemplateCombo()
{
ui->TemplateComboBox->clear();
const auto& Templates = mMinifigWizard->GetTemplates();
for (const auto& Template : Templates)
ui->TemplateComboBox->addItem(Template.first);
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::on_TemplateComboBox_currentIndexChanged(const QString& TemplateName)
{
const auto& Templates = mMinifigWizard->GetTemplates();
const auto& Position = Templates.find(TemplateName);
if (Position == Templates.end())
return;
const lcMinifigTemplate& Template = Position->second;
for (int PartIdx = 0; PartIdx < LC_MFW_NUMITEMS; PartIdx++)
{
if (!Template.Parts[PartIdx].isEmpty())
{
PieceInfo* Info = lcGetPiecesLibrary()->FindPiece(Template.Parts[PartIdx].toLatin1(), nullptr, false, false);
if (Info)
{
for (const lcMinifigPieceInfo& MinifigPieceInfo : mMinifigWizard->mSettings[PartIdx])
{
if (Info == MinifigPieceInfo.Info)
{
2020-12-19 20:50:44 +01:00
mComboBoxes[PartIdx]->setCurrentText(MinifigPieceInfo.Description);
break;
}
}
}
}
else
{
2020-12-19 20:50:44 +01:00
mComboBoxes[PartIdx]->setCurrentText("None");
}
2023-05-16 06:02:17 +02:00
mColorPickers[PartIdx]->SetCurrentColorCode(Template.Colors[PartIdx]);
2020-12-19 20:50:44 +01:00
QDoubleSpinBox* AngleSpinBox = mSpinBoxes[PartIdx];
if (AngleSpinBox)
AngleSpinBox->setValue(Template.Angles[PartIdx]);
}
mMinifigWizard->Calculate();
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::on_TemplateSaveButton_clicked()
2013-08-09 06:57:18 +02:00
{
QString CurrentName = ui->TemplateComboBox->currentText();
bool Ok;
QString TemplateName = QInputDialog::getText(this, tr("Save Template"), tr("Template Name:"), QLineEdit::Normal, CurrentName, &Ok);
if (!Ok)
return;
if (TemplateName.isEmpty())
{
QMessageBox::information(this, tr("Save Template"), tr("Template name cannot be empty."));
return;
}
if (TemplateName == CurrentName)
{
QString Question = tr("Are you sure you want to overwrite the template '%1'?").arg(TemplateName);
if (QMessageBox::question(this, tr("Overwrite Template"), Question, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
}
lcMinifigTemplate Template;
for (int PartIdx = 0; PartIdx < LC_MFW_NUMITEMS; PartIdx++)
{
Template.Parts[PartIdx] = mMinifigWizard->mSettings[PartIdx][mComboBoxes[PartIdx]->currentIndex()].Info->mFileName;
2023-05-16 06:02:17 +02:00
Template.Colors[PartIdx] = mColorPickers[PartIdx]->GetCurrentColorCode();
2020-12-19 20:50:44 +01:00
QDoubleSpinBox* AngleSpinBox = mSpinBoxes[PartIdx];
Template.Angles[PartIdx] = AngleSpinBox ? AngleSpinBox->value() : 0.0f;
}
mMinifigWizard->SaveTemplate(TemplateName, Template);
ui->TemplateComboBox->blockSignals(true);
UpdateTemplateCombo();
ui->TemplateComboBox->setCurrentText(TemplateName);
ui->TemplateComboBox->blockSignals(false);
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::on_TemplateDeleteButton_clicked()
{
QString Template = ui->TemplateComboBox->currentText();
QString Question = tr("Are you sure you want to delete the template '%1'?").arg(Template);
if (QMessageBox::question(this, tr("Delete Template"), Question, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
mMinifigWizard->DeleteTemplate(Template);
UpdateTemplateCombo();
2013-08-09 06:57:18 +02:00
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::on_TemplateImportButton_clicked()
2019-09-15 02:05:13 +02:00
{
QString FileName = QFileDialog::getOpenFileName(this, tr("Import Templates"), "", tr("Minifig Template Files (*.minifig);;All Files (*.*)"));
if (FileName.isEmpty())
return;
QFile File(FileName);
if (!File.open(QIODevice::ReadOnly))
{
2020-12-19 20:50:44 +01:00
QMessageBox::warning(this, tr("Error"), tr("Error reading file '%1':\n%2").arg(FileName, File.errorString()));
2019-09-15 02:05:13 +02:00
return;
}
QByteArray FileData = File.readAll();
mMinifigWizard->AddTemplatesJson(FileData);
2019-09-15 02:05:13 +02:00
UpdateTemplateCombo();
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::on_TemplateExportButton_clicked()
2019-09-15 02:05:13 +02:00
{
QString FileName = QFileDialog::getSaveFileName(this, tr("Export Templates"), "", tr("Minifig Template Files (*.minifig);;All Files (*.*)"));
if (FileName.isEmpty())
return;
QFile File(FileName);
if (!File.open(QIODevice::WriteOnly))
{
2020-12-19 20:50:44 +01:00
QMessageBox::warning(this, tr("Error"), tr("Error writing to file '%1':\n%2").arg(FileName, File.errorString()));
2019-09-15 02:05:13 +02:00
return;
}
QByteArray Templates = mMinifigWizard->GetTemplatesJson();
2019-09-15 02:05:13 +02:00
File.write(Templates);
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::TypeChanged(int Index)
2013-08-09 06:57:18 +02:00
{
2020-12-19 20:50:44 +01:00
std::array<QComboBox*, LC_MFW_NUMITEMS>::iterator Search = std::find(mComboBoxes.begin(), mComboBoxes.end(), sender());
2013-08-09 06:57:18 +02:00
2020-12-19 20:50:44 +01:00
if (Search == mComboBoxes.end())
return;
2013-08-09 06:57:18 +02:00
mView->MakeCurrent();
mMinifigWizard->SetSelectionIndex(std::distance(mComboBoxes.begin(), Search), Index);
mView->Redraw();
2013-08-09 06:57:18 +02:00
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::ColorChanged(int Index)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
std::array<lcColorPicker*, LC_MFW_NUMITEMS>::iterator Search = std::find(mColorPickers.begin(), mColorPickers.end(), sender());
2013-08-09 06:57:18 +02:00
2020-12-19 20:50:44 +01:00
if (Search == mColorPickers.end())
return;
2013-08-09 06:57:18 +02:00
mMinifigWizard->SetColor(std::distance(mColorPickers.begin(), Search), Index);
mView->Redraw();
2013-08-09 06:57:18 +02:00
}
2020-12-20 20:24:50 +01:00
void lcMinifigDialog::AngleChanged(double Value)
{
2020-12-19 20:50:44 +01:00
std::array<QDoubleSpinBox*, LC_MFW_NUMITEMS>::iterator Search = std::find(mSpinBoxes.begin(), mSpinBoxes.end(), sender());
2020-12-19 20:50:44 +01:00
if (Search == mSpinBoxes.end())
return;
mMinifigWizard->SetAngle(std::distance(mSpinBoxes.begin(), Search), Value);
mView->Redraw();
2013-08-09 06:57:18 +02:00
}