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