Version: 9.16.0
SceneNodeItem.cxx
Go to the documentation of this file.
1// Copyright (C) 2006-2026 CEA, EDF
2//
3// This library is free software; you can redistribute it and/or
4// modify it under the terms of the GNU Lesser General Public
5// License as published by the Free Software Foundation; either
6// version 2.1 of the License, or (at your option) any later version.
7//
8// This library is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11// Lesser General Public License for more details.
12//
13// You should have received a copy of the GNU Lesser General Public
14// License along with this library; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16//
17// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18//
19
20#include "SceneNodeItem.hxx"
22#include "SceneProcItem.hxx"
24#include "SceneProgressItem.hxx"
25#include "SceneInPortItem.hxx"
26#include "SceneOutPortItem.hxx"
29#include "SceneLinkItem.hxx"
30#include "Scene.hxx"
31#include "QtGuiContext.hxx"
32#include "Menus.hxx"
33#include "GuiEditor.hxx"
34#include "InPort.hxx"
35#include "OutPort.hxx"
36
37#include "Switch.hxx"
38#include "Resource.hxx"
39
40#include <QGraphicsSceneHoverEvent>
41#include <QPointF>
42
43#include <cmath>
44#include <sstream>
45#include <cassert>
46#include <vector>
47
48//#define _DEVDEBUG_
49#include "YacsTrace.hxx"
50
51using namespace std;
52using namespace YACS::ENGINE;
53using namespace YACS::HMI;
54
55SceneNodeItem::SceneNodeItem(QGraphicsScene *scene, SceneItem *parent,
56 QString label, Subject *subject)
57 : SceneObserverItem(scene, parent, label, subject)
58{
59 _inPorts.clear();
60 _outPorts.clear();
61 _header = 0;
62 _progressItem = 0;
64 _moving = false;
65 _moved = false;
66 _blocX = false;
67 _blocY = false;
68 _dragable = true;
69 _dragButton = Qt::MidButton;
71 _expanded = true;
72 _expandedPos = QPointF(0,0);
76}
77
79{
80 if (SceneComposedNodeItem* parent = getParent())
81 parent->removeChildFromList(this);
82}
83
84void SceneNodeItem::setWidth(qreal width)
85{
86 if (width != _width)
87 {
88 prepareGeometryChange();
89 _width = width;
92 QGraphicsItem::update();
93 }
94}
95
96void SceneNodeItem::setHeight(qreal height)
97{
98 if (height != _height)
99 {
100 prepareGeometryChange();
101 _height = height;
103 QGraphicsItem::update();
104 }
105}
106
108{
109 DEBTRACE("SceneNodeItem::addHeader " << _label.toStdString());
110 if (!_hasHeader)
111 {
112 _hasHeader = true;
114 this,
116 updateState();
118 }
119}
120
122{
123 return _header;
124}
125
127{
128 DEBTRACE("SceneNodeItem::addProgressItem ");
129 if (!_progressItem)
130 {
132 this,
133 "progress");
135 updateState();
137 }
138}
139
140void SceneNodeItem::paint(QPainter *painter,
141 const QStyleOptionGraphicsItem *option,
142 QWidget *widget)
143{
144 //DEBTRACE("SceneNodeItem::paint");
145}
146
147void SceneNodeItem::update(GuiEvent event, int type, Subject* son)
148{
149 DEBTRACE("SceneNodeItem::update "<< eventName(event)<<" "<<type<<" "<<son);
150 SceneObserverItem::update(event, type, son);
151 SubjectNode *snode = 0;
152 Node *node = 0;
153 switch (event)
154 {
156 DEBTRACE("SceneNodeItem::update RENAME " << _subject->getName());
157 updateName();
158 break;
159 case YACS::HMI::EDIT:
160 if (_header) _header->setEdited(type);
161 break;
163 updateState();
164 break;
166 setExecState(type);
167 break;
168 default:
169 ;
170 }
171}
172
174void SceneNodeItem::arrangeNodes(bool isRecursive)
175{
176}
177
179{
180}
181
183{
184}
185
187{
188 if (_hasHeader) {
189 return _header->getHeaderBottom();
190 } else {
191 return 0;
192 };
193}
194
196}
197
198void SceneNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
199{
200 NodeMenu m;
201 m.popupMenu(caller, globalPos);
202}
203
205{
206 if (_parent)
207 return dynamic_cast<SceneComposedNodeItem*>(_parent);
208 else
209 return 0;
210}
212{
213 _inPorts.remove(inPort);
214}
215
217{
218 _outPorts.remove(outPort);
219}
220
222{
223 //DEBTRACE("SceneNodeItem::setMoving " << _label.toStdString() << " " << moving);
224 _moving = moving;
225 SceneNodeItem *nodep = 0;
226 if (_parent && (nodep = dynamic_cast<SceneNodeItem*>(_parent)))
227 nodep->setMoving(false);
228}
229
231{
232 if (! _header) return 0;
233 return _header->getCtrlInPortItem();
234}
235
237{
238 if (! _header) return 0;
239 return _header->getCtrlOutPortItem();
240}
241
243{
245}
246
248{
249 return "yacs/subjectNode";
250}
251
252void SceneNodeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
253{
254 DEBTRACE("SceneNodeItem::mousePressEvent " << _label.toStdString());
256 if (!_scene->isZooming())
257 {
258 if (!_draging) setMoving(true);
259 _prevPos = pos();
260 }
261}
262
263void SceneNodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
264{
265 //DEBTRACE("SceneNodeItem::mouseReleaseEvent " << _label.toStdString());
267 setMoving(false);
268 if ((pos() != _prevPos) && Scene::_autoComputeLinks)
269 {
272 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
273 proc->rebuildLinks();
274 }
275 if (_moved)
277 QtGuiContext::getQtCurrent()->getView()->ensureVisible(this);
278 _moved = false;
279}
280
281void SceneNodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
282{
284 if (_moving)
285 {
286 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
287 {
288 QPointF oldPos = pos();
289 QPointF aPos = oldPos + event->scenePos() - event->lastScenePos();
290 if (aPos != oldPos)
291 _moved = true;
292 if (aPos.x() > oldPos.x()) _blocX = false;
293 if (aPos.y() > oldPos.y()) _blocY = false;
294 if (aPos.x() < Resource::Border_Margin)
295 {
296 aPos.setX(Resource::Border_Margin);
297 _blocX = true;
298 }
299 if ( aPos.y() < bloc->getHeaderBottom() )
300 {
301 aPos.setY(bloc->getHeaderBottom());
302 _blocY = true;
303 }
304 setTopLeft(aPos);
305 bloc->collisionResolv(this, oldPos);
306 }
307 }
308}
309
310void SceneNodeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
311{
312 DEBTRACE("SceneNodeItem::mouseDoubleClickEvent");
313 QtGuiContext::getQtCurrent()->getGMain()->_guiEditor->shrinkExpand(QApplication::keyboardModifiers());
314}
315
316void SceneNodeItem::setTopLeft(QPointF topLeft)
317{
318 if (_shownState == shrinkHidden) {
319 _expandedPos = topLeft;
320 return;
321 }
322 QPointF oldPos = pos();
323 setPos(topLeft);
324
325 //update links
326 updateLinks();
327
328 if (_parent)
329 {
330 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
331 bloc->collisionResolv(this, oldPos);
333 }
334 _expandedPos = pos();
335}
336
338{
340}
341
343{
344 SubjectNode *snode = dynamic_cast<SubjectNode*>(_subject);
345 YASSERT(snode);
346 Node *node = snode->getNode();
347 YASSERT(node);
348 switch (node->getState())
349 {
350 case YACS::INVALID:
351 if (_header) _header->setValid(false);
352 break;
353 case YACS::READY:
354 if (_header) _header->setValid(true);
355 break;
356 default:
357 break;
358 }
359}
360
362{
363 DEBTRACE("SceneNodeItem::setExecState " << execState);
364 _execState = execState;
365 if (_header) _header->setExecState(execState);
366}
367
369{
370 DEBTRACE("SceneNodeItem::getHeaderLabel");
371 QString extLabel = _subject->getName().c_str();
372
373 SceneObserverItem *soi = 0;
374 SubjectSwitch* sswi = 0;
375
376 if ((_parent)
377 && (soi = dynamic_cast<SceneObserverItem*>(_parent))
378 && (sswi = dynamic_cast<SubjectSwitch*>(soi->getSubject())))
379 {
380 Switch *aswi = dynamic_cast<Switch*>(sswi->getNode());
381 SubjectNode *sno = dynamic_cast<SubjectNode*>(_subject);
382 int idcase = aswi->getRankOfNode(sno->getNode());
383 stringstream caseid;
384 if (idcase == Switch::ID_FOR_DEFAULT_NODE)
385 caseid << "default";
386 else
387 caseid << idcase;
388 extLabel += " (";
389 //extLabel += aswi->getCaseId(sno->getNode()).c_str();
390 extLabel += caseid.str().c_str();
391 extLabel += ")";
392 DEBTRACE(extLabel.toStdString());
393 }
394
395 return extLabel;
396}
397
399{
400 //update control links
401 std::list<SubjectControlLink*> lscl=dynamic_cast<SubjectNode*>(_subject)->getSubjectControlLinks();
402 for (std::list<SubjectControlLink*>::const_iterator it = lscl.begin(); it != lscl.end(); ++it)
403 {
405 item->updateShape();
406 }
407
408 //update data links through child items update (SceneDataPortItem)
410}
411
413{
414 foreach (QGraphicsItem *child, childItems())
415 {
416 if (SceneItem *sci = dynamic_cast<SceneItem*>(child))
417 {
418 sci->updateLinks();
419 }
420 }
421}
422
424{
425 foreach (QGraphicsItem *child, childItems())
426 {
427 if (SceneItem *sci = dynamic_cast<SceneItem*>(child))
428 {
429 sci->shrinkExpandLink(se);
430 }
431 }
432}
433
435{
436 SubjectNode *snode = dynamic_cast<SubjectNode*>(_subject);
437 YASSERT(snode);
438 Node *node = snode->getNode();
439 YASSERT(node);
440 vector<pair<OutPort *, InPort *> > listLeaving = node->getSetOfLinksLeavingCurrentScope();
441 vector<pair<InPort *, OutPort *> > listIncoming = node->getSetOfLinksComingInCurrentScope();
442 vector<pair<OutPort *, InPort *> > outScope = listLeaving;
443 vector<pair<InPort *, OutPort *> >::iterator it1;
444 for (it1 = listIncoming.begin(); it1 != listIncoming.end(); ++it1)
445 {
446 pair<OutPort *, InPort *> outin = pair<OutPort *, InPort *>((*it1).second, (*it1).first);
447 outScope.push_back(outin);
448 }
449 vector<pair<OutPort*, InPort*> >::const_iterator it = outScope.begin();
450 for( ; it != outScope.end(); ++it)
451 {
452 YASSERT(QtGuiContext::getQtCurrent()->_mapOfSubjectLink.count(*it));
454 YASSERT(QtGuiContext::getQtCurrent()->_mapOfSceneItem.count(slink));
456 item->show();
457 }
458}
459
461{
462 _shownState = ss;
463}
464
466{
467 return _progressItem != 0;
468}
#define YASSERT(val)
YASSERT macro is always defined, used like assert, but throw a YACS::Exception instead of abort.
Definition: YacsTrace.hxx:59
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
Base class for all nodes.
Definition: Node.hxx:70
virtual std::vector< std::pair< OutPort *, InPort * > > getSetOfLinksLeavingCurrentScope() const =0
virtual YACS::StatesForNode getState() const
Definition: Node.hxx:118
virtual std::vector< std::pair< InPort *, OutPort * > > getSetOfLinksComingInCurrentScope() const =0
Control node that emulates the C switch.
Definition: Switch.hxx:85
int getRankOfNode(Node *node) const
Definition: Switch.cxx:752
YACS::HMI::Scene * _scene
Definition: SceneItem.hxx:98
enum Qt::MouseButton _dragButton
Definition: SceneItem.hxx:111
YACS::HMI::GuiEditor * _guiEditor
Definition: GenericGui.hxx:201
std::map< std::pair< YACS::ENGINE::OutPort *, YACS::ENGINE::InPort * >, YACS::HMI::SubjectLink * > _mapOfSubjectLink
Definition: guiContext.hxx:72
YACS::HMI::SubjectProc * getSubjectProc()
Definition: guiContext.hxx:52
void shrinkExpand(Qt::KeyboardModifiers kbModifiers=Qt::NoModifier)
Definition: GuiEditor.cxx:392
static std::string eventName(GuiEvent event)
static QtGuiContext * getQtCurrent()
YACS::HMI::GenericGui * getGMain()
std::map< YACS::HMI::Subject *, YACS::HMI::SceneItem * > _mapOfSceneItem
YACS::HMI::GraphicsView * getView()
static int Border_Margin
Definition: Resource.hxx:310
static QColor Scene_pen
Definition: Resource.hxx:271
static bool ensureVisibleWhenMoved
Definition: Resource.hxx:213
virtual void setValid(bool isValid)
virtual SceneCtrlPortItem * getCtrlInPortItem()
virtual void setExecState(int execState)
virtual void setText(QString label)
virtual void setEdited(bool isEdited)
virtual SceneCtrlPortItem * getCtrlOutPortItem()
virtual void checkGeometryChange()
Definition: SceneItem.cxx:248
virtual void setTopLeft(QPointF topLeft)
virtual qreal getHeaderBottom()
SceneHeaderNodeItem * _header
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
virtual void reorganizeShrinkExpand(ShrinkMode theShrinkMode)
virtual void shrinkExpandLink(bool se)
virtual void arrangeNodes(bool isRecursive)
generic method to compute a graph for child nodes. implemented in some derived classes
virtual void popupMenu(QWidget *caller, const QPoint &globalPos)
SceneProgressItem * _progressItem
virtual void addProgressItem()
virtual void setExecState(int execState)
virtual void update(GuiEvent event, int type, Subject *son)
virtual ScenePortItem * getCtrlInPortItem()
std::list< AbstractSceneItem * > _outPorts
virtual void setHeight(qreal height)
AbstractSceneItem cannot be resized (only ComposedNodeItem can)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
virtual SceneComposedNodeItem * getParent()
virtual void setShownState(shownState ss)
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
virtual void removeOutPortFromList(AbstractSceneItem *outPort)
virtual SceneHeaderItem * getHeader()
virtual void updateChildItems()
virtual void removeInPortFromList(AbstractSceneItem *inPort)
virtual void setMoving(bool moving)
virtual void showOutScopeLinks()
virtual QString getMimeFormat()
virtual void arrangeChildNodes()
virtual void autoPosNewPort(AbstractSceneItem *item, int nbPorts)
virtual void setWidth(qreal width)
AbstractSceneItem cannot be resized (only ComposedNodeItem can)
std::list< AbstractSceneItem * > _inPorts
virtual ScenePortItem * getCtrlOutPortItem()
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
virtual void update(GuiEvent event, int type, Subject *son)
virtual void setText(QString label)
static bool _autoComputeLinks
Definition: Scene.hxx:37
bool isZooming()
Definition: Scene.cxx:64
virtual YACS::ENGINE::Node * getNode()
virtual std::string getName()
@ INVALID
Definition: define.hxx:36
@ UNDEFINED
Definition: define.hxx:35
@ READY
Definition: define.hxx:37