Added minifig template import/export.
This commit is contained in:
+41
-12
@@ -227,20 +227,25 @@ void MinifigWizard::DeleteTemplate(const QString& TemplateName)
|
||||
mTemplates.erase(TemplateName);
|
||||
}
|
||||
|
||||
void MinifigWizard::LoadTemplates()
|
||||
void MinifigWizard::AddTemplatesJson(const QByteArray& TemplateData)
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
|
||||
QSettings Settings;
|
||||
Settings.beginGroup("Minifig");
|
||||
QByteArray TemplateData = Settings.value("Templates").toByteArray();
|
||||
|
||||
mTemplates.clear();
|
||||
|
||||
QJsonDocument Document = QJsonDocument::fromJson(TemplateData);
|
||||
QJsonObject RootObject = Document.object();
|
||||
|
||||
for (QJsonObject::const_iterator ElementIt = RootObject.constBegin(); ElementIt != RootObject.constEnd(); ElementIt++)
|
||||
int Version = RootObject["Version"].toInt(0);
|
||||
QJsonObject TemplatesObject;
|
||||
|
||||
if (Version > 0)
|
||||
TemplatesObject = RootObject["Templates"].toObject();
|
||||
else
|
||||
TemplatesObject = RootObject;
|
||||
|
||||
for (QJsonObject::const_iterator ElementIt = TemplatesObject.constBegin(); ElementIt != TemplatesObject.constEnd(); ElementIt++)
|
||||
{
|
||||
if (!ElementIt.value().isObject())
|
||||
continue;
|
||||
|
||||
QJsonObject TemplateObject = ElementIt.value().toObject();
|
||||
lcMinifigTemplate Template;
|
||||
|
||||
@@ -258,11 +263,16 @@ void MinifigWizard::LoadTemplates()
|
||||
#endif
|
||||
}
|
||||
|
||||
void MinifigWizard::SaveTemplates()
|
||||
QByteArray MinifigWizard::GetTemplatesJson() const
|
||||
{
|
||||
QByteArray TemplateData;
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
|
||||
QJsonObject RootObject;
|
||||
|
||||
RootObject["Version"] = 1;
|
||||
QJsonObject TemplatesObject;
|
||||
|
||||
for (const auto& TemplateEntry : mTemplates)
|
||||
{
|
||||
const lcMinifigTemplate& Template = TemplateEntry.second;
|
||||
@@ -279,14 +289,33 @@ void MinifigWizard::SaveTemplates()
|
||||
TemplateObject[QLatin1String(mSectionNames[PartIdx])] = PartObject;
|
||||
}
|
||||
|
||||
RootObject[TemplateEntry.first] = TemplateObject;
|
||||
TemplatesObject[TemplateEntry.first] = TemplateObject;
|
||||
}
|
||||
|
||||
QByteArray TemplateData = QJsonDocument(RootObject).toJson(QJsonDocument::Compact);
|
||||
RootObject["Templates"] = TemplatesObject;
|
||||
TemplateData = QJsonDocument(RootObject).toJson();
|
||||
#endif
|
||||
|
||||
return TemplateData;
|
||||
}
|
||||
|
||||
void MinifigWizard::LoadTemplates()
|
||||
{
|
||||
mTemplates.clear();
|
||||
|
||||
QSettings Settings;
|
||||
Settings.beginGroup("Minifig");
|
||||
Settings.setValue("Templates", TemplateData);
|
||||
QByteArray TemplateData = Settings.value("Templates").toByteArray();
|
||||
|
||||
AddTemplatesJson(TemplateData);
|
||||
}
|
||||
|
||||
void MinifigWizard::SaveTemplates()
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
|
||||
QSettings Settings;
|
||||
Settings.beginGroup("Minifig");
|
||||
Settings.setValue("Templates", GetTemplatesJson());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,8 @@ public:
|
||||
|
||||
void SaveTemplate(const QString& TemplateName, const lcMinifigTemplate& Template);
|
||||
void DeleteTemplate(const QString& TemplateName);
|
||||
void AddTemplatesJson(const QByteArray& TemplateData);
|
||||
QByteArray GetTemplatesJson() const;
|
||||
|
||||
void OnDraw();
|
||||
void OnLeftButtonDown();
|
||||
|
||||
@@ -216,6 +216,46 @@ void lcQMinifigDialog::on_TemplateDeleteButton_clicked()
|
||||
UpdateTemplateCombo();
|
||||
}
|
||||
|
||||
void lcQMinifigDialog::on_TemplateImportButton_clicked()
|
||||
{
|
||||
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))
|
||||
{
|
||||
QMessageBox::warning(gMainWindow, tr("Error"), tr("Error reading file '%1':\n%2").arg(FileName, File.errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray FileData = File.readAll();
|
||||
mMinifigWidget->AddTemplatesJson(FileData);
|
||||
|
||||
UpdateTemplateCombo();
|
||||
}
|
||||
|
||||
void lcQMinifigDialog::on_TemplateExportButton_clicked()
|
||||
{
|
||||
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))
|
||||
{
|
||||
QMessageBox::warning(gMainWindow, tr("Error"), tr("Error writing to file '%1':\n%2").arg(FileName, File.errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray Templates = mMinifigWidget->GetTemplatesJson();
|
||||
File.write(Templates);
|
||||
}
|
||||
|
||||
void lcQMinifigDialog::typeChanged(int index)
|
||||
{
|
||||
mMinifigWidget->SetSelectionIndex(getTypeIndex(sender()), index);
|
||||
|
||||
@@ -23,6 +23,8 @@ public slots:
|
||||
void on_TemplateComboBox_currentIndexChanged(const QString& TemplateName);
|
||||
void on_TemplateSaveButton_clicked();
|
||||
void on_TemplateDeleteButton_clicked();
|
||||
void on_TemplateImportButton_clicked();
|
||||
void on_TemplateExportButton_clicked();
|
||||
void typeChanged(int index);
|
||||
void colorChanged(int index);
|
||||
void angleChanged(double value);
|
||||
|
||||
@@ -710,6 +710,20 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="TemplateImportButton">
|
||||
<property name="text">
|
||||
<string>Import...</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="TemplateExportButton">
|
||||
<property name="text">
|
||||
<string>Export...</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
Reference in New Issue
Block a user