]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureEditMulti.cpp
Salome HOME
d1ee29448d7b9e1d4a44a660c9a15830691faefc
[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   //blockSelection(true);
114   if (myCurPoint.myIsInitialized) {
115     double aCurX, aCurY;
116     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), aView, aCurX, aCurY);
117
118     double aX, anY;
119     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
120
121     double aDeltaX = aX - aCurX;
122     double aDeltaY = anY - aCurY;
123
124     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
125         SketchPlugin_Feature>(feature());
126     aSketchFeature->move(aDeltaX, aDeltaY);
127
128     foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
129       ObjectPtr aObject = aPrs.object();
130       if (!aObject || aObject == feature())
131         continue;
132       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
133       if (aFeature) {
134         aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
135         if (aSketchFeature)
136           aSketchFeature->move(aDeltaX, aDeltaY);
137       }
138     }
139   }
140   sendFeatures();
141
142   myCurPoint.setPoint(aPoint);
143 }
144
145 void PartSet_OperationFeatureEditMulti::mouseReleased(
146     QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
147     ModuleBase_ISelection* theSelection)
148 {
149   if (commit()) {
150     foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
151       ObjectPtr aFeature = aPrs.object();
152       if (aFeature) {
153         emit featureConstructed(aFeature, FM_Deactivation);
154       }
155     }
156   }
157 }
158
159 void PartSet_OperationFeatureEditMulti::startOperation()
160 {
161   PartSet_OperationSketchBase::startOperation();
162   //emit multiSelectionEnabled(false);
163
164   //blockSelection(true);
165
166   myCurPoint.clear();
167 }
168
169 void PartSet_OperationFeatureEditMulti::stopOperation()
170 {
171   //emit multiSelectionEnabled(true);
172
173   //blockSelection(false, true);
174
175   myFeatures.clear();
176 }
177
178 //void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked,
179 //                                                       const bool isRestoreSelection)
180 //{
181 //  if (myIsBlockedSelection == isBlocked)
182 //    return;
183 //
184 //  myIsBlockedSelection = isBlocked;
185 //  QList<ObjectPtr> aFeatureList;
186 ////  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast =
187 ////      myFeatures.end();
188 //  /*for(; anIt != aLast; anIt++)
189 //   aFeatureList.append((*anIt).feature());*/
190 //  //if (isBlocked) {
191 //  //  emit setSelection(QList<ObjectPtr>());
192 //  //  emit stopSelection(aFeatureList, true);
193 //  //} else {
194 //  //  emit stopSelection(aFeatureList, false);
195 //  //  if (isRestoreSelection) {
196 //  //    emit setSelection(aFeatureList);
197 //  //  }
198 //  //}
199 //}
200
201 void PartSet_OperationFeatureEditMulti::sendFeatures()
202 {
203   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
204
205   foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
206     ObjectPtr aFeature = aPrs.object();
207     if (!aFeature)
208       continue;
209
210     ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
211   }
212   Events_Loop::loop()->flush(anEvent);
213   flushUpdated();
214 }
215