Salome HOME
Copyright update 2021
[modules/yacs.git] / src / genericgui / EditionPyFunc.cxx
1 // Copyright (C) 2006-2021  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 "EditionPyFunc.hxx"
21 #include "InlineNode.hxx"
22 #include "InputPort.hxx"
23 #include "OutputPort.hxx"
24 #include "QtGuiContext.hxx"
25 #include "Container.hxx"
26
27 #include <QToolButton>
28
29 #ifdef HAS_PYEDITOR
30 #include <PyEditor_Editor.h>
31 #endif
32
33 #include <cassert>
34
35 //#define _DEVDEBUG_
36 #include "YacsTrace.hxx"
37
38 using namespace std;
39
40 using namespace YACS;
41 using namespace YACS::HMI;
42 using namespace YACS::ENGINE;
43
44
45 EditionPyFunc::EditionPyFunc(Subject* subject,
46                              QWidget* parent,
47                              const char* name)
48   : EditionScript(subject, parent, name)
49 {
50   _subFuncNode = 0;
51   _funcName ="";
52   _liFuncName = 0;
53
54   _subFuncNode = dynamic_cast<SubjectPyFuncNode*>(_subject);
55   YASSERT(_subFuncNode);
56
57   YACS::ENGINE::InlineFuncNode *pyFuncNode
58     = dynamic_cast<YACS::ENGINE::InlineFuncNode*>(_subFuncNode->getNode());
59   YASSERT(pyFuncNode);
60
61   _glayout->removeWidget( _sci );
62
63   QGridLayout *glt = new QGridLayout();
64   _funcName = pyFuncNode->getFname();
65   QLabel* laFuncName = new QLabel("laFuncName", this );
66   glt->addWidget(laFuncName, 0, 0, 1, 1);
67   laFuncName->setText("Function Name:");
68   _liFuncName = new QLineEdit( "liFuncName", this );
69   glt->addWidget(_liFuncName, 0, 1, 1, 1);
70   _liFuncName->setText(_funcName.c_str());
71   QPushButton* gener_template = new QPushButton("Template",this);
72   connect(gener_template,SIGNAL(clicked()),this, SLOT(onTemplate()));
73   glt->addWidget(gener_template, 0, 2, 1, 1);
74   _glayout->addLayout( glt , 1);
75
76   _glayout->addWidget( _sci );
77   if (!QtGuiContext::getQtCurrent()->isEdition())
78   {
79     gener_template->setEnabled (false);
80     _liFuncName->setEnabled (false);
81   }
82
83
84   connect(_liFuncName, SIGNAL(textChanged(const QString&)),
85           this, SLOT(onFuncNameModified(const QString&)));
86 }
87
88 EditionPyFunc::~EditionPyFunc()
89 {
90 }
91
92 void EditionPyFunc::onApply()
93 {
94   bool funcNameEdited = false;
95   string funcName = _liFuncName->text().toStdString();
96   if (funcName.empty())
97     {
98       _liFuncName->setText(_funcName.c_str());
99       funcName = _funcName;
100     }
101   if (funcName != _funcName)
102     {
103       funcNameEdited = true;
104       bool ret = _subFuncNode->setFunctionName(funcName);
105       if (ret)
106         {
107           funcNameEdited = false;
108           _funcName = funcName;
109         }
110     }
111   _isEdited = _isEdited || funcNameEdited;
112   EditionScript::onApply();
113 }
114
115 void EditionPyFunc::onCancel()
116 {
117   _liFuncName->setText(_funcName.c_str());
118   EditionScript::onCancel();
119 }
120
121 void EditionPyFunc::onFuncNameModified(const QString &text)
122 {
123   if (_funcName != text.toStdString()) setEdited(true);
124 }
125
126 void EditionPyFunc::onTemplate()
127 {
128   if(_funcName=="")return;
129
130   ElementaryNode* node = dynamic_cast<ElementaryNode*>(_subFuncNode->getNode());
131
132   std::string text;
133   text = "def " + _funcName + "(";
134
135   std::list<InputPort*> iplist = node->getSetOfInputPort();
136   std::list<InputPort*>::iterator ipos = iplist.begin();
137   for (; ipos != iplist.end(); ipos++)
138     {
139       text = text + (*ipos)->getName() + ",";
140     }
141   text = text + "):\n";
142   text = text + "  return ";
143
144   std::list<OutputPort*> oplist = node->getSetOfOutputPort();
145   std::list<OutputPort*>::iterator opos = oplist.begin();
146   for (; opos != oplist.end(); opos++)
147     {
148       text = text + (*opos)->getName() + ",";
149     }
150   text[text.length()-1]=' ';
151   text = text + "\n";
152   _sci->append(text.c_str());
153   onApply();
154 }
155