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