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