Salome HOME
Implementation of mechanism of grouping of messages
[modules/shaper.git] / src / PartSet / PartSet_OperationEditLine.cpp
1 // File:        PartSet_OperationEditLine.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationEditLine.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_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
36                                                   QObject* theParent,
37                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
38 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
39 {
40 }
41
42 PartSet_OperationEditLine::~PartSet_OperationEditLine()
43 {
44 }
45
46 bool PartSet_OperationEditLine::isGranted(ModuleBase_IOperation* theOperation) const
47 {
48   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
49 }
50
51 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
52 {
53   return PartSet_OperationSketchBase::getSelectionModes(theFeature);
54 }
55
56 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
57                                      const std::list<XGUI_ViewerPrs>& thePresentations)
58 {
59   setFeature(theFeature);
60   myFeatures = thePresentations;
61 }
62
63 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::sketch() const
64 {
65   return mySketch;
66 }
67
68 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
69 {
70   if (!(theEvent->buttons() &  Qt::LeftButton))
71     return;
72   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
73   myCurPoint.setPoint(aPoint);
74 }
75
76 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
77 {
78   if (!(theEvent->buttons() &  Qt::LeftButton))
79     return;
80
81   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
82
83   if (myCurPoint.myIsInitialized) {
84     double aCurX, aCurY;
85     PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
86
87     double aX, anY;
88     PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
89
90     double aDeltaX = aX - aCurX;
91     double aDeltaY = anY - aCurY;
92
93     moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
94     moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
95
96     std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
97     for (; anIt != aLast; anIt++) {
98       boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
99       if (!aFeature || aFeature == feature())
100         continue;
101       moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
102       moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
103     }
104   }
105   flushUpdated();
106   sendFeatures();
107
108   myCurPoint.setPoint(aPoint);
109 }
110
111 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
112                                               const std::list<XGUI_ViewerPrs>& theSelected)
113 {
114   std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
115   if (myFeatures.size() == 1) {
116     if (theSelected.empty())
117       return;
118
119     boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
120     commit();
121     emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
122   }
123   else {
124     commit();
125     std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
126     for (; anIt != aLast; anIt++) {
127       boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
128       if (aFeature)
129         emit featureConstructed(aFeature, FM_Deactivation);
130     }
131   }
132 }
133
134 void PartSet_OperationEditLine::startOperation()
135 {
136   // do nothing in order to do not create a new feature
137   emit multiSelectionEnabled(false);
138   emit setSelection(std::list<XGUI_ViewerPrs>());
139   emit stopSelection(myFeatures, true);
140   myCurPoint.clear();
141 }
142
143 void PartSet_OperationEditLine::stopOperation()
144 {
145   emit multiSelectionEnabled(true);
146   bool isSelectFeatures = myFeatures.size() > 1;
147   emit stopSelection(myFeatures, false);
148   if (isSelectFeatures)
149     emit setSelection(myFeatures);
150
151   myFeatures.clear();
152 }
153
154 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature(const bool /*theFlushMessage*/)
155 {
156   // do nothing in order to do not create a new feature
157   return boost::shared_ptr<ModelAPI_Feature>();
158 }
159
160 void PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
161                                                double theDeltaX, double theDeltaY,
162                                                const std::string& theAttribute)
163 {
164   if (!theFeature || theFeature->getKind() != "SketchLine")
165     return;
166
167   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
168   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
169         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
170
171   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
172 }
173
174 void PartSet_OperationEditLine::sendFeatures()
175 {
176   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
177
178   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures;
179   std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
180   for (; anIt != aLast; anIt++) {
181     boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
182     if (!aFeature)
183       continue;
184
185     Model_FeatureUpdatedMessage aMessage(aFeature, anEvent);
186     Events_Loop::loop()->send(aMessage);
187   }
188   Events_Loop::loop()->flush(anEvent);
189 }
190