Salome HOME
Fixes and default values in resource edition GUI
[modules/jobmanager.git] / src / genericgui / JM_EditSalomeResource.cxx
1 // Copyright (C) 2009-2013  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
20 #include "JM_EditSalomeResource.hxx"
21 #include "BL_Traces.hxx"
22
23 JM::EditSalomeResource::EditSalomeResource(QWidget *parent, BL::SALOMEServices * salome_services,
24                                            const std::string & resource_name) : QDialog(parent)
25 {
26   DEBTRACE("Creating JM::EditSalomeResource");
27   BL_ASSERT(parent);
28   BL_ASSERT(salome_services);
29   _parent = parent;
30   _salome_services = salome_services;
31   _resource_name = resource_name;
32
33   //setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
34
35   // Widget code
36
37   // Part 1
38   QGroupBox * main_groupBox = new QGroupBox("Main values");
39   QLabel * name_label = new QLabel("Name:");
40   _name_line = new QLineEdit(this);
41   QLabel * hostname_label = new QLabel("Hostname:");
42   _hostname_line = new QLineEdit(this);
43   QLabel * username_label = new QLabel("Username:");
44   _username_line = new QLineEdit(this);
45   QLabel * applipath_label = new QLabel("Applipath:");
46   _applipath_line = new QLineEdit(this);
47
48   QLabel * protocol_label = new QLabel("Protocol:");
49   _protocol_line = new QComboBox(this);
50   _protocol_line->addItem("ssh");
51   _protocol_line->addItem("rsh");
52   _protocol_line->addItem("sh");
53   _protocol_line->addItem("srun");
54   _protocol_line->addItem("pbsdsh");
55   _protocol_line->addItem("blaunch");
56   _protocol_line->setCurrentIndex(0);
57
58   QLabel * componentList_label = new QLabel("Component List:");
59   _add_button = new QPushButton("Add");
60   _remove_button = new QPushButton("Remove");
61   _remove_button->setEnabled(false);
62   QWidget * component_widget = new QWidget(this);
63   _componentList = new QListWidget(this);
64   _componentList->setSelectionMode(QAbstractItemView::MultiSelection);
65   QGridLayout * input_box = new QGridLayout(this);
66   input_box->addWidget(_add_button, 0, 0);
67   input_box->addWidget(_remove_button, 0, 1);
68   input_box->addWidget(_componentList, 1, 0, 1, -1);
69   component_widget->setLayout(input_box);
70   connect(_add_button, SIGNAL(clicked()), this, SLOT(add_component()));
71   connect(_remove_button, SIGNAL(clicked()), this, SLOT(remove_components()));
72   connect(_componentList, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
73
74   QLabel * working_directory_label = new QLabel("Working Directory:");
75   _working_directory = new QLineEdit(this);
76   _can_launch_batch_jobs = new QCheckBox("This resource can be used to launch batch jobs", this);
77   _can_run_containers = new QCheckBox("This resource can be used to run interactive containers", this);
78
79   QGridLayout * m_layout = new QGridLayout;
80   m_layout->addWidget(name_label, 0, 0);
81   m_layout->addWidget(_name_line, 0, 1);
82   m_layout->addWidget(hostname_label, 1, 0);
83   m_layout->addWidget(_hostname_line, 1, 1);
84   m_layout->addWidget(protocol_label, 2, 0);
85   m_layout->addWidget(_protocol_line, 2, 1);
86   m_layout->addWidget(username_label, 3, 0);
87   m_layout->addWidget(_username_line, 3, 1);
88   m_layout->addWidget(applipath_label, 4, 0);
89   m_layout->addWidget(_applipath_line, 4, 1);
90   m_layout->addWidget(componentList_label, 5, 0);
91   m_layout->addWidget(component_widget, 5, 1);
92   m_layout->addWidget(working_directory_label, 6, 0);
93   m_layout->addWidget(_working_directory, 6, 1);
94   m_layout->addWidget(_can_launch_batch_jobs, 7, 1);
95   m_layout->addWidget(_can_run_containers, 8, 1);
96   main_groupBox->setLayout(m_layout);
97
98   // Part 2
99   QGroupBox * config_groupBox = new QGroupBox("Configuration values");
100   QLabel * os_label = new QLabel("OS:");
101   _os_line = new QLineEdit(this);
102
103   QLabel * mem_mb_label = new QLabel("Memory (mb):");
104   _mem_mb_line = new QSpinBox(this);
105   _mem_mb_line->setMinimum(0);
106   _mem_mb_line->setMaximum(1000000);
107   _mem_mb_line->setValue(0);
108   QLabel * cpu_clock_label = new QLabel("CPU Clock:");
109   _cpu_clock_line = new QSpinBox(this);
110   _cpu_clock_line->setMinimum(0);
111   _cpu_clock_line->setMaximum(1000000);
112   _cpu_clock_line->setValue(0);
113   QLabel * nb_node_label = new QLabel("Nb node:");
114   _nb_node_line = new QSpinBox(this);
115   _nb_node_line->setMinimum(1);
116   _nb_node_line->setMaximum(1000000);
117   _nb_node_line->setValue(1);
118   QLabel * nb_proc_per_node_label = new QLabel("Nb proc/node:");
119   _nb_proc_per_node_line = new QSpinBox(this);
120   _nb_proc_per_node_line->setMinimum(1);
121   _nb_proc_per_node_line->setMaximum(1000000);
122   _nb_proc_per_node_line->setValue(1);
123
124   QLabel * iprotocol_label = new QLabel("Internal protocol:");
125   _iprotocol_line = new QComboBox(this);
126   _iprotocol_line->addItem("ssh");
127   _iprotocol_line->addItem("rsh");
128   _iprotocol_line->addItem("srun");
129   _iprotocol_line->addItem("pbsdsh");
130   _iprotocol_line->addItem("blaunch");
131   _iprotocol_line->setCurrentIndex(0);
132
133   QLabel * batch_label = new QLabel("Batch:");
134   _batch_line = new QComboBox(this);
135   _batch_line->addItem("pbs");
136   _batch_line->addItem("lsf");
137   _batch_line->addItem("sge");
138   _batch_line->addItem("ssh");
139   _batch_line->addItem("ccc");
140   _batch_line->addItem("slurm");
141   _batch_line->addItem("ll");
142   _batch_line->addItem("vishnu");
143   _batch_line->setCurrentIndex(-1);
144
145   QLabel * mpiImpl_label = new QLabel("MPI impl:");
146   _mpiImpl_line = new QComboBox(this);
147   _mpiImpl_line->addItem("lam");
148   _mpiImpl_line->addItem("mpich1");
149   _mpiImpl_line->addItem("mpich2");
150   _mpiImpl_line->addItem("openmpi");
151   _mpiImpl_line->addItem("slurmmpi");
152   _mpiImpl_line->addItem("prun");
153   _mpiImpl_line->setCurrentIndex(-1);
154
155   QGridLayout * c_layout = new QGridLayout;
156   c_layout->addWidget(os_label, 0, 0);
157   c_layout->addWidget(_os_line, 0, 1);
158   c_layout->addWidget(mem_mb_label, 1, 0);
159   c_layout->addWidget(_mem_mb_line, 1, 1);
160   c_layout->addWidget(cpu_clock_label, 2, 0);
161   c_layout->addWidget(_cpu_clock_line, 2, 1);
162   c_layout->addWidget(nb_node_label, 3, 0);
163   c_layout->addWidget(_nb_node_line, 3, 1);
164   c_layout->addWidget(nb_proc_per_node_label, 4, 0);
165   c_layout->addWidget(_nb_proc_per_node_line, 4, 1);
166   c_layout->addWidget(batch_label, 5, 0);
167   c_layout->addWidget(_batch_line, 5, 1);
168   c_layout->addWidget(mpiImpl_label, 6, 0);
169   c_layout->addWidget(_mpiImpl_line, 6, 1);
170   c_layout->addWidget(iprotocol_label, 7, 0);
171   c_layout->addWidget(_iprotocol_line, 7, 1);
172   config_groupBox->setLayout(c_layout);
173
174   // Part 3
175   QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
176                                                       | QDialogButtonBox::Cancel);
177
178   connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
179   connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
180
181   // Main Layout
182   QVBoxLayout * mainLayout = new QVBoxLayout(this);
183   mainLayout->addWidget(main_groupBox);
184   mainLayout->addWidget(config_groupBox);
185   mainLayout->addWidget(buttonBox);
186   setLayout(mainLayout);
187
188   setWindowTitle("Edit/Add a resource");
189   if (_resource_name != "")
190     get_infos();
191 }
192
193 JM::EditSalomeResource::~EditSalomeResource()
194 {
195   DEBTRACE("Destroying JM::EditSalomeResource");
196 }
197
198 void
199 JM::EditSalomeResource::get_infos()
200 {
201   BL::ResourceDescr resource_descr = _salome_services->getResourceDescr(_resource_name);
202
203   _name_line->setText(QString(resource_descr.name.c_str()));
204   _hostname_line->setText(QString(resource_descr.hostname.c_str()));
205   _username_line->setText(QString(resource_descr.username.c_str()));
206   _applipath_line->setText(QString(resource_descr.applipath.c_str()));
207   _os_line->setText(QString(resource_descr.OS.c_str()));
208   _working_directory->setText(QString(resource_descr.working_directory.c_str()));
209
210   if (resource_descr.can_launch_batch_jobs)
211     _can_launch_batch_jobs->setCheckState(Qt::Checked);
212   else
213     _can_launch_batch_jobs->setCheckState(Qt::Unchecked);
214
215   if (resource_descr.can_run_containers)
216     _can_run_containers->setCheckState(Qt::Checked);
217   else
218     _can_run_containers->setCheckState(Qt::Unchecked);
219
220   std::string protocol = resource_descr.protocol.c_str();
221   if (protocol == "ssh")
222     _protocol_line->setCurrentIndex(0);
223   else if(protocol == "rsh")
224     _protocol_line->setCurrentIndex(1);
225   else if(protocol == "sh")
226     _protocol_line->setCurrentIndex(2);
227   else if(protocol == "srun")
228     _protocol_line->setCurrentIndex(3);
229   else if(protocol == "pbsdsh")
230     _protocol_line->setCurrentIndex(4);
231   else if(protocol == "blaunch")
232     _protocol_line->setCurrentIndex(5);
233   else
234     _protocol_line->setCurrentIndex(-1);
235
236   std::string iprotocol = resource_descr.iprotocol.c_str();
237   if (iprotocol == "ssh")
238     _iprotocol_line->setCurrentIndex(0);
239   else if (iprotocol == "rsh")
240     _iprotocol_line->setCurrentIndex(1);
241   else if (iprotocol == "srun")
242     _iprotocol_line->setCurrentIndex(2);
243   else if (iprotocol == "pbsdsh")
244     _iprotocol_line->setCurrentIndex(3);
245   else if (iprotocol == "blaunch")
246     _iprotocol_line->setCurrentIndex(4);
247   else
248     _iprotocol_line->setCurrentIndex(-1);
249
250   std::string batch = resource_descr.batch.c_str();
251   if (batch == "pbs")
252     _batch_line->setCurrentIndex(0);
253   else if (batch == "lsf")  
254     _batch_line->setCurrentIndex(1);
255   else if (batch == "sge")  
256     _batch_line->setCurrentIndex(2);
257   else if (batch == "ssh_batch")
258     _batch_line->setCurrentIndex(3);
259   else if (batch == "ccc")
260     _batch_line->setCurrentIndex(4);
261   else if (batch == "slurm")
262     _batch_line->setCurrentIndex(5);
263   else if (batch == "ll")
264     _batch_line->setCurrentIndex(6);
265   else if (batch == "vishnu")
266     _batch_line->setCurrentIndex(7);
267   else
268     _batch_line->setCurrentIndex(-1);
269
270   std::string mpiImpl = resource_descr.mpiImpl.c_str();
271   if (mpiImpl == "lam")
272     _mpiImpl_line->setCurrentIndex(0);
273   else if (mpiImpl == "mpich1")  
274     _mpiImpl_line->setCurrentIndex(1);
275   else if (mpiImpl == "mpich2")  
276     _mpiImpl_line->setCurrentIndex(2);
277   else if (mpiImpl == "openmpi")
278     _mpiImpl_line->setCurrentIndex(3);
279   else if (mpiImpl == "slurmmpi")
280     _mpiImpl_line->setCurrentIndex(4);
281   else if (mpiImpl == "prun")
282     _mpiImpl_line->setCurrentIndex(5);
283   else  
284     _mpiImpl_line->setCurrentIndex(-1);
285
286   int value = resource_descr.mem_mb;
287   if (value > 0)
288     _mem_mb_line->setValue(value);
289   value = resource_descr.cpu_clock;
290   if (value > 0)
291     _cpu_clock_line->setValue(value);
292   value = resource_descr.nb_node;
293   if (value > 1)
294     _nb_node_line->setValue(value);
295   value = resource_descr.nb_proc_per_node;
296   if (value > 1)
297     _nb_proc_per_node_line->setValue(value);
298
299    std::list<std::string>::iterator it = resource_descr.componentList.begin();
300    for(; it != resource_descr.componentList.end(); it++)
301      _componentList->addItem(QString((*it).c_str()));
302    
303   // Better if string's length is bigger than the widget
304   _name_line->setCursorPosition(0);
305   _hostname_line->setCursorPosition(0);
306   _username_line->setCursorPosition(0);
307   _applipath_line->setCursorPosition(0);
308   _os_line->setCursorPosition(0);
309   _working_directory->setCursorPosition(0);
310 }
311
312
313 void 
314 JM::EditSalomeResource::itemSelectionChanged()
315 {
316   if (_componentList->selectedItems().size() > 0)
317     _remove_button->setEnabled(true);
318   else
319     _remove_button->setEnabled(false);
320 }
321
322 void 
323 JM::EditSalomeResource::add_component()
324 {
325   bool ok;
326   QString text = QInputDialog::getText(this, "Add a component",
327                                        "Component name:", QLineEdit::Normal,
328                                        "", &ok);
329   if (ok && !text.isEmpty())
330     _componentList->addItem(text);
331 }
332
333 void 
334 JM::EditSalomeResource::remove_components()
335 {
336   QList<QListWidgetItem *> list = _componentList->selectedItems();
337   for (int i = 0; i < list.size(); ++i)
338   {
339     int row = _componentList->row( list.at(i) );
340     delete _componentList->takeItem(row);
341   }
342 }
343
344 void
345 JM::EditSalomeResource::accept()
346 {
347   BL::ResourceDescr resource;
348
349   // Text
350   resource.name = _name_line->text().toStdString();
351   resource.hostname = _hostname_line->text().toStdString();
352   resource.username = _username_line->text().toStdString();
353   resource.applipath = _applipath_line->text().toStdString();
354   resource.OS = _os_line->text().toStdString();
355   resource.working_directory = _working_directory->text().toStdString();
356   resource.can_launch_batch_jobs = (_can_launch_batch_jobs->checkState() == Qt::Checked);
357   resource.can_run_containers = (_can_run_containers->checkState() == Qt::Checked);
358
359   // Components
360   int count = _componentList->count();
361   for (int i = 0; i < count; i++)
362     resource.componentList.push_back(_componentList->item(i)->text().toStdString());
363
364   // ComboBox
365   resource.protocol = _protocol_line->currentText().toStdString();
366   resource.iprotocol = _iprotocol_line->currentText().toStdString();
367   resource.batch = _batch_line->currentText().toStdString();
368   if (resource.batch == "ssh")
369     resource.batch = "ssh_batch";
370   resource.mpiImpl = _mpiImpl_line->currentText().toStdString();
371
372   // QSpinBox
373   resource.mem_mb = _mem_mb_line->value();
374   resource.cpu_clock = _cpu_clock_line->value();
375   resource.nb_node = _nb_node_line->value();
376   resource.nb_proc_per_node = _nb_proc_per_node_line->value();
377
378   if (resource.name != "" &&
379       resource.hostname != "" &&
380       resource.protocol != "")
381   {
382     try
383     {
384       _salome_services->addResource(resource);
385       QDialog::accept();
386     }
387     catch (const BL::Exception & ex)
388     {
389       QMessageBox::critical(this, "Error", QString("Cannot add resource: ") + ex.what());
390     }
391   }
392   else
393   {
394     QMessageBox::warning(NULL, "Values missing", "name, hostname and protocol are mandatory! Cancel or add values!");
395   }
396 }