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>& theSelected,
57 const std::list<XGUI_ViewerPrs>& theHighlighted)
59 setFeature(theFeature);
61 if (!theHighlighted.empty()) {
62 // if there is highlighted object, we check whether it is in the list of selected objects
63 // in that case this object is a handle of the moved lines. If there no such object in the selection,
64 // the hightlighted object should moved and the selection is skipped. The skipped selection will be
65 // deselected in the viewer by blockSelection signal in the startOperation method.
66 bool isSelected = false;
67 std::list<XGUI_ViewerPrs>::const_iterator anIt = theSelected.begin(), aLast = theSelected.end();
68 for (; anIt != aLast && !isSelected; anIt++) {
69 isSelected = (*anIt).feature() == feature();
72 myFeatures = theHighlighted;
74 myFeatures = theSelected;
77 myFeatures = theSelected;
80 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::sketch() const
85 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
86 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
87 const std::list<XGUI_ViewerPrs>& theHighlighted)
89 if (myFeatures.size() == 1)
91 boost::shared_ptr<ModelAPI_Feature> aFeature;
92 if (!theHighlighted.empty())
93 aFeature = theHighlighted.front().feature();
95 if (aFeature && aFeature == feature()) { // continue the feature edit
100 emit featureConstructed(feature(), FM_Deactivation);
102 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
108 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
110 if (!(theEvent->buttons() & Qt::LeftButton))
113 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
115 if (myCurPoint.myIsInitialized) {
117 PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
120 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
122 double aDeltaX = aX - aCurX;
123 double aDeltaY = anY - aCurY;
125 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
126 moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
128 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
129 for (; anIt != aLast; anIt++) {
130 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
131 if (!aFeature || aFeature == feature())
133 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
134 moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
139 myCurPoint.setPoint(aPoint);
142 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
143 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
144 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
146 std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
147 if (myFeatures.size() == 1) {
148 blockSelection(false);
152 std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
153 for (; anIt != aLast; anIt++) {
154 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
156 emit featureConstructed(aFeature, FM_Deactivation);
161 void PartSet_OperationEditLine::startOperation()
163 // do nothing in order to do not create a new feature
164 emit multiSelectionEnabled(false);
166 blockSelection(true);
171 void PartSet_OperationEditLine::stopOperation()
173 emit multiSelectionEnabled(true);
175 blockSelection(false, myFeatures.size() > 1);
180 void PartSet_OperationEditLine::blockSelection(bool isBlocked, const bool isRestoreSelection)
183 emit setSelection(std::list<XGUI_ViewerPrs>());
184 emit stopSelection(myFeatures, true);
187 emit stopSelection(myFeatures, false);
188 if (isRestoreSelection)
189 emit setSelection(myFeatures);
193 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
195 // do nothing in order to do not create a new feature
196 return boost::shared_ptr<ModelAPI_Feature>();
199 void PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
200 double theDeltaX, double theDeltaY,
201 const std::string& theAttribute)
203 if (!theFeature || theFeature->getKind() != "SketchLine")
206 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
207 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
208 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
210 aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
213 void PartSet_OperationEditLine::sendFeatures()
215 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures;
216 std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
217 for (; anIt != aLast; anIt++) {
218 boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
219 if (!aFeature || aFeature == feature())
223 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetEditEvent");
224 Model_FeaturesMovedMessage aMessage;
225 aMessage.setFeatures(aFeatures);
226 Events_Loop::loop()->send(aMessage);