]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureEdit.cpp
Salome HOME
887ec4f9ce8cf51a6afa43e3dfa1a214a9db16ab
[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_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(ObjectPtr 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<ModuleBase_ViewerPrs>& theSelected,
74                                              const std::list<ModuleBase_ViewerPrs>& theHighlighted)
75 {
76   ObjectPtr aObject;
77   if (!theHighlighted.empty())
78     aObject = theHighlighted.front().object();
79   if (!aObject && !theSelected.empty()) // changed for a constrain
80     aObject = theSelected.front().object();
81
82   FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
83   if (!aFeature || aFeature != feature()) {
84     if (commit()) {
85       emit featureConstructed(feature(), FM_Deactivation);
86
87       bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
88       if(aHasShift && !theHighlighted.empty()) {
89         QList<ObjectPtr> aSelected;
90         std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
91         for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
92           aSelected.append((*aIt).object());
93         /*for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
94           if (!aSelected.contains((*aIt).object()))
95             aSelected.append((*aIt).object());
96         }*/
97         //aSelected.push_back(feature());
98         //aSelected.push_back(theHighlighted.front().object());
99         emit setSelection(aSelected);
100       }
101       else if (aFeature) {
102         restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
103       }
104     }
105   }
106 }
107
108 void PartSet_OperationFeatureEdit::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
109 {
110   if (!(theEvent->buttons() &  Qt::LeftButton))
111     return;
112
113   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
114
115   blockSelection(true);
116   if (myCurPoint.myIsInitialized) {
117     double aCurX, aCurY;
118     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
119
120     double aX, anY;
121     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
122
123     double aDeltaX = aX - aCurX;
124     double aDeltaY = anY - aCurY;
125
126     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
127                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(feature());
128     aSketchFeature->move(aDeltaX, aDeltaY);
129     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
130     ModelAPI_EventCreator::get()->sendUpdated(feature(), anEvent);
131   }
132   sendFeatures();
133
134   myCurPoint.setPoint(aPoint);
135 }
136
137 void PartSet_OperationFeatureEdit::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
138                                               const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
139                                               const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
140 {
141   blockSelection(false);
142 }
143
144 void PartSet_OperationFeatureEdit::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
145                                                     const std::list<ModuleBase_ViewerPrs>& theSelected,
146                                                     const std::list<ModuleBase_ViewerPrs>& theHighlighted)
147 {
148   // TODO the functionality is important only for constraint feature. Should be moved in another place
149   if (!theSelected.empty()) {
150     ModuleBase_ViewerPrs aFeaturePrs = theSelected.front();
151     if (!aFeaturePrs.owner().IsNull()) {
152       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(aFeaturePrs.owner());
153       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
154         bool isValid;
155         double aValue = PartSet_Tools::featureValue(feature(), SketchPlugin_Constraint::VALUE(), isValid);
156         if (isValid) {
157           ModuleBase_WidgetEditor::editFeatureValue(feature(), SketchPlugin_Constraint::VALUE());
158           flushUpdated();
159         }
160       }
161     }
162   }
163 }
164
165
166 void PartSet_OperationFeatureEdit::keyReleased(const int theKey)
167 {
168   if (theKey == Qt::Key_Return) {
169     commit();
170   } else 
171     PartSet_OperationSketchBase::keyReleased(theKey);
172 }
173
174 void PartSet_OperationFeatureEdit::startOperation()
175 {
176   PartSet_OperationSketchBase::startOperation();
177   emit multiSelectionEnabled(false);
178
179   myCurPoint.clear();
180 }
181
182 void PartSet_OperationFeatureEdit::stopOperation()
183 {
184   emit multiSelectionEnabled(true);
185
186   blockSelection(false, false);
187 }
188
189 void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
190 {
191   if (myIsBlockedSelection == isBlocked)
192     return;
193
194   myIsBlockedSelection = isBlocked;
195   QList<ObjectPtr> aFeatureList;
196   aFeatureList.append(feature());
197
198   if (isBlocked) {
199     emit setSelection(QList<ObjectPtr>());
200     emit stopSelection(aFeatureList, true);
201   }
202   else {
203     emit stopSelection(aFeatureList, false);
204     if (isRestoreSelection)
205       emit setSelection(aFeatureList);
206   }
207 }
208
209 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool /*theFlushMessage*/)
210 {
211   // do nothing in order to do not create a new feature
212   return FeaturePtr();
213 }
214
215 void PartSet_OperationFeatureEdit::sendFeatures()
216 {
217   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
218
219   FeaturePtr aFeature = feature();
220   ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
221
222   Events_Loop::loop()->flush(anEvent);
223   flushUpdated();
224 }
225