1 // SUPERV SUPERVGUI : GUI for Supervisor component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SUPERVGUI_Clipboard.cxx
25 // Author : Alexander SLADKOV
29 #include "SUPERVGUI_Clipboard.h"
30 #include "SUPERVGUI_CanvasNode.h"
31 #include "SUPERVGUI.h"
34 SUPERVGUI_Clipboard* SUPERVGUI_Clipboard::myCB = 0;
38 * Compute the next valid name for a Python function (add "_N" if a function with given name already exists)
40 QString getNewName( QStringList& allNames, const QString& oldName ) {
42 int id = 1; //increment index
43 newName = oldName + QString("_") + QString::number( id );
44 while ( allNames.contains( newName ) )
45 newName = oldName + QString("_") + QString::number( ++id );
51 * Replaces origName string with newName string in all lines of theFunc
52 * origName must be preceeded by space and end by space or '('.
53 * asv : 14.01.05 : fix of a bug (exception on node creation):
54 * if origName and theFunc is null, return non-null empty strings!
56 void replaceName( SUPERV::ListOfStrings_var& theFunc, const QString& origName, const QString& newName ) {
57 for ( int i = 0, n = theFunc->length(); i < n; i++ ) {
58 QString aLine( theFunc[i] );
59 int index = aLine.find( origName, 0 ); // find FIRST occurance of origName in aLine
60 while ( index >= 0 ) {
61 bool preceedingCharOk = ( index==0 || ( index > 0 && aLine[index-1].isSpace() ) );
62 const int ll = aLine.length();
63 const int ol = origName.length();
64 const int ni = index + ol;
65 bool nextCharOk = ( ll==ni || ( ll>ni && ( aLine[ni].isSpace() || aLine[ni]=='(' ) ) );
66 if ( preceedingCharOk && nextCharOk ) {
67 aLine = aLine.replace( index, origName.length(), newName );
68 theFunc[i] = aLine.latin1();
70 index = aLine.find( origName, index+newName.length() ); // find NEXT occurance of origName in aLine
76 * "Copies" all ports from fromNode to toNode: creates the sames ports in toNode in fact
78 void copyPorts( const SUPERV::CNode_var& fromNode, const SUPERV::INode_var& toNode ) {
79 if ( CORBA::is_nil( fromNode ) || CORBA::is_nil( toNode ) )
81 SUPERV::ListOfPorts_var aPList = fromNode->Ports();
83 for (int i = 0; i < aPList->length(); i++) {
84 aName = aPList[i].in()->Name();
85 aType = aPList[i].in()->Type();
86 if ( aPList[i].in()->IsInput() )
87 toNode->InPort( aName.latin1(), aType.latin1() );
89 toNode->OutPort( aName.latin1(), aType.latin1() );
96 SUPERVGUI_Clipboard::SUPERVGUI_Clipboard( QObject* parent )
103 SUPERVGUI_Clipboard::~SUPERVGUI_Clipboard() {
107 * Returns all inline functions defined in inline (switch, loop, goto) nodes of given dataflow
109 QStringList getAllFunctions( SUPERV::Graph_var dataflow ) {
110 QStringList aFuncNames;
111 if ( !CORBA::is_nil( dataflow ) ) {
112 SUPERV::ListOfNodes_var nodes = dataflow->Nodes();
114 for(int i = 0; i < nodes->INodes.length(); i++)
115 aFuncNames.append(nodes->INodes[i]->PyFuncName());
117 for(int i = 0; i < nodes->LNodes.length(); i++) {
118 aFuncNames.append(nodes->LNodes[i]->PyInitName());
119 aFuncNames.append(nodes->LNodes[i]->PyMoreName());
120 aFuncNames.append(nodes->LNodes[i]->PyNextName());
123 for(int i = 0; i < nodes->SNodes.length(); i++)
124 aFuncNames.append(nodes->SNodes[i]->PyFuncName());
126 for(int i = 0; i < nodes->GNodes.length(); i++)
127 aFuncNames.append(nodes->GNodes[i]->PyFuncName());
133 * Called on Paste Node command. Inserts a new node to the dataflow equal to the copied node.
134 * For InLine nodes the Python function name is changed ("_N" added).
136 void SUPERVGUI_Clipboard::pasteNode() {
137 Trace("SUPERVGUI_Main::pasteNode");
138 SUPERV::CNode_var aNode = getCopyNode();
139 SUPERVGUI_Main* aMain = Supervision.getMain();
140 if ( !CORBA::is_nil( aNode ) && aMain ) {
142 if ( !aMain->ReadyToModify() ) // null dataflow or executing, ..
145 aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
147 SUPERV::Graph_var dataflow = aMain->getDataflow();
149 switch ( aNode->Kind() ) {
151 case SUPERV::FactoryNode :
153 SUPERV::FNode_var aFNode = dataflow->FNode( SUPERV::FNode::_narrow(aNode)->GetComponentName(),
154 SUPERV::FNode::_narrow(aNode)->GetInterfaceName(),
155 *SUPERV::FNode::_narrow(aNode)->Service() );
156 if (CORBA::is_nil(aFNode)) {
157 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
161 SUPERV::INode_var aDummyEndNode;
162 Supervision.getBrowser()->addNode(SUPERV::CNode::_narrow(aFNode), aDummyEndNode, myXCopyNode, myYCopyNode);
166 case SUPERV::ComputingNode :
168 SUPERV::CNode_var aCNode = dataflow->CNode(*SUPERV::CNode::_narrow(aNode)->Service());
169 if (CORBA::is_nil(aCNode)) {
170 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
174 SUPERV::INode_var aDummyEndNode;
175 Supervision.getBrowser()->addNode(aCNode, aDummyEndNode, myXCopyNode, myYCopyNode);
179 case SUPERV::InLineNode :
181 QString aFName; // new node's Py_function name
182 SUPERV::ListOfStrings_var aFunc; // new node's Py_function body
184 // Automatic change of Py_function name ( + "_1", etc.)
185 // 1. collect all python functions names of allready exist InLine nodes
186 QStringList aFuncNames = getAllFunctions( dataflow );
187 // 2. "fix" Main function_name and Main function_strings
188 QString aOriginalName = SUPERV::INode::_narrow(aNode)->PyFuncName();
189 if ( !aOriginalName.isEmpty() ) {
190 aFName = getNewName( aFuncNames, aOriginalName );
191 aFunc = SUPERV::INode::_narrow(aNode)->PyFunction();
192 replaceName( aFunc, aOriginalName, aFName );
194 else { // empty function name and body
195 aFName = QString( "" );
196 aFunc = new SUPERV::ListOfStrings();
199 // create the Engine's node
200 SUPERV::INode_var aINode = dataflow->INode( aFName, aFunc );
202 if (CORBA::is_nil(aINode)) {
203 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
206 copyPorts( aNode, aINode );
208 SUPERV::INode_var aDummyEndNode;
209 Supervision.getBrowser()->addNode(SUPERV::CNode::_narrow(aINode), aDummyEndNode, myXCopyNode, myYCopyNode);
213 case SUPERV::LoopNode :
215 QString aInitFName, aMoreFName, aNextFName; // new node's Py_functions names
216 SUPERV::ListOfStrings_var aInitFunc, aMoreFunc, aNextFunc; // new node's Py_functions bodies
218 // Automatic change of Py_function name ( + "_1", etc.)
219 // 1. collect all python functions names of allready exist InLine nodes
220 QStringList aFuncNames = getAllFunctions( dataflow );
221 // 2.1 "fix" Init function_name and Init function_strings
222 QString aOriginalName = SUPERV::LNode::_narrow(aNode)->PyInitName();
223 if (!aOriginalName.isEmpty()) {
224 aInitFName = getNewName( aFuncNames, aOriginalName );
225 aInitFunc = SUPERV::LNode::_narrow(aNode)->PyInit();
226 replaceName( aInitFunc, aOriginalName, aInitFName );
228 else { // empty function name and body
229 aInitFName = QString( "" );
230 aInitFunc = new SUPERV::ListOfStrings();
232 // 2.2 "fix" More function_name and More function_strings
233 aOriginalName = SUPERV::LNode::_narrow(aNode)->PyMoreName();
234 if (!aOriginalName.isEmpty()) {
235 aMoreFName = getNewName( aFuncNames, aOriginalName );
236 aMoreFunc = SUPERV::LNode::_narrow(aNode)->PyMore();
237 replaceName( aMoreFunc, aOriginalName, aMoreFName );
239 else { // empty function name and body
240 aMoreFName = QString( "" );
241 aMoreFunc = new SUPERV::ListOfStrings();
243 // 2.3 "fix" Next function_name and Next function_strings
244 aOriginalName = SUPERV::LNode::_narrow(aNode)->PyNextName();
245 if (!aOriginalName.isEmpty()) {
246 aNextFName = getNewName( aFuncNames, aOriginalName );
247 aNextFunc = SUPERV::LNode::_narrow(aNode)->PyNext();
248 replaceName( aNextFunc, aOriginalName, aNextFName );
250 else { // empty function name and body
251 aNextFName = QString( "" );
252 aNextFunc = new SUPERV::ListOfStrings();
255 // create the Engine's node
256 SUPERV::INode_var aEndNode;
257 SUPERV::LNode_var aStartNode = dataflow->LNode(aInitFName, aInitFunc, aMoreFName, aMoreFunc, aNextFName, aNextFunc, aEndNode);
258 if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
259 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
262 copyPorts( aNode, SUPERV::INode::_narrow( aStartNode ) );
264 Supervision.getBrowser()->addNode(SUPERV::CNode::_narrow(aStartNode), aEndNode, myXCopyNode, myYCopyNode);
268 case SUPERV::SwitchNode :
270 QString aFName; // new node's Py_function name
271 SUPERV::ListOfStrings_var aFunc; // new node's Py_function body
273 // Automatic change of Py_function name ( + "_1", etc.)
274 // 1. collect all python functions names of allready exist InLine nodes
275 QStringList aFuncNames = getAllFunctions( dataflow );
276 // 2. "fix" Main function_name and Main function_strings
277 QString aOriginalName = SUPERV::INode::_narrow(aNode)->PyFuncName();
278 if ( !aOriginalName.isEmpty() ) {
279 aFName = getNewName( aFuncNames, aOriginalName );
280 aFunc = SUPERV::INode::_narrow(aNode)->PyFunction();
281 replaceName( aFunc, aOriginalName, aFName );
283 else { // empty function name and body
284 aFName = QString( "" );
285 aFunc = new SUPERV::ListOfStrings();
288 // create the Engine's node
289 SUPERV::INode_var aEndNode;
290 SUPERV::SNode_var aStartNode = dataflow->SNode(aFName, aFunc, aEndNode);
291 if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
292 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
295 copyPorts( aNode, SUPERV::INode::_narrow( aStartNode ) );
296 SUPERV::INode_var aNodeEnd = SUPERV::SNode::_narrow(aNode)->Coupled();
297 copyPorts( SUPERV::CNode::_narrow( aNodeEnd ), aEndNode );
299 Supervision.getBrowser()->addNode(SUPERV::CNode::_narrow(aStartNode), aEndNode, myXCopyNode, myYCopyNode);
303 case SUPERV::GOTONode :
305 QString aFName; // new node's Py_function name
306 SUPERV::ListOfStrings_var aFunc; // new node's Py_function body
308 // Automatic change of Py_function name ( + "_1", etc.)
309 // 1. collect all python functions names of allready exist InLine nodes
310 QStringList aFuncNames = getAllFunctions( dataflow );
311 // 2. "fix" Main function_name and Main function_strings
312 QString aOriginalName = SUPERV::INode::_narrow(aNode)->PyFuncName();
313 if ( !aOriginalName.isEmpty() ) {
314 aFName = getNewName( aFuncNames, aOriginalName );
315 aFunc = SUPERV::INode::_narrow(aNode)->PyFunction();
316 replaceName( aFunc, aOriginalName, aFName );
318 else { // empty function name and body
319 aFName = QString( "" );
320 aFunc = new SUPERV::ListOfStrings();
323 // create the Engine's node
324 SUPERV::GNode_var aGNode = dataflow->GNode(aFName, aFunc, "");
325 if (CORBA::is_nil(aGNode)) {
326 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
329 copyPorts( aNode, SUPERV::INode::_narrow( aGNode ) );
331 SUPERV::INode_var aDummyEndNode;
332 Supervision.getBrowser()->addNode(SUPERV::CNode::_narrow(aGNode), aDummyEndNode, myXCopyNode, myYCopyNode);
336 case SUPERV::MacroNode :
338 /* to implement in the future */
340 //get SubGraph from MacroNode
341 SUPERV::Graph_var aMacro = SUPERV::Graph::_narrow(aNode);
342 SUPERV::Graph_var aGraph;
343 if (aMacro->IsStreamMacro())
344 aGraph = aMacro->StreamObjRef();
346 aGraph = aMacro->FlowObjRef();
347 SUPERV::Graph_var aMacroNode = dataflow->GraphMNode(aGraph);
349 if (CORBA::is_nil(aMacroNode)) {
350 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
354 SUPERV::INode_var aDummyEndNode;
355 Supervision.getBrowser()->addNode(SUPERV::CNode::_narrow(aMacroNode), aDummyEndNode, myXCopyNode, myYCopyNode);
365 * Called from CanvasNode on "Paste port" command of popup menu
367 void SUPERVGUI_Clipboard::pastePort( SUPERVGUI_CanvasNode* node )
369 SUPERV::Port_var aPort = getCopyPort();
370 SUPERVGUI_Main* aMain = Supervision.getMain();
371 if ( !CORBA::is_nil(aPort) && aMain ) {
373 SUPERV::INode_var aNode = node->getInlineNode();
374 if (!CORBA::is_nil(aNode)) {
375 QString aName = aPort->Name();
376 QString aType = aPort->Type();
377 SUPERV::Port_var aPastePort;
378 if (aPort->IsInput()) {
379 //check if port with such name is alredy exists
380 QStringList aNames = node->getPortsNamesIN(aNode, true);
381 if (aNames.contains(aName))
382 QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_PORT_EXIST") );
384 aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
385 aPastePort = aNode->InPort(aName.latin1(), aType.latin1());
389 //check if port with such name is already exists
390 QStringList aNames = node->getPortsNamesIN(aNode, false);
391 if (aNames.contains(aName))
392 QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_PORT_EXIST") );
394 aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
395 aPastePort = aNode->OutPort(aName.latin1(), aType.latin1());
398 if ( !CORBA::is_nil(aPastePort) )
399 node->createPort( aPastePort.in() );