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