Skip to content

Commit

Permalink
OcdFileImport: Import embedded images
Browse files Browse the repository at this point in the history
Layout raster image objects can also be stored as embedded images
directly in the .ocd file.
This commit allows to save the embedded image locally in order to be
added as a template to the map.
  • Loading branch information
dl3sdo committed Mar 17, 2023
1 parent 874282c commit 71f821c
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/fileformats/ocd_file_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#include <QTextCodec>
#include <QTextDecoder>
#include <QVariant>
#include <QMessageBox>
#include <QFile>
#include <QFileInfo>
#include <QFileDevice>

#include "settings.h"
#include "core/georeferencing.h"
Expand Down Expand Up @@ -83,6 +87,7 @@
#include "templates/template_placeholder.h"
#include "util/encoding.h"
#include "util/util.h"
#include "gui/file_dialog.h"


namespace OpenOrienteering {
Expand Down Expand Up @@ -1116,6 +1121,7 @@ void OcdFileImport::importLayoutObjects(const QString& param_string)
double x_value = 0.0;
double y_value = 0.0;
int dimming = 0;
QByteArray embedded_image;

while (parameters.readNext())
{
Expand Down Expand Up @@ -1173,6 +1179,9 @@ void OcdFileImport::importLayoutObjects(const QString& param_string)
case 'd':
dimming = param_value.toInt();
break;
case 'F':
embedded_image = QByteArray::fromBase64(param_value.toUtf8());
break;
default:
; // nothing
}
Expand All @@ -1184,7 +1193,42 @@ void OcdFileImport::importLayoutObjects(const QString& param_string)
}
else if (type == 1)
{
addWarning(tr("Layout image object (%1) imported as template.").arg(path_or_description));
if (!embedded_image.isEmpty())
{
QMessageBox msgBox(QMessageBox::Question, tr("Import embedded image"),
tr("The map contains an embedded image (%1) which cannot be imported directly.\nDo you want to save the image locally and import it as template?").arg(path_or_description),
QMessageBox::Yes | QMessageBox::No, nullptr);
if (msgBox.exec() != QMessageBox::Yes)
return;

auto const filename = path_or_description.replace(QLatin1Char('\\'), QLatin1Char('/'));
const QFileInfo proposed_imagefile(QFileInfo(QDir::cleanPath(path)).dir(), QFileInfo(QDir::cleanPath(filename)).fileName());
QString imagefilename = FileDialog::getSaveFileName(
nullptr,
tr("Save embedded image"),
proposed_imagefile.filePath(),
tr("All files (*.*)"),
nullptr);
if (imagefilename.isEmpty())
return;

QFile file(imagefilename);
if (file.open(QIODevice::WriteOnly))
{
file.write(embedded_image);
file.close();
}
if (file.error() != QFileDevice::NoError)
{
addWarning(tr("Error when trying to save embedded image (%1) locally.").arg(imagefilename));
return;
}
path_or_description = imagefilename;
}
else
{
addWarning(tr("Layout image object (%1) imported as template.").arg(path_or_description));
}

auto const filename = path_or_description.replace(QLatin1Char('\\'), QLatin1Char('/'));
auto const clean_path = QDir::cleanPath(filename);
Expand Down

0 comments on commit 71f821c

Please sign in to comment.