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