Salome HOME
Copyright update 2020
[modules/yacs.git] / src / genericgui / EditionForEachLoop.cxx
1 // Copyright (C) 2006-2020  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 "EditionForEachLoop.hxx"
21 #include "FormEachLoop.hxx"
22 #include "Node.hxx"
23 #include "ForEachLoop.hxx"
24 #include "TypeCode.hxx"
25 #include "QtGuiContext.hxx"
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 #include <cassert>
31 #include <sstream>
32
33 using namespace std;
34
35 using namespace YACS;
36 using namespace YACS::HMI;
37 using namespace YACS::ENGINE;
38
39 EditionForEachLoop::EditionForEachLoop(Subject* subject,
40                                        QWidget* parent,
41                                        const char* name)
42   : EditionNode(subject, parent, name)
43 {
44   _formEachLoop = new FormEachLoop(this);
45   _nbBranches = 1;
46   _wid->gridLayout1->addWidget(_formEachLoop);
47   _formEachLoop->sb_nbranch->setMinimum(1);
48   _formEachLoop->sb_nbranch->setMaximum(INT_MAX);
49   if (!QtGuiContext::getQtCurrent()->isEdition())
50     _formEachLoop->setEnabled (false);
51   Node* node=_subjectNode->getNode();
52   ForEachLoop *fe = dynamic_cast<ForEachLoop*>(node);
53   if(fe)
54     _formEachLoop->lineEdit->setText(fe->edGetSamplePort()->edGetType()->name());
55   connect(_formEachLoop->sb_nbranch, SIGNAL(editingFinished()),
56           this, SLOT(onNbBranchesEdited()));
57
58   connect(_formEachLoop->lineEdit_2, SIGNAL(editingFinished()),this,SLOT(onModifyCollection()));
59
60 }
61
62 EditionForEachLoop::~EditionForEachLoop()
63 {
64 }
65
66 void EditionForEachLoop::onModifyCollection()
67 {
68   bool isOk = false;
69   Node* node=_subjectNode->getNode();
70   ForEachLoop *fe = dynamic_cast<ForEachLoop*>(node);
71   InputPort* dp=fe->edGetSeqOfSamplesPort();
72   SubjectDataPort* sdp = QtGuiContext::getQtCurrent()->_mapOfSubjectDataPort[dp];
73   isOk=sdp->setValue(_formEachLoop->lineEdit_2->text().toStdString());
74   DEBTRACE(isOk);
75 }
76
77 void EditionForEachLoop::onNbBranchesEdited()
78 {
79   int newval = _formEachLoop->sb_nbranch->value();
80   DEBTRACE("EditionForEachLoop::onNbBranchesEdited " << _nbBranches << " --> " << newval);
81   if (newval != _nbBranches)
82     {
83       SubjectForEachLoop *sfe = dynamic_cast<SubjectForEachLoop*>(_subject);
84       YASSERT(sfe);
85       QString text = _formEachLoop->sb_nbranch->cleanText();
86       sfe->setNbBranches(text.toStdString());
87     }
88 }
89
90 void EditionForEachLoop::synchronize()
91 {
92   _subject->update(SETVALUE, 0, _subject);
93 }
94
95 void EditionForEachLoop::update(GuiEvent event, int type, Subject* son)
96 {
97   DEBTRACE("EditionForEachLoop::update " << eventName(event) << " " << type);
98   EditionNode::update(event, type, son);
99   switch (event)
100     {
101     case SETVALUE:
102       SubjectComposedNode * scn = dynamic_cast<SubjectComposedNode*>(_subject);
103       string val = scn->getValue();
104       istringstream ss(val);
105       DEBTRACE( val);
106       int i = 0;
107       ss >> i;
108       DEBTRACE(i);
109       _formEachLoop->sb_nbranch->setValue(i);
110       _nbBranches = i; 
111
112       //smplscollection
113       InputPort* dp=_subjectNode->getNode()->getInputPort("SmplsCollection");
114       _formEachLoop->lineEdit_2->setText(dp->getAsString().c_str());
115       break;
116     }
117 }