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 "SceneItem.hxx"
21 #include "SceneNodeItem.hxx"
22 #include "SceneHeaderNodeItem.hxx"
23 #include "SceneProcItem.hxx"
24 #include "SceneComposedNodeItem.hxx"
25 #include "GuiEditor.hxx"
27 #include "QtGuiContext.hxx"
29 #include <QGraphicsSceneHoverEvent>
36 #include "YacsTrace.hxx"
39 using namespace YACS::ENGINE;
40 using namespace YACS::HMI;
42 // ============================================================================
44 RootSceneItem::RootSceneItem(YACS::HMI::Subject *context)
47 _context->attach(this);
50 RootSceneItem::~RootSceneItem()
54 void RootSceneItem::update(GuiEvent event, int type, Subject* son)
56 DEBTRACE("RootSceneItem::update "<<eventName(event)<<" "<<type<<" "<<son);
57 GuiEditor *guiEditor = 0;
60 case YACS::HMI::NEWROOT:
63 case YACS::HMI::ENDLOAD:
64 guiEditor = QtGuiContext::getQtCurrent()->getGMain()->_guiEditor;
65 GuiContext::getCurrent()->getSubjectProc()->select(true);
66 guiEditor->rebuildLinks();
71 void RootSceneItem::setNewRoot(YACS::HMI::Subject *root)
73 DEBTRACE("RootSceneItem::setNewRoot");
75 QString name = _root->getName().c_str();
76 QGraphicsScene* scene = QtGuiContext::getQtCurrent()->getScene();
77 SceneProcItem *procItem = new SceneProcItem(scene, 0, name, root);
78 scene->addItem(procItem);
79 procItem->addHeader();
82 // ============================================================================
84 AbstractSceneItem::AbstractSceneItem(QGraphicsScene *scene, SceneItem *parent,
87 _scene = dynamic_cast<Scene*>(scene);
95 _penColor = QColor(0,0,128);
96 _hiPenColor = QColor(0,0,190);
97 _brushColor = QColor(128,128,128);
98 _hiBrushColor = QColor(190,190,190);
101 _optimize = true; // to be set individually or globally by user (shrink items)
103 _dragButton = Qt::LeftButton;
105 _level = _parent->getLevel() +1;
106 DEBTRACE("AbstractSceneItem::AbstractSceneItem "<<label.toStdString()
107 <<" "<<this<<" "<<_parent<<" "<< _level);
110 AbstractSceneItem::~AbstractSceneItem()
114 int AbstractSceneItem::getLevel()
119 qreal AbstractSceneItem::getMargin()
124 void AbstractSceneItem::reorganize()
128 QString AbstractSceneItem::getLabel()
133 void AbstractSceneItem::addHeader()
137 qreal AbstractSceneItem::getHeaderBottom()
141 qreal AbstractSceneItem::getWidth()
146 qreal AbstractSceneItem::getHeight()
151 //! AbstractSceneItem cannot be resized (only ComposedNodeItem can)
152 void AbstractSceneItem::setWidth(qreal width)
156 //! AbstractSceneItem cannot be resized (only ComposedNodeItem can)
157 void AbstractSceneItem::setHeight(qreal height)
161 qreal AbstractSceneItem::getInternWidth()
163 return _width -2*_margin -2*_hasNml*_nml;
166 qreal AbstractSceneItem::getInternHeight()
168 return _height -2*_hasNml*_nml;
171 QRectF AbstractSceneItem::childBoundingRect(AbstractSceneItem *child) const
173 QGraphicsItem *item = dynamic_cast<QGraphicsItem*>(child);
175 return (item->mapToParent(item->boundingRect())).boundingRect();
178 void AbstractSceneItem::activateSelection(bool selected)
180 if (_parent) _parent->activateSelection(selected);
183 void AbstractSceneItem::setGeometryOptimization(bool optimize)
185 _optimize = optimize;
187 // ============================================================================
189 SceneItem::SceneItem(QGraphicsScene *scene, SceneItem *parent,
191 : QGraphicsItem(parent), AbstractSceneItem(scene, parent, label)
195 DEBTRACE("SceneItem::SceneItem "<<label.toStdString()<<" "<<this<<" "<<_parent<<" "<< _level);
196 setFlag(QGraphicsItem::ItemIsSelectable);
197 setAcceptsHoverEvents(true);
200 SceneItem::~SceneItem()
204 void SceneItem::setParent(SceneItem* parent)
206 setParentItem(parent);
209 _level = _parent->getLevel() +1;
212 QRectF SceneItem::boundingRect() const
214 // DEBTRACE("SceneItem::boundingRect " <<_label.toStdString()
215 // <<" "<<_width<<" "<< _height);
217 return QRectF(- penWidth/2, - penWidth/2,
218 _width + penWidth/2, _height + penWidth/2);
221 QRectF SceneItem::childrenBoundingRect() const
223 return QGraphicsItem::childrenBoundingRect();
226 void SceneItem::paint(QPainter *painter,
227 const QStyleOptionGraphicsItem *option,
230 //DEBTRACE("SceneItem::paint");
232 painter->setPen(getPenColor());
233 painter->setBrush(getBrushColor());
234 painter->drawRoundRect(QRectF(0, 0, _width, _height), 33*_height/_width, 33);
238 void SceneItem::setTopLeft(QPointF topLeft)
242 _parent->checkGeometryChange();
245 void SceneItem::checkGeometryChange()
247 QRectF childrenBox = childrenBoundingRect();
248 qreal newWidth = childrenBox.width() + 2*_margin;
249 qreal newHeight = childrenBox.height() + 2*_margin;
250 SceneNodeItem *aNode = dynamic_cast<SceneNodeItem*>(this);
257 bool wider = (newWidth > _width + 0.5);
259 bool higher = (newHeight > _height + 0.5);
261 bool changeWidth = (fabs(newWidth - _width) > 0.5);
263 if (wider || (_optimize && (newWidth < _width)))
265 deltaW = newWidth - _width;
269 if (higher || (_optimize && (newHeight < _height)))
271 deltaH = newHeight - _height;
275 // DEBTRACE("SceneItem::checkGeometryChange "<<_label.toStdString() <<
276 // " " << wider << " " << higher << " " << changeWidth << " " << resize);
279 if (changeWidth) aNode->adjustHeader();
282 QPointF oldPos(pos().x() - deltaW, pos().y() - deltaH);
283 if (SceneComposedNodeItem *bloc = dynamic_cast<SceneComposedNodeItem*>(_parent))
284 bloc->collisionResolv(aNode, oldPos);
289 // DEBTRACE("SceneItem::checkGeometryChange "<<_label.toStdString()<<" "<<_width<<" "<<_height);
290 prepareGeometryChange();
292 _parent->checkGeometryChange();
297 // * When Zooming, filter all mouse events to items:
298 // * do not work, scene do not receive...
300 // bool SceneItem::sceneEvent(QEvent *event)
302 // if (_scene->isZooming())
305 // return QGraphicsItem::sceneEvent(event);
308 void SceneItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
310 DEBTRACE("SceneItem::mousePressEvent " << _label.toStdString()
311 << " " << acceptedMouseButtons () << " " << _scene->isZooming());
312 if (!_scene->isZooming()) activateSelection(true);
315 QColor SceneItem::getPenColor()
323 QColor SceneItem::hoverColor(QColor origColor)
326 origColor.getHsvF(&h, &s, &v, &a);
328 return QColor::fromHsvF(h, s, v, a);
331 void SceneItem::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
337 void SceneItem::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
341 void SceneItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
347 QColor SceneItem::getBrushColor()
351 color = _hiBrushColor;
355 color = hoverColor(color);
359 void SceneItem::popupMenu(QWidget *caller, const QPoint &globalPos)
362 m.popupMenu(caller, globalPos);