From c855af8f46c9fbff52979046999fcbde7ccddde7 Mon Sep 17 00:00:00 2001 From: abn Date: Thu, 28 Jul 2016 10:00:08 +0200 Subject: [PATCH] [MEDCalc]: replace mode now targets the current view only. To be confirmed with Cedric! --- idl/MEDPresentationManager.idl | 2 +- src/MEDCalc/cmp/MEDDataManager_i.cxx | 2 +- src/MEDCalc/cmp/MEDPresentation.cxx | 55 +++++++++++--- src/MEDCalc/cmp/MEDPresentation.hxx | 13 ++-- src/MEDCalc/cmp/MEDPresentationManager_i.cxx | 39 ++++++---- src/MEDCalc/cmp/MEDPresentationManager_i.hxx | 4 +- src/MEDCalc/cmp/MEDPresentationManager_i.txx | 32 +++++++-- src/MEDCalc/cmp/MEDPresentationScalarMap.cxx | 10 +-- src/MEDCalc/cmp/MEDPresentationScalarMap.hxx | 3 +- src/MEDCalc/gui/PresentationController.cxx | 75 +++++++++++++------- 10 files changed, 164 insertions(+), 71 deletions(-) diff --git a/idl/MEDPresentationManager.idl b/idl/MEDPresentationManager.idl index 4007aa8d5..b5566fe5c 100644 --- a/idl/MEDPresentationManager.idl +++ b/idl/MEDPresentationManager.idl @@ -152,7 +152,7 @@ module MEDCALC // Low level ParaVis dump string getParavisDump(in long presId); - MEDPresentationViewMode getPresentationViewMode(in long presId); +// MEDPresentationViewMode getPresentationViewMode(in long presId); }; }; diff --git a/src/MEDCalc/cmp/MEDDataManager_i.cxx b/src/MEDCalc/cmp/MEDDataManager_i.cxx index 7896465c8..f3f5b325a 100644 --- a/src/MEDCalc/cmp/MEDDataManager_i.cxx +++ b/src/MEDCalc/cmp/MEDDataManager_i.cxx @@ -337,7 +337,7 @@ MEDCALC::FieldHandlerList * MEDDataManager_i::getFieldHandlerList() { * This returns a copy of the fieldHandler associated to the specified id. */ MEDCALC::FieldHandler * MEDDataManager_i::getFieldHandler(CORBA::Long fieldHandlerId) { - LOG("getFieldHandler: START") +// LOG("getFieldHandler: START") FieldHandlerMapIterator fieldIt = _fieldHandlerMap.find(fieldHandlerId); if ( fieldIt != _fieldHandlerMap.end() ) { diff --git a/src/MEDCalc/cmp/MEDPresentation.cxx b/src/MEDCalc/cmp/MEDPresentation.cxx index 61c631d68..e4bd9c9ae 100644 --- a/src/MEDCalc/cmp/MEDPresentation.cxx +++ b/src/MEDCalc/cmp/MEDPresentation.cxx @@ -38,14 +38,17 @@ const std::string MEDPresentation::PROP_SCALAR_BAR_RANGE = "scalarBarRange"; MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const std::string& name, const MEDCALC::MEDPresentationViewMode viewMode, - const MEDCALC::MEDPresentationColorMap colorMap) + const MEDCALC::MEDPresentationColorMap colorMap, + const MEDCALC::MEDPresentationScalarBarRange sbRange) : _fieldHandlerId(fieldHandlerId), _propertiesStr(), //_pipeline(0), _display(0) _selectedComponentIndex(-1), _viewMode(viewMode), _colorMap(colorMap), - _renderViewPyId(GeneratePythonId()), - _objId(_renderViewPyId), _dispId(_renderViewPyId), _lutId(_renderViewPyId), + _sbRange(sbRange), + _renderViewPyId(-1), // will be set by getRenderViewCommand() + _objId(GeneratePythonId()), + _dispId(_objId), _lutId(_objId), _globalDict(0) { MEDCALC::MEDDataManager_ptr dataManager(MEDFactoryClient::getDataManager()); @@ -58,7 +61,8 @@ MEDPresentation::MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const s _fieldType = getFieldTypeString((MEDCoupling::TypeOfField) fieldHandler->type); if (_fileName.substr(0, 7) != std::string("file://")) { - const char* msg = "Data source is not a file! Can not proceed."; + const char* msg = "MEDPresentation(): Data source is not a file! Can not proceed."; + STDLOG(msg); throw MEDPresentationException(msg); } _fileName = _fileName.substr(7, _fileName.size()); @@ -104,18 +108,25 @@ MEDPresentation::generatePipeline() void MEDPresentation::pushAndExecPyLine(const std::string & lin) +{ + execPyLine(lin); + _pythonCmds.push_back(lin); +} + +void +MEDPresentation::execPyLine(const std::string & lin) { PyLockWrapper lock; -// std::cerr << lin << std::endl; +// STDLOG("@@@@ MEDPresentation::execPyLine() about to exec >> " << lin); if(PyRun_SimpleString(lin.c_str())) { std::ostringstream oss; - oss << "MEDPresentation::pushAndExecPyLine(): following Python command failed!\n"; + oss << "MEDPresentation::execPyLine(): following Python command failed!\n"; oss << ">> " << lin; STDLOG(oss.str()); throw KERNEL::createSalomeException(oss.str().c_str()); } - _pythonCmds.push_back(lin); + } void @@ -132,6 +143,7 @@ MEDPresentation::getStringProperty(const std::string& propName) const return (*it).second; } else { + STDLOG("MEDPresentation::getStringProperty(): no property named " + propName); throw MEDPresentationException("MEDPresentation::getStringProperty(): no property named " + propName); } } @@ -150,6 +162,7 @@ MEDPresentation::getIntProperty(const std::string& propName) const return (*it).second; } else { + STDLOG("MEDPresentation::getIntProperty(): no property named " + propName); throw MEDPresentationException("MEDPresentation::getIntProperty(): no property named " + propName); } } @@ -180,6 +193,13 @@ MEDPresentation::getIntProperty(const std::string& propName) const } } + void + MEDPresentation::internalGeneratePipeline() + { + PyLockWrapper lock; + pushAndExecPyLine( "import pvsimple as pvs;"); + } + PyObject* MEDPresentation::getPythonObjectFromMain(const char* python_var) const @@ -212,12 +232,16 @@ std::string MEDPresentation::getRenderViewCommand() const { std::ostringstream oss, oss2; + oss << "__view" << _renderViewPyId; std::string view(oss.str()); oss2 << "pvs._DisableFirstRenderCameraReset();" << std::endl; if (_viewMode == MEDCALC::VIEW_MODE_OVERLAP) { + // this might potentially re-assign to an existing view variable, but this is OK, we + // normally reassign exaclty the same RenderView object. oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl; } else if (_viewMode == MEDCALC::VIEW_MODE_REPLACE) { + // same as above oss2 << view << " = pvs.GetActiveViewOrCreate('RenderView');" << std::endl; oss2 << "pvs.active_objects.source and pvs.Hide(view=" << view << ");" << std::endl; oss2 << "pvs.Render();" << std::endl; @@ -269,6 +293,7 @@ MEDPresentation::getColorMapCommand() const oss << lut << ".ApplyPreset('Cool to Warm',True);"; break; default: + STDLOG("MEDPresentation::getColorMapCommand(): invalid colormap!"); throw KERNEL::createSalomeException("MEDPresentation::getColorMapCommand(): invalid colormap!"); } return oss.str(); @@ -287,6 +312,7 @@ MEDPresentation::getRescaleCommand() const oss << disp << ".RescaleTransferFunctionToDataRange(False);"; break; default: + STDLOG("MEDPresentation::getRescaleCommand(): invalid range!"); throw KERNEL::createSalomeException("MEDPresentation::getRescaleCommand(): invalid range!"); } return oss.str(); @@ -348,30 +374,37 @@ MEDPresentation::fillAvailableFieldComponents() } else { std::string msg("Unsupported spatial discretisation: " + _fieldType); + STDLOG(msg); throw KERNEL::createSalomeException(msg.c_str()); } std::ostringstream oss; oss << "__nbCompo = " << obj << "." << typ << ".GetArray('" << _fieldName << "').GetNumberOfComponents();"; - PyRun_SimpleString(oss.str().c_str()); + execPyLine(oss.str()); PyObject* p_obj = getPythonObjectFromMain("__nbCompo"); long nbCompo; if (p_obj && PyInt_Check(p_obj)) nbCompo = PyInt_AS_LONG(p_obj); else - throw KERNEL::createSalomeException("Unexpected Python error"); + { + STDLOG("Unexpected Python error"); + throw KERNEL::createSalomeException("Unexpected Python error"); + } setIntProperty(MEDPresentation::PROP_NB_COMPONENTS, nbCompo); for (long i = 0; i PyObjectId; static int GeneratePythonId(); MEDPresentation(MEDPresentation::TypeID fieldHandlerId, const std::string& name, const MEDCALC::MEDPresentationViewMode viewMode, - const MEDCALC::MEDPresentationColorMap colorMap); + const MEDCALC::MEDPresentationColorMap colorMap, + const MEDCALC::MEDPresentationScalarBarRange sbRange); std::string getRenderViewCommand() const; std::string getResetCameraCommand() const; @@ -77,18 +81,17 @@ protected: std::string getColorMapCommand() const; std::string getRescaleCommand() const; - virtual void internalGeneratePipeline() = 0; + virtual void internalGeneratePipeline(); PyObject* getPythonObjectFromMain(const char* var) const; // void pushPyObjects(PyObjectId obj, PyObjectId disp); + void execPyLine(const std::string & lin); void pushAndExecPyLine(const std::string & lin); MEDPresentation::TypeID getID() const { return _fieldHandlerId; } - long getPyViewID() const { return _renderViewPyId; } void fillAvailableFieldComponents(); - // TODO: follow the pattern of the others methods: template! - virtual MEDCALC::MEDPresentationViewMode getViewMode() = 0; +// virtual MEDCALC::MEDPresentationViewMode getViewMode() = 0; template void updateComponent(const std::string& newCompo); diff --git a/src/MEDCalc/cmp/MEDPresentationManager_i.cxx b/src/MEDCalc/cmp/MEDPresentationManager_i.cxx index f2608c125..b50697813 100644 --- a/src/MEDCalc/cmp/MEDPresentationManager_i.cxx +++ b/src/MEDCalc/cmp/MEDPresentationManager_i.cxx @@ -43,7 +43,9 @@ MEDPresentationManager_i::getInstance() { return _instance; } -MEDPresentationManager_i::MEDPresentationManager_i() +MEDPresentationManager_i::MEDPresentationManager_i() : + _presentations(), + _activeViewPythonId(-1) { } @@ -214,10 +216,7 @@ MEDPresentationManager_i::removePresentation(MEDPresentation::TypeID presentatio delete presentation; _presentations.erase(presentationID); - std::stringstream sstm; - sstm << "Presentation " << presentationID << " has been removed.\n"; - STDLOG(sstm.str()); - + STDLOG("Presentation " << presentationID << " has been removed."); return true; } @@ -232,21 +231,33 @@ MEDPresentationManager_i::activateView(MEDPresentation::TypeID presentationID) MEDPresentation* presentation = (*citr).second; presentation->activateView(); + _activeViewPythonId = presentation->getPyViewID(); return true; } -MEDCALC::MEDPresentationViewMode -MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID) +CORBA::Long +MEDPresentationManager_i::getActiveViewPythonId() { - MEDPresentation* pres = _getPresentation(presentationID); - if (pres) { - return pres->getViewMode(); - } else { - std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl; - return MEDCALC::VIEW_MODE_DEFAULT; - } + //TODO: should be more elaborate to re-identify the active ParaView view when another + //mechanism than MED module has activated another view. + // Idea: 1/ call Python to current active view + // 2/ compare with all id(__viewX) from currently existing presentations + return _activeViewPythonId; } + +//MEDCALC::MEDPresentationViewMode +//MEDPresentationManager_i::getPresentationViewMode(MEDPresentation::TypeID presentationID) +//{ +// MEDPresentation* pres = _getPresentation(presentationID); +// if (pres) { +// return pres->getViewMode(); +// } else { +// std::cerr << "setPresentationProperty(): presentation not found!!" << std::endl; +// return MEDCALC::VIEW_MODE_DEFAULT; +// } +//} + char* MEDPresentationManager_i::getParavisDump(MEDPresentation::TypeID presentationID) { diff --git a/src/MEDCalc/cmp/MEDPresentationManager_i.hxx b/src/MEDCalc/cmp/MEDPresentationManager_i.hxx index 509286729..095843ce8 100644 --- a/src/MEDCalc/cmp/MEDPresentationManager_i.hxx +++ b/src/MEDCalc/cmp/MEDPresentationManager_i.hxx @@ -63,7 +63,8 @@ class MEDPresentationManager_i: public POA_MEDCALC::MEDPresentationManager, MEDCALC_EXPORT CORBA::Boolean removePresentation(MEDPresentation::TypeID); MEDCALC_EXPORT CORBA::Boolean activateView(MEDPresentation::TypeID); - MEDCALC_EXPORT MEDCALC::MEDPresentationViewMode getPresentationViewMode(MEDPresentation::TypeID); + MEDCALC_EXPORT CORBA::Long getActiveViewPythonId(); +// MEDCALC_EXPORT MEDCALC::MEDPresentationViewMode getPresentationViewMode(MEDPresentation::TypeID); MEDCALC_EXPORT char* getParavisDump(MEDPresentation::TypeID presentationID); private: @@ -95,6 +96,7 @@ class MEDPresentationManager_i: public POA_MEDCALC::MEDPresentationManager, // Owns a list of MEDPresentation objects std::map _presentations; + long _activeViewPythonId; }; #include "MEDPresentationManager_i.txx" diff --git a/src/MEDCalc/cmp/MEDPresentationManager_i.txx b/src/MEDCalc/cmp/MEDPresentationManager_i.txx index 80f5e84f8..3ae7c4986 100644 --- a/src/MEDCalc/cmp/MEDPresentationManager_i.txx +++ b/src/MEDCalc/cmp/MEDPresentationManager_i.txx @@ -20,30 +20,52 @@ #ifndef _MED_PRESENTATION_MANAGER_I_TXX_ #define _MED_PRESENTATION_MANAGER_I_TXX_ +#include + template MEDPresentation::TypeID MEDPresentationManager_i::_makePresentation(const PresentationParameters params, const MEDCALC::MEDPresentationViewMode viewMode) { + int activeViewId = getActiveViewPythonId(); + // Replace = Remove then add - if (viewMode == MEDCALC::VIEW_MODE_REPLACE) { - MEDPresentation::TypeID currentPresentationId = _getActivePresentationId(); - if (currentPresentationId > -1) - removePresentation(currentPresentationId); + if (viewMode == MEDCALC::VIEW_MODE_REPLACE) + { + // Remove all presentations from this view: + std::map::const_iterator it; + std::vector to_del; + for (it = _presentations.begin(); it != _presentations.end(); ++it) + { + int viewId2 = (*it).second->getPyViewID(); + if (viewId2 == activeViewId) + to_del.push_back((*it).first); + } + for (std::vector::const_iterator it2 = to_del.begin(); it2 != to_del.end(); ++it2) + removePresentation(*it2); } // Create a new presentation instance PresentationType* presentation = NULL; + MEDPresentation::TypeID newID = MEDPresentationManager_i::GenerateID(); + STDLOG("Generated presentation ID: " << newID); try { presentation = new PresentationType(params, viewMode); // on stack or on heap?? heap for now + // In replace or overlap mode we force the display in the active view: + if(activeViewId != -1 && (viewMode == MEDCALC::VIEW_MODE_REPLACE || viewMode == MEDCALC::VIEW_MODE_OVERLAP)) + presentation->setPyViewID(activeViewId); + else + presentation->setPyViewID(newID); // ensures a new ID for the view } catch (const std::exception& e) { std::cerr << e.what() << std::endl; + STDLOG("Error: " << e.what()); return -1; } - MEDPresentation::TypeID newID = MEDPresentationManager_i::GenerateID(); _presentations.insert( std::pair(newID, presentation) ); presentation->generatePipeline(); + // Make the view holding the newly created presentation the active one: + activateView(newID); return newID; } diff --git a/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx b/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx index 03c46af00..9c0181b3e 100644 --- a/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx +++ b/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx @@ -31,25 +31,25 @@ const std::string MEDPresentationScalarMap::TYPE_NAME = "MEDPresentationScalarMa MEDPresentationScalarMap::MEDPresentationScalarMap(const MEDCALC::ScalarMapParameters& params, const MEDCALC::MEDPresentationViewMode viewMode) : - MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap), _params(params) + MEDPresentation(params.fieldHandlerId, TYPE_NAME, viewMode, params.colorMap, params.scalarBarRange), _params(params) { } void MEDPresentationScalarMap::internalGeneratePipeline() { + MEDPresentation::internalGeneratePipeline(); + PyLockWrapper lock; std::ostringstream oss_o, oss_d,oss_l, oss, oss_v; oss_o << "__obj" << _objId; std::string obj(oss_o.str()); oss_d << "__disp" << _dispId; std::string disp(oss_d.str()); oss_l << "__lut" << _lutId; std::string lut(oss_l.str()); - - pushAndExecPyLine( "import pvsimple as pvs;"); - pushAndExecPyLine( getRenderViewCommand() ); // define __viewXXX - oss_v << "__view" << _renderViewPyId; std::string view(oss_v.str()); + pushAndExecPyLine( getRenderViewCommand() ); // instanciate __viewXXX + oss << obj << " = pvs.MEDReader(FileName='" << _fileName << "');"; pushAndExecPyLine(oss.str()); oss.str(""); diff --git a/src/MEDCalc/cmp/MEDPresentationScalarMap.hxx b/src/MEDCalc/cmp/MEDPresentationScalarMap.hxx index 71c5521d4..d396d24f3 100644 --- a/src/MEDCalc/cmp/MEDPresentationScalarMap.hxx +++ b/src/MEDCalc/cmp/MEDPresentationScalarMap.hxx @@ -33,11 +33,12 @@ public: virtual ~MEDPresentationScalarMap() {} void updatePipeline(const MEDCALC::ScalarMapParameters& params); - MEDCALC::MEDPresentationViewMode getViewMode() { return _params.viewMode; } void getParameters(MEDCALC::ScalarMapParameters & params) const { params = _params; } ; void setParameters(const MEDCALC::ScalarMapParameters & params) { _params = params; } ; +// MEDCALC::MEDPresentationViewMode getViewMode() { return _params.viewMode; } + protected: virtual void internalGeneratePipeline(); diff --git a/src/MEDCalc/gui/PresentationController.cxx b/src/MEDCalc/gui/PresentationController.cxx index 0f7735122..032127d55 100644 --- a/src/MEDCalc/gui/PresentationController.cxx +++ b/src/MEDCalc/gui/PresentationController.cxx @@ -599,7 +599,7 @@ PresentationController::updateTreeViewWithNewPresentation(long fieldId, long pre return; } - std::string name(MEDFactoryClient::getPresentationManager()->getPresentationStringProperty(presentationId, MEDPresentation::PROP_NAME.c_str())); + std::string name(_presManager->getPresentationStringProperty(presentationId, MEDPresentation::PROP_NAME.c_str())); std::string type = name; std::string icon = std::string("ICO_") + type; icon = _getIconName(icon); @@ -617,30 +617,30 @@ PresentationController::updateTreeViewWithNewPresentation(long fieldId, long pre oss.str().c_str(), type.c_str(),ico.c_str(), presentationId); - MEDCALC::MEDPresentationViewMode viewMode = MEDFactoryClient::getPresentationManager()->getPresentationViewMode(presentationId); - - // Remove sibling presentations if view mode is set to REPLACE - if (viewMode == MEDCALC::VIEW_MODE_REPLACE) { - MED_ORB::PresentationsList* presList = _salomeModule->engine()->getSiblingPresentations(_CAST(Study, studyDS)->GetStudy(), presentationId); - CORBA::ULong size = presList->length(); - - std::stringstream sstm; - sstm << "Removing sibling presentation(s): "; - for (int i = 0; i < size; ++i) - sstm << (*presList)[i] << " "; - STDLOG(sstm.str()); - - for (int i = 0; i < size; ++i) { - PresentationEvent* event = new PresentationEvent(); - event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION; - XmedDataObject* dataObject = new XmedDataObject(); - dataObject->setPresentationId((*presList)[i]); - event->objectdata = dataObject; - emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent - } - - delete presList; - } +// MEDCALC::MEDPresentationViewMode viewMode = MEDFactoryClient::getPresentationManager()->getPresentationViewMode(presentationId); +// +// // Remove sibling presentations if view mode is set to REPLACE +// if (viewMode == MEDCALC::VIEW_MODE_REPLACE) { +// MED_ORB::PresentationsList* presList = _salomeModule->engine()->getSiblingPresentations(_CAST(Study, studyDS)->GetStudy(), presentationId); +// CORBA::ULong size = presList->length(); +// +// std::stringstream sstm; +// sstm << "Removing sibling presentation(s): "; +// for (int i = 0; i < size; ++i) +// sstm << (*presList)[i] << " "; +// STDLOG(sstm.str()); +// +// for (int i = 0; i < size; ++i) { +// PresentationEvent* event = new PresentationEvent(); +// event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION; +// XmedDataObject* dataObject = new XmedDataObject(); +// dataObject->setPresentationId((*presList)[i]); +// event->objectdata = dataObject; +// emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent +// } +// +// delete presList; +// } // update Object browser _salomeModule->getApp()->updateObjectBrowser(true); @@ -667,10 +667,31 @@ void PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event) { if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) { - this->updateTreeViewWithNewPresentation(event->dataId, event->presentationId); + updateTreeViewWithNewPresentation(event->dataId, event->presentationId); + // Deal with replace mode: presentations with invalid IDs have to be removed: + std::map::iterator it; + std::vector to_del; + for (it = _presHelperMap.begin(); it != _presHelperMap.end(); ++it) + { + try { + // TODO: not the best way to test for presentation existence at the engine level? + _presManager->getPresentationStringProperty((*it).first, MEDPresentation::PROP_NAME.c_str()); + } + catch(...){ + to_del.push_back((*it).first); + delete((*it).second); + (*it).second = 0; + } + } + std::vector::const_iterator it2; + for (it2 = to_del.begin(); it2 != to_del.end(); ++it2) + { + updateTreeViewForPresentationRemoval(*it2); + _presHelperMap.erase(*it2); + } } else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) { - this->updateTreeViewForPresentationRemoval(event->presentationId); + updateTreeViewForPresentationRemoval(event->presentationId); } } -- 2.39.2