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