1 // Copyright (C) 2006-2014 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 <qsciscintilla.h>
30 #include <qscilexerpython.h>
33 #include <QToolButton>
35 #include <QTemporaryFile>
36 #include <QTextStream>
42 #include "YacsTrace.hxx"
47 using namespace YACS::HMI;
50 class myQsciScintilla: public QsciScintilla
53 myQsciScintilla(QWidget *parent=0)
54 : QsciScintilla(parent) {};
56 virtual QSize sizeHint() const { return QSize(350, 150); };
58 virtual bool event(QEvent *e);
61 bool myQsciScintilla::event(QEvent *e)
63 if (e->type() == QEvent::ShortcutOverride)
68 return QsciScintilla::event(e);
74 EditionScript::EditionScript(Subject* subject,
77 : EditionElementaryNode(subject, parent, name)
82 _subInlineNode = dynamic_cast<SubjectInlineNode*>(_subject);
83 YASSERT(_subInlineNode);
85 QSplitter *splitter = new QSplitter(this);
86 splitter->setOrientation(Qt::Vertical);
87 _wid->gridLayout1->addWidget(splitter);
89 QWidget* widg=new QWidget;
90 _portslayout = new QVBoxLayout;
91 widg->setLayout(_portslayout);
92 _portslayout->setMargin(1);
93 splitter->addWidget(widg);
95 QWidget* window=new QWidget;
96 _glayout=new QVBoxLayout;
97 window->setLayout(_glayout);
98 _glayout->setMargin(1);
99 splitter->addWidget(window);
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);
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);
112 QLabel* label = new QLabel("Execution Mode");
116 label->setFont(font);
117 hboxLayout->addWidget(label);
119 _portslayout->addLayout(hboxLayout);
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);
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);
140 formcontainer = new FormContainer(this);
141 formcontainer->on_tb_container_toggled(false);
142 _portslayout->addWidget(formcontainer);
143 //end of insertion of execution mode
145 createTablePorts(_portslayout);
146 setEditablePorts(true);
150 _sci = new myQsciScintilla(this);
152 _sci = new QTextEdit(this);
154 _wid->gridLayout->removeItem(_wid->spacerItem);
156 _editor = new QPushButton("External Editor", this);
157 connect(_editor, SIGNAL(clicked()), this, SLOT(onEdit()));
158 if(!Resource::pythonExternalEditor.isEmpty())
160 _glayout->addWidget( _editor );
162 _glayout->addWidget( _sci );
166 QsciLexerPython *lex = new QsciLexerPython(_sci);
167 lex->setFont(Resource::pythonfont);
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);
180 if (!QtGuiContext::getQtCurrent()->isEdition())
181 _sci->setReadOnly(true);
183 if (YACS::ENGINE::InlineNode* pyNode =
184 dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode()))
186 _sci->append(pyNode->getScript().c_str());
188 connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
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)));
196 on_tb_options_toggled(false);
199 EditionScript::~EditionScript()
203 void EditionScript::synchronize()
205 DEBTRACE("EditionScript::synchronize " << this->_isEdited);
206 EditionElementaryNode::synchronize();
207 YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
209 disconnect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
213 _sci->append(pyNode->getScript().c_str());
215 connect(_sci, SIGNAL(textChanged()), this, SLOT(onScriptModified()));
218 void EditionScript::onApply()
220 DEBTRACE("EditionScript::onApply");
221 bool scriptEdited = false;
223 _sci->lexer()->setFont(Resource::pythonfont);
226 if(Resource::pythonExternalEditor.isEmpty())
229 _glayout->removeWidget(_editor);
234 if(_glayout->itemAt(0)->widget() != _editor)
235 _glayout->insertWidget ( 0, _editor );
241 if (_sci->isModified())
244 std::string text=_sci->text().toStdString();
245 if(text[text.length()-1] != '\n')
247 bool ret = _subInlineNode->setScript(text);
248 if (ret) scriptEdited = false;
251 if (_sci->document()->isModified())
254 std::string text=_sci->document()->toPlainText().toStdString();
255 if(text[text.length()-1] != '\n')
257 bool ret = _subInlineNode->setScript(text);
258 if (ret) scriptEdited = false;
263 bool containerEdited = true;
264 if (formcontainer->onApply()) {
265 fillContainerPanel();
266 containerEdited = false;
268 Message mess(GuiContext::getCurrent()->_lastErrorMessage);
272 _isEdited = _isEdited || scriptEdited || containerEdited;
274 EditionElementaryNode::onApply();
277 void EditionScript::onCancel()
280 _sci->setText(_subInlineNode->getScript().c_str());
281 formcontainer->onCancel();
282 EditionElementaryNode::onCancel();
285 void EditionScript::onScriptModified()
287 DEBTRACE("EditionScript::onScriptModified");
292 void EditionScript::onEdit()
294 DEBTRACE("EditionScript::onEdit");
295 QTemporaryFile outFile;
298 QString fileName = outFile.fileName();
299 QTextStream out(&outFile);
303 out << _sci->toPlainText();
307 QStringList options=Resource::pythonExternalEditor.split(" ");
308 QString prog=options.takeAt(0);
309 QProcess::execute(prog, QStringList() << options << fileName);
311 QFile inFile(fileName);
312 if (!inFile.open(QIODevice::ReadOnly))
314 QTextStream in(&inFile);
316 _sci->setText(in.readAll());
318 _sci->setPlainText(in.readAll());
323 void EditionScript::on_tb_options_toggled(bool checked)
325 DEBTRACE("EditionScript::on_tb_options_toggled " << checked);
331 fr_container->show();
332 formcontainer->show();
338 fr_container->hide();
339 formcontainer->hide();
343 void EditionScript::on_remote_toggled(bool checked)
345 DEBTRACE("EditionScript::on_remote_toggled " << checked);
347 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
348 std::string mode = pyNode->getExecutionMode();
353 //remote radio button is checked
355 _subInlineNode->setExecutionMode("remote");
356 fr_container->show();
357 formcontainer->show();
358 fillContainerPanel();
362 //remote radio button is unchecked
364 _subInlineNode->setExecutionMode("local");
365 fr_container->hide();
366 formcontainer->hide();
370 void EditionScript::fillContainerPanel()
372 DEBTRACE("EditionScript::fillContainerPanel ");
373 YACS::ENGINE::Proc* proc = GuiContext::getCurrent()->getProc();
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()));
380 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
382 YACS::ENGINE::Container * cont = pyNode->getContainer();
385 int index = cb_container->findText(cont->getName().c_str());
386 cb_container->setCurrentIndex(index);
387 formcontainer->FillPanel(cont);
389 else if (cb_container->count()) {
394 void EditionScript::changeContainer(int index)
396 DEBTRACE("EditionScript::changeContainer ");
397 string contName = cb_container->itemText(index).toStdString();
399 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
400 YACS::ENGINE::Container *oldContainer = pyNode->getContainer();
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];
408 DEBTRACE("-------------> not found : " << contName);
411 YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(newContainer));
412 SubjectContainer *scnt = GuiContext::getCurrent()->_mapOfSubjectContainer[newContainer];
414 _subInlineNode->setContainer(scnt);
416 // show the selected container parameters
417 formcontainer->FillPanel(newContainer);
420 void EditionScript::update(GuiEvent event, int type, Subject* son)
422 DEBTRACE("EditionScript::update " << eventName(event) <<" "<<type<<" "<<son);
423 EditionElementaryNode::update(event, type, son);
424 if(event == ASSOCIATE)
426 fillContainerPanel();
427 SubjectContainer *scont = dynamic_cast<SubjectContainer*>(son);
429 formcontainer->FillPanel(scont->getContainer());
431 else if(event == UPDATE)
433 YACS::ENGINE::InlineNode *pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(_subInlineNode->getNode());
434 std::string mode = pyNode->getExecutionMode();
438 radioremote->setChecked(true);
440 else if(mode == "local")
443 radiolocal->setChecked(true);
446 fillContainerPanel();