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