1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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
20 #include "SceneItem.hxx"
22 #include "SceneNodeItem.hxx"
23 #include "SceneHeaderNodeItem.hxx"
24 #include "SceneProcItem.hxx"
25 #include "SceneElementaryNodeItem.hxx"
26 #include "SceneComposedNodeItem.hxx"
27 #include "GuiEditor.hxx"
29 #include "QtGuiContext.hxx"
31 #include <QGraphicsSceneHoverEvent>
37 #include "Resource.hxx"
40 #include "YacsTrace.hxx"
43 using namespace YACS::ENGINE;
44 using namespace YACS::HMI;
46 // ============================================================================
48 RootSceneItem::RootSceneItem(YACS::HMI::Subject *context)
51 _context->attach(this);
54 RootSceneItem::~RootSceneItem()
58 void RootSceneItem::update(GuiEvent event, int type, Subject* son)
60 DEBTRACE("RootSceneItem::update "<<eventName(event)<<" "<<type<<" "<<son);
61 GuiEditor *guiEditor = 0;
64 case YACS::HMI::NEWROOT:
67 case YACS::HMI::ENDLOAD:
68 guiEditor = QtGuiContext::getQtCurrent()->getGMain()->_guiEditor;
69 GuiContext::getCurrent()->getSubjectProc()->select(true);
70 //guiEditor->rebuildLinks();
75 void RootSceneItem::setNewRoot(YACS::HMI::Subject *root)
77 DEBTRACE("RootSceneItem::setNewRoot");
79 QString name = _root->getName().c_str();
80 QGraphicsScene* scene = QtGuiContext::getQtCurrent()->getScene();
81 SceneProcItem *procItem = new SceneProcItem(scene, 0, name, root);
82 scene->addItem(procItem);
83 procItem->addHeader();
86 // ============================================================================
88 AbstractSceneItem::AbstractSceneItem(QGraphicsScene *scene, SceneItem *parent,
91 _scene = dynamic_cast<Scene*>(scene);
97 _incHeight = 0; // used in elementaryNode when ports added
98 _penColor = Resource::Scene_pen;
99 _hiPenColor = Resource::Scene_hiPen;
100 _brushColor = Resource::Scene_brush;
101 _hiBrushColor = Resource::Scene_hiBrush;
103 _optimize = true; // to be set individually or globally by user (shrink items)
105 _dragButton = Qt::LeftButton;
107 _level = _parent->getLevel() +1;
108 DEBTRACE("AbstractSceneItem::AbstractSceneItem "<<label.toStdString()
109 <<" "<<this<<" "<<_parent<<" "<< _level);
112 AbstractSceneItem::~AbstractSceneItem()
116 int AbstractSceneItem::getLevel()
121 void AbstractSceneItem::setLevel()
124 _level = _parent->getLevel() +1;
125 if (SceneItem *item = dynamic_cast<SceneItem*>(this))
126 foreach (QGraphicsItem *child, item->childItems())
127 if (AbstractSceneItem *sci = dynamic_cast<AbstractSceneItem*>(child))
131 void AbstractSceneItem::reorganize()
135 QString AbstractSceneItem::getLabel()
140 void AbstractSceneItem::addHeader()
144 void AbstractSceneItem::addProgressItem()
148 qreal AbstractSceneItem::getHeaderBottom()
153 qreal AbstractSceneItem::getWidth()
158 qreal AbstractSceneItem::getHeight()
163 //! AbstractSceneItem cannot be resized (only ComposedNodeItem can)
164 void AbstractSceneItem::setWidth(qreal width)
168 //! AbstractSceneItem cannot be resized (only ComposedNodeItem can)
169 void AbstractSceneItem::setHeight(qreal height)
173 QRectF AbstractSceneItem::childBoundingRect(AbstractSceneItem *child) const
175 QGraphicsItem *item = dynamic_cast<QGraphicsItem*>(child);
177 return (item->mapToParent(item->boundingRect())).boundingRect();
180 void AbstractSceneItem::activateSelection(bool selected)
182 if (_parent) _parent->activateSelection(selected);
185 void AbstractSceneItem::setGeometryOptimization(bool optimize)
187 _optimize = optimize;
189 // ============================================================================
191 SceneItem::SceneItem(QGraphicsScene *scene, SceneItem *parent,
193 : QGraphicsItem(parent), AbstractSceneItem(scene, parent, label)
196 _ancestorShrinked = false;
198 DEBTRACE("SceneItem::SceneItem "<<label.toStdString()<<" "<<this<<" "<<_parent<<" "<< _level);
199 setFlag(QGraphicsItem::ItemIsSelectable);
200 setAcceptHoverEvents(true);
203 SceneItem::~SceneItem()
207 void SceneItem::setParent(SceneItem* parent)
209 setParentItem(parent);
212 _level = _parent->getLevel() +1;
215 QRectF SceneItem::boundingRect() const
217 // DEBTRACE("SceneItem::boundingRect " <<_label.toStdString()
218 // <<" "<<_width<<" "<< _height);
220 return QRectF(- penWidth/2, - penWidth/2,
221 _width + penWidth/2, _height + penWidth/2);
224 QRectF SceneItem::childrenBoundingRect() const
226 return QGraphicsItem::childrenBoundingRect();
229 void SceneItem::paint(QPainter *painter,
230 const QStyleOptionGraphicsItem *option,
233 //DEBTRACE("SceneItem::paint");
235 // painter->setPen(getPenColor());
236 // painter->setBrush(getBrushColor());
237 // painter->drawRoundRect(QRectF(0, 0, _width, _height), 33*_height/_width, 33);
238 // painter->restore();
241 void SceneItem::setTopLeft(QPointF topLeft)
243 setPos(int(topLeft.x()), int(topLeft.y()));
245 _parent->checkGeometryChange();
248 void SceneItem::checkGeometryChange()
250 DEBTRACE("SceneItem::checkGeometryChange: enter : " << _label.toStdString() << " width= " << _width << " height= " << _height);
254 SceneElementaryNodeItem* aElemNode = dynamic_cast<SceneElementaryNodeItem*>(this);
255 if (QtGuiContext::getQtCurrent()->isLoadingPresentation())
268 DEBTRACE("elementaryNode resize true");
272 QRectF childrenBox = childrenBoundingRect();
273 newWidth = childrenBox.width();
274 newHeight = childrenBox.height();
275 DEBTRACE("composedNode newWidth= " << newWidth << " newHeight=" << newHeight);
278 bool wider = (newWidth > _width + 0.5);
280 bool higher = (newHeight > _height + 0.5);
282 bool changeWidth = (fabs(newWidth - _width) > 0.5);
284 if (wider || (_optimize && (newWidth < _width)))
286 deltaW = newWidth - _width;
289 if (higher || (_optimize && (newHeight < _height)))
291 deltaH = newHeight - _height;
294 if (_incHeight >0) //when a port has been added in an elementaryNode, force the collision resolv
301 DEBTRACE("SceneItem::checkGeometryChange "<<_label.toStdString() <<
302 " " << wider << " " << higher << " " << changeWidth << " " << resize);
306 prepareGeometryChange();
310 SceneNodeItem *aNode = dynamic_cast<SceneNodeItem*>(this);
313 if (changeWidth) aNode->adjustHeader();
316 QPointF oldPos(pos().x() - deltaW, pos().y() - deltaH);
317 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
318 bloc->collisionResolv(aNode, oldPos);
320 if (changeWidth) aNode->updateLinks();
326 _parent->checkGeometryChange();
329 aNode->setExpandedWH();
332 /*! generic behaviour for headers:
333 * obtain the tooltip from parent.
334 * Method to be redefined in derived classes.
336 QString SceneItem::getToolTip()
338 QString tooltip = _label;
339 SceneItem *parent = getParent();
341 tooltip = parent->getToolTip();
346 // * When Zooming, filter all mouse events to items:
347 // * do not work, scene do not receive...
349 // bool SceneItem::sceneEvent(QEvent *event)
351 // if (_scene->isZooming())
354 // return QGraphicsItem::sceneEvent(event);
357 void SceneItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
359 DEBTRACE("SceneItem::mousePressEvent " << _label.toStdString()
360 << " " << acceptedMouseButtons () << " " << _scene->isZooming());
361 if (!_scene->isZooming()) activateSelection(true);
364 QColor SceneItem::getPenColor()
372 QColor SceneItem::hoverColor(QColor origColor)
375 origColor.getRgbF(&r, &g, &b, &a);
379 return QColor::fromRgbF(r, g, b, a);
382 // origColor.getHsvF(&h, &s, &v, &a);
384 // if (s>254.0) s=255.0;
385 // return QColor::fromHsvF(h, s, v, a);
388 void SceneItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
394 void SceneItem::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
398 void SceneItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
404 QColor SceneItem::getBrushColor()
408 color = _hiBrushColor;
412 color = hoverColor(color);
416 void SceneItem::popupMenu(QWidget *caller, const QPoint &globalPos)
419 m.popupMenu(caller, globalPos);
422 void SceneItem::setEventPos(QPointF point)
424 QPointF localPoint = mapFromScene(point);
425 if (localPoint.y() <= getHeaderBottom())
426 localPoint.setY(getHeaderBottom()+1);
427 _eventPos = localPoint;
430 void SceneItem::updateChildItems()
434 void SceneItem::shrinkExpandLink(bool se)
439 void SceneItem::shrinkExpandRecursive(bool isExpanding, bool fromHere, ShrinkMode theShrinkMode)
443 void SceneItem::updateLinks()