Skip to content

Commit

Permalink
Gui: fixes FreeCAD#9306: problem importing image if path contains a s…
Browse files Browse the repository at this point in the history
…ingle quote
  • Loading branch information
wwmayer committed Apr 30, 2023
1 parent 1e830a8 commit b5a548e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/Gui/FileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "PythonEditor.h"
#include "MainWindow.h"
#include <App/Application.h>
#include <Base/Tools.h>

using namespace Gui;

Expand Down Expand Up @@ -162,49 +163,44 @@ bool FileHandler::openInternal()
return false;
}

void FileHandler::openInventor()
void FileHandler::openInternal(const char* type, const char* prop)
{
App::Document* doc = createDocumentIfNeeded();

QFileInfo fi;
fi.setFile(filename);

Gui::cmdAppDocumentArgs(doc, "addObject('%s', '%s')", "App::InventorObject", fi.baseName().toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.FileName = '%s'", fi.absoluteFilePath().toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.Label = '%s'", fi.baseName().toStdString());
QString encBase = Base::Tools::escapeEncodeString(fi.baseName());
QString encPath = Base::Tools::escapeEncodeString(fi.absoluteFilePath());

Gui::cmdAppDocumentArgs(doc, "addObject('%s', '%s')", type, encBase.toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.%s = '%s'", prop, encPath.toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.Label = '%s'", encBase.toStdString());
Gui::cmdAppDocument(doc, "recompute()");
}

void FileHandler::openVRML()
void FileHandler::openInventor()
{
App::Document* doc = createDocumentIfNeeded();
openInternal("App::InventorObject", "FileName");
}

void FileHandler::openVRML()
{
QFileInfo fi;
fi.setFile(filename);

// Add this to the search path in order to read inline files (#0002029)
QByteArray path = fi.absolutePath().toUtf8();
SoInput::addDirectoryFirst(path.constData());

Gui::cmdAppDocumentArgs(doc, "addObject('%s', '%s')", "App::VRMLObject", fi.baseName().toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.VrmlFile = '%s'", fi.absoluteFilePath().toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.Label = '%s'", fi.baseName().toStdString());
Gui::cmdAppDocument(doc, "recompute()");
openInternal("App::VRMLObject", "VrmlFile");

SoInput::removeDirectory(path.constData());
}

void FileHandler::openImage()
{
App::Document* doc = createDocumentIfNeeded();

QFileInfo fi;
fi.setFile(filename);

Gui::cmdAppDocumentArgs(doc, "addObject('%s', '%s')", "Image::ImagePlane", fi.baseName().toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.ImageFile = '%s'", fi.absoluteFilePath().toStdString());
Gui::cmdAppDocumentArgs(doc, "ActiveObject.Label = '%s'", fi.baseName().toStdString());
Gui::cmdAppDocument(doc, "recompute()");
openInternal("Image::ImagePlane", "ImageFile");
}

void FileHandler::openPython()
Expand Down
1 change: 1 addition & 0 deletions src/Gui/FileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class FileHandler
App::Document* getOrCreateDocument();
App::Document* getOrCreateDocument(const std::string& document);
bool openInternal();
void openInternal(const char* type, const char* prop);
void openInventor();
void openVRML();
void openImage();
Expand Down

0 comments on commit b5a548e

Please sign in to comment.