]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationEditConstraint.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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   setEditingFeature(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
149     if (feature()->getKind() == PartSet_ConstraintRadiusPrs::getKind()) {
150       boost::shared_ptr<PartSet_ConstraintRadiusPrs> anArcPrs =
151                               boost::dynamic_pointer_cast<PartSet_ConstraintRadiusPrs>(myFeaturePrs);
152       if (anArcPrs) {
153         anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
154       }
155     }
156     myFeaturePrs->setPoint(aX, anY, SM_LastPoint);
157   }
158   sendFeatures();
159
160   myCurPoint.setPoint(aPoint);
161 }
162
163 void PartSet_OperationEditConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
164                                               const std::list<XGUI_ViewerPrs>& /*theSelected*/,
165                                               const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
166 {
167   //std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
168   //if (myFeatures.size() == 1) {
169     blockSelection(false);
170   /*}
171   else {
172     commit();
173     std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
174     for (; anIt != aLast; anIt++) {
175       FeaturePtr aFeature = (*anIt).feature();
176       if (aFeature)
177         emit featureConstructed(aFeature, FM_Deactivation);
178     }
179   }*/
180 }
181
182 void PartSet_OperationEditConstraint::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
183                                                const std::list<XGUI_ViewerPrs>& theSelected,
184                                                const std::list<XGUI_ViewerPrs>& theHighlighted)
185 {
186   // changed
187   if (!theSelected.empty()) {
188     bool isValid;
189     double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
190     if (isValid) {
191       QPoint aPos = theEvent->globalPos();
192       myEditor->start(aPos, aValue);
193     }
194   }
195 }
196
197 void PartSet_OperationEditConstraint::startOperation()
198 {
199   PartSet_OperationSketchBase::startOperation();
200   emit multiSelectionEnabled(false);
201
202   //if (myFeatures.size() > 1)
203   //  blockSelection(true);
204
205   myCurPoint.clear();
206 }
207
208 void PartSet_OperationEditConstraint::stopOperation()
209 {
210   emit multiSelectionEnabled(true);
211
212   //blockSelection(false, myFeatures.size() > 1);
213
214   //myFeatures.clear();
215 }
216
217 void PartSet_OperationEditConstraint::blockSelection(bool isBlocked, const bool isRestoreSelection)
218 {
219   if (myIsBlockedSelection == isBlocked)
220     return;
221
222   myIsBlockedSelection = isBlocked;
223   if (isBlocked) {
224     emit setSelection(std::list<XGUI_ViewerPrs>());
225     //emit stopSelection(myFeatures, true);
226   }
227   else {
228     //emit stopSelection(myFeatures, false);
229     //if (isRestoreSelection)
230     //  emit setSelection(myFeatures);
231   }
232 }
233
234 FeaturePtr PartSet_OperationEditConstraint::createFeature(const bool /*theFlushMessage*/)
235 {
236   // do nothing in order to do not create a new feature
237   return FeaturePtr();
238 }
239
240 void PartSet_OperationEditConstraint::sendFeatures()
241 {
242   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
243
244   std::list<FeaturePtr> aFeatures;
245   aFeatures.push_back(feature());
246   std::list<FeaturePtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
247   //std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
248   for (; anIt != aLast; anIt++) {
249     FeaturePtr aFeature = (*anIt);//.feature();
250     if (!aFeature)
251       continue;
252
253     Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
254     Events_Loop::loop()->send(aMessage);
255   }
256   Events_Loop::loop()->flush(anEvent);
257   flushUpdated();
258 }
259
260 void PartSet_OperationEditConstraint::onEditStopped(double theValue)
261 {
262   PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
263
264   flushUpdated();
265   commit();
266   emit featureConstructed(feature(), FM_Deactivation);
267   //restartOperation(feature()->getKind(), FeaturePtr());
268 }