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