Salome HOME
Change color action for a body object
authornds <natalia.donis@opencascade.com>
Fri, 6 Mar 2015 17:05:01 +0000 (20:05 +0300)
committernds <natalia.donis@opencascade.com>
Fri, 6 Mar 2015 17:05:01 +0000 (20:05 +0300)
src/ExchangePlugin/ExchangePlugin_Plugin.cpp
src/Model/CMakeLists.txt
src/Model/Model_AttributeColor.cpp [new file with mode: 0644]
src/Model/Model_AttributeColor.h [new file with mode: 0644]
src/Model/Model_Data.cpp
src/Model/Model_Document.cpp
src/Model/Model_ResultBody.cpp
src/ModelAPI/ModelAPI_AttributeColor.h
src/XGUI/XGUI_Workshop.cpp

index cf32016859c4088cd15224867e8eddfc2122478b..11034f4f6b11d0ac072b0cd98a068ab9d666c2d1 100644 (file)
@@ -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)
index 4865602acdf89002d21df797244afb7402ac605c..20c1c0b29733f0b0bddafad48ffc1ed7d61f2f86 100644 (file)
@@ -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 (file)
index 0000000..920e35a
--- /dev/null
@@ -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 <Model_AttributeColor.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Attribute.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_Object.h>
+
+#include <Standard_TypeDef.hxx>
+#include <TCollection_AsciiString.hxx>
+#include <TCollection_ExtendedString.hxx>
+#include <TDataStd_Integer.hxx>
+#include <TDataStd_Name.hxx>
+
+#include <string>
+
+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 (file)
index 0000000..7c5922d
--- /dev/null
@@ -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 <Model.h>
+#include <ModelAPI_AttributeColor.h>
+
+#include <TDF_Label.hxx>
+#include <TDataStd_Integer.hxx>
+
+#include <string>
+
+/**\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
index e13eaa406e5b6199c198ebf86dfb074140c5130a..4d0ed5d23bceac061d541d288830bf553acce213 100644 (file)
@@ -15,6 +15,7 @@
 #include <Model_AttributeString.h>
 #include <Model_AttributeSelection.h>
 #include <Model_AttributeSelectionList.h>
+#include <Model_AttributeColor.h>
 #include <Model_Events.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Result.h>
@@ -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()) {
index 32ae6102648ffdb3c727c403ae78acb019d19c2b..fb00ed5bf3f3c43e29ecf690de1d39317e8ca79b 100644 (file)
@@ -893,8 +893,8 @@ void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theT
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(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)
index e2459e7043b67020524056195fdbb2c3ec5b1f64..edee5b1495958450a9af23e078d881b36aafb4c3 100644 (file)
@@ -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<ModelAPI_AttributeColor>
-                                                    (aData->attribute(COLOR_ID()));
+                                                             (aData->attribute(COLOR_ID()));
   if (anIsRandomColor)
     aColorAttr->setValuesRandom();
   else {
index 945d9f868d2768f7ebff2152df3241aa4021b5c0..6d3986a25d0f9ff9cd2e54534dd9ae3857d33bc1 100644 (file)
@@ -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;
index 633b08dc2902516c45f1f4bc5a683b88b26a0864..e90037b3c10c19bba36750f06f8570e54e0f079b 100644 (file)
@@ -1406,26 +1406,33 @@ bool XGUI_Workshop::canChangeColor() const
 //**************************************************************
 #include <QDialog>
 #include <QHBoxLayout>
-#include <QLineEdit>
+#include <QtxColorButton.h>
+#include <ModelAPI_AttributeColor.h>
 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<ModelAPI_Result>(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<ModelAPI_AttributeColor>(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<ModelAPI_Result>(anObj);
+    if (aResult.get() != NULL) {
+      AttributePtr anAttr = aResult->data()->attribute(ModelAPI_Result::COLOR_ID());
+      if (anAttr.get() != NULL) {
+        aColorAttr = std::dynamic_pointer_cast<ModelAPI_AttributeColor>(anAttr);
+        if (aColorAttr.get() != NULL)
+          aColorAttr->setValues(aRedResult, aGreenResult, aBlueResult);
+      }
+    }
+  }
 }
 
 //**************************************************************