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