Salome HOME
bc73304dd1c8b7f52307a63cc6f69e4884ee770d
[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       emit featureConstructed(feature(), FM_Deactivation);
90
91       // If we have selection and prehilighting with shift pressed 
92       // Then we have to select all these objects and restart as multi edit operfation
93       //bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
94       //if (aHasShift && !theHighlighted.empty()) {
95       //  QList<ObjectPtr> aSelected;
96       //  std::list<ModuleBase_ViewerPrs>::const_iterator aIt;
97       //  for (aIt = theSelected.cbegin(); aIt != theSelected.cend(); ++aIt)
98       //    aSelected.append((*aIt).object());
99
100       //  for (aIt = theHighlighted.cbegin(); aIt != theHighlighted.cend(); ++aIt) {
101       //    if (!aSelected.contains((*aIt).object()))
102       //      aSelected.append((*aIt).object());
103       //  }
104       //  emit setSelection(aSelected);
105       //} else 
106       if (aFeature) {
107         std::string anOperationType =
108             (aSelected.size() > 1) ?
109                 PartSet_OperationFeatureEditMulti::Type() : PartSet_OperationFeatureEdit::Type();
110         restartOperation(anOperationType, aFeature);
111       }
112       //}
113     }
114   } 
115 }
116
117 void PartSet_OperationFeatureEdit::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
118 {
119   if (!(theEvent->buttons() & Qt::LeftButton))
120     return;
121   Handle(V3d_View) aView = theViewer->activeView();
122   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
123
124   if (theViewer->isSelectionEnabled())
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   ModuleBase_ModelWidget* aActiveWgt = 0;
157   if (myPropertyPanel)
158     aActiveWgt = myPropertyPanel->activeWidget();
159   if(aActiveWgt && aActiveWgt->isViewerSelector()) {
160     // Almost do nothing, all stuff in on PartSet_OperationFeatureBase::mouseReleased
161     PartSet_OperationFeatureBase::mouseReleased(theEvent, theViewer, theSelection);
162   }// else {
163     //blockSelection(false);
164   //}
165   if (!theViewer->isSelectionEnabled())
166     theViewer->enableSelection(true);
167 }
168
169 void PartSet_OperationFeatureEdit::mouseDoubleClick(
170     QMouseEvent* theEvent, Handle_V3d_View theView,
171     ModuleBase_ISelection* theSelection)
172 {
173   // TODO the functionality is important only for constraint feature. Should be moved in another place
174   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
175   if (!aSelected.empty()) {
176     ModuleBase_ViewerPrs aFeaturePrs = aSelected.first();
177     if (!aFeaturePrs.owner().IsNull()) {
178       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(
179           aFeaturePrs.owner());
180       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
181         bool isValid;
182         double aValue = PartSet_Tools::featureValue(feature(), SketchPlugin_Constraint::VALUE(),
183                                                     isValid);
184         if (isValid) {
185           ModuleBase_WidgetEditor::editFeatureValue(feature(), SketchPlugin_Constraint::VALUE());
186           flushUpdated();
187         }
188       }
189     }
190   }
191 }
192
193 void PartSet_OperationFeatureEdit::startOperation()
194 {
195   PartSet_OperationSketchBase::startOperation();
196   //emit multiSelectionEnabled(false);
197
198   myCurPoint.clear();
199 }
200
201 void PartSet_OperationFeatureEdit::stopOperation()
202 {
203   //emit multiSelectionEnabled(true);
204
205   //blockSelection(false, false);
206 }
207
208 //void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
209 //{
210 //  if (myIsBlockedSelection == isBlocked)
211 //    return;
212 //
213 //  myIsBlockedSelection = isBlocked;
214 //  QList<ObjectPtr> aFeatureList;
215 //  aFeatureList.append(feature());
216 //
217 //  //if (isBlocked) {
218 //  //  emit setSelection(QList<ObjectPtr>());
219 //  //  emit stopSelection(aFeatureList, true);
220 //  //} else {
221 //  //  emit stopSelection(aFeatureList, false);
222 //  //  if (isRestoreSelection)
223 //  //    emit setSelection(aFeatureList);
224 //  //}
225 //}
226
227 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool theFlushMessage,
228   CompositeFeaturePtr theCompositeFeature)
229 {
230   // do nothing in order to do not create a new feature
231   return FeaturePtr();
232 }
233
234 void PartSet_OperationFeatureEdit::sendFeatures()
235 {
236   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
237
238   FeaturePtr aFeature = feature();
239   ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
240
241   Events_Loop::loop()->flush(anEvent);
242   flushUpdated();
243 }
244