From 02bde297c205d1f7dd3f4b4c91bd5d97b5a7d791 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 6 Mar 2015 20:05:01 +0300 Subject: [PATCH] Change color action for a body object --- src/ExchangePlugin/ExchangePlugin_Plugin.cpp | 4 ++ src/Model/CMakeLists.txt | 2 + src/Model/Model_AttributeColor.cpp | 57 ++++++++++++++++++++ src/Model/Model_AttributeColor.h | 50 +++++++++++++++++ src/Model/Model_Data.cpp | 3 ++ src/Model/Model_Document.cpp | 2 +- src/Model/Model_ResultBody.cpp | 4 +- src/ModelAPI/ModelAPI_AttributeColor.h | 5 +- src/XGUI/XGUI_Workshop.cpp | 50 ++++++++++++----- 9 files changed, 161 insertions(+), 16 deletions(-) create mode 100644 src/Model/Model_AttributeColor.cpp create mode 100644 src/Model/Model_AttributeColor.h diff --git a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp index cf3201685..11034f4f6 100644 --- a/src/ExchangePlugin/ExchangePlugin_Plugin.cpp +++ b/src/ExchangePlugin/ExchangePlugin_Plugin.cpp @@ -32,6 +32,10 @@ ExchangePlugin_Plugin::ExchangePlugin_Plugin() // register construction properties Config_PropManager::registerProp("Visualization", "import_feature_color", "Imported feature color", Config_Prop::Color, IMPORTED_FEATURE_COLOR); + + // register random result color properties + Config_PropManager::registerProp("Visualization", "random_result_color", "Use random color for results", + Config_Prop::Bool, "false"); } FeaturePtr ExchangePlugin_Plugin::createFeature(string theFeatureID) diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 4865602ac..20c1c0b29 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -14,6 +14,7 @@ SET(PROJECT_HEADERS Model_AttributeRefAttr.h Model_AttributeRefList.h Model_AttributeBoolean.h + Model_AttributeColor.h Model_AttributeString.h Model_AttributeInteger.h Model_AttributeSelection.h @@ -39,6 +40,7 @@ SET(PROJECT_SOURCES Model_AttributeRefAttr.cpp Model_AttributeRefList.cpp Model_AttributeBoolean.cpp + Model_AttributeColor.cpp Model_AttributeString.cpp Model_AttributeInteger.cpp Model_AttributeSelection.cpp diff --git a/src/Model/Model_AttributeColor.cpp b/src/Model/Model_AttributeColor.cpp new file mode 100644 index 000000000..920e35a7a --- /dev/null +++ b/src/Model/Model_AttributeColor.cpp @@ -0,0 +1,57 @@ +// 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 new file mode 100644 index 000000000..7c5922d16 --- /dev/null +++ b/src/Model/Model_AttributeColor.h @@ -0,0 +1,50 @@ +// 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_Data.cpp b/src/Model/Model_Data.cpp index e13eaa406..4d0ed5d23 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -90,6 +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); } // create also GeomData attributes here because only here the OCAF strucure is known else if (theAttrType == GeomData_Point::type()) { diff --git a/src/Model/Model_Document.cpp b/src/Model/Model_Document.cpp index 32ae61026..fb00ed5bf 100644 --- a/src/Model/Model_Document.cpp +++ b/src/Model/Model_Document.cpp @@ -893,8 +893,8 @@ void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theT FeaturePtr aFeature = std::dynamic_pointer_cast(theObj); if (aFeature) { setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name - aFeature->initAttributes(); } + theObj->initAttributes(); } void Model_Document::synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences) diff --git a/src/Model/Model_ResultBody.cpp b/src/Model/Model_ResultBody.cpp index e2459e704..edee5b149 100644 --- a/src/Model/Model_ResultBody.cpp +++ b/src/Model/Model_ResultBody.cpp @@ -51,9 +51,9 @@ void Model_ResultBody::initAttributes() aData->addAttribute(COLOR_ID(), ModelAPI_AttributeColor::type()); // set the default value bool anIsRandomColor = Config_PropManager::boolean("Visualization", "random_result_color", - false); + "false"); AttributeColorPtr aColorAttr = std::dynamic_pointer_cast - (aData->attribute(COLOR_ID())); + (aData->attribute(COLOR_ID())); if (anIsRandomColor) aColorAttr->setValuesRandom(); else { diff --git a/src/ModelAPI/ModelAPI_AttributeColor.h b/src/ModelAPI/ModelAPI_AttributeColor.h index 945d9f868..6d3986a25 100644 --- a/src/ModelAPI/ModelAPI_AttributeColor.h +++ b/src/ModelAPI/ModelAPI_AttributeColor.h @@ -15,7 +15,7 @@ /**\class ModelAPI_AttributeColor * \ingroup DataModel - * \brief API for the attribute that contains color (int, int, int). + * \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 */ @@ -23,6 +23,9 @@ 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; diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 633b08dc2..e90037b3c 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -1406,26 +1406,33 @@ bool XGUI_Workshop::canChangeColor() const //************************************************************** #include #include -#include +#include +#include void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects) { - // 1. find the initial value of the material - std::string aFirstValue = ""; - foreach(ObjectPtr anObj, theObjects) - { + // 1. find the initial value of the color + AttributeColorPtr aColorAttr; + foreach(ObjectPtr anObj, theObjects) { ResultPtr aResult = std::dynamic_pointer_cast(anObj); - if (aResult.get() == NULL) - continue; + if (aResult.get() != NULL) { + AttributePtr anAttr = aResult->data()->attribute(ModelAPI_Result::COLOR_ID()); + if (anAttr.get() != NULL) + aColorAttr = std::dynamic_pointer_cast(anAttr); + } } + // there is no object with the color attribute + if (aColorAttr.get() == NULL) + return; + int aRed, aGreen, aBlue; + aColorAttr->values(aRed, aGreen, aBlue); // 2. show the dialog to change the value QDialog aDlg; QHBoxLayout* aLay = new QHBoxLayout(&aDlg); - QLineEdit* anEditor = new QLineEdit("QString::number(theValue)", &aDlg); - anEditor->setText(aFirstValue.c_str()); - anEditor->selectAll(); - aLay->addWidget(anEditor); + QtxColorButton* aColorBtn = new QtxColorButton(&aDlg); + aLay->addWidget(aColorBtn); + aColorBtn->setColor(QColor(aRed, aGreen, aBlue)); QPoint aPoint = QCursor::pos(); aDlg.move(aPoint); @@ -1434,11 +1441,30 @@ void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects) if (!isDone) return; - std::string aValue = anEditor->text().toStdString(); + QColor aColorResult = aColorBtn->color(); + int aRedResult = aColorResult.red(), + aGreenResult = aColorResult.green(), + aBlueResult = aColorResult.blue(); + + if (aRedResult == aRed && aGreenResult == aGreen && aBlueResult == aBlue) + return; // 3. abort the previous operation and start a new one + if(!isActiveOperationAborted()) + return; // 4. set the value to all results + 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); + } + } + } } //************************************************************** -- 2.30.2