Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_OperationEditFeature.cpp
1 // File:        PartSet_OperationEditFeature.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationEditFeature.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_OperationSketch.h>
8
9 #include <ModuleBase_OperationDescription.h>
10 #include <Model_Events.h>
11
12 #include <XGUI_ViewerPrs.h>
13
14 #include <SketchPlugin_Feature.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
18
19 #include <Model_Events.h>
20
21 #include <Events_Loop.h>
22
23 #include <SketchPlugin_Line.h>
24
25 #include <V3d_View.hxx>
26
27 #ifdef _DEBUG
28 #include <QDebug>
29 #endif
30
31 #include <QMouseEvent>
32
33 using namespace std;
34
35 PartSet_OperationEditFeature::PartSet_OperationEditFeature(const QString& theId,
36                                                   QObject* theParent,
37                                               FeaturePtr theFeature)
38 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myIsBlockedSelection(false)
39 {
40 }
41
42 PartSet_OperationEditFeature::~PartSet_OperationEditFeature()
43 {
44 }
45
46 bool PartSet_OperationEditFeature::isGranted(ModuleBase_IOperation* theOperation) const
47 {
48   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
49 }
50
51 std::list<int> PartSet_OperationEditFeature::getSelectionModes(FeaturePtr theFeature) const
52 {
53   return PartSet_OperationSketchBase::getSelectionModes(theFeature);
54 }
55
56 void PartSet_OperationEditFeature::init(FeaturePtr theFeature,
57                                      const std::list<XGUI_ViewerPrs>& theSelected,
58                                      const std::list<XGUI_ViewerPrs>& theHighlighted)
59 {
60   setEditingFeature(theFeature);
61
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();
71     }
72     if (!isSelected)
73       myFeatures = theHighlighted;
74     else
75       myFeatures = theSelected;
76   }
77   else
78     myFeatures = theSelected;
79 }
80
81 FeaturePtr PartSet_OperationEditFeature::sketch() const
82 {
83   return mySketch;
84 }
85
86 void PartSet_OperationEditFeature::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
87                                              const std::list<XGUI_ViewerPrs>& /*theSelected*/,
88                                              const std::list<XGUI_ViewerPrs>& theHighlighted)
89 {
90   if (myFeatures.size() == 1)
91   {
92     FeaturePtr aFeature;
93     if (!theHighlighted.empty())
94       aFeature = theHighlighted.front().feature();
95
96     if (aFeature && aFeature == feature()) { // continue the feature edit
97     }
98     else {
99       XGUI_ViewerPrs aFeaturePrs = myFeatures.front();
100       commit();
101       emit featureConstructed(feature(), FM_Deactivation);
102
103       bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
104       if(aHasShift && !theHighlighted.empty()) {
105         std::list<XGUI_ViewerPrs> aSelected;
106         aSelected.push_back(aFeaturePrs);
107         aSelected.push_back(theHighlighted.front());
108         emit setSelection(aSelected);
109       }
110       else if (aFeature) {
111         restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
112       }
113     }
114   }
115 }
116
117 void PartSet_OperationEditFeature::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
118 {
119   if (!(theEvent->buttons() &  Qt::LeftButton))
120     return;
121
122   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
123
124   blockSelection(true);
125   if (myCurPoint.myIsInitialized) {
126     double aCurX, aCurY;
127     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
128
129     double aX, anY;
130     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
131
132     double aDeltaX = aX - aCurX;
133     double aDeltaY = anY - aCurY;
134
135     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
136                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(feature());
137     aSketchFeature->move(aDeltaX, aDeltaY);
138
139     std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
140     for (; anIt != aLast; anIt++) {
141       FeaturePtr aFeature = (*anIt).feature();
142       if (!aFeature || aFeature == feature())
143         continue;
144       aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
145       aSketchFeature->move(aDeltaX, aDeltaY);
146     }
147   }
148   sendFeatures();
149
150   myCurPoint.setPoint(aPoint);
151 }
152
153 void PartSet_OperationEditFeature::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
154                                               const std::list<XGUI_ViewerPrs>& /*theSelected*/,
155                                               const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
156 {
157   std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
158   if (myFeatures.size() == 1) {
159     blockSelection(false);
160   }
161   else {
162     commit();
163     std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
164     for (; anIt != aLast; anIt++) {
165       FeaturePtr aFeature = (*anIt).feature();
166       if (aFeature)
167         emit featureConstructed(aFeature, FM_Deactivation);
168     }
169   }
170 }
171
172 void PartSet_OperationEditFeature::startOperation()
173 {
174   PartSet_OperationSketchBase::startOperation();
175   emit multiSelectionEnabled(false);
176
177   if (myFeatures.size() > 1)
178     blockSelection(true);
179
180   myCurPoint.clear();
181 }
182
183 void PartSet_OperationEditFeature::stopOperation()
184 {
185   emit multiSelectionEnabled(true);
186
187   blockSelection(false, myFeatures.size() > 1);
188
189   myFeatures.clear();
190 }
191
192 void PartSet_OperationEditFeature::blockSelection(bool isBlocked, const bool isRestoreSelection)
193 {
194   if (myIsBlockedSelection == isBlocked)
195     return;
196
197   myIsBlockedSelection = isBlocked;
198   if (isBlocked) {
199     emit setSelection(std::list<XGUI_ViewerPrs>());
200     emit stopSelection(myFeatures, true);
201   }
202   else {
203     emit stopSelection(myFeatures, false);
204     if (isRestoreSelection)
205       emit setSelection(myFeatures);
206   }
207 }
208
209 FeaturePtr PartSet_OperationEditFeature::createFeature(const bool /*theFlushMessage*/)
210 {
211   // do nothing in order to do not create a new feature
212   return FeaturePtr();
213 }
214
215 void PartSet_OperationEditFeature::sendFeatures()
216 {
217   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
218
219   std::list<FeaturePtr > aFeatures;
220   std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
221   for (; anIt != aLast; anIt++) {
222     FeaturePtr aFeature = (*anIt).feature();
223     if (!aFeature)
224       continue;
225
226     Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
227     Events_Loop::loop()->send(aMessage);
228   }
229   Events_Loop::loop()->flush(anEvent);
230   flushUpdated();
231 }
232