]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureEdit.cpp
Salome HOME
d87a1b5b58b313ac9e3fbf647e7e93fd80ee803d
[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 <Model_Events.h>
14
15 #include <XGUI_ViewerPrs.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 <Model_Events.h>
23
24 #include <Events_Loop.h>
25
26 #include <SketchPlugin_Line.h>
27
28 #include <V3d_View.hxx>
29
30 #ifdef _DEBUG
31 #include <QDebug>
32 #endif
33
34 #include <QMouseEvent>
35
36 using namespace std;
37
38 PartSet_OperationFeatureEdit::PartSet_OperationFeatureEdit(const QString& theId,
39                                                   QObject* theParent,
40                                               FeaturePtr theFeature)
41 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myIsBlockedSelection(false)
42 {
43 }
44
45 PartSet_OperationFeatureEdit::~PartSet_OperationFeatureEdit()
46 {
47 }
48
49 bool PartSet_OperationFeatureEdit::isGranted(ModuleBase_IOperation* theOperation) const
50 {
51   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
52 }
53
54 std::list<int> PartSet_OperationFeatureEdit::getSelectionModes(FeaturePtr theFeature) const
55 {
56   return PartSet_OperationSketchBase::getSelectionModes(theFeature);
57 }
58
59 void PartSet_OperationFeatureEdit::init(FeaturePtr theFeature,
60                                      const std::list<XGUI_ViewerPrs>& theSelected,
61                                      const std::list<XGUI_ViewerPrs>& theHighlighted)
62 {
63   setEditingFeature(theFeature);
64 }
65
66 FeaturePtr PartSet_OperationFeatureEdit::sketch() const
67 {
68   return mySketch;
69 }
70
71 void PartSet_OperationFeatureEdit::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
72                                              const std::list<XGUI_ViewerPrs>& theSelected,
73                                              const std::list<XGUI_ViewerPrs>& theHighlighted)
74 {
75   FeaturePtr aFeature;
76   if (!theHighlighted.empty())
77     aFeature = theHighlighted.front().feature();
78   if (!aFeature && !theSelected.empty()) // changed for a constrain
79     aFeature = theSelected.front().feature();
80
81   if (!aFeature || aFeature != feature())
82   {
83     commit();
84     emit featureConstructed(feature(), FM_Deactivation);
85
86     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
87     if(aHasShift && !theHighlighted.empty()) {
88       QFeatureList aSelected;
89       aSelected.push_back(feature());
90       aSelected.push_back(theHighlighted.front().feature());
91       emit setSelection(aSelected);
92     }
93     else if (aFeature) {
94       restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
95     }
96   }
97 }
98
99 void PartSet_OperationFeatureEdit::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
100 {
101   if (!(theEvent->buttons() &  Qt::LeftButton))
102     return;
103
104   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
105
106   blockSelection(true);
107   if (myCurPoint.myIsInitialized) {
108     double aCurX, aCurY;
109     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
110
111     double aX, anY;
112     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
113
114     double aDeltaX = aX - aCurX;
115     double aDeltaY = anY - aCurY;
116
117     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
118                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(feature());
119     aSketchFeature->move(aDeltaX, aDeltaY);
120   }
121   sendFeatures();
122
123   myCurPoint.setPoint(aPoint);
124 }
125
126 void PartSet_OperationFeatureEdit::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
127                                               const std::list<XGUI_ViewerPrs>& /*theSelected*/,
128                                               const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
129 {
130   blockSelection(false);
131 }
132
133 void PartSet_OperationFeatureEdit::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
134                                                     const std::list<XGUI_ViewerPrs>& theSelected,
135                                                     const std::list<XGUI_ViewerPrs>& theHighlighted)
136 {
137   // changed
138   if (!theSelected.empty()) {
139     bool isValid;
140     double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
141     if (isValid) {
142       ModuleBase_WidgetEditor::editFeatureValue(feature(), CONSTRAINT_ATTR_VALUE);
143
144       //QPoint aPos = theEvent->globalPos();
145       //myEditor->start(aPos, aValue);
146     }
147   }
148 }
149
150 void PartSet_OperationFeatureEdit::startOperation()
151 {
152   PartSet_OperationSketchBase::startOperation();
153   emit multiSelectionEnabled(false);
154
155   myCurPoint.clear();
156 }
157
158 void PartSet_OperationFeatureEdit::stopOperation()
159 {
160   emit multiSelectionEnabled(true);
161
162   blockSelection(false, false);
163 }
164
165 void PartSet_OperationFeatureEdit::blockSelection(bool isBlocked, const bool isRestoreSelection)
166 {
167   if (myIsBlockedSelection == isBlocked)
168     return;
169
170   myIsBlockedSelection = isBlocked;
171   QFeatureList aFeatureList;
172   aFeatureList.append(feature());
173
174   if (isBlocked) {
175     emit setSelection(QFeatureList());
176     emit stopSelection(aFeatureList, true);
177   }
178   else {
179     emit stopSelection(aFeatureList, false);
180     if (isRestoreSelection)
181       emit setSelection(aFeatureList);
182   }
183 }
184
185 FeaturePtr PartSet_OperationFeatureEdit::createFeature(const bool /*theFlushMessage*/)
186 {
187   // do nothing in order to do not create a new feature
188   return FeaturePtr();
189 }
190
191 void PartSet_OperationFeatureEdit::sendFeatures()
192 {
193   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
194
195   FeaturePtr aFeature = feature();
196
197   Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
198   Events_Loop::loop()->send(aMessage);
199
200   Events_Loop::loop()->flush(anEvent);
201   flushUpdated();
202 }
203