1 // Copyright (C) 2006-2012 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.
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
21 #include "GuiEditor.hxx"
22 #include "RuntimeSALOME.hxx"
25 #include "ForEachLoop.hxx"
26 #include "Catalog.hxx"
27 #include "Container.hxx"
28 #include "ComponentInstance.hxx"
29 #include "guiObservers.hxx"
30 #include "QtGuiContext.hxx"
31 #include "TypeCode.hxx"
33 #include "SceneComposedNodeItem.hxx"
34 #include "SceneLinkItem.hxx"
35 #include "Catalog.hxx"
36 #include "ItemMimeData.hxx"
37 #include "Message.hxx"
38 #include "Resource.hxx"
39 #include "FormUndoRedo.hxx"
40 #include <QMessageBox>
47 #include "YacsTrace.hxx"
50 using namespace YACS::ENGINE;
51 using namespace YACS::HMI;
53 GuiEditor::GuiEditor()
55 // approximative conversion from latin1 to US ascii (removing accentuation)
56 DEBTRACE("GuiEditor::GuiEditor");
57 _table = "________________" ; // 0 - 15
58 _table += "________________" ; // 16 - 31
59 _table += " !\"#$%&'()*+,-./" ; // 32 - 47
60 _table += "0123456789:;<=>?" ; // 48 - 63
61 _table += "@ABCDEFGHIJKLMNO" ; // 64 - 79
62 _table += "PQRSTUVWXYZ[\\]^_" ; // 80 - 95
63 _table += "`abcdefghijklmno" ; // 96 - 111
64 _table += "pqrstuvwxyz{|}~_" ; // 112 - 127
65 _table += "________________" ; // 128 - 143
66 _table += "________________" ; // 144 - 159
67 _table += "_icLoY|-_ca-__r-" ; // 160 - 175
68 _table += "-_23'u_..10\"___?" ; // 176 - 191
69 _table += "AAAAAAACEEEEIIII" ; // 192 - 207
70 _table += "DNOOOOOx0UUUUYPB" ; // 208 - 223
71 _table += "aaaaaaaceeeeiiii" ; // 224 - 239
72 _table += "onooooo-0uuuuypy" ; // 240 - 255
73 //_table[167] = char(167); // '§'
74 //_table[176] = char(176); // '°'
75 DEBTRACE(_table.size() << " " << _table);
78 GuiEditor::~GuiEditor()
80 DEBTRACE("GuiEditor::~GuiEditor");
83 void GuiEditor::CreateNodeFromCatalog(const ItemMimeData* myData, SubjectComposedNode *cnode,bool createNewComponentInstance)
85 DEBTRACE("GuiEditor::CreateNodeFromCatalog");
86 int nb = myData->getDataSize();
88 for (int i=0; i<nb; i++)
90 Catalog* catalog = myData->getCatalog(i);
91 string compoName = myData->getCompo(i);
92 string service = myData->getType(i);
93 DEBTRACE(compoName << "/" << service);
94 _createNode(catalog, cnode, service, compoName,createNewComponentInstance);
98 void GuiEditor::CreateNode(std::string typeNode)
100 DEBTRACE("GuiEditor::CreateNode " << typeNode);
101 YACS::ENGINE::Catalog *catalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
103 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
106 DEBTRACE("GuiEditor::CreateNode : invalid selection!");
109 DEBTRACE(sub->getName());
111 YACS::HMI::SubjectComposedNode *cnode = dynamic_cast< YACS::HMI::SubjectComposedNode*>(sub);
114 DEBTRACE("GuiEditor::CreateNode : no ComposedNode selected!");
118 _createNode(catalog, cnode, typeNode, "", Resource::COMPONENT_INSTANCE_NEW);
121 void GuiEditor::_createNode(YACS::ENGINE::Catalog* catalog,
122 SubjectComposedNode *cnode,
124 std::string compoName,
125 bool createNewComponentInstance)
127 // --- find a name not used
129 string name = service;
130 if (name == "PresetNode")
132 Node *node =cnode->getNode();
133 ComposedNode *father = dynamic_cast<ComposedNode*>(node);
135 list<Node*> children = father->edGetDirectDescendants();
136 bool nameInUse = true;
137 std::stringstream tryname;
141 long newid = GuiContext::getCurrent()->getNewId();
143 tryname << name << newid;
144 if (newid > 100000) break;
145 for (list<Node*>::iterator it = children.begin(); it != children.end(); ++it)
147 if ((*it)->getName() == tryname.str())
151 name = tryname.str();
154 SubjectSwitch *aSwitch = dynamic_cast<SubjectSwitch*>(cnode);
157 map<int, SubjectNode*> bodyMap = aSwitch->getBodyMap();
158 if (bodyMap.empty()) swCase = 1;
161 map<int, SubjectNode*>::reverse_iterator rit = bodyMap.rbegin();
162 swCase = (*rit).first + 1;
164 if (!aSwitch->addNode(catalog, compoName, service, name, createNewComponentInstance, swCase))
167 else if (cnode && (dynamic_cast<SubjectBloc*>(cnode) == 0) && cnode->getChild() != 0)
169 // loop with a body : can't add a node
172 msg="This loop has already a body. It is not possible to add directly another node\n";
173 msg=msg+"Do you want to put the existing node in a bloc and add the new node in this bloc ?\n";
174 msgBox.setText(msg.c_str());
175 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No );
176 msgBox.setDefaultButton(QMessageBox::No);
177 int ret = msgBox.exec();
178 if(ret == QMessageBox::Yes)
180 // User wants to put body node in bloc
181 if (cnode->getChild()->putInComposedNode("Bloc1","Bloc"))
183 //the bloc has been successfully created. Add the new node
184 SubjectBloc* newbloc = dynamic_cast<SubjectBloc*>(cnode->getChild());
185 if (!newbloc->addNode(catalog, compoName, service, name, createNewComponentInstance))
193 if (!cnode->addNode(catalog, compoName, service, name, createNewComponentInstance))
197 void GuiEditor::AddTypeFromCatalog(const ItemMimeData* myData)
199 DEBTRACE("GuiEditor::AddTypeFromCatalog");
200 SubjectProc* sProc = QtGuiContext::getQtCurrent()->getSubjectProc();
201 int nb = myData->getDataSize();
203 for (int i=0; i<nb; i++)
205 Catalog* catalog = myData->getCatalog(i);
206 DEBTRACE("catalog " << catalog);
207 string aType = myData->getType(i);
209 sProc->addDataType(catalog, aType);
213 void GuiEditor::CreateBloc()
215 DEBTRACE("GuiEditor::CreateBloc");
219 void GuiEditor::CreateForLoop()
221 DEBTRACE("GuiEditor::CreateForLoop");
222 CreateNode("ForLoop");
225 void GuiEditor::CreateForEachLoop(std::string type)
227 DEBTRACE("GuiEditor::CreateForEachLoop");
228 // The ForEachLoop node for datatype type must exist in builtin catalog
229 // So create it in builtin catalog if it does not exist and datatype is loaded in the current Proc
230 YACS::ENGINE::Catalog *catalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
231 Proc* proc = GuiContext::getCurrent()->getProc();
232 std::string typeName="ForEachLoop_"+type;
233 if (!catalog->_composednodeMap.count(typeName))
235 if(proc->typeMap.count(type))
237 catalog->_composednodeMap[typeName]=new ForEachLoop(typeName,proc->typeMap[type]);
240 CreateNode(typeName);
243 void GuiEditor::CreateWhileLoop()
245 DEBTRACE("GuiEditor::CreateWhileLoop");
246 CreateNode("WhileLoop");
249 void GuiEditor::CreateSwitch()
251 DEBTRACE("GuiEditor::CreateSwitch");
252 CreateNode("Switch");
255 void GuiEditor::CreateOptimizerLoop()
257 DEBTRACE("GuiEditor::CreateOptimizerLoop");
258 CreateNode("OptimizerLoop");
261 void GuiEditor::CreateContainer()
263 DEBTRACE("GuiEditor::CreateContainer");
264 SubjectProc *sproc = QtGuiContext::getQtCurrent()->getSubjectProc();
266 SubjectContainer *scont = 0;
269 std::stringstream name;
270 long newid = GuiContext::getCurrent()->getNewId();
271 if (newid > 100000) break;
273 name << "container" << newid;
274 scont = sproc->addContainer(name.str());
278 void GuiEditor::CreateComponentInstance()
280 DEBTRACE("GuiEditor::CreateComponentInstance");
281 SubjectProc *sproc = QtGuiContext::getQtCurrent()->getSubjectProc();
283 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
284 SubjectComponent *sco = dynamic_cast<SubjectComponent*>(sub);
287 DEBTRACE("GuiEditor::CreateComponentInstance: " << "selection is not a component");
290 string compoName = sco->getComponent()->getCompoName();
291 string containerName = sco->getComponent()->getContainer()->getName();
292 sproc->addComponent(compoName, containerName);
296 SubjectDataPort* GuiEditor::CreateInputPort(SubjectElementaryNode* seNode,
298 YACS::ENGINE::Catalog *catalog,
300 SubjectDataPort* before)
302 DEBTRACE("GuiEditor::CreateInputPort");
303 SubjectDataPort *sdp = 0;
306 std::stringstream aName;
308 while (newid < 100000)
310 newid = GuiContext::getCurrent()->getNewId();
312 aName << "i" << newid;
315 seNode->getNode()->getInputPort(aName.str());
322 sdp = seNode->addInputPort(catalog,type, aName.str());
325 sdp = seNode->addInputPort(catalog,type, name);
331 SubjectDataPort* GuiEditor::CreateOutputPort(SubjectElementaryNode* seNode,
333 YACS::ENGINE::Catalog *catalog,
335 SubjectDataPort* before)
337 DEBTRACE("GuiEditor::CreateOutputPort");
338 SubjectDataPort *sdp = 0;
341 std::stringstream aName;
343 while (newid < 100000)
345 newid = GuiContext::getCurrent()->getNewId();
347 aName << "o" << newid;
350 seNode->getNode()->getOutputPort(aName.str());
357 sdp = seNode->addOutputPort(catalog,type, aName.str());
360 sdp = seNode->addOutputPort(catalog,type, name);
367 * Subject shrink or expand, command from popup menu: needs a valid selection
369 void GuiEditor::shrinkExpand() {
370 DEBTRACE("GuiEditor::shrinkExpand");
372 Subject* sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
374 DEBTRACE("GuiEditor::shrinkExpand : invalid selection!");
378 if (! QtGuiContext::getQtCurrent()->_mapOfSceneItem.count(sub)) {
379 DEBTRACE("GuiEditor::shrinkExpand: no scene item corresponding to this subject");
383 SceneItem* item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
384 SceneNodeItem *sni = dynamic_cast<SceneNodeItem*>(item);
386 DEBTRACE("GuiEditor::shrinkExpand: no scene node item corresponding to this subject");
390 if (sni->isExpanded()) {
391 sni->setExpanded(false);
393 sni->setExpanded(true);
395 sni->reorganizeShrinkExpand();
396 sni->showOutScopeLinks();
401 * Subject destruction, command from popup menu: needs a valid selection
403 void GuiEditor::DeleteSubject()
405 DEBTRACE("GuiEditor::DeleteSubject");
406 if (!QtGuiContext::getQtCurrent()->isEdition()) return;
407 QModelIndexList selList
408 = QtGuiContext::getQtCurrent()->getSelectionModel()->selectedIndexes();
409 if (selList.isEmpty())
412 QModelIndex selected = selList.front();
413 if (!selected.isValid())
416 SchemaItem *selItem = static_cast<SchemaItem*>(selected.internalPointer());
417 if (!selItem) return;
418 Subject *subToRemove = selItem->getSubject();
419 Subject *subParent = subToRemove->getParent();
420 DeleteSubject(subParent, subToRemove);
424 * Subject destruction, command from button in port table view or via selection
426 void GuiEditor::DeleteSubject(Subject* parent,
429 DEBTRACE("GuiEditor::DeleteSubject "<<parent->getName()<<" "<<toRemove->getName());
430 if (!QtGuiContext::getQtCurrent()->isEdition()) return;
431 toRemove->askRegisterUndoDestroy();
432 if(!parent->destroy(toRemove))
436 void GuiEditor::CutSubject()
438 DEBTRACE("GuiEditor::CutSubject");
439 if (!QtGuiContext::getQtCurrent()->isEdition()) return;
440 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
443 Message mess("GuiEditor::Cut : invalid selection!");
446 DEBTRACE(sub->getName());
447 QtGuiContext::getQtCurrent()->setSubjectToCut(sub);
450 void GuiEditor::CopySubject()
452 DEBTRACE("GuiEditor::CopySubject");
453 if (!QtGuiContext::getQtCurrent()->isEdition()) return;
454 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
457 Message mess("GuiEditor::Copy : invalid selection!");
460 DEBTRACE(sub->getName());
461 QtGuiContext::getQtCurrent()->setSubjectToCopy(sub);
464 void GuiEditor::PasteSubject()
466 DEBTRACE("GuiEditor::PasteSubject");
467 if (!QtGuiContext::getQtCurrent()->isEdition()) return;
468 Subject *newParent = QtGuiContext::getQtCurrent()->getSelectedSubject();
471 Message mess("GuiEditor::Paste : invalid selection!");
475 Subject *sub = QtGuiContext::getQtCurrent()->getSubjectToPaste(isCut);
478 Message mess("Nothing to paste");
481 if (SubjectNode *snode = dynamic_cast<SubjectNode*>(sub))
486 if (!snode->reparent(newParent))
492 if (!snode->copy(newParent))
497 Message mess("Paste not possible for this kind of object");
500 void GuiEditor::PutSubjectInBloc()
502 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
505 Message mess("GuiEditor::PutSubjectInBloc : invalid selection!");
508 if (SubjectNode *snode = dynamic_cast<SubjectNode*>(sub))
510 //Build the set of children node names
511 Node* node=snode->getNode();
512 ComposedNode* father=node->getFather();
513 std::list<Node*> children = father->edGetDirectDescendants();
514 std::set<std::string> names;
515 for (std::list<Node*>::iterator it = children.begin(); it != children.end(); ++it)
516 names.insert((*it)->getName());
518 std::stringstream tryname;
520 while (newid < 100000)
523 tryname << "Bloc" << newid;
524 if(names.find(tryname.str()) == names.end())break;
528 if (!snode->putInComposedNode(tryname.str(),"Bloc"))
533 Message mess("Put in Bloc not possible for this kind of object");
536 void GuiEditor::rebuildLinks()
538 // --- only global link redraw for now...
539 DEBTRACE("GuiEditor::rebuildLinks");
540 YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
541 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
542 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
543 proc->rebuildLinks();
546 void GuiEditor::arrangeNodes(bool isRecursive)
548 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
551 DEBTRACE("GuiEditor::arrangeNodes : invalid selection!");
554 DEBTRACE(sub->getName());
556 if (! QtGuiContext::getQtCurrent()->_mapOfSceneItem.count(sub))
558 DEBTRACE("no scene item corresponding to this subject");
561 SceneItem* item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
562 SceneComposedNodeItem *sci = dynamic_cast<SceneComposedNodeItem*>(item);
565 DEBTRACE("no scene composed node item corresponding to this subject");
568 sci->arrangeNodes(isRecursive);
569 // if (Scene::_autoComputeLinks && !QtGuiContext::getQtCurrent()->isLoading())
571 // YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
572 // SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
573 // SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
574 // proc->rebuildLinks();
578 void GuiEditor::arrangeProc()
580 YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
581 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
582 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
583 proc->arrangeNodes(true);
586 void GuiEditor::showUndo(QWidget *parent)
588 FormUndoRedo *undo = new FormUndoRedo(parent);
589 undo->tabWidget->setCurrentWidget(undo->undoTab);
593 void GuiEditor::showRedo(QWidget *parent)
595 FormUndoRedo *redo = new FormUndoRedo(parent);
596 redo->tabWidget->setCurrentWidget(redo->redoTab);
600 /*! Replace accentuated characters from latin1 to US ascii equivalent without accent.
601 * I did not found anything to do that in Qt...
603 QString GuiEditor::asciiFilter(const QString & name)
605 DEBTRACE(name.toStdString());
606 string aName = name.toAscii().data();
608 for (int i=0; i < aName.size(); i++)
610 int v = (unsigned char)(aName[i]);
611 DEBTRACE(v << " " << _table[v]);
612 aName[i] = _table[v];
615 return aName.c_str();