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