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>
10 #include <Model_Events.h>
12 #include <XGUI_ViewerPrs.h>
14 #include <SketchPlugin_Feature.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
19 #include <Model_Events.h>
21 #include <Events_Loop.h>
23 #include <SketchPlugin_Line.h>
25 #include <V3d_View.hxx>
31 #include <QMouseEvent>
35 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
37 boost::shared_ptr<ModelAPI_Feature> theFeature)
38 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
42 PartSet_OperationEditLine::~PartSet_OperationEditLine()
46 bool PartSet_OperationEditLine::isGranted(ModuleBase_IOperation* theOperation) const
48 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
51 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
53 return PartSet_OperationSketchBase::getSelectionModes(theFeature);
56 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
57 const std::list<XGUI_ViewerPrs>& thePresentations)
59 setFeature(theFeature);
60 myFeatures = thePresentations;
63 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::sketch() const
68 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
70 if (!(theEvent->buttons() & Qt::LeftButton))
72 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
73 myCurPoint.setPoint(aPoint);
76 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
78 if (!(theEvent->buttons() & Qt::LeftButton))
81 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
83 if (myCurPoint.myIsInitialized) {
85 PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
88 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
90 double aDeltaX = aX - aCurX;
91 double aDeltaY = anY - aCurY;
93 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
94 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
96 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
97 for (; anIt != aLast; anIt++) {
98 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
99 if (!aFeature || aFeature == feature())
101 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
102 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
107 myCurPoint.setPoint(aPoint);
110 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
111 const std::list<XGUI_ViewerPrs>& theSelected)
113 std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
114 if (myFeatures.size() == 1) {
115 if (theSelected.empty())
118 boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
120 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
124 std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
125 for (; anIt != aLast; anIt++) {
126 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
128 emit featureConstructed(aFeature, FM_Deactivation);
133 void PartSet_OperationEditLine::startOperation()
135 // do nothing in order to do not create a new feature
136 emit multiSelectionEnabled(false);
137 emit setSelection(std::list<XGUI_ViewerPrs>());
138 emit stopSelection(myFeatures, true);
142 void PartSet_OperationEditLine::stopOperation()
144 emit multiSelectionEnabled(true);
145 bool isSelectFeatures = myFeatures.size() > 1;
146 emit stopSelection(myFeatures, false);
147 if (isSelectFeatures)
148 emit setSelection(myFeatures);
153 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature(const bool /*theFlushMessage*/)
155 // do nothing in order to do not create a new feature
156 return boost::shared_ptr<ModelAPI_Feature>();
159 void PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
160 double theDeltaX, double theDeltaY,
161 const std::string& theAttribute)
163 if (!theFeature || theFeature->getKind() != "SketchLine")
166 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
167 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
168 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
170 aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
173 void PartSet_OperationEditLine::sendFeatures()
175 static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
177 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures;
178 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
179 for (; anIt != aLast; anIt++) {
180 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
181 if (!aFeature || aFeature == feature())
184 Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
185 Events_Loop::loop()->send(aMessage);
187 Events_Loop::loop()->flush(anEvent);