]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureEdit.cpp
Salome HOME
Provide preselection
[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 #include <PartSet_OperationFeatureEditMulti.h>
9
10 #include <SketchPlugin_Constraint.h>
11
12 #include <ModuleBase_OperationDescription.h>
13 #include <ModuleBase_WidgetEditor.h>
14 #include <ModuleBase_ViewerPrs.h>
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_ISelection.h>
17 #include <ModuleBase_IViewer.h>
18
19 #include <ModelAPI_Events.h>
20
21 #include <SketchPlugin_Feature.h>
22 #include <GeomDataAPI_Point2D.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Events.h>
27
28 #include <Events_Loop.h>
29
30 #include <SketchPlugin_Line.h>
31
32 #include <V3d_View.hxx>
33 #include <AIS_DimensionOwner.hxx>
34 #include <AIS_DimensionSelectionMode.hxx>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40 #include <QMouseEvent>
41
42 using namespace std;
43
44 PartSet_OperationFeatureEdit::PartSet_OperationFeatureEdit(const QString& theId,
45                                                            QObject* theParent,
46                                                            CompositeFeaturePtr theFeature)
47     : PartSet_OperationFeatureBase(theId, theParent, theFeature),
48       myIsBlockedSelection(false)
49 {
50   myIsEditing = true;
51 }
52
53 PartSet_OperationFeatureEdit::~PartSet_OperationFeatureEdit()
54 {
55 }
56
57
58 void PartSet_OperationFeatureEdit::mousePressed(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer, ModuleBase_ISelection* theSelection)
59 {
60   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
61   if(aActiveWgt && aActiveWgt->isViewerSelector()) {
62     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
63     PartSet_OperationFeatureBase::mousePressed(theEvent, theViewer, theSelection);
64     return;
65   }
66   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
67   QList<ModuleBase_ViewerPrs> aHighlighted = theSelection->getHighlighted();
68   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
69   if (aHasShift && !aHighlighted.empty()) {
70     foreach (ModuleBase_ViewerPrs aPrs, aHighlighted) {
71       aSelected.append(aPrs);
72     }
73   }
74   ObjectPtr aObject;
75   if (!aSelected.empty()) {
76     aObject = aSelected.first().object();
77   } else {   
78     if (!aHighlighted.empty())
79       aObject = aHighlighted.first().object();
80   }
81   //if (!theHighlighted.empty())
82   //  aObject = theHighlighted.front().object();
83   //if (!aObject && !theSelected.empty())  // changed for a constrain
84   //  aObject = theSelected.front().object();
85
86   FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
87   if (!aFeature || aFeature != feature() || (aSelected.size() > 1)) {
88     if (commit()) {
89       theViewer->enableSelection(true);
90       emit featureConstructed(feature(), FM_Deactivation);
91
92       // If we have selection and prehilighting with shift pressed 
93       // Then we have to select all these objects and restart as multi edit operfation
94       //bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
95       //if (aHasShift && !theHighlighted.empty()) {
96       //  QList<ObjectPtr> aSelected;
97       //  std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
98       //  for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
99       //    aSelected.append((*aIt).object());
100
101       //  for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
102       //    if (!aSelected.contains((*aIt).object()))
103       //      aSelected.append((*aIt).object());
104       //  }
105       //  emit setSelection(aSelected);
106       //} else 
107       if (aFeature) {
108         std::string anOperationType =
109             (aSelected.size() > 1) ?
110                 PartSet_OperationFeatureEditMulti::Type() : PartSet_OperationFeatureEdit::Type();
111         restartOperation(anOperationType, aFeature);
112       }
113       //}
114     }
115   }
116 }
117
118 void PartSet_OperationFeatureEdit::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
119 {
120   if (!(theEvent->buttons() & Qt::LeftButton))
121     return;
122   Handle(V3d_View) aView = theViewer->activeView();
123   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
124
125   theViewer->enableSelection(false);
126
127   //blockSelection(true);
128   if (myCurPoint.myIsInitialized) {
129     double aCurX, aCurY;
130     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), aView, aCurX, aCurY);
131
132     double aX, anY;
133     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
134
135     double aDeltaX = aX - aCurX;
136     double aDeltaY = anY - aCurY;
137
138     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
139         SketchPlugin_Feature>(feature());
140     // MPV: added condition because it could be external edge of some object, not sketch
141     if (aSketchFeature && aSketchFeature->sketch() == sketch().get()) {
142       aSketchFeature->move(aDeltaX, aDeltaY);
143       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
144       ModelAPI_EventCreator::get()->sendUpdated(feature(), anEvent);
145     }
146   }
147   sendFeatures();
148
149   myCurPoint.setPoint(aPoint);
150 }
151
152 void PartSet_OperationFeatureEdit::mouseReleased(
153     QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
154     ModuleBase_ISelection* theSelection)
155 {
156   theViewer->enableSelection(true);
157   ModuleBase_ModelWidget* aActiveWgt = 0;
158   if (myPropertyPanel)
159     aActiveWgt = myPropertyPanel->activeWidget();
160   if(aActiveWgt && aActiveWgt->isViewerSelector()) {
161     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
162     PartSet_OperationFeatureBase::mouseReleased(theEvent, theViewer, theSelection);
163   }// else {
164     //blockSelection(false);
165   //}
166 }
167
168 void PartSet_OperationFeatureEdit::mouseDoubleClick(
169     QMouseEvent* theEvent, Handle_V3d_View theView,
170     ModuleBase_ISelection* theSelection)
171 {
172   // TODO the functionality is important only for constraint feature. Should be moved in another place
173   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
174   if (!aSelected.empty()) {
175     ModuleBase_ViewerPrs aFeaturePrs = aSelected.first();
176     if (!aFeaturePrs.owner().IsNull()) {
177       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(
178           aFeaturePrs.owner());
179       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
180         bool isValid;
181         double aValue = PartSet_Tools::featureValue(feature(), SketchPlugin_Constraint::VALUE(),
182                                                     isValid);
183         if (isValid) {
184           ModuleBase_WidgetEditor::editFeatureValue(feature(), SketchPlugin_Constraint::VALUE());
185           flushUpdated();
186         }
187       }
188     }
189   }
190 }
191
192 void PartSet_OperationFeatureEdit::startOperation()
193 {
194   PartSet_OperationSketchBase::startOperation();
195   //emit multiSelectionEnabled(false);
196
197   myCurPoint.clear();
198 }
199
200 void PartSet_OperationFeatureEdit::stopOperation()
201 {
202   //emit multiSelectionEnabled(true);
203
204   //blockSelection(false, false);
205 }
206
207 //void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
208 //{
209 //  if (myIsBlockedSelection == isBlocked)
210 //    return;
211 //
212 //  myIsBlockedSelection = isBlocked;
213 //  QList<ObjectPtr> aFeatureList;
214 //  aFeatureList.append(feature());
215 //
216 //  //if (isBlocked) {
217 //  //  emit setSelection(QList<ObjectPtr>());
218 //  //  emit stopSelection(aFeatureList, true);
219 //  //} else {
220 //  //  emit stopSelection(aFeatureList, false);
221 //  //  if (isRestoreSelection)
222 //  //    emit setSelection(aFeatureList);
223 //  //}
224 //}
225
226 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool theFlushMessage,
227   CompositeFeaturePtr theCompositeFeature)
228 {
229   // do nothing in order to do not create a new feature
230   return FeaturePtr();
231 }
232
233 void PartSet_OperationFeatureEdit::sendFeatures()
234 {
235   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
236
237   FeaturePtr aFeature = feature();
238   ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
239
240   Events_Loop::loop()->flush(anEvent);
241   flushUpdated();
242 }
243