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