1 // Copyright (C) 2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #include "YDFXGUIHostParametrizer.hxx"
23 #include "YDFXGUIWrap.hxx"
25 #include "YACSEvalResource.hxx"
27 #include <QDialogButtonBox>
28 #include <QVBoxLayout>
29 #include <QHBoxLayout>
30 #include <QPushButton>
39 const char YDFXGUIHostParametrizer::MACHINE[]="machine/machine";
41 const char YDFXGUIBatchInfo::NBPROCS[]="machine/nbprocs";
43 const char YDFXGUIBatchInfo::REMOTEDIR[]="machine/remotedir";
45 const char YDFXGUIBatchInfo::LOCALDIR[]="machine/localdir";
47 const char YDFXGUIBatchInfo::WCKEY[]="machine/wckey";
49 const char YDFXGUIBatchInfo::MAXDUR[]="machine/maxdur";
51 YDFXGUIHostParametrizer::YDFXGUIHostParametrizer(QWidget *parent, YACSEvalYFXWrap *efx):QWidget(parent),_hostSelector(0),_clusterAdvInfo(0),_buttonBox(0),_efx(efx),_wasInteractive(true)
53 _efx->lockPortsForEvaluation();
54 YACSEvalListOfResources *res(_efx->giveResources());
55 std::vector<std::string> machines(res->getAllFittingMachines());
57 QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Maximum);
58 sizePolicy1.setHorizontalStretch(0);
59 sizePolicy1.setVerticalStretch(0);
60 sizePolicy1.setHeightForWidth(sizePolicy().hasHeightForWidth());
61 QVBoxLayout *verticalLayout_3(new QVBoxLayout(this));
62 QHBoxLayout *horizontalLayout_2(new QHBoxLayout);
63 QLabel *label(new QLabel(this));
64 QSizePolicy sizePolicy2(QSizePolicy::Preferred, QSizePolicy::Maximum);
65 sizePolicy2.setHorizontalStretch(0);
66 sizePolicy2.setVerticalStretch(0);
67 sizePolicy2.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
68 label->setText("Select machine");
69 horizontalLayout_2->addWidget(label);
70 _hostSelector=new QComboBox(this);
72 foreach(std::string mach,machines)
73 _hostSelector->insertItem(i++,mach.c_str());
74 connect(_hostSelector,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(changeMachine(const QString &)));
75 sizePolicy2.setHeightForWidth(_hostSelector->sizePolicy().hasHeightForWidth());
76 horizontalLayout_2->addWidget(_hostSelector);
77 verticalLayout_3->addLayout(horizontalLayout_2,1);
79 _clusterAdvInfo=new YDFXGUIBatchInfo(this,_efx);
80 verticalLayout_3->addWidget(_clusterAdvInfo,1);
81 connect(_clusterAdvInfo,SIGNAL(statusOfEntryChanged(bool)),this,SLOT(clusterAdvParamStatusChanged(bool)));
83 QHBoxLayout *horizontalLayout_3(new QHBoxLayout);
84 _buttonBox=new QDialogButtonBox(this);
85 _buttonBox->setOrientation(Qt::Horizontal);
86 _buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
87 horizontalLayout_3->addWidget(_buttonBox,0);
88 verticalLayout_3->addLayout(horizontalLayout_3,0);
90 _hostSelector->setCurrentIndex(0);
92 YACSEvalListOfResources *res(_efx->giveResources());
93 _wasInteractive=res->isMachineInteractive(machines[0]);
95 this->changeMachine(QString(machines[0].c_str()));
98 void YDFXGUIHostParametrizer::loadFromSettings(const QSettings& settings)
100 if(!settings.contains(MACHINE))
102 QVariant v(settings.value(MACHINE));
103 QString vs(v.toString());
106 int id(_hostSelector->findText(vs));
109 _hostSelector->setCurrentIndex(id);
110 YACSEvalListOfResources *res(_efx->giveResources());
111 if(!res->isMachineInteractive(vs.toStdString()))
112 _clusterAdvInfo->loadFromSettings(settings);
115 void YDFXGUIHostParametrizer::learnSettings(QSettings& settings) const
117 settings.setValue(MACHINE,_hostSelector->currentText());
118 YACSEvalListOfResources *res(_efx->giveResources());
119 std::string selectedRes(_hostSelector->currentText().toStdString());
120 if(!res->isMachineInteractive(selectedRes))//res->isInteractive() does not work here if efx has no Resource (no containers !)
121 _clusterAdvInfo->learnSettings(settings);
124 void YDFXGUIHostParametrizer::applyToEFX()
126 YACSEvalListOfResources *res(_efx->giveResources());
127 std::string selectedRes(_hostSelector->currentText().toStdString());
128 res->setWantedMachine(selectedRes);
129 if(!res->isMachineInteractive(selectedRes))//res->isInteractive() does not work here if efx has no Resource (no containers !)
131 YACSEvalParamsForCluster& ps(res->getAddParamsForCluster());
132 _clusterAdvInfo->applyToParams(ps);
134 emit readyForRunSignal(true);
137 QString YDFXGUIHostParametrizer::getNameOfHost()
139 return _hostSelector->currentText();
142 void YDFXGUIHostParametrizer::changeMachine(const QString& newMachineSelected)
144 YACSEvalListOfResources *res(_efx->giveResources());
145 bool isInterac(res->isMachineInteractive(newMachineSelected.toStdString()));
146 _clusterAdvInfo->setVisible(!isInterac);
147 QPushButton *ok(_buttonBox->button(QDialogButtonBox::Ok));
148 ok->setDisabled(!isInterac && !_clusterAdvInfo->isOK());
149 if(_wasInteractive!=isInterac)
151 emit interactivityChanged(isInterac);
152 _wasInteractive=isInterac;
156 void YDFXGUIHostParametrizer::clusterAdvParamStatusChanged(bool newStatus)
158 QPushButton *ok(_buttonBox->button(QDialogButtonBox::Ok));
159 ok->setDisabled(!newStatus);
162 QValidator::State YDFXGUIDurationValidator::validate(QString &input, int &) const
164 QString input2(input.simplified());
165 input2.replace(' ',QString());
166 QStringList sp(input2.split(QChar(':')));
168 return QValidator::Invalid;
169 QRegExp rx("\\d{1,2}");
173 return QValidator::Intermediate;
174 if(!rx.exactMatch(input2))
175 return QValidator::Invalid;
176 return QValidator::Intermediate;
178 if(sp.size()==2 && sp[1].isEmpty())
181 return QValidator::Invalid;
182 return QValidator::Intermediate;
185 sp.insert(0,QString("00"));
187 return QValidator::Intermediate;
189 foreach(QString elt,sp)
191 if(!rx.exactMatch(elt))
192 return QValidator::Invalid;
194 uint val(elt.toUInt(&isOK));
196 return QValidator::Invalid;
199 return QValidator::Invalid;
202 return QValidator::Acceptable;
205 YDFXGUIBatchInfo::YDFXGUIBatchInfo(QWidget *parent, YACSEvalYFXWrap *efx):QWidget(parent),_efx(efx),_nbProcs(0),_remoteDir(0),_localDir(0),_WCKey(0),_maxDuration(0),_wasOK(false)
207 QVBoxLayout *verticalLayout(new QVBoxLayout(this));
208 QGroupBox *gb(new QGroupBox(this));
209 gb->setTitle("Specific info for cluster");
210 verticalLayout->addWidget(gb);
211 QVBoxLayout *verticalLayout_1(new QVBoxLayout(gb));
212 QFrame *frame(new QFrame(gb));
213 verticalLayout_1->addWidget(frame);
214 frame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
215 frame->setFrameShadow(QFrame::Raised);
216 QVBoxLayout *verticalLayout_2(new QVBoxLayout(frame));
217 QSpacerItem *verticalSpacer(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
218 verticalLayout_2->addItem(verticalSpacer);
219 QHBoxLayout *horizontalLayout_3(0);
221 horizontalLayout_3=new QHBoxLayout;
222 QLabel *label_5(new QLabel(frame));
223 label_5->setText("Number of processes");
224 horizontalLayout_3->addWidget(label_5);
225 _nbProcs=new QLineEdit(frame);
226 QValidator *nbProcVal(new QIntValidator(1,10000,_nbProcs));
227 _nbProcs->setValidator(nbProcVal);
228 horizontalLayout_3->addWidget(_nbProcs);
229 verticalLayout_2->addLayout(horizontalLayout_3);
230 verticalSpacer=new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
231 verticalLayout_2->addItem(verticalSpacer);
232 connect(_nbProcs,SIGNAL(textChanged(const QString&)),this,SLOT(somethingChanged()));
234 horizontalLayout_3=new QHBoxLayout;
235 QLabel *label_2(new QLabel(frame));
236 label_2->setText("Remote dir");
237 horizontalLayout_3->addWidget(label_2);
238 _remoteDir=new QLineEdit(frame);
239 horizontalLayout_3->addWidget(_remoteDir);
240 verticalLayout_2->addLayout(horizontalLayout_3);
241 connect(_remoteDir,SIGNAL(textChanged(const QString&)),this,SLOT(somethingChanged()));
243 verticalSpacer=new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
244 verticalLayout_2->addItem(verticalSpacer);
246 horizontalLayout_3=new QHBoxLayout;
247 QLabel *label_3(new QLabel(frame));
248 label_3->setText("Local dir");
249 horizontalLayout_3->addWidget(label_3);
250 _localDir=new QLineEdit(frame);
251 horizontalLayout_3->addWidget(_localDir);
252 connect(_localDir,SIGNAL(textChanged(const QString&)),this,SLOT(somethingChanged()));
253 verticalLayout_2->addLayout(horizontalLayout_3);
254 verticalSpacer=new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
255 verticalLayout_2->addItem(verticalSpacer);
257 horizontalLayout_3=new QHBoxLayout;
258 QLabel *label_4(new QLabel(frame));
259 label_4->setText("Working Caracterization Key");
260 horizontalLayout_3->addWidget(label_4);
261 _WCKey=new QLineEdit(frame);
262 connect(_WCKey,SIGNAL(textChanged(const QString&)),this,SLOT(somethingChanged()));
263 horizontalLayout_3->addWidget(_WCKey);
264 verticalLayout_2->addLayout(horizontalLayout_3);
265 verticalSpacer=new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
266 verticalLayout_2->addItem(verticalSpacer);
268 horizontalLayout_3=new QHBoxLayout;
269 QLabel *label_6(new QLabel(frame));
270 label_6->setText("Max duration");
271 horizontalLayout_3->addWidget(label_6);
272 _maxDuration=new QLineEdit(frame);
273 QValidator *maxDurVal(new YDFXGUIDurationValidator(_maxDuration));
274 _maxDuration->setText("05:00");
275 _maxDuration->setValidator(maxDurVal);
276 connect(_maxDuration,SIGNAL(textChanged(const QString&)),this,SLOT(somethingChanged()));
277 horizontalLayout_3->addWidget(_maxDuration);
278 verticalLayout_2->addLayout(horizontalLayout_3);
279 verticalSpacer=new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
280 verticalLayout_2->addItem(verticalSpacer);
283 void YDFXGUIBatchInfo::showEvent(QShowEvent *event)
285 YDFXGUIHostParametrizer *parentc(qobject_cast<YDFXGUIHostParametrizer *>(parent()));
288 QPushButton *ok(parentc->getDialogButtonBox()->button(QDialogButtonBox::Ok));
289 ok->setEnabled(isOK());
291 QWidget::showEvent(event);
294 bool YDFXGUIBatchInfo::isOK() const
296 bool ret1(!_nbProcs->text().isEmpty() && !_remoteDir->text().isEmpty() && !_localDir->text().isEmpty() && !_WCKey->text().isEmpty());
297 const QValidator *val(_maxDuration->validator());
299 QString st(_maxDuration->text());
300 QValidator::State sta(val->validate(st,dummy));
301 return ret1 && sta==QValidator::Acceptable;
304 void YDFXGUIBatchInfo::loadFromSettings(const QSettings& settings)
306 static const int NB=5;
307 const char *tab1[NB]={NBPROCS,REMOTEDIR,LOCALDIR,WCKEY,MAXDUR};
308 QLineEdit *tab2[NB]={_nbProcs,_remoteDir,_localDir,_WCKey,_maxDuration};
310 YACSEvalParamsForCluster* cp(0);
311 YACSEvalListOfResources *lr(_efx->giveResources());
314 std::vector<std::string> machines(lr->getAllChosenMachines());
315 if(machines.size()==1)
317 if(machines[0]==getNameOfHost().toStdString())
318 cp=&lr->getAddParamsForCluster();
323 tab3[0]=QString("%1").arg(cp->getNbProcs());
324 tab3[1]=cp->getRemoteWorkingDir().c_str();
325 tab3[2]=cp->getLocalWorkingDir().c_str();
326 tab3[3]=cp->getWCKey().c_str();
327 tab3[4]=cp->getMaxDuration().c_str();
329 for(int i=0;i<NB;i++)
331 const char *entry(tab1[i]);
332 QLineEdit *le(tab2[i]);
333 if(!tab3[1].isEmpty())
335 le->setText(tab3[i]);
339 if(!settings.contains(entry))
341 QVariant v(settings.value(entry));
342 QString vs(v.toString());
349 emit statusOfEntryChanged(newOK);
352 void YDFXGUIBatchInfo::learnSettings(QSettings& settings) const
354 settings.setValue(NBPROCS,_nbProcs->text());
355 settings.setValue(REMOTEDIR,_remoteDir->text());
356 settings.setValue(LOCALDIR,_localDir->text());
357 settings.setValue(WCKEY,_WCKey->text());
358 settings.setValue(MAXDUR,_maxDuration->text());
361 void YDFXGUIBatchInfo::applyToParams(YACSEvalParamsForCluster& ps) const
363 QString nbProcs(_nbProcs->text());
364 ps.setNbProcs(nbProcs.toUInt());
365 ps.setRemoteWorkingDir(_remoteDir->text().toStdString());
366 ps.setLocalWorkingDir(_localDir->text().toStdString());
367 ps.setWCKey(_WCKey->text().toStdString());
368 ps.setMaxDuration(NormalizeDuration(_maxDuration->text()).toStdString());
371 QString YDFXGUIBatchInfo::getNameOfHost()
373 YDFXGUIHostParametrizer *parentc(qobject_cast<YDFXGUIHostParametrizer *>(parent()));
375 return parentc->getNameOfHost();
380 void YDFXGUIBatchInfo::somethingChanged()
382 emit statusOfEntryChanged(isOK());
385 QString YDFXGUIBatchInfo::NormalizeDuration(const QString& txt)
387 QString input2(txt.simplified());
388 input2.replace(' ',QString());
389 QStringList sp(txt.split(QChar(':')));
391 foreach(QString elt,sp)
394 uint val(elt.toUInt(&isOK));
400 ret << QString("%1").arg(val,2,10,QChar('0'));
402 return ret.join(QChar(':'));