Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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
15 #include <ModelAPI_Events.h>
16
17 #include <SketchPlugin_Feature.h>
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
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_OperationFeatureBase(theId, theParent, theFeature),
44       myIsBlockedSelection(false)
45 {
46 }
47
48 PartSet_OperationFeatureEdit::~PartSet_OperationFeatureEdit()
49 {
50 }
51
52 void PartSet_OperationFeatureEdit::initFeature(FeaturePtr theFeature)
53 {
54   setEditingFeature(theFeature);
55 }
56
57 void PartSet_OperationFeatureEdit::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
58                                                 const std::list<ModuleBase_ViewerPrs>& theSelected,
59                                                 const std::list<ModuleBase_ViewerPrs>& theHighlighted)
60 {
61   if(myActiveWidget && myActiveWidget->isViewerSelector()) {
62     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
63     PartSet_OperationFeatureBase::mousePressed(theEvent, theView, theSelected, theHighlighted);
64     return;
65   }
66   ObjectPtr aObject;
67   if (!theHighlighted.empty())
68     aObject = theHighlighted.front().object();
69   if (!aObject && !theSelected.empty())  // changed for a constrain
70     aObject = theSelected.front().object();
71
72   FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
73   if (!aFeature || aFeature != feature()) {
74     if (commit()) {
75       emit featureConstructed(feature(), FM_Deactivation);
76
77       bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
78       if (aHasShift && !theHighlighted.empty()) {
79         QList<ObjectPtr> aSelected;
80         std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
81         for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
82           aSelected.append((*aIt).object());
83         /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
84          if (!aSelected.contains((*aIt).object()))
85          aSelected.append((*aIt).object());
86          }*/
87         //aSelected.push_back(feature());
88         //aSelected.push_back(theHighlighted.front().object());
89         emit setSelection(aSelected);
90       } else 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     aSketchFeature->move(aDeltaX, aDeltaY);
118     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
119     ModelAPI_EventCreator::get()->sendUpdated(feature(), anEvent);
120   }
121   sendFeatures();
122
123   myCurPoint.setPoint(aPoint);
124 }
125
126 void PartSet_OperationFeatureEdit::mouseReleased(
127     QMouseEvent* theEvent, Handle(V3d_View) theView,
128     const std::list<ModuleBase_ViewerPrs>& theSelected,
129     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
130 {
131   if(myActiveWidget && myActiveWidget->isViewerSelector()) {
132     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
133     PartSet_OperationFeatureBase::mouseReleased(theEvent, theView, theSelected, theHighlighted);
134   } else {
135     blockSelection(false);
136   }
137 }
138
139 void PartSet_OperationFeatureEdit::mouseDoubleClick(
140     QMouseEvent* theEvent, Handle_V3d_View theView,
141     const std::list<ModuleBase_ViewerPrs>& theSelected,
142     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
143 {
144   // TODO the functionality is important only for constraint feature. Should be moved in another place
145   if (!theSelected.empty()) {
146     ModuleBase_ViewerPrs aFeaturePrs = theSelected.front();
147     if (!aFeaturePrs.owner().IsNull()) {
148       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(
149           aFeaturePrs.owner());
150       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
151         bool isValid;
152         double aValue = PartSet_Tools::featureValue(feature(), SketchPlugin_Constraint::VALUE(),
153                                                     isValid);
154         if (isValid) {
155           ModuleBase_WidgetEditor::editFeatureValue(feature(), SketchPlugin_Constraint::VALUE());
156           flushUpdated();
157         }
158       }
159     }
160   }
161 }
162
163 void PartSet_OperationFeatureEdit::startOperation()
164 {
165   PartSet_OperationSketchBase::startOperation();
166   emit multiSelectionEnabled(false);
167
168   myCurPoint.clear();
169 }
170
171 void PartSet_OperationFeatureEdit::stopOperation()
172 {
173   emit multiSelectionEnabled(true);
174
175   blockSelection(false, false);
176 }
177
178 void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
179 {
180   if (myIsBlockedSelection == isBlocked)
181     return;
182
183   myIsBlockedSelection = isBlocked;
184   QList<ObjectPtr> aFeatureList;
185   aFeatureList.append(feature());
186
187   if (isBlocked) {
188     emit setSelection(QList<ObjectPtr>());
189     emit stopSelection(aFeatureList, true);
190   } else {
191     emit stopSelection(aFeatureList, false);
192     if (isRestoreSelection)
193       emit setSelection(aFeatureList);
194   }
195 }
196
197 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool /*theFlushMessage*/)
198 {
199   // do nothing in order to do not create a new feature
200   return FeaturePtr();
201 }
202
203 void PartSet_OperationFeatureEdit::sendFeatures()
204 {
205   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
206
207   FeaturePtr aFeature = feature();
208   ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
209
210   Events_Loop::loop()->flush(anEvent);
211   flushUpdated();
212 }
213