Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureEditMulti.cpp
1 // File:        PartSet_OperationFeatureEditMulti.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureEditMulti.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_OperationSketch.h>
8
9 #include <ModuleBase_OperationDescription.h>
10 #include <ModuleBase_ViewerPrs.h>
11
12 #include <ModelAPI_Events.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 <ModelAPI_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_OperationFeatureEditMulti::PartSet_OperationFeatureEditMulti(const QString& theId,
36                                                                      QObject* theParent,
37                                                                      CompositeFeaturePtr theFeature)
38     : PartSet_OperationSketchBase(theId, theParent),
39       mySketch(theFeature),
40       myIsBlockedSelection(false)
41 {
42   myIsEditing = true;
43 }
44
45 PartSet_OperationFeatureEditMulti::~PartSet_OperationFeatureEditMulti()
46 {
47 }
48
49
50 bool isContains(const std::list<ModuleBase_ViewerPrs>& theSelected, const ModuleBase_ViewerPrs& thePrs)
51 {
52   std::list<ModuleBase_ViewerPrs>::const_iterator anIt;
53   for (anIt = theSelected.cbegin(); anIt != theSelected.cend(); ++anIt) {
54     if ((*anIt).object() == thePrs.object())
55       return true;
56   }
57   return false;
58 }
59
60
61 void PartSet_OperationFeatureEditMulti::initSelection(
62     const std::list<ModuleBase_ViewerPrs>& theSelected,
63     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
64 {
65   //if (!theHighlighted.empty()) {
66   //  // if there is highlighted object, we check whether it is in the list of selected objects
67   //  // in that case this object is a handle of the moved lines. If there no such object in the selection,
68   //  // the hightlighted object should moved and the selection is skipped. The skipped selection will be
69   //  // deselected in the viewer by blockSelection signal in the startOperation method.
70   //  bool isSelected = false;
71   //  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(), 
72   //    aLast = theSelected.end();
73   //  for (; anIt != aLast && !isSelected; anIt++) {
74   //    isSelected = ModelAPI_Feature::feature((*anIt).object()) == feature();
75   //  }
76   //  if (!isSelected)
77   //    myFeatures = theHighlighted;
78   //  else
79   //    myFeatures = theSelected;
80   //} else
81   myFeatures = theSelected;
82   // add highlighted elements if they are not selected
83   std::list<ModuleBase_ViewerPrs>::const_iterator anIt;
84   for (anIt = theHighlighted.cbegin(); anIt != theHighlighted.cend(); ++anIt) {
85     if (!isContains(myFeatures, (*anIt)))
86       myFeatures.push_back(*anIt);
87   }
88   // Remove current feature if it is in the list (it will be moved as main feature)
89   FeaturePtr aFea = feature();
90   for (anIt = myFeatures.cbegin(); anIt != myFeatures.cend(); ++anIt) {
91     FeaturePtr aF = ModelAPI_Feature::feature((*anIt).object());
92     if (ModelAPI_Feature::feature((*anIt).object()) == feature()) {
93       myFeatures.erase(anIt);
94       break;
95     }
96   }
97 }
98
99 CompositeFeaturePtr PartSet_OperationFeatureEditMulti::sketch() const
100 {
101   return mySketch;
102 }
103
104 void PartSet_OperationFeatureEditMulti::mousePressed(
105     QMouseEvent* theEvent, Handle(V3d_View) theView,
106     const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
107     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
108 {
109 }
110
111 void PartSet_OperationFeatureEditMulti::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
112 {
113   if (!(theEvent->buttons() & Qt::LeftButton))
114     return;
115
116   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
117
118   blockSelection(true);
119   if (myCurPoint.myIsInitialized) {
120     double aCurX, aCurY;
121     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
122
123     double aX, anY;
124     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
125
126     double aDeltaX = aX - aCurX;
127     double aDeltaY = anY - aCurY;
128
129     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
130         SketchPlugin_Feature>(feature());
131     aSketchFeature->move(aDeltaX, aDeltaY);
132
133     std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), 
134       aLast = myFeatures.end();
135     for (; anIt != aLast; anIt++) {
136       ObjectPtr aObject = (*anIt).object();
137       if (!aObject || aObject == feature())
138         continue;
139       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
140       if (aFeature) {
141         aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
142         if (aSketchFeature)
143           aSketchFeature->move(aDeltaX, aDeltaY);
144       }
145     }
146   }
147   sendFeatures();
148
149   myCurPoint.setPoint(aPoint);
150 }
151
152 void PartSet_OperationFeatureEditMulti::mouseReleased(
153     QMouseEvent* theEvent, Handle(V3d_View) theView,
154     const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
155     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
156 {
157   if (commit()) {
158     std::list<ModuleBase_ViewerPrs> aFeatures = myFeatures;
159     std::list<ModuleBase_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast =
160         aFeatures.end();
161     for (; anIt != aLast; anIt++) {
162       ObjectPtr aFeature = (*anIt).object();
163       if (aFeature) {
164         emit featureConstructed(aFeature, FM_Deactivation);
165       }
166     }
167   }
168 }
169
170 void PartSet_OperationFeatureEditMulti::startOperation()
171 {
172   PartSet_OperationSketchBase::startOperation();
173   emit multiSelectionEnabled(false);
174
175   blockSelection(true);
176
177   myCurPoint.clear();
178 }
179
180 void PartSet_OperationFeatureEditMulti::stopOperation()
181 {
182   emit multiSelectionEnabled(true);
183
184   blockSelection(false, true);
185
186   myFeatures.clear();
187 }
188
189 void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked,
190                                                        const bool isRestoreSelection)
191 {
192   if (myIsBlockedSelection == isBlocked)
193     return;
194
195   myIsBlockedSelection = isBlocked;
196   QList<ObjectPtr> aFeatureList;
197   std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast =
198       myFeatures.end();
199   /*for(; anIt != aLast; anIt++)
200    aFeatureList.append((*anIt).feature());*/
201   if (isBlocked) {
202     emit setSelection(QList<ObjectPtr>());
203     emit stopSelection(aFeatureList, true);
204   } else {
205     emit stopSelection(aFeatureList, false);
206     if (isRestoreSelection) {
207       emit setSelection(aFeatureList);
208     }
209   }
210 }
211
212 void PartSet_OperationFeatureEditMulti::sendFeatures()
213 {
214   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
215
216   std::list<FeaturePtr> aFeatures;
217   std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast =
218       myFeatures.end();
219   for (; anIt != aLast; anIt++) {
220     ObjectPtr aFeature = (*anIt).object();
221     if (!aFeature)
222       continue;
223
224     ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
225   }
226   Events_Loop::loop()->flush(anEvent);
227   flushUpdated();
228 }
229