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