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_Session.h>
15 #include <Config_PropManager.h>
17 #include <Events_Error.h>
23 void getColor(ResultPtr theResult, std::vector<int>& theColor)
26 // get color from the attribute of the result
27 if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
28 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
29 if (aColorAttr.get() && aColorAttr->size()) {
30 theColor.push_back(aColorAttr->value(0));
31 theColor.push_back(aColorAttr->value(1));
32 theColor.push_back(aColorAttr->value(2));
37 void getDefaultColor(ObjectPtr theObject, std::vector<int>& theColor, const bool isEmptyColorValid)
40 // get default color from the preferences manager for the given result
41 if (theColor.empty()) {
42 std::string aSection, aName, aDefault;
43 theObject->colorConfigInfo(aSection, aName, aDefault);
44 if (!aSection.empty() && !aName.empty()) {
45 theColor = Config_PropManager::color(aSection, aName, aDefault);
48 if (!isEmptyColorValid && theColor.empty()) {
49 // all AIS objects, where the color is not set, are in black.
50 // The color should be defined in XML or set in the attribute
51 theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
52 Events_Error::send("A default color is not defined in the preferences for this kind of result");
56 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
57 : myWorkshop(theWorkshop)
61 void XGUI_CustomPrs::getResultColor(ResultPtr theResult, std::vector<int>& theColor)
63 getColor(theResult, theColor);
65 getDefaultColor(theResult, theColor, false);
68 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
69 std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
71 bool aCustomized = false;
72 if (theResult.get()) {
73 std::vector<int> aColor;
74 getResultColor(theResult, aColor);
76 SessionPtr aMgr = ModelAPI_Session::get();
77 if (aMgr->activeDocument() != theResult->document()) {
78 QColor aQColor(aColor[0], aColor[1], aColor[2]);
79 QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
80 aColor[0] = aNewColor.red();
81 aColor[1] = aNewColor.green();
82 aColor[2] = aNewColor.blue();
84 aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
87 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
88 ObjectPtr anObject = aDisplayer->getObject(thePrs);
90 std::vector<int> aColor;
91 ModuleBase_IModule* aModule = myWorkshop->module();
92 aModule->getColor(anObject, aColor);
94 getDefaultColor(anObject, aColor, true);
96 thePrs->setColor(aColor[0], aColor[1], aColor[2]);