]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureEditMulti.cpp
Salome HOME
Provide preselection
[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 #include <ModuleBase_ISelection.h>
13
14 #include <ModelAPI_Events.h>
15
16 #include <SketchPlugin_Feature.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Document.h>
20
21 #include <ModelAPI_Events.h>
22
23 #include <Events_Loop.h>
24
25 #include <SketchPlugin_Line.h>
26
27 #include <V3d_View.hxx>
28
29 #include <QMouseEvent>
30 #ifdef _DEBUG
31 #include <QDebug>
32 #endif
33
34 //using namespace std;
35
36 PartSet_OperationFeatureEditMulti::PartSet_OperationFeatureEditMulti(const QString& theId,
37                                                                      QObject* theParent,
38                                                                      CompositeFeaturePtr theFeature)
39     : PartSet_OperationSketchBase(theId, theParent),
40       mySketch(theFeature),
41       myIsBlockedSelection(false)
42 {
43   myIsEditing = true;
44 }
45
46 PartSet_OperationFeatureEditMulti::~PartSet_OperationFeatureEditMulti()
47 {
48 }
49
50
51 bool isContains(const QList<ModuleBase_ViewerPrs>& theSelected, const ModuleBase_ViewerPrs& thePrs)
52 {
53   foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
54     if (aPrs.object() == thePrs.object())
55       return true;
56   }
57   return false;
58 }
59
60
61 void PartSet_OperationFeatureEditMulti::initSelection(ModuleBase_ISelection* theSelection)
62 {
63   //if (!theHighlighted.empty()) {
64   //  // if there is highlighted object, we check whether it is in the list of selected objects
65   //  // in that case this object is a handle of the moved lines. If there no such object in the selection,
66   //  // the hightlighted object should moved and the selection is skipped. The skipped selection will be
67   //  // deselected in the viewer by blockSelection signal in the startOperation method.
68   //  bool isSelected = false;
69   //  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(), 
70   //    aLast = theSelected.end();
71   //  for (; anIt != aLast && !isSelected; anIt++) {
72   //    isSelected = ModelAPI_Feature::feature((*anIt).object()) == feature();
73   //  }
74   //  if (!isSelected)
75   //    myFeatures = theHighlighted;
76   //  else
77   //    myFeatures = theSelected;
78   //} else
79   myFeatures = theSelection->getSelected();
80   QList<ModuleBase_ViewerPrs> aHighlighted = theSelection->getHighlighted();
81   // add highlighted elements if they are not selected
82   foreach (ModuleBase_ViewerPrs aPrs, aHighlighted) {
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   if (theViewer->isSelectionEnabled())
111     theViewer->enableSelection(false);
112
113   Handle(V3d_View) aView = theViewer->activeView();
114   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
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   theViewer->enableSelection(true);
153   if (commit()) {
154     foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
155       ObjectPtr aFeature = aPrs.object();
156       if (aFeature) {
157         emit featureConstructed(aFeature, FM_Deactivation);
158       }
159     }
160   }
161 }
162
163 void PartSet_OperationFeatureEditMulti::startOperation()
164 {
165   PartSet_OperationSketchBase::startOperation();
166   //emit multiSelectionEnabled(false);
167
168   //blockSelection(true);
169
170   myCurPoint.clear();
171 }
172
173 void PartSet_OperationFeatureEditMulti::stopOperation()
174 {
175   //emit multiSelectionEnabled(true);
176
177   //blockSelection(false, true);
178
179   myFeatures.clear();
180 }
181
182 //void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked,
183 //                                                       const bool isRestoreSelection)
184 //{
185 //  if (myIsBlockedSelection == isBlocked)
186 //    return;
187 //
188 //  myIsBlockedSelection = isBlocked;
189 //  QList<ObjectPtr> aFeatureList;
190 ////  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast =
191 ////      myFeatures.end();
192 //  /*for(; anIt != aLast; anIt++)
193 //   aFeatureList.append((*anIt).feature());*/
194 //  //if (isBlocked) {
195 //  //  emit setSelection(QList<ObjectPtr>());
196 //  //  emit stopSelection(aFeatureList, true);
197 //  //} else {
198 //  //  emit stopSelection(aFeatureList, false);
199 //  //  if (isRestoreSelection) {
200 //  //    emit setSelection(aFeatureList);
201 //  //  }
202 //  //}
203 //}
204
205 void PartSet_OperationFeatureEditMulti::sendFeatures()
206 {
207   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
208
209   foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
210     ObjectPtr aFeature = aPrs.object();
211     if (!aFeature)
212       continue;
213
214     ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
215   }
216   Events_Loop::loop()->flush(anEvent);
217   flushUpdated();
218 }
219