1 // Copyright (C) 2006-2012 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
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 qreal AbstractSceneItem::getHeaderBottom()
148 qreal AbstractSceneItem::getWidth()
153 qreal AbstractSceneItem::getHeight()
158 //! AbstractSceneItem cannot be resized (only ComposedNodeItem can)
159 void AbstractSceneItem::setWidth(qreal width)
163 //! AbstractSceneItem cannot be resized (only ComposedNodeItem can)
164 void AbstractSceneItem::setHeight(qreal height)
168 QRectF AbstractSceneItem::childBoundingRect(AbstractSceneItem *child) const
170 QGraphicsItem *item = dynamic_cast<QGraphicsItem*>(child);
172 return (item->mapToParent(item->boundingRect())).boundingRect();
175 void AbstractSceneItem::activateSelection(bool selected)
177 if (_parent) _parent->activateSelection(selected);
180 void AbstractSceneItem::setGeometryOptimization(bool optimize)
182 _optimize = optimize;
184 // ============================================================================
186 SceneItem::SceneItem(QGraphicsScene *scene, SceneItem *parent,
188 : QGraphicsItem(parent), AbstractSceneItem(scene, parent, label)
191 _ancestorShrinked = false;
193 DEBTRACE("SceneItem::SceneItem "<<label.toStdString()<<" "<<this<<" "<<_parent<<" "<< _level);
194 setFlag(QGraphicsItem::ItemIsSelectable);
195 setAcceptsHoverEvents(true);
198 SceneItem::~SceneItem()
202 void SceneItem::setParent(SceneItem* parent)
204 setParentItem(parent);
207 _level = _parent->getLevel() +1;
210 QRectF SceneItem::boundingRect() const
212 // DEBTRACE("SceneItem::boundingRect " <<_label.toStdString()
213 // <<" "<<_width<<" "<< _height);
215 return QRectF(- penWidth/2, - penWidth/2,
216 _width + penWidth/2, _height + penWidth/2);
219 QRectF SceneItem::childrenBoundingRect() const
221 return QGraphicsItem::childrenBoundingRect();
224 void SceneItem::paint(QPainter *painter,
225 const QStyleOptionGraphicsItem *option,
228 //DEBTRACE("SceneItem::paint");
230 // painter->setPen(getPenColor());
231 // painter->setBrush(getBrushColor());
232 // painter->drawRoundRect(QRectF(0, 0, _width, _height), 33*_height/_width, 33);
233 // painter->restore();
236 void SceneItem::setTopLeft(QPointF topLeft)
238 setPos(int(topLeft.x()), int(topLeft.y()));
240 _parent->checkGeometryChange();
243 void SceneItem::checkGeometryChange()
245 DEBTRACE("SceneItem::checkGeometryChange: enter : " << _label.toStdString() << " width= " << _width << " height= " << _height);
249 SceneElementaryNodeItem* aElemNode = dynamic_cast<SceneElementaryNodeItem*>(this);
250 if (QtGuiContext::getQtCurrent()->isLoadingPresentation())
263 DEBTRACE("elementaryNode resize true");
267 QRectF childrenBox = childrenBoundingRect();
268 newWidth = childrenBox.width();
269 newHeight = childrenBox.height();
270 DEBTRACE("composedNode newWidth= " << newWidth << " newHeight=" << newHeight);
273 bool wider = (newWidth > _width + 0.5);
275 bool higher = (newHeight > _height + 0.5);
277 bool changeWidth = (fabs(newWidth - _width) > 0.5);
279 if (wider || (_optimize && (newWidth < _width)))
281 deltaW = newWidth - _width;
284 if (higher || (_optimize && (newHeight < _height)))
286 deltaH = newHeight - _height;
289 if (_incHeight >0) //when a port has been added in an elementaryNode, force the collision resolv
296 DEBTRACE("SceneItem::checkGeometryChange "<<_label.toStdString() <<
297 " " << wider << " " << higher << " " << changeWidth << " " << resize);
301 prepareGeometryChange();
305 SceneNodeItem *aNode = dynamic_cast<SceneNodeItem*>(this);
308 if (changeWidth) aNode->adjustHeader();
311 QPointF oldPos(pos().x() - deltaW, pos().y() - deltaH);
312 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
313 bloc->collisionResolv(aNode, oldPos);
315 if (changeWidth) aNode->updateLinks();
321 _parent->checkGeometryChange();
324 aNode->setExpandedWH();
327 /*! generic behaviour for headers:
328 * obtain the tooltip from parent.
329 * Method to be redefined in derived classes.
331 QString SceneItem::getToolTip()
333 QString tooltip = _label;
334 SceneItem *parent = getParent();
336 tooltip = parent->getToolTip();
341 // * When Zooming, filter all mouse events to items:
342 // * do not work, scene do not receive...
344 // bool SceneItem::sceneEvent(QEvent *event)
346 // if (_scene->isZooming())
349 // return QGraphicsItem::sceneEvent(event);
352 void SceneItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
354 DEBTRACE("SceneItem::mousePressEvent " << _label.toStdString()
355 << " " << acceptedMouseButtons () << " " << _scene->isZooming());
356 if (!_scene->isZooming()) activateSelection(true);
359 QColor SceneItem::getPenColor()
367 QColor SceneItem::hoverColor(QColor origColor)
370 origColor.getRgbF(&r, &g, &b, &a);
374 return QColor::fromRgbF(r, g, b, a);
377 // origColor.getHsvF(&h, &s, &v, &a);
379 // if (s>254.0) s=255.0;
380 // return QColor::fromHsvF(h, s, v, a);
383 void SceneItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
389 void SceneItem::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
393 void SceneItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
399 QColor SceneItem::getBrushColor()
403 color = _hiBrushColor;
407 color = hoverColor(color);
411 void SceneItem::popupMenu(QWidget *caller, const QPoint &globalPos)
414 m.popupMenu(caller, globalPos);
417 void SceneItem::setEventPos(QPointF point)
419 QPointF localPoint = mapFromScene(point);
420 if (localPoint.y() <= getHeaderBottom())
421 localPoint.setY(getHeaderBottom()+1);
422 _eventPos = localPoint;
425 void SceneItem::updateChildItems()
429 void SceneItem::shrinkExpandLink(bool se)
434 void SceneItem::shrinkExpandRecursive(bool isExpanding, bool fromHere)
438 void SceneItem::updateLinks()