Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureEditMulti.cpp
1 // File:        PartSet_OperationFeatureEditMulti.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureEditMulti.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_OperationFeatureEditMulti::PartSet_OperationFeatureEditMulti(const QString& theId,
36                                                   QObject* theParent,
37                                               FeaturePtr theFeature)
38 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myIsBlockedSelection(false)
39 {
40 }
41
42 PartSet_OperationFeatureEditMulti::~PartSet_OperationFeatureEditMulti()
43 {
44 }
45
46 bool PartSet_OperationFeatureEditMulti::isGranted(ModuleBase_IOperation* theOperation) const
47 {
48   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
49 }
50
51 void PartSet_OperationFeatureEditMulti::init(FeaturePtr theFeature,
52                                      const std::list<XGUI_ViewerPrs>& theSelected,
53                                      const std::list<XGUI_ViewerPrs>& theHighlighted)
54 {
55   setEditingFeature(theFeature);
56
57   if (!theHighlighted.empty()) {
58     // if there is highlighted object, we check whether it is in the list of selected objects
59     // in that case this object is a handle of the moved lines. If there no such object in the selection,
60     // the hightlighted object should moved and the selection is skipped. The skipped selection will be
61     // deselected in the viewer by blockSelection signal in the startOperation method.
62     bool isSelected = false;
63     std::list<XGUI_ViewerPrs>::const_iterator anIt = theSelected.begin(), aLast = theSelected.end();
64     for (; anIt != aLast && !isSelected; anIt++) {
65       isSelected = (*anIt).feature() == feature();
66     }
67     if (!isSelected)
68       myFeatures = theHighlighted;
69     else
70       myFeatures = theSelected;
71   }
72   else
73     myFeatures = theSelected;
74 }
75
76 FeaturePtr PartSet_OperationFeatureEditMulti::sketch() const
77 {
78   return mySketch;
79 }
80
81 void PartSet_OperationFeatureEditMulti::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
82                                              const std::list<XGUI_ViewerPrs>& /*theSelected*/,
83                                              const std::list<XGUI_ViewerPrs>& theHighlighted)
84 {
85 }
86
87 void PartSet_OperationFeatureEditMulti::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
88 {
89   if (!(theEvent->buttons() &  Qt::LeftButton))
90     return;
91
92   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
93
94   blockSelection(true);
95   if (myCurPoint.myIsInitialized) {
96     double aCurX, aCurY;
97     PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
98
99     double aX, anY;
100     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
101
102     double aDeltaX = aX - aCurX;
103     double aDeltaY = anY - aCurY;
104
105     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
106                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(feature());
107     aSketchFeature->move(aDeltaX, aDeltaY);
108
109     std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
110     for (; anIt != aLast; anIt++) {
111       FeaturePtr aFeature = (*anIt).feature();
112       if (!aFeature || aFeature == feature())
113         continue;
114       aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
115       aSketchFeature->move(aDeltaX, aDeltaY);
116     }
117   }
118   sendFeatures();
119
120   myCurPoint.setPoint(aPoint);
121 }
122
123 void PartSet_OperationFeatureEditMulti::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
124                                               const std::list<XGUI_ViewerPrs>& /*theSelected*/,
125                                               const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
126 {
127   std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
128   commit();
129   std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
130   for (; anIt != aLast; anIt++) {
131     FeaturePtr aFeature = (*anIt).feature();
132     if (aFeature) {
133       emit featureConstructed(aFeature, FM_Deactivation);
134         }
135   }
136 }
137
138 void PartSet_OperationFeatureEditMulti::startOperation()
139 {
140   PartSet_OperationSketchBase::startOperation();
141   emit multiSelectionEnabled(false);
142
143   blockSelection(true);
144
145   myCurPoint.clear();
146 }
147
148 void PartSet_OperationFeatureEditMulti::stopOperation()
149 {
150   emit multiSelectionEnabled(true);
151
152   blockSelection(false, true);
153
154   myFeatures.clear();
155 }
156
157 void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked, const bool isRestoreSelection)
158 {
159   if (myIsBlockedSelection == isBlocked)
160     return;
161
162   myIsBlockedSelection = isBlocked;
163   QFeatureList aFeatureList;
164   std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(),
165                                             aLast = myFeatures.end();
166   for(; anIt != aLast; anIt++)
167     aFeatureList.append((*anIt).feature());
168   if (isBlocked) {
169     emit setSelection(QFeatureList());
170     emit stopSelection(aFeatureList, true);
171   }
172   else {
173     emit stopSelection(aFeatureList, false);
174     if (isRestoreSelection) {
175       emit setSelection(aFeatureList);
176     }
177   }
178 }
179
180 void PartSet_OperationFeatureEditMulti::sendFeatures()
181 {
182   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
183
184   std::list<FeaturePtr > aFeatures;
185   std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
186   for (; anIt != aLast; anIt++) {
187     FeaturePtr aFeature = (*anIt).feature();
188     if (!aFeature)
189       continue;
190
191     Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
192     Events_Loop::loop()->send(aMessage);
193   }
194   Events_Loop::loop()->flush(anEvent);
195   flushUpdated();
196 }
197