1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: XGUI_CustomPrs.cpp
4 // Created: 10 Mar 2015
5 // Author: Natalia ERMOLAEVA
7 #include <XGUI_CustomPrs.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_Displayer.h>
11 #include <ModuleBase_IModule.h>
13 #include <ModelAPI_AttributeIntArray.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Session.h>
16 #include <Config_PropManager.h>
18 #include <Events_InfoMessage.h>
23 double getDeflection(const ResultPtr& theResult)
25 double aDeflection = -1;
26 // get color from the attribute of the result
27 if (theResult.get() != NULL &&
28 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
29 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
30 if (aDoubleAttr.get() && aDoubleAttr->isInitialized())
31 aDeflection = aDoubleAttr->value();
36 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
39 // get color from the attribute of the result
40 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
41 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
42 if (aColorAttr.get() && aColorAttr->size()) {
43 theColor.push_back(aColorAttr->value(0));
44 theColor.push_back(aColorAttr->value(1));
45 theColor.push_back(aColorAttr->value(2));
50 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
51 std::vector<int>& theColor)
54 // get default color from the preferences manager for the given result
55 if (theColor.empty()) {
56 std::string aSection, aName, aDefault;
57 theObject->colorConfigInfo(aSection, aName, aDefault);
58 if (!aSection.empty() && !aName.empty()) {
59 theColor = Config_PropManager::color(aSection, aName, aDefault);
62 if (!isEmptyColorValid && theColor.empty()) {
63 // all AIS objects, where the color is not set, are in black.
64 // The color should be defined in XML or set in the attribute
65 theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
66 Events_InfoMessage("XGUI_CustomPrs",
67 "A default color is not defined in the preferences for this kind of result").send();
71 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
73 return Config_PropManager::real("Visualization", "result_deflection", "0.001");
76 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
77 : myWorkshop(theWorkshop)
81 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
83 getColor(theResult, theColor);
85 getDefaultColor(theResult, false, theColor);
88 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
90 double aDeflection = getDeflection(theResult);
92 aDeflection = getDefaultDeflection(theResult);
96 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
97 std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
99 bool aCustomized = false;
100 if (theResult.get()) {
101 std::vector<int> aColor;
102 getResultColor(theResult, aColor);
104 SessionPtr aMgr = ModelAPI_Session::get();
105 if (aMgr->activeDocument() != theResult->document()) {
106 QColor aQColor(aColor[0], aColor[1], aColor[2]);
107 QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
108 aColor[0] = aNewColor.red();
109 aColor[1] = aNewColor.green();
110 aColor[2] = aNewColor.blue();
112 aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
114 aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
116 ModuleBase_IModule* aModule = myWorkshop->module();
117 aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;