Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureEdit.cpp
1 // File:        PartSet_OperationFeatureEdit.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureEdit.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_OperationSketch.h>
8
9 #include <SketchPlugin_Constraint.h>
10
11 #include <ModuleBase_OperationDescription.h>
12 #include <ModuleBase_WidgetEditor.h>
13 #include <ModelAPI_Events.h>
14
15 #include <XGUI_ViewerPrs.h>
16
17 #include <SketchPlugin_Feature.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21
22 #include <ModelAPI_Events.h>
23
24 #include <Events_Loop.h>
25
26 #include <SketchPlugin_Line.h>
27
28 #include <V3d_View.hxx>
29 #include <AIS_DimensionOwner.hxx>
30 #include <AIS_DimensionSelectionMode.hxx>
31
32 #ifdef _DEBUG
33 #include <QDebug>
34 #endif
35
36 #include <QMouseEvent>
37
38 using namespace std;
39
40 PartSet_OperationFeatureEdit::PartSet_OperationFeatureEdit(const QString& theId,
41                                                   QObject* theParent,
42                                               FeaturePtr theFeature)
43 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myIsBlockedSelection(false)
44 {
45 }
46
47 PartSet_OperationFeatureEdit::~PartSet_OperationFeatureEdit()
48 {
49 }
50
51 bool PartSet_OperationFeatureEdit::isGranted(ModuleBase_IOperation* theOperation) const
52 {
53   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
54 }
55
56 std::list<int> PartSet_OperationFeatureEdit::getSelectionModes(FeaturePtr theFeature) const
57 {
58   return PartSet_OperationSketchBase::getSelectionModes(theFeature);
59 }
60
61 void PartSet_OperationFeatureEdit::initFeature(FeaturePtr theFeature)
62 {
63   setEditingFeature(theFeature);
64 }
65
66
67 FeaturePtr PartSet_OperationFeatureEdit::sketch() const
68 {
69   return mySketch;
70 }
71
72 void PartSet_OperationFeatureEdit::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
73                                              const std::list<XGUI_ViewerPrs>& theSelected,
74                                              const std::list<XGUI_ViewerPrs>& theHighlighted)
75 {
76   FeaturePtr aFeature;
77   if (!theHighlighted.empty())
78     aFeature = theHighlighted.front().feature();
79   if (!aFeature && !theSelected.empty()) // changed for a constrain
80     aFeature = theSelected.front().feature();
81
82   if (!aFeature || aFeature != feature())
83   {
84     commit();
85     emit featureConstructed(feature(), FM_Deactivation);
86
87     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
88     if(aHasShift && !theHighlighted.empty()) {
89       QFeatureList aSelected;
90       aSelected.push_back(feature());
91       aSelected.push_back(theHighlighted.front().feature());
92       emit setSelection(aSelected);
93     }
94     else if (aFeature) {
95       restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
96     }
97   }
98 }
99
100 void PartSet_OperationFeatureEdit::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
101 {
102   if (!(theEvent->buttons() &  Qt::LeftButton))
103     return;
104
105   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
106
107   blockSelection(true);
108   if (myCurPoint.myIsInitialized) {
109     double aCurX, aCurY;
110     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
111
112     double aX, anY;
113     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
114
115     double aDeltaX = aX - aCurX;
116     double aDeltaY = anY - aCurY;
117
118     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
119                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(feature());
120     aSketchFeature->move(aDeltaX, aDeltaY);
121   }
122   sendFeatures();
123
124   myCurPoint.setPoint(aPoint);
125 }
126
127 void PartSet_OperationFeatureEdit::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
128                                               const std::list<XGUI_ViewerPrs>& /*theSelected*/,
129                                               const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
130 {
131   blockSelection(false);
132 }
133
134 void PartSet_OperationFeatureEdit::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
135                                                     const std::list<XGUI_ViewerPrs>& theSelected,
136                                                     const std::list<XGUI_ViewerPrs>& theHighlighted)
137 {
138   // TODO the functionality is important only for constraint feature. Should be moved in another place
139   if (!theSelected.empty()) {
140     XGUI_ViewerPrs aFeaturePrs = theSelected.front();
141     if (!aFeaturePrs.owner().IsNull()) {
142       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(aFeaturePrs.owner());
143       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
144         bool isValid;
145         double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
146         if (isValid) {
147           ModuleBase_WidgetEditor::editFeatureValue(feature(), CONSTRAINT_ATTR_VALUE);
148           flushUpdated();
149         }
150       }
151     }
152   }
153 }
154
155 void PartSet_OperationFeatureEdit::startOperation()
156 {
157   PartSet_OperationSketchBase::startOperation();
158   emit multiSelectionEnabled(false);
159
160   myCurPoint.clear();
161 }
162
163 void PartSet_OperationFeatureEdit::stopOperation()
164 {
165   emit multiSelectionEnabled(true);
166
167   blockSelection(false, false);
168 }
169
170 void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
171 {
172   if (myIsBlockedSelection == isBlocked)
173     return;
174
175   myIsBlockedSelection = isBlocked;
176   QFeatureList aFeatureList;
177   aFeatureList.append(feature());
178
179   if (isBlocked) {
180     emit setSelection(QFeatureList());
181     emit stopSelection(aFeatureList, true);
182   }
183   else {
184     emit stopSelection(aFeatureList, false);
185     if (isRestoreSelection)
186       emit setSelection(aFeatureList);
187   }
188 }
189
190 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool /*theFlushMessage*/)
191 {
192   // do nothing in order to do not create a new feature
193   return FeaturePtr();
194 }
195
196 void PartSet_OperationFeatureEdit::sendFeatures()
197 {
198   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
199
200   FeaturePtr aFeature = feature();
201   ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
202
203   Events_Loop::loop()->flush(anEvent);
204   flushUpdated();
205 }
206