Salome HOME
Implementation of naming parametrisation of sketch result
[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 <ModuleBase_ViewerPrs.h>
14 #include <ModuleBase_IPropertyPanel.h>
15
16 #include <ModelAPI_Events.h>
17
18 #include <SketchPlugin_Feature.h>
19 #include <GeomDataAPI_Point2D.h>
20
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Events.h>
24
25 #include <Events_Loop.h>
26
27 #include <SketchPlugin_Line.h>
28
29 #include <V3d_View.hxx>
30 #include <AIS_DimensionOwner.hxx>
31 #include <AIS_DimensionSelectionMode.hxx>
32
33 #ifdef _DEBUG
34 #include <QDebug>
35 #endif
36
37 #include <QMouseEvent>
38
39 using namespace std;
40
41 PartSet_OperationFeatureEdit::PartSet_OperationFeatureEdit(const QString& theId,
42                                                            QObject* theParent,
43                                                            CompositeFeaturePtr theFeature)
44     : PartSet_OperationFeatureBase(theId, theParent, theFeature),
45       myIsBlockedSelection(false)
46 {
47   myIsEditing = true;
48 }
49
50 PartSet_OperationFeatureEdit::~PartSet_OperationFeatureEdit()
51 {
52 }
53
54
55 void PartSet_OperationFeatureEdit::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
56                                                 const std::list<ModuleBase_ViewerPrs>& theSelected,
57                                                 const std::list<ModuleBase_ViewerPrs>& theHighlighted)
58 {
59   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
60   if(aActiveWgt && aActiveWgt->isViewerSelector()) {
61     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
62     PartSet_OperationFeatureBase::mousePressed(theEvent, theView, theSelected, theHighlighted);
63     return;
64   }
65   ObjectPtr aObject;
66   if (!theHighlighted.empty())
67     aObject = theHighlighted.front().object();
68   if (!aObject && !theSelected.empty())  // changed for a constrain
69     aObject = theSelected.front().object();
70
71   FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
72   if (!aFeature || aFeature != feature()) {
73     if (commit()) {
74       emit featureConstructed(feature(), FM_Deactivation);
75
76       //bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
77       //if (aHasShift && !theHighlighted.empty()) {
78       //  QList<ObjectPtr> aSelected;
79       //  std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
80       //  for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
81       //    aSelected.append((*aIt).object());
82         /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
83          if (!aSelected.contains((*aIt).object()))
84          aSelected.append((*aIt).object());
85          }*/
86         //aSelected.push_back(feature());
87         //aSelected.push_back(theHighlighted.front().object());
88         //emit setSelection(aSelected);
89       //} else 
90       if (aFeature) {
91         restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
92       }
93     }
94   }
95 }
96
97 void PartSet_OperationFeatureEdit::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
98 {
99   if (!(theEvent->buttons() & Qt::LeftButton))
100     return;
101
102   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
103
104   blockSelection(true);
105   if (myCurPoint.myIsInitialized) {
106     double aCurX, aCurY;
107     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
108
109     double aX, anY;
110     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
111
112     double aDeltaX = aX - aCurX;
113     double aDeltaY = anY - aCurY;
114
115     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
116         SketchPlugin_Feature>(feature());
117     // MPV: added condition because it could be external edge of some object, not sketch
118     if (aSketchFeature && aSketchFeature->sketch() == sketch().get()) {
119       aSketchFeature->move(aDeltaX, aDeltaY);
120       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
121       ModelAPI_EventCreator::get()->sendUpdated(feature(), anEvent);
122     }
123   }
124   sendFeatures();
125
126   myCurPoint.setPoint(aPoint);
127 }
128
129 void PartSet_OperationFeatureEdit::mouseReleased(
130     QMouseEvent* theEvent, Handle(V3d_View) theView,
131     const std::list<ModuleBase_ViewerPrs>& theSelected,
132     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
133 {
134   ModuleBase_ModelWidget* aActiveWgt = 0;
135   if (myPropertyPanel)
136     aActiveWgt = myPropertyPanel->activeWidget();
137   if(aActiveWgt && aActiveWgt->isViewerSelector()) {
138     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
139     PartSet_OperationFeatureBase::mouseReleased(theEvent, theView, theSelected, theHighlighted);
140   } else {
141     blockSelection(false);
142   }
143 }
144
145 void PartSet_OperationFeatureEdit::mouseDoubleClick(
146     QMouseEvent* theEvent, Handle_V3d_View theView,
147     const std::list<ModuleBase_ViewerPrs>& theSelected,
148     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
149 {
150   // TODO the functionality is important only for constraint feature. Should be moved in another place
151   if (!theSelected.empty()) {
152     ModuleBase_ViewerPrs aFeaturePrs = theSelected.front();
153     if (!aFeaturePrs.owner().IsNull()) {
154       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(
155           aFeaturePrs.owner());
156       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
157         bool isValid;
158         double aValue = PartSet_Tools::featureValue(feature(), SketchPlugin_Constraint::VALUE(),
159                                                     isValid);
160         if (isValid) {
161           ModuleBase_WidgetEditor::editFeatureValue(feature(), SketchPlugin_Constraint::VALUE());
162           flushUpdated();
163         }
164       }
165     }
166   }
167 }
168
169 void PartSet_OperationFeatureEdit::startOperation()
170 {
171   PartSet_OperationSketchBase::startOperation();
172   emit multiSelectionEnabled(false);
173
174   myCurPoint.clear();
175 }
176
177 void PartSet_OperationFeatureEdit::stopOperation()
178 {
179   emit multiSelectionEnabled(true);
180
181   blockSelection(false, false);
182 }
183
184 void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
185 {
186   if (myIsBlockedSelection == isBlocked)
187     return;
188
189   myIsBlockedSelection = isBlocked;
190   QList<ObjectPtr> aFeatureList;
191   aFeatureList.append(feature());
192
193   if (isBlocked) {
194     emit setSelection(QList<ObjectPtr>());
195     emit stopSelection(aFeatureList, true);
196   } else {
197     emit stopSelection(aFeatureList, false);
198     if (isRestoreSelection)
199       emit setSelection(aFeatureList);
200   }
201 }
202
203 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool theFlushMessage,
204   CompositeFeaturePtr theCompositeFeature)
205 {
206   // do nothing in order to do not create a new feature
207   return FeaturePtr();
208 }
209
210 void PartSet_OperationFeatureEdit::sendFeatures()
211 {
212   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
213
214   FeaturePtr aFeature = feature();
215   ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
216
217   Events_Loop::loop()->flush(anEvent);
218   flushUpdated();
219 }
220