Salome HOME
0e37baa770fb33885c1986c6fb803bb46f19dc0e
[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 #include <ModuleBase_IViewer.h>
12
13 #include <ModelAPI_Events.h>
14
15 #include <SketchPlugin_Feature.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19
20 #include <ModelAPI_Events.h>
21
22 #include <Events_Loop.h>
23
24 #include <SketchPlugin_Line.h>
25
26 #include <V3d_View.hxx>
27
28 #include <QMouseEvent>
29 #ifdef _DEBUG
30 #include <QDebug>
31 #endif
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 QList<ModuleBase_ViewerPrs>& theSelected, const ModuleBase_ViewerPrs& thePrs)
51 {
52   foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
53     if (aPrs.object() == thePrs.object())
54       return true;
55   }
56   return false;
57 }
58
59
60 void PartSet_OperationFeatureEditMulti::initSelection(
61     const QList<ModuleBase_ViewerPrs>& theSelected,
62     const QList<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   foreach (ModuleBase_ViewerPrs aPrs, theHighlighted) {
83     if (!isContains(myFeatures, aPrs))
84       myFeatures.append(aPrs);
85   }
86   // Remove current feature if it is in the list (it will be moved as main feature)
87   foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
88     FeaturePtr aF = ModelAPI_Feature::feature(aPrs.object());
89     if (ModelAPI_Feature::feature(aPrs.object()) == feature()) {
90       myFeatures.removeOne(aPrs);
91       break;
92     }
93   }
94 }
95
96 CompositeFeaturePtr PartSet_OperationFeatureEditMulti::sketch() const
97 {
98   return mySketch;
99 }
100
101 void PartSet_OperationFeatureEditMulti::mousePressed(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer, ModuleBase_ISelection* theSelection)
102 {
103 }
104
105 void PartSet_OperationFeatureEditMulti::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
106 {
107   if (!(theEvent->buttons() & Qt::LeftButton))
108     return;
109
110   Handle(V3d_View) aView = theViewer->activeView();
111   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
112
113   if (theViewer->isSelectionEnabled())
114     theViewer->enableSelection(false);
115
116   //blockSelection(true);
117   if (myCurPoint.myIsInitialized) {
118     double aCurX, aCurY;
119     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), aView, aCurX, aCurY);
120
121     double aX, anY;
122     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, 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     foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
132       ObjectPtr aObject = aPrs.object();
133       if (!aObject || aObject == feature())
134         continue;
135       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
136       if (aFeature) {
137         aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
138         if (aSketchFeature)
139           aSketchFeature->move(aDeltaX, aDeltaY);
140       }
141     }
142   }
143   sendFeatures();
144
145   myCurPoint.setPoint(aPoint);
146 }
147
148 void PartSet_OperationFeatureEditMulti::mouseReleased(
149     QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
150     ModuleBase_ISelection* theSelection)
151 {
152   if (commit()) {
153     foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
154       ObjectPtr aFeature = aPrs.object();
155       if (aFeature) {
156         emit featureConstructed(aFeature, FM_Deactivation);
157       }
158     }
159   }
160   if (!theViewer->isSelectionEnabled())
161     theViewer->enableSelection(true);
162 }
163
164 void PartSet_OperationFeatureEditMulti::startOperation()
165 {
166   PartSet_OperationSketchBase::startOperation();
167   //emit multiSelectionEnabled(false);
168
169   //blockSelection(true);
170
171   myCurPoint.clear();
172 }
173
174 void PartSet_OperationFeatureEditMulti::stopOperation()
175 {
176   //emit multiSelectionEnabled(true);
177
178   //blockSelection(false, true);
179
180   myFeatures.clear();
181 }
182
183 //void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked,
184 //                                                       const bool isRestoreSelection)
185 //{
186 //  if (myIsBlockedSelection == isBlocked)
187 //    return;
188 //
189 //  myIsBlockedSelection = isBlocked;
190 //  QList<ObjectPtr> aFeatureList;
191 ////  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast =
192 ////      myFeatures.end();
193 //  /*for(; anIt != aLast; anIt++)
194 //   aFeatureList.append((*anIt).feature());*/
195 //  //if (isBlocked) {
196 //  //  emit setSelection(QList<ObjectPtr>());
197 //  //  emit stopSelection(aFeatureList, true);
198 //  //} else {
199 //  //  emit stopSelection(aFeatureList, false);
200 //  //  if (isRestoreSelection) {
201 //  //    emit setSelection(aFeatureList);
202 //  //  }
203 //  //}
204 //}
205
206 void PartSet_OperationFeatureEditMulti::sendFeatures()
207 {
208   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
209
210   foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
211     ObjectPtr aFeature = aPrs.object();
212     if (!aFeature)
213       continue;
214
215     ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
216   }
217   Events_Loop::loop()->flush(anEvent);
218   flushUpdated();
219 }
220