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