From: mpv Date: Tue, 10 Mar 2015 08:51:47 +0000 (+0300) Subject: Implementation of color as integer array attribute X-Git-Tag: V_1.1.0~143^2~4^2~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8584051f701aa3428620ebb98ecc03461cc23824;p=modules%2Fshaper.git Implementation of color as integer array attribute --- diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 20c1c0b29..ef4c82c12 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -14,7 +14,7 @@ SET(PROJECT_HEADERS Model_AttributeRefAttr.h Model_AttributeRefList.h Model_AttributeBoolean.h - Model_AttributeColor.h + Model_AttributeIntArray.h Model_AttributeString.h Model_AttributeInteger.h Model_AttributeSelection.h @@ -40,7 +40,7 @@ SET(PROJECT_SOURCES Model_AttributeRefAttr.cpp Model_AttributeRefList.cpp Model_AttributeBoolean.cpp - Model_AttributeColor.cpp + Model_AttributeIntArray.cpp Model_AttributeString.cpp Model_AttributeInteger.cpp Model_AttributeSelection.cpp diff --git a/src/Model/Model_AttributeColor.cpp b/src/Model/Model_AttributeColor.cpp deleted file mode 100644 index 920e35a7a..000000000 --- a/src/Model/Model_AttributeColor.cpp +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: Model_AttributeColor.cpp -// Created: 6 Mar 2015 -// Author: Natalia ERMOLAEVA - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -void Model_AttributeColor::setValues(const int theRed, - const int theGreen, - const int theBlue) -{ - if (!myIsInitialized || myRed->Get() != theRed || - myGreen->Get() != theGreen || myBlue->Get() != theBlue) { - myRed->Set(theRed); - myGreen->Set(theGreen); - myBlue->Set(theBlue); - - owner()->data()->sendAttributeUpdated(this); - } -} - -void Model_AttributeColor::setValuesRandom() -{ - setValues(300, 150, 40); -} - -void Model_AttributeColor::values(int& theRed, int& theGreen, int& theBlue) -{ - theRed = myRed->Get(); - theGreen = myGreen->Get(); - theBlue = myBlue->Get(); -} - -Model_AttributeColor::Model_AttributeColor(TDF_Label& theLabel) -{ - // check the attribute could be already presented in this doc (after load document) - myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), myRed) == Standard_True; - if (!myIsInitialized) { - // create attribute: not initialized by value yet, just zero - myRed = TDataStd_Integer::Set(theLabel, 0); - myGreen = TDataStd_Integer::Set(theLabel, 0); - myBlue = TDataStd_Integer::Set(theLabel, 0); - } -} diff --git a/src/Model/Model_AttributeColor.h b/src/Model/Model_AttributeColor.h deleted file mode 100644 index 7c5922d16..000000000 --- a/src/Model/Model_AttributeColor.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: Model_AttributeColor.h -// Created: 6 Mar 2015 -// Author: Natalia ERMOLAEVA - -#ifndef MODEL_ATTRIBUTECOLOR_H_ -#define MODEL_ATTRIBUTECOLOR_H_ - -#include -#include - -#include -#include - -#include - -/**\class Model_AttributeColor - * \ingroup DataModel - * \brief Attribute that contains three integer values which define the color. - */ - -class Model_AttributeColor : public ModelAPI_AttributeColor -{ - Handle_TDataStd_Integer myRed; - Handle_TDataStd_Integer myGreen; - Handle_TDataStd_Integer myBlue; - public: - /// Defines the color value - /// \param theRed the red part of the color - /// \param theRed the green part of the color - /// \param theRed the blue part of the color - MODELAPI_EXPORT virtual void setValues(const int theRed, - const int theGreen, - const int theBlue); - - /// Fills the attribute values by a random color - MODELAPI_EXPORT virtual void setValuesRandom(); - - /// Returns the color value - MODELAPI_EXPORT virtual void values(int& theRed, int& theGreen, int& theBlue); - - protected: - /// Initializes attibutes - Model_AttributeColor(TDF_Label& theLabel); - - friend class Model_Data; -}; - -#endif diff --git a/src/Model/Model_AttributeIntArray.cpp b/src/Model/Model_AttributeIntArray.cpp new file mode 100644 index 000000000..8f21a427d --- /dev/null +++ b/src/Model/Model_AttributeIntArray.cpp @@ -0,0 +1,70 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: Model_AttributeIntArray.cpp +// Created: 6 Mar 2015 +// Author: Natalia ERMOLAEVA + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +int Model_AttributeIntArray::size() +{ + return myArray.IsNull() ? 0 : myArray->Length(); +} + +void Model_AttributeIntArray::setSize(const int theSize) +{ + if (myArray.IsNull()) { // create array if it is not done yet + if (theSize != 0) { // if size is zero, nothing to do (null array means there is no array) + myArray = TDataStd_IntegerArray::Set(myLab, 0, theSize); + owner()->data()->sendAttributeUpdated(this); + } + } else { // reset the old array + if (theSize) { + if (theSize != myArray->Length()) { // old data is not keept, a new array is created + Handle(TColStd_HArray1OfInteger) aNewArray = new TColStd_HArray1OfInteger(0, theSize - 1); + myArray->ChangeArray(aNewArray); + owner()->data()->sendAttributeUpdated(this); + } + } else { // size is zero => array must be erased + if (!myArray.IsNull()) { + myArray.Nullify(); + myLab.ForgetAttribute(TDataStd_IntegerArray::GetID()); + owner()->data()->sendAttributeUpdated(this); + } + } + } +} + +void Model_AttributeIntArray::setValue(const int theIndex, + const int theValue) +{ + if (myArray->Value(theIndex) != theValue) { + myArray->SetValue(theIndex, theValue); + owner()->data()->sendAttributeUpdated(this); + } +} + +int Model_AttributeIntArray::value(const int theIndex) +{ + return myArray->Value(theIndex); +} + +Model_AttributeIntArray::Model_AttributeIntArray(TDF_Label& theLabel) +{ + myLab = theLabel; + // check the attribute could be already presented in this doc (after load document) + myIsInitialized = + myLab.FindAttribute(TDataStd_IntegerArray::GetID(), myArray) == Standard_True; +} diff --git a/src/Model/Model_AttributeIntArray.h b/src/Model/Model_AttributeIntArray.h new file mode 100644 index 000000000..82808f537 --- /dev/null +++ b/src/Model/Model_AttributeIntArray.h @@ -0,0 +1,54 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: Model_AttributeIntArray.h +// Created: 6 Mar 2015 +// Author: Natalia ERMOLAEVA + +#ifndef MODEL_ATTRIBUTEINTARRAY_H_ +#define MODEL_ATTRIBUTEINTARRAY_H_ + +#include +#include + +#include +#include + +#include + +/**\class Model_AttributeIntArray + * \ingroup DataModel + * \brief API for the attribute that contains several integers in the array inside. + * Used for RGB color storage for an example. By default size is one, zero-based. + * Represented as array in OCCT. Empty array means that this attribute does not exists. + */ + +class Model_AttributeIntArray : public ModelAPI_AttributeIntArray +{ + /// The OCCT array that keeps all values. + Handle_TDataStd_IntegerArray myArray; + /// Stores the label as a field to set array if size is not null (null array is buggy in OCAF) + TDF_Label myLab; + public: + + /// Returns the size of the array (zero means that it is empty) + MODEL_EXPORT virtual int size(); + + /// Sets the new size of the array. The previous data is erased. + MODEL_EXPORT virtual void setSize(const int theSize); + + /// Defines the value of the array by index [0; size-1] + MODEL_EXPORT virtual void setValue(const int theIndex, + const int theValue); + + /// Returns the value by the index + MODEL_EXPORT virtual int value(const int theIndex); + + + protected: + /// Initializes attibutes + Model_AttributeIntArray(TDF_Label& theLabel); + + friend class Model_Data; +}; + +#endif diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 4d0ed5d23..b8aeabaff 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -91,8 +91,8 @@ void Model_Data::addAttribute(const std::string& theID, const std::string theAtt anAttr = new Model_AttributeRefAttr(anAttrLab); } else if (theAttrType == ModelAPI_AttributeRefList::type()) { anAttr = new Model_AttributeRefList(anAttrLab); - } else if (theAttrType == ModelAPI_AttributeColor::type()) { - anAttr = new Model_AttributeColor(anAttrLab); + } else if (theAttrType == ModelAPI_AttributeIntArray::type()) { + anAttr = new Model_AttributeIntArray(anAttrLab); } // create also GeomData attributes here because only here the OCAF strucure is known else if (theAttrType == GeomData_Point::type()) { @@ -133,6 +133,7 @@ GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection); GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList); GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr); GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist); +GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray); std::shared_ptr Model_Data::attribute(const std::string& theID) { diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index 6604e86e8..1787b2a9e 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -100,6 +101,10 @@ class Model_Data : public ModelAPI_Data /// Returns the attribute that contains real value with double precision MODEL_EXPORT virtual std::shared_ptr string(const std::string& theID); + /// Returns the attribute that contains integer values array + MODEL_EXPORT virtual std::shared_ptr + intArray(const std::string& theID); + /// Returns the generic attribute by identifier /// \param theID identifier of the attribute MODEL_EXPORT virtual std::shared_ptr attribute(const std::string& theID); diff --git a/src/Model/Model_ResultBody.cpp b/src/Model/Model_ResultBody.cpp index 6db5c9430..7b7e3c3be 100644 --- a/src/Model/Model_ResultBody.cpp +++ b/src/Model/Model_ResultBody.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -48,19 +48,14 @@ void Model_ResultBody::initAttributes() { // append the color attribute DataPtr aData = data(); - aData->addAttribute(COLOR_ID(), ModelAPI_AttributeColor::type()); - // set the default value - bool anIsRandomColor = Config_PropManager::boolean("Visualization", "random_result_color", - "true"); - AttributeColorPtr aColorAttr = std::dynamic_pointer_cast - (aData->attribute(COLOR_ID())); - if (anIsRandomColor) - aColorAttr->setValuesRandom(); - else { - std::vector aRGB; - aRGB = Config_PropManager::color("Visualization", "result_body_color", RESULT_BODY_COLOR); - aColorAttr->setValues(aRGB[0], aRGB[1], aRGB[2]); - } + aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::type()); + AttributeIntArrayPtr aColorAttr = aData->intArray(COLOR_ID()); + std::vector aRGB; + aRGB = Config_PropManager::color("Visualization", "result_body_color", RESULT_BODY_COLOR); + aColorAttr->setSize(3); + aColorAttr->setValue(0, aRGB[0]); + aColorAttr->setValue(1, aRGB[1]); + aColorAttr->setValue(2, aRGB[2]); } void Model_ResultBody::store(const std::shared_ptr& theShape) diff --git a/src/ModelAPI/CMakeLists.txt b/src/ModelAPI/CMakeLists.txt index 497fe5365..44602ce1c 100644 --- a/src/ModelAPI/CMakeLists.txt +++ b/src/ModelAPI/CMakeLists.txt @@ -9,7 +9,7 @@ SET(PROJECT_HEADERS ModelAPI.h ModelAPI_Attribute.h ModelAPI_AttributeBoolean.h - ModelAPI_AttributeColor.h + ModelAPI_AttributeIntArray.h ModelAPI_AttributeDocRef.h ModelAPI_AttributeDouble.h ModelAPI_AttributeInteger.h @@ -45,7 +45,7 @@ SET(PROJECT_HEADERS SET(PROJECT_SOURCES ModelAPI_Attribute.cpp ModelAPI_AttributeBoolean.cpp - ModelAPI_AttributeColor.cpp + ModelAPI_AttributeIntArray.cpp ModelAPI_AttributeDocRef.cpp ModelAPI_AttributeDouble.cpp ModelAPI_AttributeInteger.cpp diff --git a/src/ModelAPI/ModelAPI_AttributeColor.cpp b/src/ModelAPI/ModelAPI_AttributeColor.cpp deleted file mode 100644 index 1909fc325..000000000 --- a/src/ModelAPI/ModelAPI_AttributeColor.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: ModelAPI_AttributeColor.cpp -// Created: 6 Mar 2015 -// Author: Natalia ERMOLAEVA - -#include - - -std::string ModelAPI_AttributeColor::attributeType() -{ - return type(); -} - -/// To virtually destroy the fields of successors -ModelAPI_AttributeColor::~ModelAPI_AttributeColor() -{ -} - -ModelAPI_AttributeColor::ModelAPI_AttributeColor() -{ -} diff --git a/src/ModelAPI/ModelAPI_AttributeColor.h b/src/ModelAPI/ModelAPI_AttributeColor.h deleted file mode 100644 index 6d3986a25..000000000 --- a/src/ModelAPI/ModelAPI_AttributeColor.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: ModelAPI_AttributeColor.h -// Created: 6 Mar 2015 -// Author: Natalia ERMOLAEVA - -#ifndef ModelAPI_AttributeColor_H_ -#define ModelAPI_AttributeColor_H_ - -#include -#include - -#include - - -/**\class ModelAPI_AttributeColor - * \ingroup DataModel - * \brief API for the attribute that contains color (int, int, int). The color is in the range [0, 255] - * There is an opportunity to fill the attribute by a random color - */ - -class ModelAPI_AttributeColor : public ModelAPI_Attribute -{ - public: - /// Defines the color value - /// \param theRed the red part of the color - /// \param theRed the green part of the color - /// \param theRed the blue part of the color - MODELAPI_EXPORT virtual void setValues(const int theRed, - const int theGreen, - const int theBlue) = 0; - - /// Fills the attribute values by a random color - MODELAPI_EXPORT virtual void setValuesRandom() = 0; - - /// Returns the color value - MODELAPI_EXPORT virtual void values(int& theRed, int& theGreen, int& theBlue) = 0; - - /// Returns the type of this class of attributes - MODELAPI_EXPORT static std::string type() - { - return "Color"; - } - - /// Returns the type of this class of attributes, not static method - MODELAPI_EXPORT virtual std::string attributeType(); - - /// To virtually destroy the fields of successors - MODELAPI_EXPORT virtual ~ModelAPI_AttributeColor(); - - protected: - /// Objects are created for features automatically - MODELAPI_EXPORT ModelAPI_AttributeColor(); -}; - -//! Pointer on double attribute -typedef std::shared_ptr AttributeColorPtr; - -#endif diff --git a/src/ModelAPI/ModelAPI_AttributeIntArray.cpp b/src/ModelAPI/ModelAPI_AttributeIntArray.cpp new file mode 100644 index 000000000..0e0304c46 --- /dev/null +++ b/src/ModelAPI/ModelAPI_AttributeIntArray.cpp @@ -0,0 +1,22 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModelAPI_AttributeIntArray.cpp +// Created: 6 Mar 2015 +// Author: Natalia ERMOLAEVA + +#include + + +std::string ModelAPI_AttributeIntArray::attributeType() +{ + return type(); +} + +/// To virtually destroy the fields of successors +ModelAPI_AttributeIntArray::~ModelAPI_AttributeIntArray() +{ +} + +ModelAPI_AttributeIntArray::ModelAPI_AttributeIntArray() +{ +} diff --git a/src/ModelAPI/ModelAPI_AttributeIntArray.h b/src/ModelAPI/ModelAPI_AttributeIntArray.h new file mode 100644 index 000000000..177db0422 --- /dev/null +++ b/src/ModelAPI/ModelAPI_AttributeIntArray.h @@ -0,0 +1,59 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModelAPI_AttributeIntArray.h +// Created: 6 Mar 2015 +// Author: Natalia ERMOLAEVA + +#ifndef ModelAPI_AttributeIntArray_H_ +#define ModelAPI_AttributeIntArray_H_ + +#include +#include + +#include + + +/**\class ModelAPI_AttributeIntArray + * \ingroup DataModel + * \brief API for the attribute that contains several integers in the array inside. + * Used for RGB color storage for an example. By default size is one, zero-based. + */ + +class ModelAPI_AttributeIntArray : public ModelAPI_Attribute +{ + public: + + /// Returns the size of the array (zero means that it is empty) + MODELAPI_EXPORT virtual int size() = 0; + + /// Sets the new size of the array. The previous data is erased. + MODELAPI_EXPORT virtual void setSize(const int theSize) = 0; + + /// Defines the value of the array by index [0; size-1] + MODELAPI_EXPORT virtual void setValue(const int theIndex, + const int theValue) = 0; + + /// Returns the value by the index + MODELAPI_EXPORT virtual int value(const int theIndex) = 0; + + /// Returns the type of this class of attributes + MODELAPI_EXPORT static std::string type() + { + return "IntArray"; + } + + /// Returns the type of this class of attributes, not static method + MODELAPI_EXPORT virtual std::string attributeType(); + + /// To virtually destroy the fields of successors + MODELAPI_EXPORT virtual ~ModelAPI_AttributeIntArray(); + + protected: + /// Objects are created for features automatically + MODELAPI_EXPORT ModelAPI_AttributeIntArray(); +}; + +//! Pointer on double attribute +typedef std::shared_ptr AttributeIntArrayPtr; + +#endif diff --git a/src/ModelAPI/ModelAPI_Data.h b/src/ModelAPI/ModelAPI_Data.h index bb532ce08..2d6baddb0 100644 --- a/src/ModelAPI/ModelAPI_Data.h +++ b/src/ModelAPI/ModelAPI_Data.h @@ -26,6 +26,7 @@ class ModelAPI_Attribute; class ModelAPI_Feature; class ModelAPI_AttributeSelection; class ModelAPI_AttributeSelectionList; +class ModelAPI_AttributeIntArray; class ModelAPI_Object; class GeomAPI_Shape; @@ -75,6 +76,8 @@ class MODELAPI_EXPORT ModelAPI_Data virtual std::shared_ptr boolean(const std::string& theID) = 0; /// Returns the attribute that contains boolean value virtual std::shared_ptr string(const std::string& theID) = 0; + /// Returns the attribute that contains integer values array + virtual std::shared_ptr intArray(const std::string& theID) = 0; /// Returns the generic attribute by identifier /// \param theID identifier of the attribute diff --git a/src/XGUI/XGUI_Displayer.cpp b/src/XGUI/XGUI_Displayer.cpp index 88e8d599c..0db30a145 100644 --- a/src/XGUI/XGUI_Displayer.cpp +++ b/src/XGUI/XGUI_Displayer.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include @@ -764,12 +764,13 @@ void XGUI_Displayer::customizeObject(ObjectPtr theObject) // correct the result's color it it has the attribute ResultPtr aResult = std::dynamic_pointer_cast(theObject); if (aResult.get() != NULL && aResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) { - int aRed, aGreen, aBlue; - - AttributeColorPtr aColorAttr = std::dynamic_pointer_cast( - aResult->data()->attribute(ModelAPI_Result::COLOR_ID())); - aColorAttr->values(aRed, aGreen, aBlue); - anAISObj->setColor(aRed, aGreen, aBlue); + AttributeIntArrayPtr aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID()); + if (aColorAttr.get() && aColorAttr->size()) { + int aRed = aColorAttr->value(0); + int aGreen = aColorAttr->value(1); + int aBlue = aColorAttr->value(2); + anAISObj->setColor(aRed, aGreen, aBlue); + } } else { // Customization of presentation diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 70b6c6156..a1a872754 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -1407,24 +1407,23 @@ bool XGUI_Workshop::canChangeColor() const #include #include #include -#include +#include void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects) { // 1. find the initial value of the color - AttributeColorPtr aColorAttr; + AttributeIntArrayPtr aColorAttr; foreach(ObjectPtr anObj, theObjects) { ResultPtr aResult = std::dynamic_pointer_cast(anObj); if (aResult.get() != NULL) { - AttributePtr anAttr = aResult->data()->attribute(ModelAPI_Result::COLOR_ID()); - if (anAttr.get() != NULL) - aColorAttr = std::dynamic_pointer_cast(anAttr); + aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID()); } } // there is no object with the color attribute - if (aColorAttr.get() == NULL) + if (aColorAttr.get() == NULL || aColorAttr->size() == 0) return; - int aRed, aGreen, aBlue; - aColorAttr->values(aRed, aGreen, aBlue); + int aRed = aColorAttr->value(0); + int aGreen = aColorAttr->value(1); + int aBlue = aColorAttr->value(2); // 2. show the dialog to change the value QDialog aDlg; @@ -1457,11 +1456,14 @@ void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects) foreach(ObjectPtr anObj, theObjects) { ResultPtr aResult = std::dynamic_pointer_cast(anObj); if (aResult.get() != NULL) { - AttributePtr anAttr = aResult->data()->attribute(ModelAPI_Result::COLOR_ID()); - if (anAttr.get() != NULL) { - aColorAttr = std::dynamic_pointer_cast(anAttr); - if (aColorAttr.get() != NULL) - aColorAttr->setValues(aRedResult, aGreenResult, aBlueResult); + aColorAttr = aResult->data()->intArray(ModelAPI_Result::COLOR_ID()); + if (aColorAttr.get() != NULL) { + if (!aColorAttr->size()) { + aColorAttr->setSize(3); + } + aColorAttr->setValue(0, aRedResult); + aColorAttr->setValue(1, aGreenResult); + aColorAttr->setValue(2, aBlueResult); } } }