Salome HOME
d617b365aa4f6efbb4be8ca54a7fdc818c9a7356
[modules/shaper.git] / src / PartSet / PartSet_OperationEditConstraint.cpp
1 // File:        PartSet_OperationEditConstraint.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationEditConstraint.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_OperationSketch.h>
8 #include <PartSet_EditLine.h>
9 #include <PartSet_FeaturePrs.h>
10 #include <PartSet_ConstraintRadiusPrs.h>
11
12 #include <SketchPlugin_Constraint.h>
13
14 #include <ModuleBase_OperationDescription.h>
15 #include <Model_Events.h>
16
17 #include <XGUI_ViewerPrs.h>
18
19 #include <SketchPlugin_Feature.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Document.h>
23
24 #include <Model_Events.h>
25
26 #include <Events_Loop.h>
27
28 #include <SketchPlugin_Line.h>
29
30 #include <V3d_View.hxx>
31 #include <AIS_LengthDimension.hxx>
32
33 #ifdef _DEBUG
34 #include <QDebug>
35 #endif
36
37 #include <QMouseEvent>
38
39 using namespace std;
40
41 PartSet_OperationEditConstraint::PartSet_OperationEditConstraint(const QString& theId,
42                                               const std::string& theFeatureKind,
43                                                   QObject* theParent,
44                                               FeaturePtr theFeature)
45 : PartSet_OperationSketchBase(theId, theParent), myIsBlockedSelection(false)
46 {
47   myFeaturePrs = PartSet_Tools::createFeaturePrs(theFeatureKind, theFeature);
48
49   // changed
50   myEditor = new PartSet_EditLine(0);
51   connect(myEditor, SIGNAL(stopped(double)), this, SLOT(onEditStopped(double)));
52 }
53
54 PartSet_OperationEditConstraint::~PartSet_OperationEditConstraint()
55 {
56 }
57
58 bool PartSet_OperationEditConstraint::isGranted(ModuleBase_IOperation* theOperation) const
59 {
60   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
61 }
62
63 std::list<int> PartSet_OperationEditConstraint::getSelectionModes(FeaturePtr theFeature) const
64 {
65   return PartSet_OperationSketchBase::getSelectionModes(theFeature);
66 }
67
68 void PartSet_OperationEditConstraint::init(FeaturePtr theFeature,
69                                      const std::list<XGUI_ViewerPrs>& theSelected,
70                                      const std::list<XGUI_ViewerPrs>& theHighlighted)
71 {
72   setFeature(theFeature);
73   myFeaturePrs->init(theFeature);
74
75   /*
76   if (!theHighlighted.empty()) {
77     // if there is highlighted object, we check whether it is in the list of selected objects
78     // in that case this object is a handle of the moved lines. If there no such object in the selection,
79     // the hightlighted object should moved and the selection is skipped. The skipped selection will be
80     // deselected in the viewer by blockSelection signal in the startOperation method.
81     bool isSelected = false;
82     std::list<XGUI_ViewerPrs>::const_iterator anIt = theSelected.begin(), aLast = theSelected.end();
83     for (; anIt != aLast && !isSelected; anIt++) {
84       isSelected = (*anIt).feature() == feature();
85     }
86     if (!isSelected)
87       myFeatures = theHighlighted;
88     else
89       myFeatures = theSelected;
90   }
91   else
92     myFeatures = theSelected;*/
93 }
94
95 FeaturePtr PartSet_OperationEditConstraint::sketch() const
96 {
97   return myFeaturePrs->sketch();
98 }
99
100 void PartSet_OperationEditConstraint::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
101                                              const std::list<XGUI_ViewerPrs>& theSelected,
102                                              const std::list<XGUI_ViewerPrs>& theHighlighted)
103 {
104   //if (myFeatures.size() == 1)
105   {
106     FeaturePtr aFeature;
107     if (!theHighlighted.empty())
108       aFeature = theHighlighted.front().feature();
109     if (!aFeature && !theSelected.empty()) // changed
110       aFeature = theSelected.front().feature();
111
112     if (aFeature && aFeature == feature()) { // continue the feature edit
113     }
114     else {
115       //XGUI_ViewerPrs aFeaturePrs = myFeatures.front();
116       commit();
117       emit featureConstructed(feature(), FM_Deactivation);
118
119       /*bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
120       if(aHasShift && !theHighlighted.empty()) {
121         std::list<XGUI_ViewerPrs> aSelected;
122         aSelected.push_back(aFeaturePrs);
123         aSelected.push_back(theHighlighted.front());
124         emit setSelection(aSelected);
125       }
126       else if (aFeature) {
127         restartOperation(PartSet_OperationEditConstraint::Type(), aFeature);
128       }*/
129     }
130   }
131 }
132
133 void PartSet_OperationEditConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
134 {
135   if (!(theEvent->buttons() &  Qt::LeftButton))
136     return;
137
138   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
139
140   blockSelection(true);
141   if (myCurPoint.myIsInitialized) {
142     double aCurX, aCurY;
143     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
144
145     double aX, anY;
146     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
147
148     /*double aDeltaX = aX - aCurX;
149     double aDeltaY = anY - aCurY;
150
151     PartSet_Tools::moveFeature(feature(), aDeltaX, aDeltaY);*/
152     if (feature()->getKind() == PartSet_ConstraintRadiusPrs::getKind()) {
153       boost::shared_ptr<PartSet_ConstraintRadiusPrs> anArcPrs =
154                               boost::dynamic_pointer_cast<PartSet_ConstraintRadiusPrs>(myFeaturePrs);
155       if (anArcPrs) {
156         anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
157       }
158     }
159     myFeaturePrs->setPoint(aX, anY, SM_SecondPoint);
160
161
162     /*std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
163     for (; anIt != aLast; anIt++) {
164       FeaturePtr aFeature = (*anIt).feature();
165       if (!aFeature || aFeature == feature())
166         continue;
167       PartSet_Tools::moveFeature(aFeature, aDeltaX, aDeltaY);
168     }*/
169   }
170   sendFeatures();
171
172   myCurPoint.setPoint(aPoint);
173 }
174
175 void PartSet_OperationEditConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
176                                               const std::list<XGUI_ViewerPrs>& /*theSelected*/,
177                                               const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
178 {
179   //std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
180   //if (myFeatures.size() == 1) {
181     blockSelection(false);
182   /*}
183   else {
184     commit();
185     std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
186     for (; anIt != aLast; anIt++) {
187       FeaturePtr aFeature = (*anIt).feature();
188       if (aFeature)
189         emit featureConstructed(aFeature, FM_Deactivation);
190     }
191   }*/
192 }
193
194 void PartSet_OperationEditConstraint::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
195                                                const std::list<XGUI_ViewerPrs>& theSelected,
196                                                const std::list<XGUI_ViewerPrs>& theHighlighted)
197 {
198   // changed
199   if (!theSelected.empty()) {
200     bool isValid;
201     double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
202     if (isValid) {
203       QPoint aPos = theEvent->globalPos();
204       myEditor->start(aPos, aValue);
205     }
206   }
207 }
208
209 void PartSet_OperationEditConstraint::startOperation()
210 {
211   // do nothing in order to do not create a new feature
212   emit multiSelectionEnabled(false);
213
214   //if (myFeatures.size() > 1)
215   //  blockSelection(true);
216
217   myCurPoint.clear();
218 }
219
220 void PartSet_OperationEditConstraint::stopOperation()
221 {
222   emit multiSelectionEnabled(true);
223
224   //blockSelection(false, myFeatures.size() > 1);
225
226   //myFeatures.clear();
227 }
228
229 void PartSet_OperationEditConstraint::blockSelection(bool isBlocked, const bool isRestoreSelection)
230 {
231   if (myIsBlockedSelection == isBlocked)
232     return;
233
234   myIsBlockedSelection = isBlocked;
235   if (isBlocked) {
236     emit setSelection(std::list<XGUI_ViewerPrs>());
237     //emit stopSelection(myFeatures, true);
238   }
239   else {
240     //emit stopSelection(myFeatures, false);
241     //if (isRestoreSelection)
242     //  emit setSelection(myFeatures);
243   }
244 }
245
246 FeaturePtr PartSet_OperationEditConstraint::createFeature(const bool /*theFlushMessage*/)
247 {
248   // do nothing in order to do not create a new feature
249   return FeaturePtr();
250 }
251
252 void PartSet_OperationEditConstraint::sendFeatures()
253 {
254   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
255
256   std::list<FeaturePtr> aFeatures;
257   aFeatures.push_back(feature());
258   std::list<FeaturePtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
259   //std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
260   for (; anIt != aLast; anIt++) {
261     FeaturePtr aFeature = (*anIt);//.feature();
262     if (!aFeature)
263       continue;
264
265     Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
266     Events_Loop::loop()->send(aMessage);
267   }
268   Events_Loop::loop()->flush(anEvent);
269   flushUpdated();
270 }
271
272 void PartSet_OperationEditConstraint::onEditStopped(double theValue)
273 {
274   PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
275
276   flushUpdated();
277   commit();
278   emit featureConstructed(feature(), FM_Deactivation);
279   //restartOperation(feature()->getKind(), FeaturePtr());
280 }