From 8fc043b60678c43899f63f2da96f6b846a202940 Mon Sep 17 00:00:00 2001 From: azv Date: Fri, 10 Dec 2021 10:40:56 +0300 Subject: [PATCH] Move the functionality of setting a DISPLAY option from XGUI to ModuleBase module. This gives straight behavior in case of UI libraries are not loaded yet during SALOME launching. --- src/ModuleBase/ModuleBase_Tools.cpp | 93 +++++++++++++++++++++++++++++ src/ModuleBase/ModuleBase_Tools.h | 5 ++ src/PartSet/PartSet_MenuMgr.cpp | 2 +- src/XGUI/XGUI_Tools.cpp | 45 -------------- src/XGUI/XGUI_Tools.h | 5 -- src/XGUI/XGUI_Workshop.cpp | 4 +- src/XGUI/XGUI_WorkshopListener.cpp | 15 ----- 7 files changed, 101 insertions(+), 68 deletions(-) diff --git a/src/ModuleBase/ModuleBase_Tools.cpp b/src/ModuleBase/ModuleBase_Tools.cpp index b014d60de..1b5bfc9a3 100644 --- a/src/ModuleBase/ModuleBase_Tools.cpp +++ b/src/ModuleBase/ModuleBase_Tools.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +55,12 @@ #include +#ifdef HAVE_SALOME +#include +#include +#include +#endif + #include #include #include @@ -108,6 +116,51 @@ namespace ModuleBase_Tools { //****************************************************************** + //! Waits for REDISPLAY message and set the Visible flag to the entities + //! according to Preferences choice. + class ModuleBase_RedisplayListener : public Events_Listener + { + public: + static std::shared_ptr instance() + { + static std::shared_ptr + anInstance(new ModuleBase_RedisplayListener); + return anInstance; + } + + void processEvent(const std::shared_ptr& theMessage) + { + if (theMessage->eventID() == Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY)) + { +#if HAVE_SALOME + // If the python script is being loaded now, the preferences should be used + // to display the required object + SUIT_Application * app = SUIT_Session::session()->activeApplication(); + QVariant aVar = app->property("IsLoadedScript"); + if (!aVar.isNull() && aVar.toBool()) { + DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); + int aSize = aRootDoc->size(ModelAPI_ResultPart::group()); + if (aSize > 0) { + ObjectPtr anPartObject = aRootDoc->object(ModelAPI_ResultPart::group(), aSize - 1); + ResultPartPtr aPart = std::dynamic_pointer_cast(anPartObject); + ModuleBase_Tools::setDisplaying(aPart, true); + } + } +#endif + } + } + + private: + ModuleBase_RedisplayListener() + { + Events_Loop::loop()->registerListener(this, + Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY)); + } + }; + + static std::shared_ptr + RL = ModuleBase_RedisplayListener::instance(); + //****************************************************************** void adjustMargins(QWidget* theWidget) @@ -1342,6 +1395,46 @@ qreal currentPixelRatio() } +// Set displaying status to every element on group +static void setDisplayingByLoop(DocumentPtr theDoc, int theSize, + std::string theGroup, bool theDisplayFromScript) +{ + int aDisplayingId = -1; + if (theDisplayFromScript) { + aDisplayingId = ModuleBase_Preferences::resourceMgr()->integerValue("General", + "part_visualization_script", -1); + // Increase ID to prevert using "As stored in HDF" + ++aDisplayingId; + } + else { + aDisplayingId = ModuleBase_Preferences::resourceMgr()->integerValue("General", + "part_visualization_study", -1); + + // if chosen "As stored in HDF" then don't change displaying + if (aDisplayingId == 0) + return; + } + + for (int anIndex = theSize - 1; anIndex >= 0; --anIndex) { + ObjectPtr anObject = theDoc->object(theGroup, anIndex); + anObject->setDisplayed((aDisplayingId == 1 && anIndex == theSize - 1) || aDisplayingId == 2); + } +} + +void setDisplaying(ResultPartPtr thePart, bool theDisplayFromScript) +{ + DocumentPtr aDoc = thePart->partDoc(); + int aConstructionSize = aDoc->size(ModelAPI_ResultConstruction::group()); + int aGroupSize = aDoc->size(ModelAPI_ResultGroup::group()); + int aFieldSize = aDoc->size(ModelAPI_ResultField::group()); + int aResultSize = aDoc->size(ModelAPI_ResultBody::group()); + setDisplayingByLoop(aDoc, aConstructionSize, + ModelAPI_ResultConstruction::group(), theDisplayFromScript); + setDisplayingByLoop(aDoc, aGroupSize, ModelAPI_ResultGroup::group(), theDisplayFromScript); + setDisplayingByLoop(aDoc, aFieldSize, ModelAPI_ResultField::group(), theDisplayFromScript); + setDisplayingByLoop(aDoc, aResultSize, ModelAPI_ResultBody::group(), theDisplayFromScript); +} + } // namespace ModuleBase_Tools diff --git a/src/ModuleBase/ModuleBase_Tools.h b/src/ModuleBase/ModuleBase_Tools.h index a68f2ee9e..cae27a1d9 100644 --- a/src/ModuleBase/ModuleBase_Tools.h +++ b/src/ModuleBase/ModuleBase_Tools.h @@ -406,6 +406,11 @@ std::wstring MODULEBASE_EXPORT generateName(const AttributePtr& theAttribute, /// Returns pixel ratio of a screen where main window is displayed qreal MODULEBASE_EXPORT currentPixelRatio(); + +/// Set displaying status for elements from part depending on the settings +/// \param thePart a pointer of part +void MODULEBASE_EXPORT setDisplaying(std::shared_ptr thePart, + bool theDisplayFromScript = false); } #endif diff --git a/src/PartSet/PartSet_MenuMgr.cpp b/src/PartSet/PartSet_MenuMgr.cpp index d9e7aa8be..b0cf3a6dc 100644 --- a/src/PartSet/PartSet_MenuMgr.cpp +++ b/src/PartSet/PartSet_MenuMgr.cpp @@ -501,7 +501,7 @@ void PartSet_MenuMgr::activatePart(ResultPartPtr thePart) const if (isFirstLoad) { XGUI_Workshop* aWorkshop = myModule->getWorkshop(); XGUI_ObjectsBrowser* aObjBrowser = aWorkshop->objectBrowser(); - XGUI_Tools::setDisplaying(thePart); + ModuleBase_Tools::setDisplaying(thePart); Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY)); aObjBrowser->onSelectionChanged(); DocumentPtr aDoc = thePart->partDoc(); diff --git a/src/XGUI/XGUI_Tools.cpp b/src/XGUI/XGUI_Tools.cpp index 3314814f1..6ff19daae 100644 --- a/src/XGUI/XGUI_Tools.cpp +++ b/src/XGUI/XGUI_Tools.cpp @@ -24,7 +24,6 @@ #include "ModuleBase_IWorkshop.h" #include "ModuleBase_Tools.h" -#include "ModuleBase_Preferences.h" #include #include @@ -37,13 +36,9 @@ #include #include #include -#include #include -#include #include -#include - #include #include @@ -406,44 +401,4 @@ void removeTemporaryFiles(const std::string& theDirectory, } } -// Set displaying status to every element on group -static void setDisplayingByLoop(DocumentPtr theDoc, int theSize, - std::string theGroup, bool theDisplayFromScript) -{ - int aDisplayingId = -1; - if (theDisplayFromScript) { - aDisplayingId = ModuleBase_Preferences::resourceMgr()->integerValue("General", - "part_visualization_script", -1); - // Increase ID to prevert using "As stored in HDF" - ++aDisplayingId; - } - else { - aDisplayingId = ModuleBase_Preferences::resourceMgr()->integerValue("General", - "part_visualization_study", -1); - - // if chosen "As stored in HDF" then don't change displaying - if (aDisplayingId == 0) - return; - } - - for (int anIndex = theSize - 1; anIndex >= 0; --anIndex) { - ObjectPtr anObject = theDoc->object(theGroup, anIndex); - anObject->setDisplayed((aDisplayingId == 1 && anIndex == theSize - 1) || aDisplayingId == 2); - } -} - -void setDisplaying(ResultPartPtr thePart, bool theDisplayFromScript) -{ - DocumentPtr aDoc = thePart->partDoc(); - int aConstructionSize = aDoc->size(ModelAPI_ResultConstruction::group()); - int aGroupSize = aDoc->size(ModelAPI_ResultGroup::group()); - int aFieldSize = aDoc->size(ModelAPI_ResultField::group()); - int aResultSize = aDoc->size(ModelAPI_ResultBody::group()); - setDisplayingByLoop(aDoc, aConstructionSize, - ModelAPI_ResultConstruction::group(), theDisplayFromScript); - setDisplayingByLoop(aDoc, aGroupSize, ModelAPI_ResultGroup::group(), theDisplayFromScript); - setDisplayingByLoop(aDoc, aFieldSize, ModelAPI_ResultField::group(), theDisplayFromScript); - setDisplayingByLoop(aDoc, aResultSize, ModelAPI_ResultBody::group(), theDisplayFromScript); -} - } diff --git a/src/XGUI/XGUI_Tools.h b/src/XGUI/XGUI_Tools.h index 65d0f424d..478857fb3 100644 --- a/src/XGUI/XGUI_Tools.h +++ b/src/XGUI/XGUI_Tools.h @@ -25,7 +25,6 @@ #include #include -#include #include #include @@ -136,10 +135,6 @@ std::string getTmpDirByEnv( const char* thePathEnv); /// Removes files and directory where they are located void removeTemporaryFiles(const std::string& theDirectory, const std::list& theFiles); - -/// Set displaying status for elements from part depending on the settings -/// \param thePart a pointer of part -XGUI_EXPORT void setDisplaying(ResultPartPtr thePart, bool theDisplayFromScript = false); }; #endif diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 0336029ba..f95053cbd 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -1076,7 +1076,7 @@ void XGUI_Workshop::openFile(const QString& theDirectory) ResultPartPtr aPart = std::dynamic_pointer_cast(anObject); if (aPart.get()) { aPart->activate(); - XGUI_Tools::setDisplaying(aPart); + ModuleBase_Tools::setDisplaying(aPart); } } else if (anActivationId == 1) { @@ -1085,7 +1085,7 @@ void XGUI_Workshop::openFile(const QString& theDirectory) ResultPartPtr aPart = std::dynamic_pointer_cast(anObject); if (aPart.get()) { aPart->activate(); - XGUI_Tools::setDisplaying(aPart); + ModuleBase_Tools::setDisplaying(aPart); if (anIndex < aSize - 1) { SessionPtr aMgr = ModelAPI_Session::get(); diff --git a/src/XGUI/XGUI_WorkshopListener.cpp b/src/XGUI/XGUI_WorkshopListener.cpp index 7dc112a93..34b87d99a 100644 --- a/src/XGUI/XGUI_WorkshopListener.cpp +++ b/src/XGUI/XGUI_WorkshopListener.cpp @@ -65,7 +65,6 @@ #include "XGUI_QtEvents.h" #include "XGUI_SalomeConnector.h" #include "XGUI_SelectionMgr.h" -#include "XGUI_Tools.h" #include "XGUI_Workshop.h" #include @@ -379,20 +378,6 @@ void XGUI_WorkshopListener:: } } - // If the python script is being loaded now, the preferences should be used - // to display the required object - SUIT_Application * app = SUIT_Session::session()->activeApplication(); - QVariant aVar = app->property("IsLoadedScript"); - if (!aVar.isNull() && aVar.toBool()) { - DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument(); - int aSize = aRootDoc->size(ModelAPI_ResultPart::group()); - if (aSize > 0) { - ObjectPtr anPartObject = aRootDoc->object(ModelAPI_ResultPart::group(), aSize - 1); - ResultPartPtr aPart = std::dynamic_pointer_cast(anPartObject); - XGUI_Tools::setDisplaying(aPart, true); - } - } - // this processing should be moved in another place in order to do not cause problems in // flush messages chain //if (aHiddenObjects.size() > 0) -- 2.39.2