Salome HOME
0012a4635c0ceb2ce43fed6045fd900b895291ac
[modules/yacs.git] / src / genericgui / EditionScript.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 "EditionScript.hxx"
21 #include "QtGuiContext.hxx"
22 #include "Resource.hxx"
23 #include "Container.hxx"
24 #include "InlineNode.hxx"
25 #include "FormContainer.hxx"
26 #include "Message.hxx"
27
28 #if HAS_QSCI4>0
29 #include <qsciscintilla.h>
30 #include <qscilexerpython.h>
31 #endif
32
33 #include <QToolButton>
34 #include <QSplitter>
35 #include <QTemporaryFile>
36 #include <QTextStream>
37 #include <QProcess>
38
39 #include <cassert>
40
41 //#define _DEVDEBUG_
42 #include "YacsTrace.hxx"
43
44 using namespace std;
45
46 using namespace YACS;
47 using namespace YACS::HMI;
48
49 #if HAS_QSCI4>0
50 class myQsciScintilla: public QsciScintilla
51 {
52 public:
53   myQsciScintilla(QWidget *parent=0)
54     : QsciScintilla(parent) {};
55   ~myQsciScintilla(){};
56   virtual QSize sizeHint() const { return QSize(350, 150); };
57 protected:
58   virtual bool event(QEvent *e);
59 };
60
61 bool myQsciScintilla::event(QEvent *e)
62 {
63   if (e->type() == QEvent::ShortcutOverride)
64     {
65       e->accept();
66       return true;
67     }
68   return QsciScintilla::event(e);
69 }
70
71 #endif
72
73
74 EditionScript::EditionScript(Subject* subject,
75                          QWidget* parent,
76                          const char* name)
77   : EditionElementaryNode(subject, parent, name)
78 {
79   _subInlineNode = 0;
80   _sci = 0;
81
82   _subInlineNode = dynamic_cast<SubjectInlineNode*>(_subject);
83   YASSERT(_subInlineNode);
84
85   QSplitter *splitter = new QSplitter(this);
86   splitter->setOrientation(Qt::Vertical);
87   _wid->gridLayout1->addWidget(splitter);
88
89   QWidget* widg=new QWidget;
90   _portslayout = new QVBoxLayout;
91   widg->setLayout(_portslayout);
92   _portslayout->setMargin(1);
93   splitter->addWidget(widg);
94
95   QWidget* window=new QWidget;
96   _glayout=new QVBoxLayout;
97   window->setLayout(_glayout);
98   _glayout->setMargin(1);
99   splitter->addWidget(window);
100
101   //add an options section in ports layout for execution mode (local or remote)
102   QHBoxLayout* hboxLayout = new QHBoxLayout();
103   hboxLayout->setMargin(0);
104   QToolButton* tb_options = new QToolButton();
105   tb_options->setCheckable(true);
106   QIcon icon;
107   icon.addFile("icons:icon_down.png");
108   icon.addFile("icons:icon_up.png", QSize(), QIcon::Normal, QIcon::On);
109   tb_options->setIcon(icon);
110   hboxLayout->addWidget(tb_options);
111
112   QLabel* label = new QLabel("Execution Mode");
113   QFont font;
114   font.setBold(true);
115   font.setWeight(75);
116   label->setFont(font);
117   hboxLayout->addWidget(label);
118
119   _portslayout->addLayout(hboxLayout);
120
121   fr_options = new QFrame();
122   QHBoxLayout* hboxLayout1 = new QHBoxLayout(fr_options);
123   hboxLayout1->setMargin(0);
124   radiolocal= new QRadioButton("YACS");
125   radioremote= new QRadioButton("Container");
126   radiolocal->setChecked(true);
127   hboxLayout1->addWidget(radiolocal);
128   hboxLayout1->addWidget(radioremote);
129   hboxLayout->addWidget(fr_options);
130
131   fr_container = new QFrame();
132   QHBoxLayout* hboxLayout2 = new QHBoxLayout(fr_container);
133   hboxLayout2->setMargin(0);
134   QLabel* laContainer = new QLabel("Container:");
135   hboxLayout2->addWidget(laContainer);
136   cb_container = new ComboBox();
137   hboxLayout2->addWidget(cb_container);
138   _portslayout->addWidget(fr_container);
139
140   formcontainer = new FormContainer(this);
141   formcontainer->on_tb_container_toggled(false);
142   _portslayout->addWidget(formcontainer);
143   //end of insertion of execution mode
144
145   createTablePorts(_portslayout);
146   setEditablePorts(true);
147
148   _haveScript = true;
149 #if HAS_QSCI4>0
150   _sci = new myQsciScintilla(this);
151 #else
152   _sci = new QTextEdit(this);
153 #endif
154   _wid->gridLayout->removeItem(_wid->spacerItem);
155
156   _editor = new QPushButton("External Editor", this);
157   connect(_editor, SIGNAL(clicked()), this, SLOT(onEdit()));
158   if(!Resource::pythonExternalEditor.isEmpty())
159     {
160       _glayout->addWidget( _editor );
161     }
162   _glayout->addWidget( _sci );
163
164 #if HAS_QSCI4>0
165   _sci->setUtf8(1);
166   QsciLexerPython *lex = new QsciLexerPython(_sci);
167   lex->setFont(Resource::pythonfont);
168   _sci->setLexer(lex);
169   _sci->setBraceMatching(QsciScintilla::SloppyBraceMatch);
170   _sci->setAutoIndent(1);
171   _sci->setIndentationWidth(4);
172   _sci->setIndentationGuides(1);
173   _sci->setIndentationsUseTabs(0);
174   _sci->setAutoCompletionThreshold(2);
175   //_sci->setMarginLineNumbers(1,true);
176   _sci->setMarginWidth(1,0);
177   _sci->setFolding(QsciScintilla::PlainFoldStyle);
178 #endif
179
180   if (!QtGuiContext::getQtCurrent()->isEdition())
181     _sci->setReadOnly(true);
182
183   if (YACS::ENGINE::InlineNode* pyNode =
184       dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode()))
185     {
186       _sci->append(pyNode->getScript().c_str());
187     }
188   connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
189
190   connect(tb_options, SIGNAL(toggled(bool)), this, SLOT(on_tb_options_toggled(bool)));
191   connect(radioremote, SIGNAL(toggled(bool)), this, SLOT(on_remote_toggled(bool)));
192   connect(cb_container, SIGNAL(mousePressed()), this, SLOT(fillContainerPanel()));
193   connect(cb_container, SIGNAL(activated(int)), this, SLOT(changeContainer(int)));
194
195   update(UPDATE,0,0);
196   on_tb_options_toggled(false);
197 }
198
199 EditionScript::~EditionScript()
200 {
201 }
202
203 void EditionScript::synchronize()
204 {
205   DEBTRACE("EditionScript::synchronize " << this->_isEdited);
206   EditionElementaryNode::synchronize();
207   YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
208   YASSERT(pyNode);
209   disconnect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
210   if (!_isEdited)
211   {
212     _sci->clear();
213     _sci->append(pyNode->getScript().c_str());
214   }
215   connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
216 }
217
218 void EditionScript::onApply()
219 {
220   DEBTRACE("EditionScript::onApply");
221   bool scriptEdited = false;
222 #if HAS_QSCI4>0
223   _sci->lexer()->setFont(Resource::pythonfont);
224 #endif
225
226   if(Resource::pythonExternalEditor.isEmpty())
227     {
228       _editor->hide();
229       _glayout->removeWidget(_editor);
230     }
231   else
232     {
233       _editor->show();
234       if(_glayout->itemAt(0)->widget() != _editor)
235         _glayout->insertWidget ( 0, _editor );
236     }
237
238   if (_haveScript)
239     {
240 #if HAS_QSCI4>0
241       if (_sci->isModified())
242         {
243           scriptEdited = true;
244           std::string text=_sci->text().toStdString();
245           if(text[text.length()-1] != '\n')
246             text=text+'\n';
247           bool ret = _subInlineNode->setScript(text);
248           if (ret) scriptEdited = false;
249         }
250 #else
251       if (_sci->document()->isModified())
252         {
253           scriptEdited = true;
254           std::string text=_sci->document()->toPlainText().toStdString();
255           if(text[text.length()-1] != '\n')
256             text=text+'\n';
257           bool ret = _subInlineNode->setScript(text);
258           if (ret) scriptEdited = false;
259         }
260 #endif
261     }
262
263   bool containerEdited = true;
264   if (formcontainer->onApply()) {
265     fillContainerPanel();
266     containerEdited = false;
267   } else {
268     Message mess(GuiContext::getCurrent()->_lastErrorMessage);
269     return;
270   }
271
272   _isEdited = _isEdited || scriptEdited || containerEdited;
273
274   EditionElementaryNode::onApply();
275 }
276
277 void EditionScript::onCancel()
278 {
279   if (_haveScript)
280     _sci->setText(_subInlineNode->getScript().c_str());
281   formcontainer->onCancel();
282   EditionElementaryNode::onCancel();
283 }
284
285 void EditionScript::onScriptModified()
286 {
287   DEBTRACE("EditionScript::onScriptModified");
288   _isEdited = true;
289   setEdited(true);
290 }
291
292 void EditionScript::onEdit()
293 {
294   DEBTRACE("EditionScript::onEdit");
295   QTemporaryFile outFile;
296   if (!outFile.open())
297     return;
298   QString fileName = outFile.fileName();
299   QTextStream out(&outFile);
300 #if HAS_QSCI4>0
301   out << _sci->text();
302 #else
303   out << _sci->toPlainText();
304 #endif
305   outFile.close();
306
307   QStringList options=Resource::pythonExternalEditor.split(" ");
308   QString prog=options.takeAt(0);
309   QProcess::execute(prog, QStringList() << options << fileName);
310
311   QFile inFile(fileName);
312   if (!inFile.open(QIODevice::ReadOnly))
313     return;
314   QTextStream in(&inFile);
315 #if HAS_QSCI4>0
316   _sci->setText(in.readAll());
317 #else
318   _sci->setPlainText(in.readAll());
319 #endif
320   onApply();
321 }
322
323 void EditionScript::on_tb_options_toggled(bool checked)
324 {
325   DEBTRACE("EditionScript::on_tb_options_toggled " << checked);
326   _checked = checked;
327   if(_checked)
328     {
329       fr_options->show();
330       if(_remote) {
331         fr_container->show();
332         formcontainer->show();
333       }
334     }
335   else
336     {
337       fr_options->hide();
338       fr_container->hide();
339       formcontainer->hide();
340     }
341 }
342
343 void EditionScript::on_remote_toggled(bool checked)
344 {
345   DEBTRACE("EditionScript::on_remote_toggled " << checked);
346   _remote=checked;
347   YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
348   std::string mode = pyNode->getExecutionMode();
349   DEBTRACE(mode);
350
351   if(checked)
352     {
353       //remote radio button is checked
354       if(mode != "remote")
355         _subInlineNode->setExecutionMode("remote");
356       fr_container->show();
357       formcontainer->show();
358       fillContainerPanel();
359     }
360   else
361     {
362       //remote radio button is unchecked
363       if(mode != "local")
364         _subInlineNode->setExecutionMode("local");
365       fr_container->hide();
366       formcontainer->hide();
367     }
368 }
369
370 void EditionScript::fillContainerPanel()
371 {
372   DEBTRACE("EditionScript::fillContainerPanel ");
373   YACS::ENGINE::Proc* proc = GuiContext::getCurrent()->getProc();
374
375   cb_container->clear();
376   std::map<string,YACS::ENGINE::Container*>::const_iterator it = proc->containerMap.begin();
377   for(; it != proc->containerMap.end(); ++it)
378     cb_container->addItem( QString((*it).first.c_str()));
379
380   YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
381
382   YACS::ENGINE::Container * cont = pyNode->getContainer();
383   if (cont)
384     {
385       int index = cb_container->findText(cont->getName().c_str());
386       cb_container->setCurrentIndex(index);
387       formcontainer->FillPanel(cont);
388     } 
389   else if (cb_container->count()) {
390     changeContainer(0);
391   }
392 }
393
394 void EditionScript::changeContainer(int index)
395 {
396   DEBTRACE("EditionScript::changeContainer ");
397   string contName = cb_container->itemText(index).toStdString();
398   DEBTRACE(contName);
399   YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
400   YACS::ENGINE::Container *oldContainer = pyNode->getContainer();
401
402   YACS::ENGINE::Container *newContainer = 0;
403   YACS::ENGINE::Proc* proc = GuiContext::getCurrent()->getProc();
404   if (proc->containerMap.count(contName))
405     newContainer = proc->containerMap[contName];
406   if (!newContainer)
407     {
408       DEBTRACE("-------------> not found : " << contName);
409       return;
410     }
411   YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(newContainer));
412   SubjectContainer *scnt = GuiContext::getCurrent()->_mapOfSubjectContainer[newContainer];
413
414   _subInlineNode->setContainer(scnt);
415   
416   // show the selected container parameters
417   formcontainer->FillPanel(newContainer);
418 }
419
420 void EditionScript::update(GuiEvent event, int type, Subject* son)
421 {
422   DEBTRACE("EditionScript::update " << eventName(event) <<" "<<type<<" "<<son);
423   EditionElementaryNode::update(event, type, son);
424   if(event == ASSOCIATE)
425     {
426       fillContainerPanel();
427       SubjectContainer *scont = dynamic_cast<SubjectContainer*>(son);
428       YASSERT(scont);
429       formcontainer->FillPanel(scont->getContainer());
430     }
431   else if(event == UPDATE)
432     {
433       YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
434       std::string mode = pyNode->getExecutionMode();
435       if(mode == "remote")
436         {
437           _remote=true;
438           radioremote->setChecked(true);
439         }
440       else if(mode == "local")
441         {
442           _remote=false;
443           radiolocal->setChecked(true);
444         }
445
446       fillContainerPanel();
447     }
448 }
449