1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
29 #include <PyEditor_Editor.h>
30 #include <PyEditor_Settings.h>
33 #include <QToolButton>
35 #include <QTemporaryFile>
36 #include <QTextStream>
42 #include "YacsTrace.hxx"
47 using namespace YACS::HMI;
49 EditionScript::EditionScript(Subject* subject,
52 : EditionElementaryNode(subject, parent, name)
57 _subInlineNode = dynamic_cast<SubjectInlineNode*>(_subject);
58 YASSERT(_subInlineNode);
60 QSplitter *splitter = new QSplitter(this);
61 splitter->setOrientation(Qt::Vertical);
62 _wid->gridLayout1->addWidget(splitter);
64 QWidget* widg=new QWidget;
65 _portslayout = new QVBoxLayout;
66 widg->setLayout(_portslayout);
67 _portslayout->setMargin(1);
68 splitter->addWidget(widg);
70 QWidget* window=new QWidget;
71 _glayout=new QVBoxLayout;
72 window->setLayout(_glayout);
73 _glayout->setMargin(1);
74 splitter->addWidget(window);
76 //add an options section in ports layout for execution mode (local or remote)
77 QHBoxLayout* hboxLayout = new QHBoxLayout();
78 hboxLayout->setMargin(0);
79 QToolButton* tb_options = new QToolButton();
80 tb_options->setCheckable(true);
82 icon.addFile("icons:icon_down.png");
83 icon.addFile("icons:icon_up.png", QSize(), QIcon::Normal, QIcon::On);
84 tb_options->setIcon(icon);
85 hboxLayout->addWidget(tb_options);
87 QLabel* label = new QLabel("Execution Mode");
92 hboxLayout->addWidget(label);
94 _portslayout->addLayout(hboxLayout);
96 fr_options = new QFrame();
97 QHBoxLayout* hboxLayout1 = new QHBoxLayout(fr_options);
98 hboxLayout1->setMargin(0);
99 radiolocal= new QRadioButton("YACS");
100 radioremote= new QRadioButton("Container");
101 radiolocal->setChecked(true);
102 hboxLayout1->addWidget(radiolocal);
103 hboxLayout1->addWidget(radioremote);
104 hboxLayout->addWidget(fr_options);
106 fr_container = new QFrame();
107 QHBoxLayout* hboxLayout2 = new QHBoxLayout(fr_container);
108 hboxLayout2->setMargin(0);
109 QLabel* laContainer = new QLabel("Container:");
110 hboxLayout2->addWidget(laContainer);
111 cb_container = new ComboBox();
112 hboxLayout2->addWidget(cb_container);
113 _portslayout->addWidget(fr_container);
116 YACS::ENGINE::InlineNode *pyNode(dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode()));
117 YACS::ENGINE::Container *cont(pyNode->getContainer());
118 formcontainer = new FormContainerDecorator(cont,this);
119 _portslayout->addWidget(formcontainer);
120 //_portslayout->addWidget(formcontainer->getWidget());
121 //end of insertion of execution mode
123 createTablePorts(_portslayout);
124 setEditablePorts(true);
128 _sci = new PyEditor_Editor(this);
130 _sci = new QTextEdit(this);
132 _wid->gridLayout->removeItem(_wid->spacerItem);
134 _editor = new QPushButton("External Editor", this);
135 connect(_editor, SIGNAL(clicked()), this, SLOT(onEdit()));
136 if(!Resource::pythonExternalEditor.isEmpty())
138 _glayout->addWidget( _editor );
140 _glayout->addWidget( _sci );
142 if (!QtGuiContext::getQtCurrent()->isEdition())
143 _sci->setReadOnly(true);
145 if (YACS::ENGINE::InlineNode* pyNode =
146 dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode()))
148 _sci->append(pyNode->getScript().c_str());
150 connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
152 connect(tb_options, SIGNAL(toggled(bool)), this, SLOT(on_tb_options_toggled(bool)));
153 connect(radioremote, SIGNAL(toggled(bool)), this, SLOT(on_remote_toggled(bool)));
154 connect(cb_container, SIGNAL(mousePressed()), this, SLOT(fillContainerPanel()));
155 connect(cb_container, SIGNAL(activated(int)), this, SLOT(changeContainer(int)));
158 on_tb_options_toggled(false);
161 EditionScript::~EditionScript()
165 void EditionScript::synchronize()
167 DEBTRACE("EditionScript::synchronize " << this->_isEdited);
168 EditionElementaryNode::synchronize();
169 YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
171 disconnect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
175 _sci->append(pyNode->getScript().c_str());
177 connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
180 void EditionScript::onApply()
182 DEBTRACE("EditionScript::onApply");
183 bool scriptEdited = false;
185 PyEditor_Settings settings = _sci->settings();
186 settings.setFont(Resource::pythonfont);
187 _sci->setSettings(settings);
190 if(Resource::pythonExternalEditor.isEmpty())
193 _glayout->removeWidget(_editor);
198 if(_glayout->itemAt(0)->widget() != _editor)
199 _glayout->insertWidget ( 0, _editor );
204 if (_sci->document()->isModified())
207 std::string text=_sci->document()->toPlainText().toStdString();
208 if(text[text.length()-1] != '\n')
210 bool ret = _subInlineNode->setScript(text);
211 if (ret) scriptEdited = false;
215 bool containerEdited = true;
216 if (formcontainer->onApply()) {
217 fillContainerPanel();
218 containerEdited = false;
220 Message mess(GuiContext::getCurrent()->_lastErrorMessage);
224 _isEdited = _isEdited || scriptEdited || containerEdited;
226 EditionElementaryNode::onApply();
229 void EditionScript::onCancel()
232 _sci->setText(_subInlineNode->getScript().c_str());
233 formcontainer->onCancel();
234 EditionElementaryNode::onCancel();
237 void EditionScript::onScriptModified()
239 DEBTRACE("EditionScript::onScriptModified");
244 void EditionScript::onEdit()
246 DEBTRACE("EditionScript::onEdit");
247 QTemporaryFile outFile;
250 QString fileName = outFile.fileName();
251 QTextStream out(&outFile);
252 out << _sci->toPlainText();
255 QStringList options=Resource::pythonExternalEditor.split(" ");
256 QString prog=options.takeAt(0);
257 QProcess::execute(prog, QStringList() << options << fileName);
259 QFile inFile(fileName);
260 if (!inFile.open(QIODevice::ReadOnly))
262 QTextStream in(&inFile);
263 _sci->setPlainText(in.readAll());
267 void EditionScript::on_tb_options_toggled(bool checked)
269 DEBTRACE("EditionScript::on_tb_options_toggled " << checked);
276 fr_container->show();
277 formcontainer->show();
283 fr_container->hide();
284 formcontainer->hide();
288 void EditionScript::on_remote_toggled(bool checked)
290 DEBTRACE("EditionScript::on_remote_toggled " << checked);
292 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
293 std::string mode = pyNode->getExecutionMode();
298 //remote radio button is checked
300 _subInlineNode->setExecutionMode("remote");
301 fr_container->show();
302 formcontainer->show();
303 fillContainerPanel();
307 //remote radio button is unchecked
309 _subInlineNode->setExecutionMode("local");
310 fr_container->hide();
311 formcontainer->hide();
315 void EditionScript::fillContainerPanel()
317 DEBTRACE("EditionScript::fillContainerPanel ");
318 YACS::ENGINE::Proc* proc = GuiContext::getCurrent()->getProc();
320 cb_container->clear();
321 std::map<string,YACS::ENGINE::Container*>::const_iterator it = proc->containerMap.begin();
322 for(; it != proc->containerMap.end(); ++it)
323 cb_container->addItem( QString((*it).first.c_str()));
325 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
327 YACS::ENGINE::Container * cont = pyNode->getContainer();
330 int index = cb_container->findText(cont->getName().c_str());
331 cb_container->setCurrentIndex(index);
332 formcontainer->FillPanel(cont);
334 else if (cb_container->count()) {
339 void EditionScript::changeContainer(int index)
341 DEBTRACE("EditionScript::changeContainer ");
342 string contName = cb_container->itemText(index).toStdString();
344 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
345 YACS::ENGINE::Container *oldContainer = pyNode->getContainer();
347 YACS::ENGINE::Container *newContainer = 0;
348 YACS::ENGINE::Proc* proc = GuiContext::getCurrent()->getProc();
349 if (proc->containerMap.count(contName))
350 newContainer = proc->containerMap[contName];
353 DEBTRACE("-------------> not found : " << contName);
356 YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(newContainer));
357 SubjectContainerBase *scnt(GuiContext::getCurrent()->_mapOfSubjectContainer[newContainer]);
359 _subInlineNode->setContainer(scnt);
361 // show the selected container parameters
362 formcontainer->FillPanel(newContainer);
365 void EditionScript::update(GuiEvent event, int type, Subject* son)
367 DEBTRACE("EditionScript::update " << eventName(event) <<" "<<type<<" "<<son);
368 EditionElementaryNode::update(event, type, son);
369 if(event == ASSOCIATE)
371 fillContainerPanel();
372 SubjectContainer *scont = dynamic_cast<SubjectContainer*>(son);
374 formcontainer->FillPanel(scont->getContainer());
376 else if(event == UPDATE)
378 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
379 std::string mode = pyNode->getExecutionMode();
383 radioremote->setChecked(true);
385 else if(mode == "local")
388 radiolocal->setChecked(true);
391 fillContainerPanel();