Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / genericgui / EditionPyFunc.cxx
1 // Copyright (C) 2006-2015  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   if (!QtGuiContext::getQtCurrent()->isEdition())
79   {
80     gener_template->setEnabled (false);
81     _liFuncName->setEnabled (false);
82   }
83
84
85   connect(_liFuncName, SIGNAL(textChanged(const QString&)),
86           this, SLOT(onFuncNameModified(const QString&)));
87 }
88
89 EditionPyFunc::~EditionPyFunc()
90 {
91 }
92
93 void EditionPyFunc::onApply()
94 {
95   bool funcNameEdited = false;
96   string funcName = _liFuncName->text().toStdString();
97   if (funcName.empty())
98     {
99       _liFuncName->setText(_funcName.c_str());
100       funcName = _funcName;
101     }
102   if (funcName != _funcName)
103     {
104       funcNameEdited = true;
105       bool ret = _subFuncNode->setFunctionName(funcName);
106       if (ret)
107         {
108           funcNameEdited = false;
109           _funcName = funcName;
110         }
111     }
112   _isEdited = _isEdited || funcNameEdited;
113   EditionScript::onApply();
114 }
115
116 void EditionPyFunc::onCancel()
117 {
118   _liFuncName->setText(_funcName.c_str());
119   EditionScript::onCancel();
120 }
121
122 void EditionPyFunc::onFuncNameModified(const QString &text)
123 {
124   if (_funcName != text.toStdString()) setEdited(true);
125 }
126
127 void EditionPyFunc::onTemplate()
128 {
129   if(_funcName=="")return;
130
131   ElementaryNode* node = dynamic_cast<ElementaryNode*>(_subFuncNode->getNode());
132
133   std::string text;
134   text = "def " + _funcName + "(";
135
136   std::list<InputPort*> iplist = node->getSetOfInputPort();
137   std::list<InputPort*>::iterator ipos = iplist.begin();
138   for (; ipos != iplist.end(); ipos++)
139     {
140       text = text + (*ipos)->getName() + ",";
141     }
142   text = text + "):\n";
143   text = text + "  return ";
144
145   std::list<OutputPort*> oplist = node->getSetOfOutputPort();
146   std::list<OutputPort*>::iterator opos = oplist.begin();
147   for (; opos != oplist.end(); opos++)
148     {
149       text = text + (*opos)->getName() + ",";
150     }
151   text[text.length()-1]=' ';
152   text = text + "\n";
153   _sci->append(text.c_str());
154   onApply();
155 }
156