]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
authornds <natalia.donis@opencascade.com>
Thu, 26 Jun 2014 17:42:56 +0000 (21:42 +0400)
committernds <natalia.donis@opencascade.com>
Thu, 26 Jun 2014 17:42:56 +0000 (21:42 +0400)
Distance between line points.

src/Config/Config_Keywords.h
src/ModuleBase/CMakeLists.txt
src/ModuleBase/ModuleBase_WidgetFactory.cpp
src/ModuleBase/ModuleBase_WidgetFactory.h
src/ModuleBase/ModuleBase_WidgetFeature.h
src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.cpp [new file with mode: 0644]
src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.h [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
src/SketchPlugin/plugin-Sketch.xml

index 69c2eea29883793bd168ab568cb56f05d5c11fcb..37f9194de3619ecb0e1b67743bf037ffd0990d0c 100644 (file)
@@ -34,6 +34,7 @@ const static char* WDG_POINT_SELECTOR = "point_selector";
 const static char* WDG_POINT2D_DISTANCE = "point2ddistance";
 
 const static char* WDG_FEATURE_SELECTOR = "feature_selector";
+const static char* WDG_FEATURE_OR_ATTRIBUTE_SELECTOR = "feature_or_attribute_selector";
 const static char* WDG_DOUBLEVALUE_EDITOR = "doublevalue_editor";
 
 const static char* _ID = "id";
index 2bd3c36c971082bcd636f500dedcb7d58f8c8619..ae5cf2fd9b043fc0147a0cc1e92de3e8564326dc 100644 (file)
@@ -12,6 +12,7 @@ SET(PROJECT_HEADERS
        ModuleBase_WidgetEditor.h
        ModuleBase_WidgetFactory.h
        ModuleBase_WidgetFeature.h
+       ModuleBase_WidgetFeatureOrAttribute.h
        ModuleBase_WidgetPoint2D.h
        ModuleBase_WidgetSwitch.h
        ModuleBase_WidgetSelector.h
@@ -31,6 +32,7 @@ SET(PROJECT_SOURCES
        ModuleBase_WidgetEditor.cpp
        ModuleBase_WidgetFactory.cpp
        ModuleBase_WidgetFeature.cpp
+       ModuleBase_WidgetFeatureOrAttribute.cpp
        ModuleBase_WidgetPoint2D.cpp
        ModuleBase_WidgetSwitch.cpp
        ModuleBase_WidgetSelector.cpp
index eb132cb54c3985c991ad50ff400f86e316ba8f4b..edb98aee88c9646ff232cb2632d4247518205d9c 100644 (file)
@@ -10,6 +10,7 @@
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_OperationDescription.h>
 #include <ModuleBase_WidgetPoint2D.h>
+#include <ModuleBase_WidgetFeatureOrAttribute.h>
 #include <ModuleBase_WidgetFeature.h>
 #include <ModuleBase_WidgetEditor.h>
 #include <ModuleBase_WidgetSwitch.h>
@@ -131,6 +132,9 @@ QWidget* ModuleBase_WidgetFactory::createWidgetByType(const std::string& theType
   } else if (theType == WDG_FEATURE_SELECTOR) {
     result = featureSelectorControl(theParent);
 
+  } else if (theType == WDG_FEATURE_OR_ATTRIBUTE_SELECTOR) {
+    result = featureOrAttributeSelectorControl(theParent);
+
   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
     result = doubleValueEditor(theParent);
   
@@ -189,6 +193,14 @@ QWidget* ModuleBase_WidgetFactory::featureSelectorControl(QWidget* theParent)
   return aWidget->getControl();
 }
 
+QWidget* ModuleBase_WidgetFactory::featureOrAttributeSelectorControl(QWidget* theParent)
+{
+  ModuleBase_WidgetFeatureOrAttribute* aWidget = new ModuleBase_WidgetFeatureOrAttribute(theParent,
+                                                                                      myWidgetApi);
+  myModelWidgets.append(aWidget);
+  return aWidget->getControl();
+}
+
 QWidget* ModuleBase_WidgetFactory::doubleValueEditor(QWidget* theParent)
 {
   ModuleBase_WidgetEditor* aWidget = new ModuleBase_WidgetEditor(theParent, myWidgetApi);
index b29fba9eb6543470af9724e800c6dfb147410f09..dbbdcffd21ea63f40ba1c1c66abe30cf0727b66c 100644 (file)
@@ -39,6 +39,7 @@ protected:
   QWidget* doubleSpinBoxControl(QWidget* theParent);
   QWidget* pointSelectorControl(QWidget* theParent);
   QWidget* featureSelectorControl(QWidget* theParent);
+  QWidget* featureOrAttributeSelectorControl(QWidget* theParent);
   QWidget* doubleValueEditor(QWidget* theParent);
   QWidget* createContainer(const std::string& theType, QWidget* theParent = NULL);
   QWidget* selectorControl(QWidget* theParent);
index 3d6c60051f4de2290388ac7e6547a85309dbb619..329079118288b5cf9431cb45044a38d7a60891f4 100644 (file)
@@ -55,8 +55,21 @@ public:
 protected:
   /// Fill the widget values by given point
   /// \param thePoint the point
+  /// \return the boolean result of the feature set
   bool setFeature(const FeaturePtr& theFeature);
 
+  /// Returns current widget feature
+  /// \return the feature
+  const FeaturePtr& feature() const { return myFeature; }
+
+  /// Returns the widget editor
+  /// \return the editor
+  QLineEdit* editor() const { return myEditor; }
+
+  /// Returns the possible feature kinds
+  /// \return the list of kinds
+  const QStringList& featureKinds() const { return myFeatureKinds; }
+
 private:
   FeaturePtr myFeature; ///< the current widget feature
   QStringList myFeatureKinds; ///< the kinds of possible features
diff --git a/src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.cpp b/src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.cpp
new file mode 100644 (file)
index 0000000..9a47f92
--- /dev/null
@@ -0,0 +1,124 @@
+// File:        ModuleBase_WidgetFeatureOrAttribute.cpp
+// Created:     25 Apr 2014
+// Author:      Natalia ERMOLAEVA
+
+#include <ModuleBase_WidgetFeatureOrAttribute.h>
+
+#include <ModuleBase_WidgetValueFeature.h>
+#include <ModuleBase_WidgetValue.h>
+
+#include <Config_Keywords.h>
+#include <Config_WidgetAPI.h>
+
+#include <Events_Loop.h>
+#include <Model_Events.h>
+
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_Object.h>
+#include <ModelAPI_AttributeRefAttr.h>
+#include <GeomAPI_Pnt2d.h>
+
+#include <GeomDataAPI_Point2D.h>
+
+#include <Precision.hxx>
+
+#include <QWidget>
+#include <QLineEdit>
+#include <QHBoxLayout>
+#include <QLabel>
+
+ModuleBase_WidgetFeatureOrAttribute::ModuleBase_WidgetFeatureOrAttribute(QWidget* theParent,
+                                                   const Config_WidgetAPI* theData)
+: ModuleBase_WidgetFeature(theParent, theData)
+{
+}
+
+ModuleBase_WidgetFeatureOrAttribute::~ModuleBase_WidgetFeatureOrAttribute()
+{
+}
+
+bool ModuleBase_WidgetFeatureOrAttribute::setValue(ModuleBase_WidgetValue* theValue)
+{
+  bool isDone = false;
+
+  if (theValue) {
+    ModuleBase_WidgetValueFeature* aFeatureValue = 
+                         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
+    if (aFeatureValue) {
+      boost::shared_ptr<GeomAPI_Pnt2d> aValuePoint = aFeatureValue->point();
+      FeaturePtr aValueFeature = aFeatureValue->feature();
+
+      if (aValueFeature) {
+        isDone = setFeature(aValueFeature);
+      }
+      if (!isDone) {
+        // find the given point in the feature attributes
+        std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
+                                      aValueFeature->data()->attributes(GeomDataAPI_Point2D::type());
+        std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
+                                                                          aLast = anAttiributes.end();
+        boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
+        for (;anIt!=aLast && !aFPoint; anIt++) {
+          boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
+                                              boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+          if (aCurPoint && aCurPoint->pnt()->distance(aValuePoint) < Precision::Confusion())
+            aFPoint = aCurPoint;
+        }
+        if (aFPoint)
+          isDone = setAttribute(aFPoint);
+      }
+    }
+  }
+  return isDone;
+}
+
+bool ModuleBase_WidgetFeatureOrAttribute::storeValue(FeaturePtr theFeature) const
+{
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
+          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
+
+  ModuleBase_WidgetFeatureOrAttribute* that = (ModuleBase_WidgetFeatureOrAttribute*) this;
+  if (feature())
+    aRef->setFeature(feature());
+  else if (myAttribute)
+    aRef->setAttr(myAttribute);
+
+  theFeature->execute();
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
+
+  return true;
+}
+
+bool ModuleBase_WidgetFeatureOrAttribute::restoreValue(FeaturePtr theFeature)
+{
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
+          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(attributeID()));
+
+  FeaturePtr aFeature = aRef->feature();
+  setFeature(aFeature);
+  myAttribute = aRef->attr();
+
+  std::string aText = "";
+  if (aFeature)
+    aText = aFeature->data()->getName().c_str();
+  else if (myAttribute)
+    aText = myAttribute->attributeType().c_str();
+
+  editor()->setText(aText.c_str());
+  return true;
+}
+
+bool ModuleBase_WidgetFeatureOrAttribute::setAttribute(const boost::shared_ptr<ModelAPI_Attribute>& theAttribute)
+{
+  if (!theAttribute || !featureKinds().contains(theAttribute->attributeType().c_str()))
+    return false;
+
+  myAttribute = theAttribute;
+  editor()->setText(theAttribute ? theAttribute->attributeType().c_str() : "");
+  emit valuesChanged();
+  return true;
+}
+
diff --git a/src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.h b/src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.h
new file mode 100644 (file)
index 0000000..7084165
--- /dev/null
@@ -0,0 +1,53 @@
+// File:        ModuleBase_WidgetFeatureOrAttribute.h
+// Created:     25 Apr 2014
+// Author:      Natalia ERMOLAEVA
+
+#ifndef ModuleBase_WidgetFeatureOrAttribute_H
+#define ModuleBase_WidgetFeatureOrAttribute_H
+
+#include <ModuleBase.h>
+#include "ModuleBase_WidgetFeature.h"
+
+#include <QObject>
+
+class ModuleBase_WidgetValue;
+class ModelAPI_Attribute;
+
+/**\class ModuleBase_WidgetFeatureOrAttribute
+ * \ingroup GUI
+ * \brief Custom widget. An abstract class to be redefined to fill with some GUI controls
+ */
+class MODULEBASE_EXPORT ModuleBase_WidgetFeatureOrAttribute : public ModuleBase_WidgetFeature
+{
+  Q_OBJECT
+public:
+  /// Constructor
+  /// \theParent the parent object
+  /// \theParent the parent object
+  /// \theData the widget configuation. The attribute of the model widget is obtained from
+  ModuleBase_WidgetFeatureOrAttribute(QWidget* theParent, const Config_WidgetAPI* theData);
+  /// Destructor
+  virtual ~ModuleBase_WidgetFeatureOrAttribute();
+
+  /// Set the given wrapped value to the current widget
+  /// This value should be processed in the widget according to the needs
+  /// \param theValue the wrapped widget value
+  virtual bool setValue(ModuleBase_WidgetValue* theValue);
+
+  /// Saves the internal parameters to the given feature
+  /// \param theFeature a model feature to be changed
+  virtual bool storeValue(FeaturePtr theFeature) const;
+
+  virtual bool restoreValue(FeaturePtr theFeature);
+
+protected:
+  /// Set the attribute
+  /// \param theAttribute value
+  /// \return the boolean result of the attribute set
+  bool setAttribute(const boost::shared_ptr<ModelAPI_Attribute>& theAttribute);
+
+protected:
+  boost::shared_ptr<ModelAPI_Attribute> myAttribute; /// < the attribute
+};
+
+#endif
index ed7bc297418012c1e7476bbf8dcbc9da0be5b22b..0e8d30b6accfa5057a0347a7a0282ca73090d3dd 100644 (file)
@@ -38,28 +38,14 @@ void SketchPlugin_ConstraintDistance::execute()
 {
   boost::shared_ptr<ModelAPI_Data> aData = data();
 
-  boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_A = 
-          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
-  boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_B = 
-          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
-
-  AttributeDoublePtr anAttr_Value =
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
+  if (aPoint_A && aPoint_B) {
+    AttributeDoublePtr anAttr_Value =
       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
-  if (anAttr_A->isInitialized() && anAttr_B->isInitialized() && !anAttr_Value->isInitialized())
-  {
-    FeaturePtr aFeature_A = anAttr_A->feature();
-    FeaturePtr aFeature_B = anAttr_B->feature();
-    if (aFeature_A && aFeature_A) {
-      // calculate the distance
-      boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
-                                             (aFeature_A->data()->attribute(POINT_ATTR_COORD));
-      boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
-                                             (aFeature_B->data()->attribute(POINT_ATTR_COORD));
-      if (aPoint_A && aPoint_B) {
-        anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
-      }
+
+    if (!anAttr_Value->isInitialized()) {
+      anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
     }
   }
 }
@@ -165,6 +151,9 @@ boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
   if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
                                            (aFeature->data()->attribute(POINT_ATTR_COORD));
+  else {
+    if (anAttr->attr())
+      aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
+  }
   return aPointAttr;
 }
-
index 20c8e80d547ec5dcd2b0bc6b7ca1fca87b1d0c6a..6c320ee7195fbb7af6862a906400168469512672 100644 (file)
@@ -24,8 +24,8 @@
       <feature id="SketchConstraintCoincidence" title="Coincident" tooltip="Create constraint for the coincidence of two points" internal="1"/>
       <feature id="SketchConstraintDistance" title="Distance" tooltip="Create constraint for the distance from a point to an object">
         <label title="Select point and another feature (point or point on line) between which to calculate distance" tooltip="Select point and another feature (point or point on line) between which to calculate distance"/>
-        <feature_selector id="ConstraintEntityA" label="First point" tooltip="Select an point in the viewer" keysequence="SketchPoint"/>
-        <feature_selector id="ConstraintEntityB" label="Last point" tooltip="Select an point in the viewer" keysequence="SketchPoint"/>
+        <feature_or_attribute_selector id="ConstraintEntityA" label="First point" tooltip="Select an point in the viewer" keysequence="SketchPoint Point2D"/>
+        <feature_or_attribute_selector id="ConstraintEntityB" label="Last point" tooltip="Select an point in the viewer" keysequence="SketchPoint Point2D"/>
         <point_selector id="ConstraintFlyoutValuePnt" internal="1"/>
         <doublevalue_editor label="Value" tooltip="Constraint value" id="ConstraintValue"/>
       </feature>