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 "SceneElementaryNodeItem.hxx"
21 #include "SceneInPortItem.hxx"
22 #include "SceneOutPortItem.hxx"
23 #include "SceneComposedNodeItem.hxx"
24 #include "guiObservers.hxx"
25 #include "commandsProc.hxx"
27 #include "ElementaryNode.hxx"
28 #include "InputPort.hxx"
29 #include "OutputPort.hxx"
31 #include "QtGuiContext.hxx"
34 #include "Resource.hxx"
39 #include "YacsTrace.hxx"
42 using namespace YACS::ENGINE;
43 using namespace YACS::HMI;
45 SceneElementaryNodeItem::SceneElementaryNodeItem(QGraphicsScene *scene, SceneItem *parent,
46 QString label, Subject *subject)
47 : SceneNodeItem(scene, parent, label, subject)
50 _width = 2*Resource::Corner_Margin + 2*Resource::DataPort_Width + Resource::Space_Margin;
51 _height = Resource::Header_Height + Resource::Border_Margin;
53 _brushColor = Resource::ElementaryNode_brush;
54 _hiBrushColor = Resource::ElementaryNode_hiBrush;
55 _penColor = Resource::ElementaryNode_pen;
56 _hiPenColor = Resource::ElementaryNode_hiPen;
59 SceneElementaryNodeItem::~SceneElementaryNodeItem()
63 //! SceneElementaryNodeItem cannot be resized (only ComposedNodeItem can)
64 void SceneElementaryNodeItem::setWidth(qreal width)
68 //! SceneElementaryNodeItem cannot be resized (only ComposedNodeItem can)
69 void SceneElementaryNodeItem::setHeight(qreal height)
73 void SceneElementaryNodeItem::paint(QPainter *painter,
74 const QStyleOptionGraphicsItem *option,
77 //DEBTRACE("SceneElementaryNodeItem::paint");
79 QPen pen(getPenColor());
80 pen.setWidth(Resource::Thickness);
82 painter->setBrush(getBrushColor());
83 int w = _width - 2*Resource::Border_Margin;
84 int h = _height - 2*Resource::Border_Margin;
85 painter->drawRect(QRectF(Resource::Border_Margin, Resource::Border_Margin, w, h));
89 void SceneElementaryNodeItem::update(GuiEvent event, int type, Subject* son)
91 DEBTRACE("SceneElementaryNodeItem::update "<< eventName(event)<<" "<<type<<" "<<son);
92 SceneNodeItem::update(event, type, son);
99 case YACS::HMI::INPUTPORT:
100 case YACS::HMI::INPUTDATASTREAMPORT:
101 item = new SceneInPortItem(_scene,
103 son->getName().c_str(),
105 autoPosNewPort(item, _inPorts.size());
106 _inPorts.push_back(item);
107 if (Scene::_autoComputeLinks && !QtGuiContext::getQtCurrent()->isLoading())
109 YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
110 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
111 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
112 proc->rebuildLinks();
115 case YACS::HMI::OUTPUTPORT:
116 case YACS::HMI::OUTPUTDATASTREAMPORT:
117 item = new SceneOutPortItem(_scene,
119 son->getName().c_str(),
121 autoPosNewPort(item, _outPorts.size());
122 _outPorts.push_back(item);
123 if (Scene::_autoComputeLinks && !QtGuiContext::getQtCurrent()->isLoading())
125 YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
126 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
127 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
128 proc->rebuildLinks();
132 // DEBTRACE("SceneElementaryNodeItem::update() ADD, type not handled:" << type);
135 case YACS::HMI::REMOVE:
136 case YACS::HMI::SYNCHRO:
140 // DEBTRACE("SceneElementaryNodeItem::update(), event not handled:" << eventName(event));
144 void SceneElementaryNodeItem::autoPosNewPort(AbstractSceneItem *item, int nbPorts) {
145 DEBTRACE("SceneElementaryNodeItem::autoPosNewPort "<< _label.toStdString());
146 SceneInPortItem* inPortItem = dynamic_cast<SceneInPortItem*>(item);
147 SceneOutPortItem* outPortItem = dynamic_cast<SceneOutPortItem*>(item);
149 bool toShow = (_shownState == expandShown);
153 x = Resource::Corner_Margin;
156 x = Resource::Corner_Margin + Resource::DataPort_Width + Resource::Space_Margin;
159 qreal y = Resource::Header_Height + nbPorts * (Resource::DataPort_Height + Resource::Space_Margin);
161 if (_maxPorts <= nbPorts) {
162 _maxPorts = nbPorts+1;
163 _height = Resource::Header_Height + Resource::Border_Margin;
164 _height += _maxPorts * (Resource::DataPort_Height + Resource::Space_Margin);
165 _incHeight = _height; // must just be more than the actual increment of height
166 DEBTRACE("SceneElementaryNodeItem::autoPosNewPort _height=" << _height);
169 item->setTopLeft(QPointF(x, y));
172 _height = Resource::Header_Height + Resource::Border_Margin;
173 qreal y = Resource::Corner_Margin;
175 item->setTopLeft(QPointF(Resource::Corner_Margin, y));
178 item->setTopLeft(QPointF(Resource::Corner_Margin + Resource::DataPort_Width + Resource::Space_Margin, y));
184 void SceneElementaryNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
186 ElementaryNodeMenu m;
187 m.popupMenu(caller, globalPos);
190 void SceneElementaryNodeItem::reorganizeShrinkExpand()
192 DEBTRACE("SceneElementaryNodeItem::reorganizeShrinkExpand " << isExpanded() << " " << _label.toStdString());
193 shrinkExpandRecursive(isExpanded(), true);
194 if (Scene::_autoComputeLinks)
196 SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
197 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
198 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
199 proc->rebuildLinks();
203 void SceneElementaryNodeItem::shrinkExpandRecursive(bool isExpanding, bool fromHere)
205 DEBTRACE("SceneElementaryNodeItem::shrinkExpandRecursive " << isExpanding << " " << fromHere << " " << isExpanded() << " " << _label.toStdString());
208 _ancestorShrinked = false;
210 _shownState = expandShown;
212 _shownState = shrinkShown;
217 _shownState = shrinkShown;
220 _ancestorShrinked = true;
221 _shownState = shrinkHidden;
225 if (_shownState == shrinkHidden) // shrink of ancestor
228 setPos(_expandedPos);
233 void SceneElementaryNodeItem::reorganize()
235 DEBTRACE("SceneElementaryNodeItem::reorganize() " << _label.toStdString());
237 SubjectNode* snode = dynamic_cast<SubjectNode*>(_subject);
238 ElementaryNode* father = dynamic_cast<ElementaryNode*>(snode->getNode());
243 list<InPort*> plisti = father->getSetOfInPort();
244 list<InPort*>::iterator iti = plisti.begin();
246 for (; iti != plisti.end(); ++iti)
248 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*iti)];
249 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
250 autoPosNewPort(item, nbPorts);
254 list<OutPort*> plisto = father->getSetOfOutPort();
255 list<OutPort*>::iterator ito = plisto.begin();
257 for (; ito != plisto.end(); ++ito)
259 Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*ito)];
260 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
261 autoPosNewPort(item, nbPorts);
267 void SceneElementaryNodeItem::setShownState(shownState ss)
270 if (_shownState == shrinkHidden)
272 _ancestorShrinked = true;
277 _ancestorShrinked = false;