From a2579ebb92d9a34da13ab6d20b577fef8d9c02ac Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 24 Mar 2023 23:25:48 +0100 Subject: [PATCH] Core: support to load old project files containing image planes See forum: https://forum.freecad.org/viewtopic.php?p=670545#p670545 --- src/App/Application.cpp | 18 +++++++++++++++++- src/App/ImagePlane.cpp | 10 +++++----- src/App/ImagePlane.h | 6 +++--- src/Gui/FileHandler.cpp | 2 +- src/Gui/TaskView/TaskImageScale.cpp | 2 +- src/Gui/TaskView/TaskImageScale.h | 4 ++-- src/Gui/ViewProviderImagePlane.cpp | 8 ++++---- 7 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index d5f9ac7ec6..d9f20d6ae8 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -217,6 +217,18 @@ init_freecad_module(void) return PyModule_Create(&FreeCADModuleDef); } +PyMODINIT_FUNC +init_image_module() +{ + static struct PyModuleDef ImageModuleDef = { + PyModuleDef_HEAD_INIT, + "Image", "", -1, + nullptr, + nullptr, nullptr, nullptr, nullptr + }; + return PyModule_Create(&ImageModuleDef); +} + Application::Application(std::map &mConfig) : _mConfig(mConfig), _pActiveDoc(nullptr), _isRestoring(false),_allowPartial(false) , _isClosingAll(false), _objCount(-1), _activeTransactionID(0) @@ -254,6 +266,10 @@ void Application::setupPythonTypes() }; PyObject* pConsoleModule = PyModule_Create(&ConsoleModuleDef); + // fake Image module + PyObject* imageModule = init_image_module(); + PyDict_SetItemString(modules, "Image", imageModule); + // introducing additional classes // NOTE: To finish the initialization of our own type objects we must @@ -2054,7 +2070,7 @@ void Application::initTypes() App::DocumentObjectGroup ::init(); App::DocumentObjectGroupPython ::init(); App::DocumentObjectFileIncluded::init(); - App::ImagePlane ::init(); + Image::ImagePlane ::init(); App::InventorObject ::init(); App::VRMLObject ::init(); App::Annotation ::init(); diff --git a/src/App/ImagePlane.cpp b/src/App/ImagePlane.cpp index e60f7200f8..1e9574a05d 100644 --- a/src/App/ImagePlane.cpp +++ b/src/App/ImagePlane.cpp @@ -25,18 +25,18 @@ #include "ImagePlane.h" -using namespace App; +using namespace Image; -PROPERTY_SOURCE(App::ImagePlane, App::GeoFeature) +PROPERTY_SOURCE(Image::ImagePlane, App::GeoFeature) ImagePlane::ImagePlane() : XPixelsPerMeter{1000.0} , YPixelsPerMeter{1000.0} { - ADD_PROPERTY_TYPE( ImageFile,(nullptr) , "ImagePlane",Prop_None,"File of the image"); - ADD_PROPERTY_TYPE( XSize, (100), "ImagePlane",Prop_None,"Size of a pixel in X"); - ADD_PROPERTY_TYPE( YSize, (100), "ImagePlane",Prop_None,"Size of a pixel in Y"); + ADD_PROPERTY_TYPE( ImageFile,(nullptr) , "ImagePlane",App::Prop_None,"File of the image"); + ADD_PROPERTY_TYPE( XSize, (100), "ImagePlane",App::Prop_None,"Size of a pixel in X"); + ADD_PROPERTY_TYPE( YSize, (100), "ImagePlane",App::Prop_None,"Size of a pixel in Y"); } int ImagePlane::getXSizeInPixel() diff --git a/src/App/ImagePlane.h b/src/App/ImagePlane.h index 0a83ca4302..5588af408b 100644 --- a/src/App/ImagePlane.h +++ b/src/App/ImagePlane.h @@ -27,12 +27,12 @@ #include #include -namespace App +namespace Image { class AppExport ImagePlane : public App::GeoFeature { - PROPERTY_HEADER_WITH_OVERRIDE(App::ImagePlane); + PROPERTY_HEADER_WITH_OVERRIDE(Image::ImagePlane); public: /// Constructor @@ -57,7 +57,7 @@ public: } }; -} //namespace App +} //namespace Image #endif // App_ImagePlane_H diff --git a/src/Gui/FileHandler.cpp b/src/Gui/FileHandler.cpp index 9ddf982012..8b50037fe4 100644 --- a/src/Gui/FileHandler.cpp +++ b/src/Gui/FileHandler.cpp @@ -201,7 +201,7 @@ void FileHandler::openImage() QFileInfo fi; fi.setFile(filename); - Gui::cmdAppDocumentArgs(doc, "addObject('%s', '%s')", "App::ImagePlane", fi.baseName().toStdString()); + 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()"); diff --git a/src/Gui/TaskView/TaskImageScale.cpp b/src/Gui/TaskView/TaskImageScale.cpp index a4e097eef1..881d1b66b3 100644 --- a/src/Gui/TaskView/TaskImageScale.cpp +++ b/src/Gui/TaskView/TaskImageScale.cpp @@ -39,7 +39,7 @@ using namespace Gui; -TaskImageScale::TaskImageScale(App::ImagePlane* obj, QWidget* parent) +TaskImageScale::TaskImageScale(Image::ImagePlane* obj, QWidget* parent) : QWidget(parent) , ui(new Ui_TaskImageScale) , feature(obj) diff --git a/src/Gui/TaskView/TaskImageScale.h b/src/Gui/TaskView/TaskImageScale.h index aa03db8969..198e7d9ad3 100644 --- a/src/Gui/TaskView/TaskImageScale.h +++ b/src/Gui/TaskView/TaskImageScale.h @@ -37,7 +37,7 @@ class TaskImageScale : public QWidget Q_OBJECT public: - explicit TaskImageScale(App::ImagePlane* obj, QWidget* parent = nullptr); + explicit TaskImageScale(Image::ImagePlane* obj, QWidget* parent = nullptr); ~TaskImageScale() override; private: @@ -46,7 +46,7 @@ private: private: std::unique_ptr ui; - App::WeakPtrT feature; + App::WeakPtrT feature; double aspectRatio; }; diff --git a/src/Gui/ViewProviderImagePlane.cpp b/src/Gui/ViewProviderImagePlane.cpp index aefa607083..890249a0c8 100644 --- a/src/Gui/ViewProviderImagePlane.cpp +++ b/src/Gui/ViewProviderImagePlane.cpp @@ -163,7 +163,7 @@ void ViewProviderImagePlane::manipulateImage() dynamic_cast(getObject()) ); dialog->addTaskBox(new TaskImageScale( - dynamic_cast(getObject()) + dynamic_cast(getObject()) )); Gui::Control().showDialog(dialog); @@ -179,7 +179,7 @@ void ViewProviderImagePlane::resizePlane(float xsize, float ysize) void ViewProviderImagePlane::loadImage() { - App::ImagePlane* imagePlane = static_cast(pcObject); + Image::ImagePlane* imagePlane = static_cast(pcObject); std::string fileName = imagePlane->ImageFile.getValue(); if (!fileName.empty()) { @@ -223,7 +223,7 @@ QSizeF ViewProviderImagePlane::loadRaster(const char* fileName, QImage& img) void ViewProviderImagePlane::reloadIfSvg() { - App::ImagePlane* imagePlane = static_cast(pcObject); + Image::ImagePlane* imagePlane = static_cast(pcObject); std::string fileName = imagePlane->ImageFile.getValue(); double xsize = imagePlane->XSize.getValue(); @@ -266,7 +266,7 @@ void ViewProviderImagePlane::convertToSFImage(const QImage& img) void ViewProviderImagePlane::updateData(const App::Property* prop) { - App::ImagePlane* pcPlaneObj = static_cast(pcObject); + Image::ImagePlane* pcPlaneObj = static_cast(pcObject); if (prop == &pcPlaneObj->XSize || prop == &pcPlaneObj->YSize) { float xsize = pcPlaneObj->XSize.getValue(); float ysize = pcPlaneObj->YSize.getValue();