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