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