]> SALOME platform Git repositories - modules/yacs.git/blob - src/genericgui/EditionSalomeNode.cxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / genericgui / EditionSalomeNode.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 "EditionSalomeNode.hxx"
20 #include "FormComponent.hxx"
21 #include "FormContainer.hxx"
22 #include "ServiceNode.hxx"
23 #include "ComponentInstance.hxx"
24 #include "QtGuiContext.hxx"
25 #include "Container.hxx"
26
27 #include <cassert>
28 #include <map>
29 #include <string>
30
31 //#define _DEVDEBUG_
32 #include "YacsTrace.hxx"
33
34 using namespace std;
35
36 using namespace YACS;
37 using namespace YACS::ENGINE;
38 using namespace YACS::HMI;
39
40 EditionSalomeNode::EditionSalomeNode(Subject* subject,
41                                      QWidget* parent,
42                                      const char* name)
43   : EditionElementaryNode(subject, parent, name)
44 {
45
46   // --- create container and component panels
47
48   _wContainer = new FormContainer(this);
49   _wid->gridLayout1->addWidget(_wContainer);
50
51   connect(_wContainer->cb_host, SIGNAL(mousePressed()),
52           this, SLOT(fillContainerPanel()));
53   connect(_wContainer->cb_host, SIGNAL(activated(int)),
54           this, SLOT(changeHost(int)));
55   connect(_wContainer->tb_container, SIGNAL(toggled(bool)),
56           this, SLOT(fillContainerPanel())); // --- to update display of current selection
57
58   _wComponent = new FormComponent(this);
59   _wid->gridLayout1->addWidget(_wComponent);
60
61   connect(_wComponent->cb_instance, SIGNAL(mousePressed()),
62           this, SLOT(fillComponentPanel()));
63   connect(_wComponent->cb_instance, SIGNAL(activated(int)),
64           this, SLOT(changeInstance(int)));
65
66   connect(_wComponent->cb_container, SIGNAL(mousePressed()),
67           this, SLOT(fillContainerPanel()));
68   connect(_wComponent->cb_container, SIGNAL(activated(int)),
69           this, SLOT(changeContainer(int)));
70
71   connect(_wComponent->tb_component, SIGNAL(toggled(bool)),
72           this, SLOT(fillComponentPanel())); // --- to update display of current selection
73   connect(_wComponent->tb_component, SIGNAL(toggled(bool)),
74           this, SLOT(fillContainerPanel())); // --- to update display of current selection
75
76   // --- method
77
78   _hbl_method = new QHBoxLayout();
79   _la_method = new QLabel(this);
80   _hbl_method->addWidget(_la_method);
81   _le_method = new QLineEdit(this);
82   _hbl_method->addWidget(_le_method);
83   _wid->gridLayout1->addLayout(_hbl_method, _wid->gridLayout1->rowCount(), 0, 1, 1);
84   _la_method->setText("Method:");
85 //   SubjectServiceNode *ssn = dynamic_cast<SubjectServiceNode*>(_subject);
86 //   assert(ssn);
87   _servNode =
88     dynamic_cast<YACS::ENGINE::ServiceNode*>(_subElemNode->getNode());
89   assert(_servNode);
90   _le_method->setText((_servNode->getMethod()).c_str());
91   _le_method->setReadOnly(true);
92
93   // --- fill component panel
94
95   fillComponentPanel();
96
97   createTablePorts();
98 }
99
100 EditionSalomeNode::~EditionSalomeNode()
101 {
102 }
103
104 void EditionSalomeNode::update(GuiEvent event, int type, Subject* son)
105 {
106   DEBTRACE("EditionSalomeNode::update ");
107   EditionElementaryNode::update(event, type, son);
108   SubjectReference* subref= 0;
109    switch (event)
110     {
111     case ADDREF:
112       DEBTRACE("ADDREF");
113       subref = dynamic_cast<SubjectReference*>(son);
114       assert(subref);
115       DEBTRACE(subref->getName() << " " << subref->getReference()->getName());
116       fillComponentPanel();
117       break;
118
119     case ASSOCIATE:
120       DEBTRACE("ASSOCIATE");       
121       fillContainerPanel();
122       break;
123
124     default:
125       ;
126     } 
127 }
128
129 /*! must be updated when associate service to component instance, or when the list of
130  *  available component instances changes.
131  */
132 void EditionSalomeNode::fillComponentPanel()
133 {
134   ComponentInstance *compoInst = _servNode->getComponent();
135   if (compoInst)
136     {
137       _wComponent->le_name ->setText(compoInst->getCompoName().c_str());
138       Proc* proc = GuiContext::getCurrent()->getProc();
139
140       _wComponent->cb_instance->clear();
141       map<pair<string,int>,ComponentInstance*>::const_iterator it = proc->componentInstanceMap.begin();
142       for(; it != proc->componentInstanceMap.end(); ++it)
143         {
144           ComponentInstance *inst=(*it).second;
145           QString compoName = inst->getCompoName().c_str();
146           if (! QString::compare(compoInst->getCompoName().c_str(), compoName)) // if same component name
147             //instanceMap[inst->getInstanceName()] = inst;
148             _wComponent->cb_instance->addItem( QString(inst->getInstanceName().c_str()));
149         }
150
151       int index = _wComponent->cb_instance->findText(compoInst->getInstanceName().c_str());
152       //DEBTRACE("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " << index);
153       _wComponent->cb_instance->setCurrentIndex(index);  
154     }
155 }
156
157 void EditionSalomeNode::fillContainerPanel()
158 {
159   ComponentInstance *compoInst = _servNode->getComponent();
160   if (compoInst)
161     {
162       Proc* proc = GuiContext::getCurrent()->getProc();
163
164       _wComponent->cb_container->clear();
165       map<string,Container*>::const_iterator it = proc->containerMap.begin();
166       for(; it != proc->containerMap.end(); ++it)
167         _wComponent->cb_container->addItem( QString((*it).first.c_str()));
168
169       int index = _wComponent->cb_container->findText(compoInst->getContainer()->getName().c_str());
170       _wComponent->cb_container->setCurrentIndex(index);  
171
172       _wContainer->le_name->setText(compoInst->getContainer()->getName().c_str());
173       _wContainer->le_instance->setReadOnly(true);
174
175       _wContainer->cb_host->clear();
176       _wContainer->cb_host->addItem(""); // --- when no host selected
177       list<string> machines = QtGuiContext::getQtCurrent()->getGMain()->getMachineList();
178       list<string>::iterator itm = machines.begin();
179       for( ; itm != machines.end(); ++itm)
180         {
181           _wContainer->cb_host->addItem(QString((*itm).c_str()));
182         }
183     }
184 }
185
186
187 void EditionSalomeNode::changeInstance(int index)
188 {
189   string instName = _wComponent->cb_instance->itemText(index).toStdString();
190   int i = instName.find_last_of('_');
191   if (i<0) return;
192
193   DEBTRACE(instName << " "  << i);
194   string compoName = instName;
195   compoName.erase(i);
196   string inst = instName;
197   inst.erase(0,i+1);
198   DEBTRACE(instName << " " << compoName << " " << inst);
199   i = atoi(inst.c_str());
200   pair<string,int> aKey(compoName,i);
201
202   Proc* proc = GuiContext::getCurrent()->getProc();
203   ComponentInstance *newCompoInst = 0;
204   ComponentInstance *oldCompoInst = _servNode->getComponent();
205   if (proc->componentInstanceMap.count(aKey))
206     newCompoInst = proc->componentInstanceMap[aKey];
207   else DEBTRACE("-------------> not found : " << instName);
208
209   if (newCompoInst && (newCompoInst != oldCompoInst))
210     {
211       assert(GuiContext::getCurrent()->_mapOfSubjectComponent.count(newCompoInst));
212       SubjectServiceNode *ssn = dynamic_cast<SubjectServiceNode*>(_subject);
213       ssn->associateToComponent(GuiContext::getCurrent()->_mapOfSubjectComponent[newCompoInst]);
214     }
215 }
216
217 void EditionSalomeNode::changeContainer(int index)
218 {
219   string contName = _wComponent->cb_container->itemText(index).toStdString();
220   DEBTRACE(contName);
221   ComponentInstance *compoInst = _servNode->getComponent();
222   Container *oldContainer = compoInst->getContainer();
223
224   Container *newContainer = 0;
225   Proc* proc = GuiContext::getCurrent()->getProc();
226   if (proc->containerMap.count(contName))
227     newContainer = proc->containerMap[contName];
228   if (!newContainer)
229     {
230       DEBTRACE("-------------> not found : " << contName);
231       return;
232     }
233   assert(GuiContext::getCurrent()->_mapOfSubjectContainer.count(newContainer));
234   SubjectContainer *scnt = GuiContext::getCurrent()->_mapOfSubjectContainer[newContainer];
235
236   SubjectServiceNode *ssn = dynamic_cast<SubjectServiceNode*>(_subject);
237   SubjectComponent *sco =
238     dynamic_cast<SubjectComponent*>(ssn->getSubjectReference()->getReference());
239   assert (sco);
240   sco->associateToContainer(scnt);
241 }
242
243 void EditionSalomeNode::changeHost(int index)
244 {
245   string hostName = _wContainer->cb_host->itemText(index).toStdString();
246   DEBTRACE(hostName);
247 }