From 05051c9ff89db0e367db29f630338cc0cc404e41 Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 24 Apr 2015 17:59:45 +0300 Subject: [PATCH] Integer widget is created and used in translate rotate widgets --- src/Config/Config_Keywords.h | 1 + .../GeomValidators_Positive.cpp | 9 +- src/ModuleBase/CMakeLists.txt | 2 + src/ModuleBase/ModuleBase_WidgetFactory.cpp | 3 + src/ModuleBase/ModuleBase_WidgetIntValue.cpp | 139 ++++++++++++++++++ src/ModuleBase/ModuleBase_WidgetIntValue.h | 65 ++++++++ src/PartSet/PartSet_SketcherMgr.cpp | 17 +-- .../SketchPlugin_MultiRotation.cpp | 5 +- .../SketchPlugin_MultiTranslation.cpp | 5 +- src/SketchPlugin/plugin-Sketch.xml | 10 +- .../SketchSolver_ConstraintMultiRotation.cpp | 10 +- ...ketchSolver_ConstraintMultiTranslation.cpp | 8 +- 12 files changed, 239 insertions(+), 35 deletions(-) create mode 100644 src/ModuleBase/ModuleBase_WidgetIntValue.cpp create mode 100644 src/ModuleBase/ModuleBase_WidgetIntValue.h diff --git a/src/Config/Config_Keywords.h b/src/Config/Config_Keywords.h index a9f44bf63..2827d269c 100644 --- a/src/Config/Config_Keywords.h +++ b/src/Config/Config_Keywords.h @@ -24,6 +24,7 @@ const static char* NODE_XMLPARENT = "libxml_parent"; // Widgets const static char* WDG_INFO = "label"; const static char* WDG_DOUBLEVALUE = "doublevalue"; +const static char* WDG_INTEGERVALUE = "integervalue"; const static char* WDG_BOOLVALUE = "boolvalue"; const static char* WDG_STRINGVALUE = "stringvalue"; const static char* WDG_MULTISELECTOR = "multi_selector"; diff --git a/src/GeomValidators/GeomValidators_Positive.cpp b/src/GeomValidators/GeomValidators_Positive.cpp index 58964c04f..9d3fbf983 100644 --- a/src/GeomValidators/GeomValidators_Positive.cpp +++ b/src/GeomValidators/GeomValidators_Positive.cpp @@ -6,6 +6,7 @@ #include "GeomValidators_Positive.h" #include +#include #include #include @@ -23,7 +24,11 @@ GeomValidators_Positive::GeomValidators_Positive() bool GeomValidators_Positive::isValid( const AttributePtr& theAttribute, const std::list& theArguments) const { - std::shared_ptr aDouble = + AttributeDoublePtr aDouble = std::dynamic_pointer_cast(theAttribute); - return aDouble->isInitialized() && aDouble->value() > 1.e-5; + if (aDouble.get()) + return aDouble->isInitialized() && aDouble->value() > 1.e-5; + AttributeIntegerPtr aInteger = + std::dynamic_pointer_cast(theAttribute); + return aInteger->isInitialized() && aInteger->value() > 0; } diff --git a/src/ModuleBase/CMakeLists.txt b/src/ModuleBase/CMakeLists.txt index d52f98a52..140a65d17 100644 --- a/src/ModuleBase/CMakeLists.txt +++ b/src/ModuleBase/CMakeLists.txt @@ -46,6 +46,7 @@ SET(PROJECT_HEADERS ModuleBase_WidgetValidated.h ModuleBase_WidgetExprEditor.h ModuleBase_ParamSpinBox.h + ModuleBase_WidgetIntValue.h ) SET(PROJECT_SOURCES @@ -83,6 +84,7 @@ SET(PROJECT_SOURCES ModuleBase_WidgetExprEditor.cpp ModuleBase_ParamSpinBox.cpp ModuleBase_SelectionValidator.cpp + ModuleBase_WidgetIntValue.cpp ) SET(PROJECT_LIBRARIES diff --git a/src/ModuleBase/ModuleBase_WidgetFactory.cpp b/src/ModuleBase/ModuleBase_WidgetFactory.cpp index c9561a13b..2787b00ac 100644 --- a/src/ModuleBase/ModuleBase_WidgetFactory.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFactory.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -122,6 +123,8 @@ ModuleBase_ModelWidget* ModuleBase_WidgetFactory::createWidgetByType(const std:: result = new ModuleBase_WidgetLabel(theParent, myWidgetApi, myParentId); } else if (theType == WDG_DOUBLEVALUE) { result = new ModuleBase_WidgetDoubleValue(theParent, myWidgetApi, myParentId); + } else if (theType == WDG_INTEGERVALUE) { + result = new ModuleBase_WidgetIntValue(theParent, myWidgetApi, myParentId); } else if (theType == WDG_SHAPE_SELECTOR) { result = new ModuleBase_WidgetShapeSelector(theParent, myWorkshop, myWidgetApi, myParentId); } else if (theType == WDG_BOOLVALUE) { diff --git a/src/ModuleBase/ModuleBase_WidgetIntValue.cpp b/src/ModuleBase/ModuleBase_WidgetIntValue.cpp new file mode 100644 index 000000000..aab5d1dd9 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetIntValue.cpp @@ -0,0 +1,139 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_Widgets.h +// Created: 04 June 2014 +// Author: Vitaly Smetannikov + +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#ifndef INT_MAX +#define INT_MAX 2147483647 +#endif + +#ifdef _DEBUG +#include +#endif + +ModuleBase_WidgetIntValue::ModuleBase_WidgetIntValue(QWidget* theParent, + const Config_WidgetAPI* theData, + const std::string& theParentId) + : ModuleBase_ModelWidget(theParent, theData, theParentId) +{ + QFormLayout* aControlLay = new QFormLayout(this); + ModuleBase_Tools::adjustMargins(aControlLay); + + QString aLabelText = QString::fromStdString(theData->widgetLabel()); + QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); + myLabel = new QLabel(aLabelText, this); + if (!aLabelIcon.isEmpty()) + myLabel->setPixmap(QPixmap(aLabelIcon)); + + mySpinBox = new QSpinBox(this); + QString anObjName = QString::fromStdString(attributeID()); + mySpinBox->setObjectName(anObjName); + + bool isOk = false; + std::string aProp = theData->getProperty(DOUBLE_WDG_MIN); + int aMinVal = QString::fromStdString(aProp).toInt(&isOk); + if (isOk) { + mySpinBox->setMinimum(aMinVal); + } else { + mySpinBox->setMinimum(-INT_MAX); + } + + aProp = theData->getProperty(DOUBLE_WDG_MAX); + int aMaxVal = QString::fromStdString(aProp).toInt(&isOk); + if (isOk) { + mySpinBox->setMaximum(aMaxVal); + } else { + mySpinBox->setMaximum(INT_MAX); + } + + aProp = theData->getProperty(DOUBLE_WDG_STEP); + int aStepVal = QString::fromStdString(aProp).toInt(&isOk); + if (isOk) { + mySpinBox->setSingleStep(aStepVal); + } + + int aDefVal = QString::fromStdString(getDefaultValue()).toInt(&isOk); + if (isOk) { + mySpinBox->setValue(aDefVal); + } + + QString aTTip = QString::fromStdString(theData->widgetTooltip()); + mySpinBox->setToolTip(aTTip); + + aControlLay->addRow(myLabel, mySpinBox); + connect(mySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valuesChanged())); +} + +ModuleBase_WidgetIntValue::~ModuleBase_WidgetIntValue() +{ +} + +void ModuleBase_WidgetIntValue::reset() +{ + if (isComputedDefault()) { + return; + //if (myFeature->compute(myAttributeID)) + // restoreValue(); + } else { + bool isOk; + int aDefValue = QString::fromStdString(getDefaultValue()).toInt(&isOk); + // reset the value just if there is a default value definition in the XML definition + // if the double value can not be found by the default value, do nothing + if (isOk) { + bool isBlocked = mySpinBox->blockSignals(true); + mySpinBox->setValue(isOk ? aDefValue : 0); + mySpinBox->blockSignals(isBlocked); + storeValueCustom(); + } + } +} + +bool ModuleBase_WidgetIntValue::storeValueCustom() const +{ + DataPtr aData = myFeature->data(); + AttributeIntegerPtr aIntVal = aData->integer(attributeID()); + int aVal = mySpinBox->value(); + aIntVal->setValue(mySpinBox->value()); + updateObject(myFeature); + return true; +} + +bool ModuleBase_WidgetIntValue::restoreValue() +{ + DataPtr aData = myFeature->data(); + AttributeIntegerPtr aRef = aData->integer(attributeID()); + bool isBlocked = mySpinBox->blockSignals(true); + mySpinBox->setValue(aRef->value()); + mySpinBox->blockSignals(isBlocked); + return true; +} + +QList ModuleBase_WidgetIntValue::getControls() const +{ + QList aList; + aList.append(mySpinBox); + return aList; +} diff --git a/src/ModuleBase/ModuleBase_WidgetIntValue.h b/src/ModuleBase/ModuleBase_WidgetIntValue.h new file mode 100644 index 000000000..b795bc1c8 --- /dev/null +++ b/src/ModuleBase/ModuleBase_WidgetIntValue.h @@ -0,0 +1,65 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + +// File: ModuleBase_WidgetIntValue.h +// Created: 04 June 2014 +// Author: Vitaly Smetannikov + +#ifndef ModuleBase_WidgetIntValue_H +#define ModuleBase_WidgetIntValue_H + +#include "ModuleBase.h" +#include "ModuleBase_ModelWidget.h" + +class Config_WidgetAPI; +class QWidget; +class QLabel; +class QTimer; +class QSpinBox; + +/** +* \ingroup GUI +* A class of property panel widget for double value input +* It can be defined with "doublevalue" keyword. For example: +* \code +* +* \endcode +*/ +class MODULEBASE_EXPORT ModuleBase_WidgetIntValue : public ModuleBase_ModelWidget +{ +Q_OBJECT + public: + /// Constructor + /// \param theParent the parent object + /// \param theData the widget configuation. The attribute of the model widget is obtained from + /// \param theParentId is Id of a parent structure (widget, operation, group) + ModuleBase_WidgetIntValue(QWidget* theParent, const Config_WidgetAPI* theData, + const std::string& theParentId); + + virtual ~ModuleBase_WidgetIntValue(); + + /// Fills the widget with default values + virtual void reset(); + + //! Read value of corresponded attribute from data model to the input control + // \return True in success + virtual bool restoreValue(); + + /// Returns list of widget controls + /// \return a control list + virtual QList getControls() const; + + +protected: + /// Saves the internal parameters to the given feature + /// \return True in success + virtual bool storeValueCustom() const; + +protected: + /// Label of the widget + QLabel* myLabel; + + /// Input value control + QSpinBox* mySpinBox; +}; + +#endif diff --git a/src/PartSet/PartSet_SketcherMgr.cpp b/src/PartSet/PartSet_SketcherMgr.cpp index 2634dd7a3..5e527a881 100644 --- a/src/PartSet/PartSet_SketcherMgr.cpp +++ b/src/PartSet/PartSet_SketcherMgr.cpp @@ -56,6 +56,8 @@ #include #include #include +#include +#include #include @@ -598,19 +600,8 @@ const QStringList& PartSet_SketcherMgr::sketchOperationIdList() aIds << SketchPlugin_Point::ID().c_str(); aIds << SketchPlugin_Arc::ID().c_str(); aIds << SketchPlugin_Circle::ID().c_str(); - aIds << SketchPlugin_ConstraintLength::ID().c_str(); - aIds << SketchPlugin_ConstraintDistance::ID().c_str(); - aIds << SketchPlugin_ConstraintRigid::ID().c_str(); - aIds << SketchPlugin_ConstraintRadius::ID().c_str(); - aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str(); - aIds << SketchPlugin_ConstraintParallel::ID().c_str(); - aIds << SketchPlugin_ConstraintHorizontal::ID().c_str(); - aIds << SketchPlugin_ConstraintVertical::ID().c_str(); - aIds << SketchPlugin_ConstraintEqual::ID().c_str(); - aIds << SketchPlugin_ConstraintTangent::ID().c_str(); - aIds << SketchPlugin_ConstraintCoincidence::ID().c_str(); aIds << SketchPlugin_ConstraintFillet::ID().c_str(); - aIds << SketchPlugin_ConstraintMirror::ID().c_str(); + aIds.append(constraintsIdList()); } return aIds; } @@ -631,6 +622,8 @@ const QStringList& PartSet_SketcherMgr::constraintsIdList() aIds << SketchPlugin_ConstraintTangent::ID().c_str(); aIds << SketchPlugin_ConstraintCoincidence::ID().c_str(); aIds << SketchPlugin_ConstraintMirror::ID().c_str(); + aIds << SketchPlugin_MultiRotation::ID().c_str(); + aIds << SketchPlugin_MultiTranslation::ID().c_str(); } return aIds; } diff --git a/src/SketchPlugin/SketchPlugin_MultiRotation.cpp b/src/SketchPlugin/SketchPlugin_MultiRotation.cpp index 2f5ad3a49..982524cf2 100644 --- a/src/SketchPlugin/SketchPlugin_MultiRotation.cpp +++ b/src/SketchPlugin/SketchPlugin_MultiRotation.cpp @@ -32,7 +32,7 @@ void SketchPlugin_MultiRotation::initAttributes() { data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId()); data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId()); - data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/); + data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId()); AttributeSelectionListPtr aSelection = @@ -46,8 +46,7 @@ void SketchPlugin_MultiRotation::initAttributes() void SketchPlugin_MultiRotation::execute() { AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID()); - int aNbCopies = (int)(std::dynamic_pointer_cast( - attribute(NUMBER_OF_COPIES_ID()))->value()); + int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value(); // Obtain center and angle of rotation std::shared_ptr aCenter = std::dynamic_pointer_cast( diff --git a/src/SketchPlugin/SketchPlugin_MultiTranslation.cpp b/src/SketchPlugin/SketchPlugin_MultiTranslation.cpp index 06cbd55d7..064b93363 100644 --- a/src/SketchPlugin/SketchPlugin_MultiTranslation.cpp +++ b/src/SketchPlugin/SketchPlugin_MultiTranslation.cpp @@ -28,7 +28,7 @@ void SketchPlugin_MultiTranslation::initAttributes() { data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId()); data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/); + data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId()); data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId()); AttributeSelectionListPtr aSelection = @@ -42,8 +42,7 @@ void SketchPlugin_MultiTranslation::initAttributes() void SketchPlugin_MultiTranslation::execute() { AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID()); - int aNbCopies = (int)(std::dynamic_pointer_cast( - attribute(NUMBER_OF_COPIES_ID()))->value()); + int aNbCopies = integer(NUMBER_OF_COPIES_ID())->value(); // Calculate shift vector std::shared_ptr aStart = std::dynamic_pointer_cast( diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 363f2e71a..9e2021b9c 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -277,12 +277,12 @@ title="End point" tooltip="Final point of translation"/> - - + - - - + diff --git a/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp b/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp index 2d439b2d1..ab3a4e570 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintMultiRotation.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -39,8 +40,7 @@ void SketchSolver_ConstraintMultiRotation::getAttributes( AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast( aData->attribute(SketchPlugin_Constraint::ENTITY_A())); myNumberOfObjects = anInitialRefList->size(); - myNumberOfCopies = (size_t)std::dynamic_pointer_cast( - aData->attribute(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID()))->value(); + myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())->value(); AttributeRefListPtr aRefList = std::dynamic_pointer_cast( myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); if (!aRefList) { @@ -183,10 +183,8 @@ void SketchSolver_ConstraintMultiRotation::update(ConstraintPtr theConstraint) if (!theConstraint || theConstraint == myBaseConstraint) { AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast( myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - AttributeDoublePtr aNbCopies = std::dynamic_pointer_cast( - myBaseConstraint->attribute(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())); - if (anInitialRefList->size() != myNumberOfObjects || - (size_t)aNbCopies->value() != myNumberOfCopies) { + AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID()); + if (anInitialRefList->size() != myNumberOfObjects || aNbCopies->value() != myNumberOfCopies) { remove(myBaseConstraint); process(); return; diff --git a/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp b/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp index 963012df5..2e5bafbfe 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintMultiTranslation.cpp @@ -6,9 +6,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -44,8 +46,7 @@ void SketchSolver_ConstraintMultiTranslation::getAttributes( AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast( aData->attribute(SketchPlugin_Constraint::ENTITY_A())); myNumberOfObjects = anInitialRefList->size(); - myNumberOfCopies = (size_t)std::dynamic_pointer_cast( - aData->attribute(SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID()))->value(); + myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID())->value(); AttributeRefListPtr aRefList = std::dynamic_pointer_cast( myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B())); if (!aRefList) { @@ -183,8 +184,7 @@ void SketchSolver_ConstraintMultiTranslation::update(ConstraintPtr theConstraint if (!theConstraint || theConstraint == myBaseConstraint) { AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast( myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A())); - AttributeDoublePtr aNbCopies = std::dynamic_pointer_cast( - myBaseConstraint->attribute(SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID())); + AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID()); if (anInitialRefList->size() != myNumberOfObjects || (size_t)aNbCopies->value() != myNumberOfCopies) { remove(myBaseConstraint); -- 2.39.2