1 // Copyright (C) 2006-2008 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
20 #include "GuiEditor.hxx"
21 #include "RuntimeSALOME.hxx"
24 #include "Catalog.hxx"
25 #include "guiObservers.hxx"
26 #include "QtGuiContext.hxx"
27 #include "TypeCode.hxx"
28 #include "SceneComposedNodeItem.hxx"
29 #include "SceneLinkItem.hxx"
30 #include "Catalog.hxx"
31 #include "ItemMimeData.hxx"
32 #include "Message.hxx"
39 #include "YacsTrace.hxx"
42 using namespace YACS::ENGINE;
43 using namespace YACS::HMI;
45 GuiEditor::GuiEditor()
47 DEBTRACE("GuiEditor::GuiEditor");
50 GuiEditor::~GuiEditor()
52 DEBTRACE("GuiEditor::~GuiEditor");
55 void GuiEditor::CreateNodeFromCatalog(const ItemMimeData* myData, SubjectComposedNode *cnode)
57 DEBTRACE("GuiEditor::CreateNodeFromCatalog");
58 Catalog* catalog = myData->getCatalog();
59 string compoName = myData->getCompo();
60 string service = myData->getType();
61 DEBTRACE(compoName << "/" << service);
62 std::stringstream name;
63 name << service << GuiContext::getCurrent()->getNewId();
66 SubjectSwitch *aSwitch = dynamic_cast<SubjectSwitch*>(cnode);
69 map<int, SubjectNode*> bodyMap = aSwitch->getBodyMap();
70 if (bodyMap.empty()) swCase = 1;
73 map<int, SubjectNode*>::reverse_iterator rit = bodyMap.rbegin();
74 swCase = (*rit).first + 1;
76 if (!aSwitch->addNode(catalog, compoName, service, name.str(), swCase))
80 if (! cnode->addNode(catalog, compoName, service, name.str()))
84 void GuiEditor::AddTypeFromCatalog(const ItemMimeData* myData)
86 DEBTRACE("GuiEditor::AddTypeFromCatalog");
87 Catalog* catalog = myData->getCatalog();
88 DEBTRACE("catalog " << catalog);
89 string aType = myData->getType();
91 SubjectProc* sProc = QtGuiContext::getQtCurrent()->getSubjectProc();
92 sProc->addDataType(catalog, aType);
95 void GuiEditor::CreateNode(std::string typeNode)
97 DEBTRACE("GuiEditor::CreateNode " << typeNode);
98 YACS::ENGINE::Catalog *catalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
100 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
103 DEBTRACE("GuiEditor::CreateNode : invalid selection!");
106 DEBTRACE(sub->getName());
108 YACS::HMI::SubjectComposedNode *subject = dynamic_cast< YACS::HMI::SubjectComposedNode*>(sub);
111 DEBTRACE("GuiEditor::CreateNode : no ComposedNode selected!");
115 std::stringstream name;
116 name << typeNode << GuiContext::getCurrent()->getNewId();
118 YACS::HMI::SubjectSwitch *aSwitch = dynamic_cast< YACS::HMI::SubjectSwitch*>(subject);
121 map<int, SubjectNode*> bodyMap = aSwitch->getBodyMap();
123 if (bodyMap.empty()) swCase = 1;
126 map<int, SubjectNode*>::reverse_iterator rit = bodyMap.rbegin();
127 swCase = (*rit).first + 1;
129 if (!aSwitch->addNode(catalog, "", typeNode, name.str(), swCase))
133 if (!subject->addNode(catalog, "", typeNode, name.str()))
137 void GuiEditor::CreateBloc()
139 DEBTRACE("GuiEditor::CreateBloc");
143 void GuiEditor::CreateForLoop()
145 DEBTRACE("GuiEditor::CreateForLoop");
146 CreateNode("ForLoop");
149 void GuiEditor::CreateForEachLoop()
151 DEBTRACE("GuiEditor::CreateForEachLoop");
152 CreateNode("ForEachLoopDouble");
155 void GuiEditor::CreateWhileLoop()
157 DEBTRACE("GuiEditor::CreateWhileLoop");
158 CreateNode("WhileLoop");
161 void GuiEditor::CreateSwitch()
163 DEBTRACE("GuiEditor::CreateSwitch");
164 CreateNode("Switch");
167 void GuiEditor::CreateContainer()
169 DEBTRACE("GuiEditor::CreateContainer");
170 SubjectProc *sproc = QtGuiContext::getQtCurrent()->getSubjectProc();
172 std::stringstream name;
173 name << "container" << GuiContext::getCurrent()->getNewId();
174 sproc->addContainer(name.str());
177 SubjectDataPort* GuiEditor::CreateInputPort(SubjectElementaryNode* seNode,
179 YACS::ENGINE::Catalog *catalog,
181 SubjectDataPort* before)
183 DEBTRACE("GuiEditor::CreateInputPort");
185 std::stringstream aName;
187 aName << "i" << GuiContext::getCurrent()->getNewId();
190 SubjectDataPort *sdp = seNode->addInputPort(catalog,type, aName.str());
196 SubjectDataPort* GuiEditor::CreateOutputPort(SubjectElementaryNode* seNode,
198 YACS::ENGINE::Catalog *catalog,
200 SubjectDataPort* before)
202 DEBTRACE("GuiEditor::CreateOutputPort");
204 std::stringstream aName;
206 aName << "o" << GuiContext::getCurrent()->getNewId();
209 SubjectDataPort *sdp = seNode->addOutputPort(catalog,type, aName.str());
216 * Subject destruction, command from popup menu: needs a valid selection
218 void GuiEditor::DeleteSubject()
220 DEBTRACE("GuiEditor::DeleteSubject");
221 QModelIndexList selList
222 = QtGuiContext::getQtCurrent()->getSelectionModel()->selectedIndexes();
223 if (selList.isEmpty())
226 QModelIndex selected = selList.front();
227 if (!selected.isValid())
230 SchemaItem *selItem = static_cast<SchemaItem*>(selected.internalPointer());
231 if (!selItem) return;
232 Subject *subToRemove = selItem->getSubject();
233 Subject *subParent = subToRemove->getParent();
235 DeleteSubject(subParent, subToRemove);
239 * Subject destruction, command from button in port table view or via selection
241 void GuiEditor::DeleteSubject(Subject* parent,
244 DEBTRACE("GuiEditor::DeleteSubject "<<parent->getName()<<" "<<toRemove->getName());
245 parent->destroy(toRemove);
248 void GuiEditor::CutSubject()
250 DEBTRACE("GuiEditor::CutSubject");
251 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
254 Message mess("GuiEditor::Cut : invalid selection!");
257 DEBTRACE(sub->getName());
258 QtGuiContext::getQtCurrent()->setSubjectToCut(sub);
261 void GuiEditor::CopySubject()
263 DEBTRACE("GuiEditor::CopySubject");
264 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
267 Message mess("GuiEditor::Copy : invalid selection!");
270 DEBTRACE(sub->getName());
271 QtGuiContext::getQtCurrent()->setSubjectToCopy(sub);
274 void GuiEditor::PasteSubject()
276 DEBTRACE("GuiEditor::PasteSubject");
277 Subject *newParent = QtGuiContext::getQtCurrent()->getSelectedSubject();
280 Message mess("GuiEditor::Paste : invalid selection!");
284 Subject *sub = QtGuiContext::getQtCurrent()->getSubjectToPaste(isCut);
287 Message mess("Nothing to paste");
290 if (SubjectNode *snode = dynamic_cast<SubjectNode*>(sub))
295 if (!snode->reparent(newParent))
301 if (!snode->copy(newParent))
306 Message mess("Paste not possible for this kind of object");
309 void GuiEditor::rebuildLinks()
311 // --- only global link redraw for now...
313 YACS::HMI::SubjectProc* subproc = QtGuiContext::getQtCurrent()->getSubjectProc();
314 SceneItem *item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[subproc];
315 SceneComposedNodeItem *proc = dynamic_cast<SceneComposedNodeItem*>(item);
316 proc->rebuildLinks();
319 void GuiEditor::arrangeNodes(bool isRecursive)
321 Subject *sub = QtGuiContext::getQtCurrent()->getSelectedSubject();
324 DEBTRACE("GuiEditor::arrangeNodes : invalid selection!");
327 DEBTRACE(sub->getName());
329 if (! QtGuiContext::getQtCurrent()->_mapOfSceneItem.count(sub))
331 DEBTRACE("no scene item corresponding to this subject");
334 SceneItem* item = QtGuiContext::getQtCurrent()->_mapOfSceneItem[sub];
335 SceneComposedNodeItem *sci = dynamic_cast<SceneComposedNodeItem*>(item);
338 DEBTRACE("no scene composed node item corresponding to this subject");
341 sci->arrangeNodes(isRecursive);