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>& theSelected,
58 const std::list<XGUI_ViewerPrs>& theHighlighted)
60 setFeature(theFeature);
62 if (!theHighlighted.empty()) {
63 // if there is highlighted object, we check whether it is in the list of selected objects
64 // in that case this object is a handle of the moved lines. If there no such object in the selection,
65 // the hightlighted object should moved and the selection is skipped. The skipped selection will be
66 // deselected in the viewer by blockSelection signal in the startOperation method.
67 bool isSelected = false;
68 std::list<XGUI_ViewerPrs>::const_iterator anIt = theSelected.begin(), aLast = theSelected.end();
69 for (; anIt != aLast && !isSelected; anIt++) {
70 isSelected = (*anIt).feature() == feature();
73 myFeatures = theHighlighted;
75 myFeatures = theSelected;
78 myFeatures = theSelected;
81 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::sketch() const
86 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
87 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
88 const std::list<XGUI_ViewerPrs>& theHighlighted)
90 if (myFeatures.size() == 1)
92 boost::shared_ptr<ModelAPI_Feature> aFeature;
93 if (!theHighlighted.empty())
94 aFeature = theHighlighted.front().feature();
96 if (aFeature && aFeature == feature()) { // continue the feature edit
101 emit featureConstructed(feature(), FM_Deactivation);
103 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
109 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
111 if (!(theEvent->buttons() & Qt::LeftButton))
114 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
116 if (myCurPoint.myIsInitialized) {
118 PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
121 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
123 double aDeltaX = aX - aCurX;
124 double aDeltaY = anY - aCurY;
126 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
127 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
129 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
130 for (; anIt != aLast; anIt++) {
131 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
132 if (!aFeature || aFeature == feature())
134 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
135 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
141 myCurPoint.setPoint(aPoint);
144 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
145 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
146 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
148 std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
149 if (myFeatures.size() == 1) {
150 blockSelection(false);
154 std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
155 for (; anIt != aLast; anIt++) {
156 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
158 emit featureConstructed(aFeature, FM_Deactivation);
163 void PartSet_OperationEditLine::startOperation()
165 // do nothing in order to do not create a new feature
166 emit multiSelectionEnabled(false);
168 blockSelection(true);
173 void PartSet_OperationEditLine::stopOperation()
175 emit multiSelectionEnabled(true);
177 blockSelection(false, myFeatures.size() > 1);
182 void PartSet_OperationEditLine::blockSelection(bool isBlocked, const bool isRestoreSelection)
185 emit setSelection(std::list<XGUI_ViewerPrs>());
186 emit stopSelection(myFeatures, true);
189 emit stopSelection(myFeatures, false);
190 if (isRestoreSelection)
191 emit setSelection(myFeatures);
195 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature(const bool /*theFlushMessage*/)
197 // do nothing in order to do not create a new feature
198 return boost::shared_ptr<ModelAPI_Feature>();
201 void PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
202 double theDeltaX, double theDeltaY,
203 const std::string& theAttribute)
205 if (!theFeature || theFeature->getKind() != "SketchLine")
208 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
209 if (!aData->isValid())
211 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
212 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
214 aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
217 void PartSet_OperationEditLine::sendFeatures()
219 static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
221 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures;
222 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
223 for (; anIt != aLast; anIt++) {
224 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
228 Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
229 Events_Loop::loop()->send(aMessage);
231 Events_Loop::loop()->flush(anEvent);