Salome HOME
cbefd9003714c72ca0a8d1e8f1ee39c281ab9c32
[modules/yacs.git] / src / genericgui / SceneElementaryNodeItem.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "SceneElementaryNodeItem.hxx"
20 #include "SceneInPortItem.hxx"
21 #include "SceneOutPortItem.hxx"
22 #include "guiObservers.hxx"
23 #include "commandsProc.hxx"
24 #include "Scene.hxx"
25 #include "ElementaryNode.hxx"
26 #include "InputPort.hxx"
27 #include "OutputPort.hxx"
28
29 #include "QtGuiContext.hxx"
30 #include "Menus.hxx"
31
32 #include <cassert>
33
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36
37 using namespace std;
38 using namespace YACS::ENGINE;
39 using namespace YACS::HMI;
40
41 SceneElementaryNodeItem::SceneElementaryNodeItem(QGraphicsScene *scene, SceneItem *parent,
42                                              QString label, Subject *subject)
43   : SceneNodeItem(scene, parent, label, subject)
44 {
45   _brushColor   = QColor(189,230,185);
46   _hiBrushColor = QColor(209,255,205);
47   _penColor     = QColor( 15,180,  0);
48   _hiPenColor   = QColor( 11,128,  0);
49 }
50
51 SceneElementaryNodeItem::~SceneElementaryNodeItem()
52 {
53 }
54
55 //! SceneElementaryNodeItem cannot be resized (only ComposedNodeItem can)
56 void SceneElementaryNodeItem::setWidth(qreal width)
57 {
58 }
59
60 //! SceneElementaryNodeItem cannot be resized (only ComposedNodeItem can)
61 void SceneElementaryNodeItem::setHeight(qreal height)
62 {
63 }
64
65 void SceneElementaryNodeItem::paint(QPainter *painter,
66                           const QStyleOptionGraphicsItem *option,
67                           QWidget *widget)
68 {
69   //DEBTRACE("SceneElementaryNodeItem::paint");
70   painter->save();
71   painter->setBrush(QBrush(Qt::NoBrush));
72   painter->setPen(QPen(Qt::NoPen));
73   painter->drawRect(QRectF(0, 0, _width, _height));
74   painter->setPen(getPenColor());
75   painter->setBrush(getBrushColor());
76   painter->drawRect(QRectF(_nml, _nml,
77                            _width-2*_nml, _height-2*_nml));
78   painter->restore();
79 }
80
81 void SceneElementaryNodeItem::update(GuiEvent event, int type, Subject* son)
82 {
83   DEBTRACE("SceneElementaryNodeItem::update "<< eventName(event)<<" "<<type<<" "<<son);
84   SceneNodeItem::update(event, type, son);
85   SceneItem *item;
86   switch (event)
87     {
88     case YACS::HMI::ADD:
89       switch (type)
90         {
91         case YACS::HMI::INPUTPORT:
92         case YACS::HMI::INPUTDATASTREAMPORT:
93           item =  new SceneInPortItem(_scene,
94                                       this,
95                                       son->getName().c_str(),
96                                       son);
97           autoPosNewPort(item, _inPorts.size());
98           _inPorts.push_back(item);
99           break;
100         case YACS::HMI::OUTPUTPORT:
101         case YACS::HMI::OUTPUTDATASTREAMPORT:
102           item =  new SceneOutPortItem(_scene,
103                                        this,
104                                        son->getName().c_str(),
105                                        son);
106           autoPosNewPort(item, _outPorts.size());
107           _outPorts.push_back(item);
108            break;
109 //         default:
110 //           DEBTRACE("SceneElementaryNodeItem::update() ADD, type not handled:" << type);
111         }
112       break;
113     case YACS::HMI::REMOVE:
114     case YACS::HMI::SYNCHRO:
115       reorganize();
116       break;
117 //     default:
118 //       DEBTRACE("SceneElementaryNodeItem::update(), event not handled:" << eventName(event));
119     }
120 }
121
122 void SceneElementaryNodeItem::popupMenu(QWidget *caller, const QPoint &globalPos)
123 {
124   ElementaryNodeMenu m;
125   m.popupMenu(caller, globalPos);
126 }
127
128 void SceneElementaryNodeItem::reorganize()
129 {
130   DEBTRACE("SceneElementaryNodeItem::reorganize() " << _label.toStdString());
131
132   SubjectNode* snode = dynamic_cast<SubjectNode*>(_subject);
133   ElementaryNode* father = dynamic_cast<ElementaryNode*>(snode->getNode());
134   assert(father);
135
136   list<InputPort*> plisti = father->getSetOfInputPort();
137   list<InputPort*>::iterator iti = plisti.begin();
138   int nbPorts = 0;
139   for (; iti != plisti.end(); ++iti)
140     {
141       Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*iti)];
142       SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
143       autoPosNewPort(item, nbPorts);
144       nbPorts++;
145     }
146
147   list<OutputPort*> plisto = father->getSetOfOutputPort();
148   list<OutputPort*>::iterator ito = plisto.begin();
149   nbPorts = 0;
150   for (; ito != plisto.end(); ++ito)
151     {
152       Subject *sub = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[(*ito)];
153       SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
154       autoPosNewPort(item, nbPorts);
155       nbPorts++;
156     }
157 }