From: nds Date: Thu, 26 Jun 2014 17:42:56 +0000 (+0400) Subject: refs #80 - Sketch base GUI: create/draw point, circle and arc X-Git-Tag: V_0.4.4~220 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2a694eefa196c05f47e7eeba6191f313be198f25;p=modules%2Fshaper.git refs #80 - Sketch base GUI: create/draw point, circle and arc Distance between line points. --- diff --git a/src/Config/Config_Keywords.h b/src/Config/Config_Keywords.h index 69c2eea29..37f9194de 100644 --- a/src/Config/Config_Keywords.h +++ b/src/Config/Config_Keywords.h @@ -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"; diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index 2bd3c36c9..ae5cf2fd9 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -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 diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.cpp b/src/ModuleBase/ModuleBase_WidgetFactory.cpp index eb132cb54..edb98aee8 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFactory.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -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); diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.h b/src/ModuleBase/ModuleBase_WidgetFactory.h index b29fba9eb..dbbdcffd2 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.h +++ b/src/ModuleBase/ModuleBase_WidgetFactory.h @@ -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); diff --git a/src/ModuleBase/ModuleBase_WidgetFeature.h b/src/ModuleBase/ModuleBase_WidgetFeature.h index 3d6c60051..329079118 100644 --- a/src/ModuleBase/ModuleBase_WidgetFeature.h +++ b/src/ModuleBase/ModuleBase_WidgetFeature.h @@ -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 index 000000000..9a47f9231 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.cpp @@ -0,0 +1,124 @@ +// File: ModuleBase_WidgetFeatureOrAttribute.cpp +// Created: 25 Apr 2014 +// Author: Natalia ERMOLAEVA + +#include + +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include + +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(theValue); + if (aFeatureValue) { + boost::shared_ptr aValuePoint = aFeatureValue->point(); + FeaturePtr aValueFeature = aFeatureValue->feature(); + + if (aValueFeature) { + isDone = setFeature(aValueFeature); + } + if (!isDone) { + // find the given point in the feature attributes + std::list > anAttiributes = + aValueFeature->data()->attributes(GeomDataAPI_Point2D::type()); + std::list >::const_iterator anIt = anAttiributes.begin(), + aLast = anAttiributes.end(); + boost::shared_ptr aFPoint; + for (;anIt!=aLast && !aFPoint; anIt++) { + boost::shared_ptr aCurPoint = + boost::dynamic_pointer_cast(*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 aData = theFeature->data(); + boost::shared_ptr aRef = + boost::dynamic_pointer_cast(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 aData = theFeature->data(); + boost::shared_ptr aRef = + boost::dynamic_pointer_cast(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& 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 index 000000000..7084165a5 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.h @@ -0,0 +1,53 @@ +// File: ModuleBase_WidgetFeatureOrAttribute.h +// Created: 25 Apr 2014 +// Author: Natalia ERMOLAEVA + +#ifndef ModuleBase_WidgetFeatureOrAttribute_H +#define ModuleBase_WidgetFeatureOrAttribute_H + +#include +#include "ModuleBase_WidgetFeature.h" + +#include + +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& theAttribute); + +protected: + boost::shared_ptr myAttribute; /// < the attribute +}; + +#endif diff --git a/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp b/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp index ed7bc2974..0e8d30b6a 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp @@ -38,28 +38,14 @@ void SketchPlugin_ConstraintDistance::execute() { boost::shared_ptr aData = data(); - boost::shared_ptr anAttr_A = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_A)); - boost::shared_ptr anAttr_B = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_B)); - - AttributeDoublePtr anAttr_Value = + boost::shared_ptr aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A); + boost::shared_ptr aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B); + if (aPoint_A && aPoint_B) { + AttributeDoublePtr anAttr_Value = boost::dynamic_pointer_cast(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 aPoint_A = - boost::dynamic_pointer_cast - (aFeature_A->data()->attribute(POINT_ATTR_COORD)); - boost::shared_ptr aPoint_B = - boost::dynamic_pointer_cast - (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 getFeaturePoint(DataPtr theData, if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND) aPointAttr = boost::dynamic_pointer_cast (aFeature->data()->attribute(POINT_ATTR_COORD)); + else { + if (anAttr->attr()) + aPointAttr = boost::dynamic_pointer_cast(anAttr->attr()); + } return aPointAttr; } - diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 20c8e80d5..6c320ee71 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -24,8 +24,8 @@