ModuleBase_ViewerPrs.h
ModuleBase_Tools.h
ModuleBase_ResultValidators.h
+ ModuleBase_FeatureValidator.h
)
SET(PROJECT_SOURCES
--- /dev/null
+// File: ModuleBase_FeatureValidator.h
+// Created: 8 Jul 2014
+// Author: Vitaly SMETANNIKOV
+
+#ifndef ModuleBase_FeatureValidator_H
+#define ModuleBase_FeatureValidator_H
+
+#include "ModuleBase.h"
+
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_Validator.h>
+
+
+class ModuleBase_FeatureValidator: public ModelAPI_Validator
+{
+public:
+ virtual bool isValid(const FeaturePtr theFeature) const = 0;
+};
+
+#endif
emit stopped();
}
-void ModuleBase_IOperation::commit()
+bool ModuleBase_IOperation::commit()
{
- commitOperation();
- emit committed();
+ if (canBeCommitted()) {
+ commitOperation();
+ emit committed();
- stopOperation();
+ stopOperation();
- document()->finishOperation();
- emit stopped();
+ document()->finishOperation();
+ emit stopped();
- afterCommitOperation();
+ afterCommitOperation();
+ return true;
+ }
+ return false;
}
void ModuleBase_IOperation::setRunning(bool theState)
/// /returns the instance of the description class
ModuleBase_OperationDescription* getDescription() const;
- /// Verifies whether this operator can be commited.
- /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
- virtual bool canBeCommitted() const;
-
/// Verifies whether this operator can be always started above any already running one
/// \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this )
/// This method must be redefined in derived operation if operation of derived class
/// Commits operation
/// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
/// Redefine commitOperation method to change behavior of operation instead
- void commit();
+ bool commit();
/// Alias for start/abort slots
/// Public slot. Aborts operation if false, else does nothing.
/// Virtual method called when operation started (see start() method for more description)
/// Default impl calls corresponding slot and commits immediately.
virtual void startOperation() = 0;
+
/// Virtual method called when operation stopped - committed or aborted.
virtual void stopOperation() = 0;
+
/// Virtual method called when operation aborted (see abort() method for more description)
virtual void abortOperation() = 0;
+
/// Virtual method called when operation committed (see commit() method for more description)
virtual void commitOperation() = 0;
+
/// Virtual method called after operation committed (see commit() method for more description)
/// it is important that the method is called after the stop() signal is emitted
virtual void afterCommitOperation() = 0;
+ /// Verifies whether this operator can be commited.
+ /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
+ virtual bool canBeCommitted() const;
+
/// Returns pointer to the root document.
boost::shared_ptr<ModelAPI_Document> document() const;
#include "ModuleBase_OperationDescription.h"
#include "ModuleBase_ModelWidget.h"
+#include "ModuleBase_FeatureValidator.h"
#include <ModelAPI_AttributeDouble.h>
#include <ModelAPI_Document.h>
#include <ModelAPI_Document.h>
#include <ModelAPI_Events.h>
#include <ModelAPI_Result.h>
+#include <ModelAPI_Validator.h>
#include <Events_Loop.h>
{
}
+bool ModuleBase_Operation::canBeCommitted() const
+{
+ if (ModuleBase_IOperation::canBeCommitted()) {
+ FeaturePtr aFeature = feature();
+ std::string aId = aFeature->getKind();
+
+ PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+ ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+ const ModelAPI_Validator* aValidator = aFactory->validator(aId);
+ if (aValidator) {
+ const ModuleBase_FeatureValidator* aFValidator =
+ dynamic_cast<const ModuleBase_FeatureValidator*>(aValidator);
+ if (aFValidator) {
+ return aFValidator->isValid(aFeature);
+ }
+ }
+ return true;
+ }
+ return false;
+}
+
void ModuleBase_Operation::flushUpdated()
{
Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
/// Sets the operation feature
void setFeature(FeaturePtr theFeature);
+ /// Verifies whether this operator can be commited.
+ /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
+ virtual bool canBeCommitted() const;
+
protected:
FeaturePtr myFeature; /// the operation feature to be handled
};
void PartSet_Module::createFeatures()
{
- //!! Test registering of validators
+ //Registering of validators
PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
bool PartSet_OperationFeatureCreate::canBeCommitted() const
{
- return !myActiveWidget;
+ if (PartSet_OperationSketchBase::canBeCommitted())
+ return !myActiveWidget;
+ return false;
}
bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
const std::list<ModuleBase_ViewerPrs>& theSelected,
const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
{
- if (canBeCommitted())
- {
+ if (commit()) {
// if the point creation is finished, the next mouse release should commit the modification
// the next release can happens by double click in the viewer
- commit();
restartOperation(feature()->getKind(), feature());
return;
}
void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
{
- if (canBeCommitted()) {
- commit();
+ if (commit()) {
restartOperation(feature()->getKind(), feature());
}
else {
{
switch (theKey) {
case Qt::Key_Return: {
- if (canBeCommitted())
- {
- commit();
+ if (commit()) {
// it start a new line creation at a free point
restartOperation(feature()->getKind(), FeaturePtr());
}
}
break;
case Qt::Key_Escape: {
- if (canBeCommitted())
- {
- commit();
- }
- else
- {
+ if (!commit()) {
abort();
}
}
/// Destructor
virtual ~PartSet_OperationFeatureCreate();
- /// Verifies whether this operator can be commited.
- /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
- virtual bool canBeCommitted() const;
-
/// Returns that this operator can be started above already running one.
/// The runned operation should be the sketch feature modified operation
/// \param theOperation the previous running operation
/// \returns the created feature
virtual FeaturePtr createFeature(const bool theFlushMessage = true);
+ /// Verifies whether this operator can be commited.
+ /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
+ virtual bool canBeCommitted() const;
+
protected:
/// Set value to the active widget
/// \param theFeature the feature
FeaturePtr aFeature = ModuleBase_Tools::feature(aObject);
if (!aFeature || aFeature != feature()) {
- commit();
- emit featureConstructed(feature(), FM_Deactivation);
-
- bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
- if(aHasShift && !theHighlighted.empty()) {
- QList<ObjectPtr> aSelected;
- std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
- for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
- aSelected.append((*aIt).object());
- /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
- if (!aSelected.contains((*aIt).object()))
+ if (commit()) {
+ emit featureConstructed(feature(), FM_Deactivation);
+
+ bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
+ if(aHasShift && !theHighlighted.empty()) {
+ QList<ObjectPtr> aSelected;
+ std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
+ for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
aSelected.append((*aIt).object());
- }*/
- //aSelected.push_back(feature());
- //aSelected.push_back(theHighlighted.front().object());
- emit setSelection(aSelected);
- }
- else if (aFeature) {
- restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
+ /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
+ if (!aSelected.contains((*aIt).object()))
+ aSelected.append((*aIt).object());
+ }*/
+ //aSelected.push_back(feature());
+ //aSelected.push_back(theHighlighted.front().object());
+ emit setSelection(aSelected);
+ }
+ else if (aFeature) {
+ restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
+ }
}
}
}
const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
{
- std::list<ModuleBase_ViewerPrs> aFeatures = myFeatures;
- commit();
- std::list<ModuleBase_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
- for (; anIt != aLast; anIt++) {
- ObjectPtr aFeature = (*anIt).object();
- if (aFeature) {
- emit featureConstructed(aFeature, FM_Deactivation);
- }
+ if (commit()) {
+ std::list<ModuleBase_ViewerPrs> aFeatures = myFeatures;
+ std::list<ModuleBase_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
+ for (; anIt != aLast; anIt++) {
+ ObjectPtr aFeature = (*anIt).object();
+ if (aFeature) {
+ emit featureConstructed(aFeature, FM_Deactivation);
+ }
+ }
}
}
}
return (aCount > 0) && (aCount < 2);
}
+
#include <ModuleBase_SelectionValidator.h>
#include <ModuleBase_ISelection.h>
+/*
+* Selector validators
+*/
+
//! A class to validate a selection for Distance constraint operation
class PartSet_DistanceValidator: public ModuleBase_SelectionValidator
{
PARTSET_EXPORT virtual bool isValid(const ModuleBase_ISelection* theSelection) const;
};
+
#endif
SketchPlugin_ConstraintParallel.h
SketchPlugin_ConstraintPerpendicular.h
SketchPlugin_ConstraintRadius.h
+ SketchPlugin_Validators.h
)
SET(PROJECT_SOURCES
SketchPlugin_ConstraintParallel.cpp
SketchPlugin_ConstraintPerpendicular.cpp
SketchPlugin_ConstraintRadius.cpp
+ SketchPlugin_Validators.cpp
)
SET(PROJECT_LIBRARIES
#include "SketchPlugin_ConstraintParallel.h"
#include "SketchPlugin_ConstraintPerpendicular.h"
#include "SketchPlugin_ConstraintRadius.h"
+#include "SketchPlugin_Validators.h"
#include <ModelAPI_PluginManager.h>
#include <ModelAPI_Document.h>
+#include <ModelAPI_Validator.h>
using namespace std;
SketchPlugin_Plugin::SketchPlugin_Plugin()
{
+ PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+ ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+ aFactory->registerValidator("SketchPlugin_DistanceFeatureValidator", new SketchPlugin_DistanceFeatureValidator);
+
// register this plugin
ModelAPI_PluginManager::get()->registerPlugin(this);
}
--- /dev/null
+// File: SketchPlugin_Validators.cpp
+// Created: 01 Aug 2014
+// Author: Vitaly SMETANNIKOV
+
+#include "SketchPlugin_Validators.h"
+#include "SketchPlugin_Constraint.h"
+#include "SketchPlugin_Sketch.h"
+#include "SketchPlugin_Point.h"
+#include "SketchPlugin_Line.h"
+#include "SketchPlugin_Circle.h"
+#include "SketchPlugin_Arc.h"
+#include <ModelAPI_Data.h>
+
+
+bool isValidType(const std::string& theType)
+{
+ return (theType == SketchPlugin_Point::ID()) ||
+ (theType == SketchPlugin_Circle::ID()) ||
+ (theType == SketchPlugin_Arc::ID());
+}
+
+bool SketchPlugin_DistanceFeatureValidator::isValid(const FeaturePtr theFeature) const
+{
+ if (!theFeature)
+ return false;
+ if (!theFeature->data() || !theFeature->data()->isValid())
+ return false;
+ boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+
+ boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefA =
+ boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+
+ boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefB =
+ boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+
+ if (!aRefA || !aRefB)
+ return false;
+
+ FeaturePtr aFetureA = SketchPlugin_Sketch::getFeature(aRefA->object());
+ FeaturePtr aFetureB = SketchPlugin_Sketch::getFeature(aRefB->object());
+ if (!aFetureA || !aFetureB)
+ return false;
+
+ std::string aTypeA = aFetureA->getKind();
+ std::string aTypeB = aFetureB->getKind();
+
+ if (aTypeA == SketchPlugin_Line::ID()) {
+ return isValidType(aTypeB);
+ } else if (aTypeB == SketchPlugin_Line::ID()) {
+ return isValidType(aTypeA);
+ } else
+ return isValidType(aTypeA) && isValidType(aTypeB);
+ return false;
+}
--- /dev/null
+// File: SketchPlugin_Validators.h
+// Created: 01 Aug 2014
+// Author: Vitaly SMETANNIKOV
+
+#ifndef SketchPlugin_Validators_H
+#define SketchPlugin_Validators_H
+
+#include "SketchPlugin.h"
+#include <ModuleBase_FeatureValidator.h>
+
+
+//! A class to validate a selection for Perpendicular constraint operation
+class SketchPlugin_DistanceFeatureValidator: public ModuleBase_FeatureValidator
+{
+public:
+ SKETCHPLUGIN_EXPORT virtual bool isValid(const FeaturePtr theFeature) const;
+};
+
+#endif
\ No newline at end of file
<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_or_attribute_selector id="ConstraintEntityA" label="First point" tooltip="Select an point in the viewer">
+ <feature_or_attribute_selector id="ConstraintEntityA" label="First object" tooltip="Select an point in the viewer">
<validator id="ModuleBase_ResultPointValidator"/>
</feature_or_attribute_selector>
- <feature_or_attribute_selector id="ConstraintEntityB" label="Last point" tooltip="Select an point in the viewer">
+ <feature_or_attribute_selector id="ConstraintEntityB" label="Last object" tooltip="Select an point in the viewer">
<validator id="ModuleBase_ResultPointValidator"/>
</feature_or_attribute_selector>
<point_selector id="ConstraintFlyoutValuePnt" internal="1"/>
<doublevalue_editor label="Value" tooltip="Constraint value" id="ConstraintValue"/>
- <validator id="PartSet_DistanceValidator"/>
+ <validator id="PartSet_DistanceValidator"/>
+ <validator id="SketchPlugin_DistanceFeatureValidator"/>
</feature>
<feature id="SketchConstraintLength" title="Length" tooltip="Create constraint for the given length of a line segment">
if (myOperationMgr->startOperation(anOperation)) {
myPropertyPanel->updateContentWidget(anOperation->feature());
if (!anOperation->getDescription()->hasXmlRepresentation()) {
- anOperation->commit();
- updateCommandStatus();
+ if (anOperation->commit())
+ updateCommandStatus();
}
}
return;