Salome HOME
Copyright update: 2016
[modules/yacs.git] / src / genericgui / FormAdvParamContainer.cxx
1 // Copyright (C) 2006-2016  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 "FormAdvParamContainer.hxx"
21 #include "FormComponent.hxx"
22 #include "QtGuiContext.hxx"
23 #include "Container.hxx"
24
25 #include <cassert>
26 #include <cstdlib>
27 #include <climits>
28
29 //#define _DEVDEBUG_
30 #include "YacsTrace.hxx"
31
32 #include <QList>
33 #include <sstream>
34
35 using namespace std;
36 using namespace YACS;
37 using namespace YACS::HMI;
38 using namespace YACS::ENGINE;
39
40 FormAdvParamContainer::FormAdvParamContainer(std::map<std::string, std::string>& prop, QWidget *parent):QWidget(parent),_properties(prop)
41 {
42   setupUi(this);
43   sb_mem->setMaximum(INT_MAX);
44   sb_cpu->setMaximum(INT_MAX);
45   sb_nbNodes->setMaximum(INT_MAX);
46   sb_procNode->setMaximum(INT_MAX);
47   sb_nbprocpar->setMaximum(INT_MAX);
48   sb_nbproc->setMaximum(INT_MAX);
49   
50   FillPanel("",0); // --- set widgets before signal connexion to avoid false modif detection
51
52   connect(cb_policy, SIGNAL(activated(const QString&)), this, SLOT(onModifyPolicy(const QString&)));
53   connect(cb_parallel, SIGNAL(activated(const QString&)), this, SLOT(onModifyParLib(const QString&)));
54   connect(le_workdir, SIGNAL(textChanged(const QString&)), this, SLOT(onModifyWorkDir(const QString&)));
55   connect(le_contname, SIGNAL(textChanged(const QString&)), this, SLOT(onModifyContName(const QString&)));
56   connect(le_os, SIGNAL(textChanged(const QString&)), this, SLOT(onModifyOS(const QString&)));
57   connect(le_hostname, SIGNAL(textChanged(const QString&)), this, SLOT(onModifyHostName(const QString&)));
58   connect(le_compolist, SIGNAL(textChanged(const QString&)), this, SLOT(onModifyCompoList(const QString&)));
59   connect(le_resourceList, SIGNAL(textChanged(const QString&)), this, SLOT(onModifyResourceList(const QString&)));
60   connect(ch_mpi, SIGNAL(clicked(bool)), this, SLOT(onModifyIsMPI(bool)));
61   connect(sb_mem, SIGNAL(valueChanged(const QString&)), this, SLOT(onModifyMem(const QString&)));
62   connect(sb_cpu, SIGNAL(valueChanged(const QString&)), this, SLOT(onModifyClock(const QString&)));
63   connect(sb_nbNodes, SIGNAL(valueChanged(const QString&)), this, SLOT(onModifyNodes(const QString&)));
64   connect(sb_procNode, SIGNAL(valueChanged(const QString&)), this, SLOT(onModifyProcs(const QString&)));
65   connect(sb_nbprocpar, SIGNAL(valueChanged(const QString&)), this, SLOT(onModifyProcPar(const QString&)));
66   connect(sb_nbproc, SIGNAL(valueChanged(const QString&)), this, SLOT(onModifyProcRes(const QString&)));
67 }
68
69 FormAdvParamContainer::~FormAdvParamContainer()
70 {
71 }
72
73 void FormAdvParamContainer::FillPanel(const std::string& resource, YACS::ENGINE::Container *container)
74 {
75   DEBTRACE("FormContainer::FillPanel");
76   _container = container;
77   if (_container)
78     {
79       _properties = _container->getProperties();
80     }
81   else
82     {
83       _properties.clear();
84     }
85
86   if(_properties.count("type") && _properties["type"]=="multi")
87     cb_mode->setText("multi");
88   else
89     cb_mode->setText("mono");
90
91   vector<string> parlibs;
92   parlibs.push_back("");
93   parlibs.push_back("Mpi");
94   parlibs.push_back("Dummy");
95   cb_parallel->clear();
96   for(int i=0; i< parlibs.size(); i++)
97     cb_parallel->addItem(parlibs[i].c_str());
98   if(_properties.count("parallelLib"))
99     {
100       int i=0;
101       for(i=0; i< parlibs.size(); i++)
102         if(parlibs[i] == _properties["parallelLib"])
103           {
104             cb_parallel->setCurrentIndex(i);
105             break;
106           }
107     }
108   else
109     cb_parallel->setCurrentIndex(0);
110   
111   if(_properties.count("workingdir"))
112     le_workdir->setText(_properties["workingdir"].c_str());
113   else
114     le_workdir->setText("");
115
116   if(_properties.count("container_name"))
117     le_contname->setText(_properties["container_name"].c_str());
118   else
119     le_contname->setText("");
120
121   if(_properties.count("isMPI"))
122     {
123       DEBTRACE("_properties[isMPI]=" << _properties["isMPI"]);
124       if ((_properties["isMPI"] == "0") || (_properties["isMPI"] == "false"))
125         ch_mpi->setCheckState(Qt::Unchecked);
126       else
127         ch_mpi->setCheckState(Qt::Checked);
128     }
129   else
130     ch_mpi->setCheckState(Qt::Unchecked);
131
132   if(_properties.count("nb_parallel_procs"))
133     sb_nbprocpar->setValue(atoi(_properties["nb_parallel_procs"].c_str()));
134   else
135     sb_nbprocpar->setValue(0);
136   
137   updateResource(resource);
138
139   if (!QtGuiContext::getQtCurrent()->isEdition())
140     {
141       //if the schema is in execution do not allow editing
142       cb_parallel->setEnabled(false);
143       le_workdir->setReadOnly(true);
144       le_contname->setReadOnly(true);
145       ch_mpi->setEnabled(false);
146       sb_nbprocpar->setReadOnly(true);
147       le_hostname->setEnabled(false);
148       le_os->setEnabled(false);
149       sb_nbproc->setEnabled(false);
150       sb_mem->setEnabled(false);
151       sb_cpu->setEnabled(false);
152       sb_nbNodes->setEnabled(false);
153       sb_procNode->setEnabled(false);
154       cb_policy->setEnabled(false);
155       le_compolist->setEnabled(false);
156       le_resourceList->setEnabled(false);
157     }
158 }
159
160 void FormAdvParamContainer::onModified()
161 {
162   DEBTRACE("FormContainer::onModified");
163   Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
164   if (!sub) return;
165   YASSERT(QtGuiContext::getQtCurrent()->_mapOfEditionItem.count(sub));
166   QWidget *widget = QtGuiContext::getQtCurrent()->_mapOfEditionItem[sub];
167   ItemEdition *item = dynamic_cast<ItemEdition*>(widget);
168   YASSERT(item);
169   item->setEdited(true);
170 }
171
172 void FormAdvParamContainer::updateResource(const std::string &resource)
173 {
174   DEBTRACE("FormContainer::updateResource " << resource);
175   if(resource.empty())
176   {
177     //the resource is not specified: use automatic and allow editing
178     if(_properties.count("hostname"))
179       le_hostname->setText(_properties["hostname"].c_str());
180     else
181       le_hostname->setText("");
182     le_hostname->setEnabled(true);
183
184     if(_properties.count("OS"))
185       le_os->setText(_properties["OS"].c_str());
186     else
187       le_os->setText("");
188     le_os->setEnabled(true);
189
190     if(_properties.count("nb_resource_procs"))
191       sb_nbproc->setValue(atoi(_properties["nb_resource_procs"].c_str()));
192     else
193       sb_nbproc->setValue(0);
194     sb_nbproc->setEnabled(true);
195
196     if(_properties.count("mem_mb"))
197       sb_mem->setValue(atoi(_properties["mem_mb"].c_str()));
198     else
199       sb_mem->setValue(0);
200     sb_mem->setEnabled(true);
201
202     if(_properties.count("cpu_clock"))
203       sb_cpu->setValue(atoi(_properties["cpu_clock"].c_str()));
204     else
205       sb_cpu->setValue(0);
206     sb_cpu->setEnabled(true);
207
208     if(_properties.count("nb_node"))
209       sb_nbNodes->setValue(atoi(_properties["nb_node"].c_str()));
210     else
211       sb_nbNodes->setValue(0);
212     sb_nbNodes->setEnabled(true);
213
214     if(_properties.count("nb_proc_per_node"))
215       sb_procNode->setValue(atoi(_properties["nb_proc_per_node"].c_str()));
216     else
217       sb_procNode->setValue(0);
218     sb_procNode->setEnabled(true);
219
220     std::vector<std::string> policies;
221     policies.push_back("cycl");
222     policies.push_back("altcycl");
223     policies.push_back("best");
224     policies.push_back("first");
225     cb_policy->clear();
226     for(int i=0; i< policies.size(); i++)
227       cb_policy->addItem(policies[i].c_str());
228     if(_properties.count("policy"))
229       {
230         int i=0;
231         for(i=0; i< policies.size(); i++)
232           if(policies[i] == _properties["policy"])
233             {
234               cb_policy->setCurrentIndex(i);
235               break;
236             }
237       }
238     else
239       cb_policy->setCurrentIndex(1);
240     cb_policy->setEnabled(true);
241
242     if(_properties.count("component_list"))
243       le_compolist->setText(_properties["component_list"].c_str());
244     else
245       le_compolist->setText("");
246     le_compolist->setEnabled(true);
247
248     if(_properties.count("resource_list"))
249       le_resourceList->setText(_properties["resource_list"].c_str());
250     else
251       le_resourceList->setText("");
252     le_resourceList->setEnabled(true);
253   }
254   else
255   {
256     //a specific resource has been chosen: properties are those declared in the resources manager
257     //properties can not be edited
258     std::map<std::string,std::string> properties(_container->getResourceProperties(resource));
259     if(properties.count("hostname"))
260       le_hostname->setText(properties["hostname"].c_str());
261     else
262       le_hostname->setText("");
263     le_hostname->setEnabled(false);
264
265     if(properties.count("OS"))
266       le_os->setText(properties["OS"].c_str());
267     else
268       le_os->setText("");
269     le_os->setEnabled(false);
270
271     if(properties.count("nb_resource_procs"))
272       sb_nbproc->setValue(atoi(properties["nb_resource_procs"].c_str()));
273     else
274       sb_nbproc->setValue(0);
275     sb_nbproc->setEnabled(false);
276
277     if(properties.count("mem_mb"))
278       sb_mem->setValue(atoi(properties["mem_mb"].c_str()));
279     else
280       sb_mem->setValue(0);
281     sb_mem->setEnabled(false);
282
283     if(properties.count("cpu_clock"))
284       sb_cpu->setValue(atoi(properties["cpu_clock"].c_str()));
285     else
286       sb_cpu->setValue(0);
287     sb_cpu->setEnabled(false);
288
289     if(properties.count("nb_node"))
290       sb_nbNodes->setValue(atoi(properties["nb_node"].c_str()));
291     else
292       sb_nbNodes->setValue(0);
293     sb_nbNodes->setEnabled(false);
294
295     if(properties.count("nb_proc_per_node"))
296       sb_procNode->setValue(atoi(properties["nb_proc_per_node"].c_str()));
297     else
298       sb_procNode->setValue(0);
299     sb_procNode->setEnabled(false);
300
301     cb_policy->clear();
302     cb_policy->setEnabled(false);
303
304     if(properties.count("component_list"))
305       le_compolist->setText(properties["component_list"].c_str());
306     else
307       le_compolist->setText("");
308     le_compolist->setEnabled(false);
309
310     if(properties.count("resource_list"))
311       le_resourceList->setText(properties["resource_list"].c_str());
312     else
313       le_resourceList->setText("");
314     le_resourceList->setEnabled(false);
315   }
316 }
317
318 void FormAdvParamContainer::setModeText(const std::string& mode)
319 {
320   cb_mode->setText(mode.c_str());
321 }
322
323 void FormAdvParamContainer::onModifyResource(const QString &text)
324 {
325   DEBTRACE("onModifyResource " << text.toStdString());
326   if (!_container) return;
327   std::string resource=text.toStdString();
328   if(resource=="automatic")resource="";
329   map<string,string> properties = _container->getProperties();
330   _properties["name"] = resource;
331   if (properties["name"] != resource)
332     {
333       // reset resource properties
334       _properties.erase("hostname");
335       _properties.erase("OS");
336       _properties.erase("nb_resource_procs");
337       _properties.erase("mem_mb");
338       _properties.erase("cpu_clock");
339       _properties.erase("nb_node");
340       _properties.erase("nb_proc_per_node");
341       _properties.erase("policy");
342       onModified();
343       updateResource(resource);
344     }
345 }
346
347 void FormAdvParamContainer::onModifyPolicy(const QString &text)
348 {
349   DEBTRACE("onModifyPolicy " << text.toStdString());
350   if (!_container) return;
351   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
352   map<string,string> properties = _container->getProperties();
353   _properties["policy"] = text.toStdString();
354   if (properties["policy"] != text.toStdString())
355     onModified();
356 }
357
358 void FormAdvParamContainer::onModifyWorkDir(const QString &text)
359 {
360   DEBTRACE("onModifyWorkDir " << text.toStdString());
361   if (!_container) return;
362   map<string,string> properties = _container->getProperties();
363   _properties["workingdir"] = text.toStdString();
364   if (properties["workingdir"] != text.toStdString())
365     onModified();
366 }
367
368 void FormAdvParamContainer::onModifyContName(const QString &text)
369 {
370   DEBTRACE("onModifyContName " << text.toStdString());
371   if (!_container) return;
372   map<string,string> properties = _container->getProperties();
373   _properties["container_name"] = text.toStdString();
374   if (properties["container_name"] != text.toStdString())
375     onModified();
376 }
377
378 void FormAdvParamContainer::onModifyOS(const QString &text)
379 {
380   DEBTRACE("onModifyOS " << text.toStdString());
381   if (!_container) return;
382   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
383   map<string,string> properties = _container->getProperties();
384   _properties["OS"] = text.toStdString();
385   if (properties["OS"] != text.toStdString())
386     onModified();
387 }
388
389 void FormAdvParamContainer::onModifyParLib(const QString &text)
390 {
391   DEBTRACE("onModifyParLib " << text.toStdString());
392   if (!_container) return;
393   map<string,string> properties = _container->getProperties();
394   _properties["parallelLib"] = text.toStdString();
395   if (properties["parallelLib"] != text.toStdString())
396     onModified();
397 }
398
399 void FormAdvParamContainer::onModifyIsMPI(bool isMpi)
400 {
401   DEBTRACE("onModifyIsMPI " << isMpi);
402   if (!_container) return;
403   string text = "false";
404   if (isMpi) text = "true";
405   DEBTRACE(text);
406   map<string,string> properties = _container->getProperties();
407   _properties["isMPI"] = text;
408   if (properties["isMPI"] != text)
409     onModified();
410 }
411
412 void FormAdvParamContainer::onModifyMem(const QString &text)
413 {
414   DEBTRACE("onModifyMem " << text.toStdString());
415   if (!_container) return;
416   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
417   map<string,string> properties = _container->getProperties();
418   if(properties.count("mem_mb")==0 )properties["mem_mb"]="0"; //default value
419   _properties["mem_mb"] = text.toStdString();
420   if (properties["mem_mb"] != text.toStdString())
421     onModified();
422 }
423
424 void FormAdvParamContainer::onModifyClock(const QString &text)
425 {
426   DEBTRACE("onModifyClock " << text.toStdString());
427   if (!_container) return;
428   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
429   map<string,string> properties = _container->getProperties();
430   if(properties.count("cpu_clock")==0 )properties["cpu_clock"]="0"; //default value
431   _properties["cpu_clock"] = text.toStdString();
432   if (properties["cpu_clock"] != text.toStdString())
433     onModified();
434 }
435
436 void FormAdvParamContainer::onModifyNodes(const QString &text)
437 {
438   DEBTRACE("onModifyNodes " << text.toStdString());
439   if (!_container) return;
440   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
441   map<string,string> properties = _container->getProperties();
442   if(properties.count("nb_node")==0 )properties["nb_node"]="0"; //default value
443   _properties["nb_node"] = text.toStdString();
444   if (properties["nb_node"] != text.toStdString())
445     onModified();
446 }
447
448 void FormAdvParamContainer::onModifyProcs(const QString &text)
449 {
450   DEBTRACE("onModifyProcs " << text.toStdString());
451   if (!_container) return;
452   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
453   map<string,string> properties = _container->getProperties();
454   if(properties.count("nb_proc_per_node")==0 )properties["nb_proc_per_node"]="0"; //default value
455   _properties["nb_proc_per_node"] = text.toStdString();
456   if (properties["nb_proc_per_node"] != text.toStdString())
457     onModified();
458 }
459
460 void FormAdvParamContainer::onModifyCompos(const QString &text)
461 {
462   DEBTRACE("onModifyCompo " << text.toStdString());
463   if (!_container) return;
464   map<string,string> properties = _container->getProperties();
465   _properties["nb_component_nodes"] = text.toStdString();
466   if (properties["nb_component_nodes"] != text.toStdString())
467     onModified();
468 }
469
470 void FormAdvParamContainer::onModifyProcPar(const QString &text)
471 {
472   DEBTRACE("onModifyProcPar "  << text.toStdString());
473   if (!_container) return;
474   map<string,string> properties = _container->getProperties();
475   _properties["nb_parallel_procs"] = text.toStdString();
476   if (properties["nb_parallel_procs"] != text.toStdString())
477     onModified();
478 }
479
480 void FormAdvParamContainer::onModifyResourceName(const QString &text)
481 {
482   DEBTRACE("onModifyResourceName "  << text.toStdString());
483   if (!_container) return;
484   map<string,string> properties = _container->getProperties();
485   _properties["resource_name"] = text.toStdString();
486   if (properties["resource_name"] != text.toStdString())
487     onModified();
488 }
489
490 void FormAdvParamContainer::onModifyHostName(const QString &text)
491 {
492   DEBTRACE("onModifyHostName "  << text.toStdString());
493   if (!_container) return;
494   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
495
496   map<string,string> properties = _container->getProperties();
497   _properties["hostname"] = text.toStdString();
498   if (properties["hostname"] != text.toStdString())
499     onModified();
500 }
501
502 void FormAdvParamContainer::onModifyProcRes(const QString &text)
503 {
504   DEBTRACE("onModifyProcRes "  << text.toStdString());
505   if (!_container) return;
506   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
507   map<string,string> properties = _container->getProperties();
508   if(properties.count("nb_resource_procs")==0 )properties["nb_resource_procs"]="0"; //default value
509   _properties["nb_resource_procs"] = text.toStdString();
510   if (properties["nb_resource_procs"] != text.toStdString())
511     onModified();
512 }
513
514 void FormAdvParamContainer::onModifyCompoList(const QString &text)
515 {
516   DEBTRACE("onModifyCompoList "  << text.toStdString());
517   if (!_container) return;
518   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
519   map<string,string> properties = _container->getProperties();
520   _properties["component_list"] = text.toStdString();
521   if (properties["component_list"] != text.toStdString())
522     onModified();
523 }
524
525 void FormAdvParamContainer::onModifyResourceList(const QString &text)
526 {
527   DEBTRACE("onModifyResourceList "  << text.toStdString());
528   if (!_container) return;
529   if(_properties.count("name") && _properties["name"] != "")return; //do not modify resource parameter when specific resource is set
530   map<string,string> properties = _container->getProperties();
531   _properties["resource_list"] = text.toStdString();
532   if (properties["resource_list"] != text.toStdString())
533     onModified();
534 }