]> SALOME platform Git repositories - modules/yacs.git/blob - src/hmi/nodeEdition.cxx
Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / hmi / nodeEdition.cxx
1
2 #include "nodeEdition.h"
3 #include "guiContext.hxx"
4 #include "Port.hxx"
5 #include "InlineNode.hxx"
6 #include "TypeCode.hxx"
7
8 #include <qlineedit.h>
9 #include <qlabel.h>
10 #include <qpushbutton.h>
11 #include <qlayout.h>
12
13 #ifdef HAVE_QEXTSCINTILLA_H
14 #include <qextscintilla.h>
15 #include <qextscintillalexerpython.h>
16 #endif
17
18 //#define _DEVDEBUG_
19 #include "YacsTrace.hxx"
20
21 #include <string>
22 #include <cassert>
23
24 using namespace std;
25
26 using namespace YACS;
27 using namespace YACS::HMI;
28
29 #ifdef HAVE_QEXTSCINTILLA_H
30 class myQextScintilla: public QextScintilla
31 {
32 public:
33   myQextScintilla(QWidget *parent=0, const char *name=0, WFlags f=0)
34     : QextScintilla(parent, name, f) {};
35   ~myQextScintilla(){};
36   virtual QSize sizeHint() const { return QSize(350, 150); };
37 };
38 #endif
39
40 itemEdition::itemEdition(Subject* subject)
41   : GuiObserver(), _subject(subject)
42 {
43   _subject->attach(this);
44   _stackId = -1;
45   _name = _subject->getName();
46   _type = "Unknown";
47   _category = "Unknown";
48   ProcInvoc* invoc = GuiContext::getCurrent()->getInvoc();
49   if (SubjectNode * sub = dynamic_cast<SubjectNode*>(_subject))
50     {
51       _category = "Node";
52       _type = invoc->getTypeName(invoc->getTypeOfNode(sub->getNode()));
53     }
54   else if (SubjectDataPort * sub = dynamic_cast<SubjectDataPort*>(_subject))
55     {
56       _category = "Port";
57       _type = invoc->getTypeName(invoc->getTypeOfPort(sub->getPort()));
58     }
59   else if (SubjectLink * sub = dynamic_cast<SubjectLink*>(_subject))
60     {
61       _category = "Link";
62       _type = "Link";
63     }
64   else if (SubjectControlLink * sub = dynamic_cast<SubjectControlLink*>(_subject))
65     {
66       _category = "ControlLink";
67       _type = "Control Link";
68     }
69   else if (SubjectDataType * sub = dynamic_cast<SubjectDataType*>(_subject))
70     {
71       _category = "Data";
72       switch (sub->getTypeCode()->kind())
73         {
74         case YACS::ENGINE::Double:   _type = "Double"; break;
75         case YACS::ENGINE::Int:      _type = "Int"; break;
76         case YACS::ENGINE::String:   _type = "String"; break;
77         case YACS::ENGINE::Bool:     _type = "Bool"; break;
78         case YACS::ENGINE::Objref:   _type = "Objref"; break;
79         case YACS::ENGINE::Sequence: _type = "Sequence"; break;
80         case YACS::ENGINE::Array:    _type = "Array"; break;
81         case YACS::ENGINE::Struct:   _type = "Struct"; break;
82         default: _type = "None or Unknown";
83         }
84     }
85   else if (SubjectComponent * sub = dynamic_cast<SubjectComponent*>(_subject))
86     {
87       _category = "Component";
88       _type = "Salome Component";
89     }
90   else if (SubjectContainer * sub = dynamic_cast<SubjectContainer*>(_subject))
91     {
92       _category = "Container";
93       _type = "Salome Container";
94     }
95 }
96
97 itemEdition::~itemEdition()
98 {
99 }
100
101 void itemEdition::select(bool isSelected)
102 {
103   DEBTRACE("NodeEdition::select " << isSelected);
104   if (isSelected)
105     GuiContext::getCurrent()->getWidgetStack()->raiseWidget(_stackId);
106 }
107
108
109 void itemEdition::update(GuiEvent event, int type, Subject* son)
110 {
111 }
112
113 Subject* itemEdition::getSubject()
114 {
115   return _subject;
116 }
117
118
119 NodeEdition::NodeEdition(Subject* subject,
120                          QWidget* parent,
121                          const char* name,
122                          WFlags fl)
123   : wiNodeEdition(parent, name, fl), itemEdition(subject)
124 {
125   DEBTRACE("NodeEdition::NodeEdition " << name);
126   string category = _category + " type:";
127   laNodeType->setText(category.c_str());
128   category = _category + " name:";
129   laNodeName->setText(category.c_str());
130   liNodeType->setText(_type);
131   liNodeName->setText(_name.c_str());
132   liNodeType->setText(_type);
133   setEdited(false);
134   _haveScript = false;
135   _sci = 0;
136   _subInlineNode = 0;
137   _subFuncNode = 0;
138   _funcName ="";
139   _liFuncName = 0;
140   if (_subFuncNode = dynamic_cast<SubjectPyFuncNode*>(_subject))
141     if (YACS::ENGINE::InlineFuncNode *pyFuncNode
142         = dynamic_cast<YACS::ENGINE::InlineFuncNode*>(_subFuncNode->getNode()))
143       {
144         _funcName = pyFuncNode->getFname();
145         QLabel* laFuncName = new QLabel( this, "laFuncName" );
146         layout5->addWidget( laFuncName );
147         laFuncName->setText("Function Name:");
148         _liFuncName = new QLineEdit( this, "liFuncName" );
149         layout5->addWidget( _liFuncName );
150         _liFuncName->setText(_funcName);
151         connect(_liFuncName, SIGNAL(textChanged(const QString&)), this, SLOT(onFuncNameModified(const QString&)));
152       }
153     else assert(0);
154   if (_subInlineNode = dynamic_cast<SubjectInlineNode*>(_subject))
155     {
156       _haveScript = true;
157 #ifdef HAVE_QEXTSCINTILLA_H
158       _sci = new myQextScintilla( this, "Python Editor" );
159 #else
160       _sci = new QTextEdit( this, "Python Editor" );
161 #endif
162       wiNodeEditionLayout->removeItem(spacer2);
163       layout5->addWidget( _sci );
164 #ifdef HAVE_QEXTSCINTILLA_H
165       _sci->setUtf8(1);
166       QextScintillaLexerPython *lex = new QextScintillaLexerPython(_sci);
167       _sci->setLexer(lex);
168       _sci->setBraceMatching(QextScintilla::SloppyBraceMatch);
169       _sci->setAutoIndent(1);
170       _sci->setIndentationWidth(4);
171       _sci->setIndentationGuides(1);
172       _sci->setIndentationsUseTabs(0);
173       _sci->setAutoCompletionThreshold(2);
174 #endif
175       if (YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode()))
176         {
177           _sci->append(pyNode->getScript());
178         }
179       connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
180     }
181   _stackId = GuiContext::getCurrent()->getWidgetStack()->addWidget(this);
182   DEBTRACE("_stackId " << _stackId);
183   //GuiContext::getCurrent()->getWidgetStack()->raiseWidget(_stackId);
184 }
185
186 NodeEdition::~NodeEdition()
187 {
188 }
189
190 void NodeEdition::setName(std::string name)
191 {
192   _name = name;
193   setEdited(false);
194 }
195
196 void NodeEdition::onApply()
197 {
198   string name = liNodeName->text().latin1();
199   bool nameEdited = false;
200   if (name != _name)
201     {
202       nameEdited = true;
203       bool ret = _subject->setName(name);
204       if (ret)
205         {
206           nameEdited = false;
207           _name = name;
208         }
209     }
210   bool funcNameEdited = false;
211   if (_subFuncNode)
212     {
213       funcNameEdited = true;
214       string funcName = _liFuncName->text().latin1();
215       if (funcName != _funcName)
216         {
217           bool ret = _subFuncNode->setFunctionName(funcName);
218           if (ret)
219             {
220               funcNameEdited = false;
221               _funcName = funcName;
222             }
223         }
224     }
225   bool scriptEdited = false;
226   if (_haveScript)
227     {
228       if (_sci->isModified())
229         {
230           scriptEdited = true;
231           bool ret = _subInlineNode->setScript(_sci->text().latin1());
232           if (ret)
233             {
234               scriptEdited = false;
235             }
236         }
237     }
238   bool edited = nameEdited || funcNameEdited || scriptEdited;
239   setEdited(edited);
240 }
241
242 void NodeEdition::onCancel()
243 {
244   liNodeName->setText(_name.c_str());
245   if (_haveScript)
246     _sci->setText(_subInlineNode->getScript());
247   if (_subFuncNode)
248     _liFuncName->setText(_funcName);
249   setEdited(false);
250 }
251
252 void NodeEdition::onModify(const QString &text)
253 {
254   if (_name != text.latin1()) setEdited(true);
255 }
256
257 void NodeEdition::onFuncNameModified(const QString &text)
258 {
259   if (_funcName != text.latin1()) setEdited(true);
260 }
261
262 void NodeEdition::onScriptModified()
263 {
264   setEdited(true);
265 }
266
267 void NodeEdition::setEdited(bool isEdited)
268 {
269   btApply->setEnabled(isEdited);
270   btCancel->setEnabled(isEdited);
271 }
272
273 void NodeEdition::update(GuiEvent event, int type, Subject* son)
274 {
275   DEBTRACE("NodeEdition::update ");
276   NodeEdition *item = 0;
277   switch (event)
278     {
279     case NEWROOT:
280       item =  new NodeEdition(son,
281                               GuiContext::getCurrent()->getWidgetStack(),
282                               son->getName().c_str());
283       break;
284     case ADD:
285       switch (type)
286         {
287         case YACS::HMI::SALOMEPROC:
288         case YACS::HMI::BLOC:
289         case YACS::HMI::FOREACHLOOP:
290         case YACS::HMI::OPTIMIZERLOOP:
291         case YACS::HMI::FORLOOP:
292         case YACS::HMI::WHILELOOP:
293         case YACS::HMI::SWITCH:
294         case YACS::HMI::PYTHONNODE:
295         case YACS::HMI::PYFUNCNODE:
296         case YACS::HMI::CORBANODE:
297         case YACS::HMI::SALOMENODE:
298         case YACS::HMI::CPPNODE:
299         case YACS::HMI::SALOMEPYTHONNODE:
300         case YACS::HMI::XMLNODE:
301         case YACS::HMI::PRESETNODE:
302         case YACS::HMI::OUTNODE:
303         case YACS::HMI::STUDYINNODE:
304         case YACS::HMI::STUDYOUTNODE:
305         case YACS::HMI::SPLITTERNODE:
306         case YACS::HMI::DFTODSFORLOOPNODE:
307         case YACS::HMI::DSTODFFORLOOPNODE:
308         case YACS::HMI::INPUTPORT:
309         case YACS::HMI::OUTPUTPORT:
310         case YACS::HMI::INPUTDATASTREAMPORT:
311         case YACS::HMI::OUTPUTDATASTREAMPORT:
312         case YACS::HMI::CONTAINER:
313         case YACS::HMI::COMPONENT:
314         case YACS::HMI::REFERENCE:
315         case YACS::HMI::DATATYPE:
316           item =  new NodeEdition(son,
317                                   GuiContext::getCurrent()->getWidgetStack(),
318                                   son->getName().c_str());
319           break;
320         default:
321           DEBTRACE("NodeEdition::update() ADD, type not handled:" << type);
322         }
323       break;
324     case ADDLINK:
325       switch (type)
326         {
327         case YACS::HMI::DATALINK:
328           item =  new NodeEdition(son,
329                                   GuiContext::getCurrent()->getWidgetStack(),
330                                   son->getName().c_str());
331           break;
332         default:
333           DEBTRACE("NodeEdition::update() ADDLINK, type not handled:" << type);
334         }
335       break;
336     case ADDCONTROLLINK:
337       switch (type)
338         {
339         case YACS::HMI::CONTROLLINK:
340           item =  new NodeEdition(son,
341                                   GuiContext::getCurrent()->getWidgetStack(),
342                                   son->getName().c_str());
343           break;
344         default:
345           DEBTRACE("NodeEdition::update() ADDCONTROLLINK, type not handled:" << type);
346         }
347       break;
348     default:
349       DEBTRACE("NodeEdition::update(), event not handled: " << event);
350     }
351 }