1 // File: PartSet_OperationEditLine.h
2 // Created: 05 May 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationEditLine.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_OperationSketch.h>
9 #include <ModuleBase_OperationDescription.h>
11 #include <XGUI_ViewerPrs.h>
13 #include <SketchPlugin_Feature.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Document.h>
18 #include <Model_Events.h>
20 #include <Events_Loop.h>
22 #include <SketchPlugin_Line.h>
24 #include <V3d_View.hxx>
30 #include <QMouseEvent>
34 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
36 boost::shared_ptr<ModelAPI_Feature> theFeature)
37 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
41 PartSet_OperationEditLine::~PartSet_OperationEditLine()
45 bool PartSet_OperationEditLine::isGranted(ModuleBase_IOperation* theOperation) const
47 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
50 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
52 return PartSet_OperationSketchBase::getSelectionModes(theFeature);
55 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
56 const std::list<XGUI_ViewerPrs>& thePresentations)
58 setFeature(theFeature);
59 myFeatures = thePresentations;
62 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::sketch() const
67 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
69 if (!(theEvent->buttons() & Qt::LeftButton))
71 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
72 myCurPoint.setPoint(aPoint);
75 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
77 if (!(theEvent->buttons() & Qt::LeftButton))
80 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
82 if (myCurPoint.myIsInitialized) {
84 PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
87 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
89 double aDeltaX = aX - aCurX;
90 double aDeltaY = anY - aCurY;
92 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
93 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
95 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
96 for (; anIt != aLast; anIt++) {
97 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
98 if (!aFeature || aFeature == feature())
100 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
101 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
106 myCurPoint.setPoint(aPoint);
109 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
110 const std::list<XGUI_ViewerPrs>& theSelected)
112 std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
113 if (myFeatures.size() == 1) {
114 if (theSelected.empty())
117 boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
119 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
123 std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
124 for (; anIt != aLast; anIt++) {
125 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
127 emit featureConstructed(aFeature, FM_Deactivation);
132 void PartSet_OperationEditLine::startOperation()
134 // do nothing in order to do not create a new feature
135 emit multiSelectionEnabled(false);
136 emit setSelection(std::list<XGUI_ViewerPrs>());
137 emit stopSelection(myFeatures, true);
141 void PartSet_OperationEditLine::stopOperation()
143 emit multiSelectionEnabled(true);
144 bool isSelectFeatures = myFeatures.size() > 1;
145 emit stopSelection(myFeatures, false);
146 if (isSelectFeatures)
147 emit setSelection(myFeatures);
152 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
154 // do nothing in order to do not create a new feature
155 return boost::shared_ptr<ModelAPI_Feature>();
158 void PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
159 double theDeltaX, double theDeltaY,
160 const std::string& theAttribute)
162 if (!theFeature || theFeature->getKind() != "SketchLine")
165 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
166 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
167 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
169 aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
172 void PartSet_OperationEditLine::sendFeatures()
174 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures;
175 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
176 for (; anIt != aLast; anIt++) {
177 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
178 if (!aFeature || aFeature == feature())
182 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetEditEvent");
183 Model_FeaturesMovedMessage aMessage;
184 aMessage.setFeatures(aFeatures);
185 Events_Loop::loop()->send(aMessage);