1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "SceneNodeItem.hxx"
20 #include "SceneComposedNodeItem.hxx"
21 #include "SceneHeaderNodeItem.hxx"
22 #include "SceneInPortItem.hxx"
23 #include "SceneOutPortItem.hxx"
24 #include "SceneCtrlInPortItem.hxx"
25 #include "SceneCtrlOutPortItem.hxx"
27 #include "QtGuiContext.hxx"
32 #include <QGraphicsSceneHoverEvent>
39 #include "YacsTrace.hxx"
42 using namespace YACS::ENGINE;
43 using namespace YACS::HMI;
45 SceneNodeItem::SceneNodeItem(QGraphicsScene *scene, SceneItem *parent,
46 QString label, Subject *subject)
47 : SceneObserverItem(scene, parent, label, subject)
52 _height = 50 + 2*_nml;
53 _width = 2*ScenePortItem::getPortWidth() + 3*_margin + 2*_nml;
54 _brushColor = QColor(0,0,128);
58 _dragButton = Qt::MidButton;
59 _execState = YACS::UNDEFINED;
62 SceneNodeItem::~SceneNodeItem()
64 if (SceneComposedNodeItem* parent = getParent())
65 parent->removeChildFromList(this);
68 void SceneNodeItem::setWidth(qreal width)
74 void SceneNodeItem::setHeight(qreal height)
79 void SceneNodeItem::addHeader()
81 DEBTRACE("SceneNodeItem::addHeader " << _label.toStdString());
85 _header = new SceneHeaderNodeItem(_scene,
89 QPointF topLeft(_margin + _nml, _margin + _nml);
90 _header->setTopLeft(topLeft);
91 checkGeometryChange();
95 SceneHeaderItem* SceneNodeItem::getHeader()
100 void SceneNodeItem::paint(QPainter *painter,
101 const QStyleOptionGraphicsItem *option,
104 //DEBTRACE("SceneNodeItem::paint");
106 painter->setBrush(QBrush(Qt::NoBrush));
107 painter->setPen(QPen(Qt::NoPen));
108 painter->drawRect(QRectF(0, 0, _width, _height));
110 painter->setPen(getPenColor());
111 painter->setBrush(getBrushColor());
112 painter->drawRect(QRectF(_nml, _nml,
113 _width-2*_nml, _height-2*_nml));
117 void SceneNodeItem::update(GuiEvent event, int type, Subject* son)
119 DEBTRACE("SceneNodeItem::update "<< eventName(event)<<" "<<type<<" "<<son);
120 SubjectNode *snode = 0;
124 case YACS::HMI::RENAME:
125 DEBTRACE("SceneNodeItem::update RENAME " << _subject->getName());
128 case YACS::HMI::EDIT:
129 if (_header) _header->setEdited(type);
131 case YACS::HMI::UPDATE:
134 case YACS::HMI::UPDATEPROGRESS:
142 //! generic method to compute a graph for child nodes. implemented in some derived classes
143 void SceneNodeItem::arrangeNodes(bool isRecursive)
147 void SceneNodeItem::arrangeChildNodes()
151 qreal SceneNodeItem::getHeaderBottom()
155 bottom = childBoundingRect(_header).bottom();
159 void SceneNodeItem::autoPosNewPort(AbstractSceneItem *item, int nbPorts)
161 SceneInPortItem* inPortItem = dynamic_cast<SceneInPortItem*>(item);
162 bool isInPort = (inPortItem != 0);
164 qreal xLeft = _margin + _nml;
165 if (!isInPort) xLeft += ScenePortItem::getPortWidth() + _margin + _nml;
167 qreal yTop = getHeaderBottom() + _margin;
168 yTop += nbPorts *(ScenePortItem::getPortHeight() + _margin);
170 DEBTRACE("left, top " << xLeft << " " << yTop);
171 QPointF topLeft(xLeft, yTop);
172 item->setTopLeft(topLeft);
175 void SceneNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
178 m.popupMenu(caller, globalPos);
181 SceneComposedNodeItem* SceneNodeItem::getParent()
184 return dynamic_cast<SceneComposedNodeItem*>(_parent);
188 void SceneNodeItem::removeInPortFromList(AbstractSceneItem* inPort)
190 _inPorts.remove(inPort);
193 void SceneNodeItem::removeOutPortFromList(AbstractSceneItem* outPort)
195 _outPorts.remove(outPort);
198 void SceneNodeItem::setMoving(bool moving)
200 //DEBTRACE("SceneNodeItem::setMoving " << _label.toStdString() << " " << moving);
202 SceneNodeItem *nodep = 0;
203 if (_parent && (nodep = dynamic_cast<SceneNodeItem*>(_parent)))
204 nodep->setMoving(false);
207 ScenePortItem* SceneNodeItem::getCtrlInPortItem()
209 if (! _header) return 0;
210 return _header->getCtrlInPortItem();
213 ScenePortItem* SceneNodeItem::getCtrlOutPortItem()
215 if (! _header) return 0;
216 return _header->getCtrlOutPortItem();
219 void SceneNodeItem::updateName()
221 if (_header) _header->setText(getHeaderLabel());
224 QString SceneNodeItem::getMimeFormat()
226 return "yacs/subjectNode";
229 void SceneNodeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
231 DEBTRACE("SceneNodeItem::mousePressEvent " << _label.toStdString());
232 SceneObserverItem::mousePressEvent(event);
233 if (!_scene->isZooming())
235 if (!_draging) setMoving(true);
240 void SceneNodeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
242 //DEBTRACE("SceneNodeItem::mouseReleaseEvent " << _label.toStdString());
243 SceneObserverItem::mouseReleaseEvent(event);
245 if ((pos() != _prevPos) && Scene::_autoComputeLinks)
247 YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
248 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
249 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
250 proc->rebuildLinks();
254 void SceneNodeItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
256 SceneObserverItem::mouseMoveEvent(event);
259 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
261 QPointF oldPos = pos();
262 QPointF aPos = oldPos + event->scenePos() - event->lastScenePos();
263 if (aPos.x() < _margin + _nml)
264 aPos.setX(_margin + _nml);
265 if (aPos.y() < _margin + bloc->getHeaderBottom() + _nml)
266 aPos.setY(_margin + bloc->getHeaderBottom() + _nml);
268 bloc->collisionResolv(this, oldPos);
273 void SceneNodeItem::setTopLeft(QPointF topLeft)
275 QPointF oldPos = pos();
279 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
280 bloc->collisionResolv(this, oldPos);
281 _parent->checkGeometryChange();
285 void SceneNodeItem::adjustHeader()
287 if (_header) _header->adjustGeometry();
290 void SceneNodeItem::updateState()
292 SubjectNode *snode = dynamic_cast<SubjectNode*>(_subject);
294 Node *node = snode->getNode();
296 switch (node->getState())
299 if (_header) _header->setValid(false);
302 if (_header) _header->setValid(true);
309 void SceneNodeItem::setExecState(int execState)
311 DEBTRACE("SceneNodeItem::setExecState " << execState);
312 _execState = execState;
313 if (_header) _header->setExecState(execState);
316 QString SceneNodeItem::getHeaderLabel()
318 QString extLabel = _subject->getName().c_str();
320 SceneObserverItem *soi = 0;
321 SubjectSwitch* sswi = 0;
324 && (soi = dynamic_cast<SceneObserverItem*>(_parent))
325 && (sswi = dynamic_cast<SubjectSwitch*>(soi->getSubject())))
327 Switch *aswi = dynamic_cast<Switch*>(sswi->getNode());
328 SubjectNode *sno = dynamic_cast<SubjectNode*>(_subject);
329 int idcase = aswi->getRankOfNode(sno->getNode());
331 if (idcase == Switch::ID_FOR_DEFAULT_NODE)
336 //extLabel += aswi->getCaseId(sno->getNode()).c_str();
337 extLabel += caseid.str().c_str();
339 DEBTRACE(extLabel.toStdString());