Salome HOME
0be9d3f8b769812a26f447a8d6eed5c15f765e3d
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Clipboard.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SUPERVGUI_Clipboard.cxx
25 //  Author : Alexander SLADKOV
26 //  Module : SUPERV
27
28
29 #include "SUPERVGUI_Clipboard.h"
30 #include "SUPERVGUI_CanvasNode.h"
31 #include "SUPERVGUI.h"
32
33
34 SUPERVGUI_Clipboard* SUPERVGUI_Clipboard::myCB = 0;
35
36
37 /**
38  * Compute the next valid name for a Python function (add "_N" if a function with given name already exists)
39  */
40 QString getNewName( QStringList& allNames, const QString& oldName ) {
41   QString newName;
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 );
46   
47   return newName;
48 }
49
50 /**
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!
55  */
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();
69       }
70       index = aLine.find( origName, index+newName.length() ); // find NEXT occurance of origName in aLine
71     }
72   }
73 }
74
75 /**
76  * "Copies" all ports from fromNode to toNode: creates the sames ports in toNode in fact
77  */
78 void copyPorts( const SUPERV::CNode_var& fromNode, const SUPERV::INode_var& toNode ) {
79   if ( CORBA::is_nil( fromNode ) || CORBA::is_nil( toNode ) )
80     return;
81   SUPERV::ListOfPorts_var aPList = fromNode->Ports();
82   QString aName, aType;
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() );
88     else
89       toNode->OutPort( aName.latin1(), aType.latin1() );
90   }
91 }
92
93 /**
94  * Constructor
95  */
96 SUPERVGUI_Clipboard::SUPERVGUI_Clipboard( QObject* parent ) 
97 : QObject( parent ) {
98 }
99
100 /**
101  * Destructor
102  */
103 SUPERVGUI_Clipboard::~SUPERVGUI_Clipboard() {
104 }
105
106 /**
107  * Returns all inline functions defined in inline (switch, loop, goto) nodes of given dataflow
108  */
109 QStringList getAllFunctions( SUPERV::Graph_var dataflow ) {
110   QStringList aFuncNames;
111   if ( !CORBA::is_nil( dataflow ) ) {
112     SUPERV::ListOfNodes_var nodes = dataflow->Nodes();
113     //InLine nodes
114     for(int i = 0; i < nodes->INodes.length(); i++)
115       aFuncNames.append(nodes->INodes[i]->PyFuncName());
116     //Loop nodes
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());
121     }
122     //Switch nodes
123     for(int i = 0; i < nodes->SNodes.length(); i++)
124       aFuncNames.append(nodes->SNodes[i]->PyFuncName());
125     //GOTO nodes
126     for(int i = 0; i < nodes->GNodes.length(); i++)
127       aFuncNames.append(nodes->GNodes[i]->PyFuncName());
128   }
129   return aFuncNames;
130 }
131
132 /**
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).
135  */
136 void SUPERVGUI_Clipboard::pasteNode() {
137   Trace("SUPERVGUI_Main::pasteNode");
138   SUPERV::CNode_var aNode = getCopyNode();
139   
140   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
141   if ( !aSupMod ) {
142     MESSAGE("NULL Supervision module!");
143     return;
144   }
145   
146   SUPERVGUI_Main* aMain = aSupMod->getMain();
147   if ( !CORBA::is_nil( aNode ) && aMain ) {
148     
149     if ( !aMain->ReadyToModify() ) // null dataflow or executing, ..
150       return;
151
152     aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
153
154     SUPERV::Graph_var dataflow = aMain->getDataflow();
155
156     switch ( aNode->Kind() ) {
157
158     case SUPERV::FactoryNode : 
159       {
160         SUPERV::FNode_var aFNode = dataflow->FNode( SUPERV::FNode::_narrow(aNode)->GetComponentName(),
161                                                     SUPERV::FNode::_narrow(aNode)->GetInterfaceName(),
162                                                     *SUPERV::FNode::_narrow(aNode)->Service(),
163                                                     SUPERV::FNode::_narrow(aNode)->IsCimpl()); // mkr : PAL11273
164         if (CORBA::is_nil(aFNode)) {
165           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
166           return;
167         }
168
169         SUPERV::INode_var aDummyEndNode;
170         aSupMod->getBrowser()->addNode(SUPERV::CNode::_narrow(aFNode), aDummyEndNode, myXCopyNode, myYCopyNode);
171       }
172       break;
173
174     case SUPERV::ComputingNode : 
175       {
176         SUPERV::CNode_var aCNode = dataflow->CNode(*SUPERV::CNode::_narrow(aNode)->Service());
177         if (CORBA::is_nil(aCNode)) {
178           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
179           return;
180         }
181         
182         SUPERV::INode_var aDummyEndNode;
183         aSupMod->getBrowser()->addNode(aCNode, aDummyEndNode, myXCopyNode, myYCopyNode);
184       }
185       break;
186
187     case SUPERV::InLineNode : 
188       {
189         QString aFName;                  // new node's Py_function name
190         SUPERV::ListOfStrings_var aFunc; // new node's Py_function body
191
192         // Automatic change of Py_function name ( + "_1", etc.)
193         // 1. collect all python functions names of allready exist InLine nodes 
194         QStringList aFuncNames = getAllFunctions( dataflow );
195         // 2. "fix" Main function_name and Main function_strings
196         QString aOriginalName = SUPERV::INode::_narrow(aNode)->PyFuncName();
197         if ( !aOriginalName.isEmpty() ) {
198           aFName = getNewName( aFuncNames, aOriginalName );
199           aFunc = SUPERV::INode::_narrow(aNode)->PyFunction();
200           replaceName( aFunc, aOriginalName, aFName );
201         } 
202         else { // empty function name and body
203           aFName = QString( "" ); 
204           aFunc = new SUPERV::ListOfStrings(); 
205         }
206
207         // create the Engine's node
208         SUPERV::INode_var aINode = dataflow->INode( aFName, aFunc );
209
210         if (CORBA::is_nil(aINode)) {
211           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
212           return;
213         }
214         copyPorts( aNode, aINode );
215
216         SUPERV::INode_var aDummyEndNode;
217         aSupMod->getBrowser()->addNode(SUPERV::CNode::_narrow(aINode), aDummyEndNode, myXCopyNode, myYCopyNode);
218       }
219       break;
220
221     case SUPERV::LoopNode :
222       {
223         QString aInitFName, aMoreFName, aNextFName;                // new node's Py_functions names
224         SUPERV::ListOfStrings_var aInitFunc, aMoreFunc, aNextFunc; // new node's Py_functions bodies
225
226         // Automatic change of Py_function name ( + "_1", etc.)
227         // 1. collect all python functions names of allready exist InLine nodes 
228         QStringList aFuncNames = getAllFunctions( dataflow );
229         // 2.1 "fix" Init function_name and Init function_strings
230         QString aOriginalName = SUPERV::LNode::_narrow(aNode)->PyInitName();
231         if (!aOriginalName.isEmpty()) {
232           aInitFName = getNewName( aFuncNames, aOriginalName );
233           aInitFunc = SUPERV::LNode::_narrow(aNode)->PyInit();
234           replaceName( aInitFunc, aOriginalName, aInitFName );
235         }
236         else { // empty function name and body
237           aInitFName = QString( "" ); 
238           aInitFunc = new SUPERV::ListOfStrings(); 
239         }
240         // 2.2 "fix" More function_name and More function_strings
241         aOriginalName = SUPERV::LNode::_narrow(aNode)->PyMoreName();
242         if (!aOriginalName.isEmpty()) {
243           aMoreFName = getNewName( aFuncNames, aOriginalName );
244           aMoreFunc = SUPERV::LNode::_narrow(aNode)->PyMore();
245           replaceName( aMoreFunc, aOriginalName, aMoreFName );
246         }
247         else { // empty function name and body
248           aMoreFName = QString( "" ); 
249           aMoreFunc = new SUPERV::ListOfStrings(); 
250         }
251         // 2.3 "fix" Next function_name and Next function_strings
252         aOriginalName = SUPERV::LNode::_narrow(aNode)->PyNextName();
253         if (!aOriginalName.isEmpty()) {
254           aNextFName = getNewName( aFuncNames, aOriginalName );
255           aNextFunc = SUPERV::LNode::_narrow(aNode)->PyNext();
256           replaceName( aNextFunc, aOriginalName, aNextFName );
257         }
258         else { // empty function name and body
259           aNextFName = QString( "" ); 
260           aNextFunc = new SUPERV::ListOfStrings(); 
261         }
262
263         // create the Engine's node
264         SUPERV::INode_var aEndNode;
265         SUPERV::LNode_var aStartNode = dataflow->LNode(aInitFName, aInitFunc, aMoreFName, aMoreFunc, aNextFName, aNextFunc, aEndNode);
266         if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
267           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
268           return;
269         }
270         copyPorts( aNode, SUPERV::INode::_narrow( aStartNode ) );
271
272         aSupMod->getBrowser()->addNode(SUPERV::CNode::_narrow(aStartNode), aEndNode, myXCopyNode, myYCopyNode);
273       }
274       break;
275
276     case SUPERV::SwitchNode :
277       {
278         QString aFName;                  // new node's Py_function name
279         SUPERV::ListOfStrings_var aFunc; // new node's Py_function body
280
281         // Automatic change of Py_function name ( + "_1", etc.)
282         // 1. collect all python functions names of allready exist InLine nodes 
283         QStringList aFuncNames = getAllFunctions( dataflow );
284         // 2. "fix" Main function_name and Main function_strings
285         QString aOriginalName = SUPERV::INode::_narrow(aNode)->PyFuncName();
286         if ( !aOriginalName.isEmpty() ) {
287           aFName = getNewName( aFuncNames, aOriginalName );
288           aFunc = SUPERV::INode::_narrow(aNode)->PyFunction();
289           replaceName( aFunc, aOriginalName, aFName );
290         } 
291         else { // empty function name and body
292           aFName = QString( "" ); 
293           aFunc = new SUPERV::ListOfStrings(); 
294         }
295
296         // create the Engine's node
297         SUPERV::INode_var aEndNode;
298         SUPERV::SNode_var aStartNode = dataflow->SNode(aFName, aFunc, aEndNode);
299         if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
300           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
301           return;
302         }
303         copyPorts( aNode, SUPERV::INode::_narrow( aStartNode ) );
304         SUPERV::INode_var aNodeEnd = SUPERV::SNode::_narrow(aNode)->Coupled();
305         copyPorts( SUPERV::CNode::_narrow( aNodeEnd ), aEndNode );
306
307         aSupMod->getBrowser()->addNode(SUPERV::CNode::_narrow(aStartNode), aEndNode, myXCopyNode, myYCopyNode);
308       }
309       break;
310
311     case SUPERV::GOTONode :
312       {
313         QString aFName;                  // new node's Py_function name
314         SUPERV::ListOfStrings_var aFunc; // new node's Py_function body
315
316         // Automatic change of Py_function name ( + "_1", etc.)
317         // 1. collect all python functions names of allready exist InLine nodes 
318         QStringList aFuncNames = getAllFunctions( dataflow );
319         // 2. "fix" Main function_name and Main function_strings
320         QString aOriginalName = SUPERV::INode::_narrow(aNode)->PyFuncName();
321         if ( !aOriginalName.isEmpty() ) {
322           aFName = getNewName( aFuncNames, aOriginalName );
323           aFunc = SUPERV::INode::_narrow(aNode)->PyFunction();
324           replaceName( aFunc, aOriginalName, aFName );
325         } 
326         else { // empty function name and body
327           aFName = QString( "" ); 
328           aFunc = new SUPERV::ListOfStrings(); 
329         }
330
331         // create the Engine's node
332         SUPERV::GNode_var aGNode = dataflow->GNode(aFName, aFunc, "");
333         if (CORBA::is_nil(aGNode)) {
334           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
335           return;
336         }
337         copyPorts( aNode, SUPERV::INode::_narrow( aGNode ) );
338
339         SUPERV::INode_var aDummyEndNode;
340         aSupMod->getBrowser()->addNode(SUPERV::CNode::_narrow(aGNode), aDummyEndNode, myXCopyNode, myYCopyNode);
341       }
342       break;
343
344     case SUPERV::MacroNode :
345       {
346         /* to implement in the future */
347         /*
348         //get SubGraph from MacroNode
349         SUPERV::Graph_var aMacro = SUPERV::Graph::_narrow(aNode);
350         SUPERV::Graph_var aGraph;
351         if (aMacro->IsStreamMacro())
352           aGraph = aMacro->StreamObjRef();
353         else
354           aGraph = aMacro->FlowObjRef();
355         SUPERV::Graph_var aMacroNode = dataflow->GraphMNode(aGraph);
356         
357         if (CORBA::is_nil(aMacroNode)) {
358           QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));
359           return;
360         }
361
362         SUPERV::INode_var aDummyEndNode;
363         aSupMod->getBrowser()->addNode(SUPERV::CNode::_narrow(aMacroNode), aDummyEndNode, myXCopyNode, myYCopyNode);
364         */
365       }
366       break;
367     }
368   }
369 }
370
371
372 /** 
373  * Called from CanvasNode on "Paste port" command of popup menu
374  */
375 void SUPERVGUI_Clipboard::pastePort( SUPERVGUI_CanvasNode* node )
376 {
377   SUPERV::Port_var aPort = getCopyPort();
378
379   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
380   if ( !aSupMod ) {
381     MESSAGE("NULL Supervision module!");
382     return;
383   }
384
385   SUPERVGUI_Main* aMain = aSupMod->getMain();
386   if ( !CORBA::is_nil(aPort) && aMain ) {
387
388     SUPERV::INode_var aNode = node->getInlineNode();
389     if (!CORBA::is_nil(aNode)) {
390       QString aName = aPort->Name();
391       QString aType = aPort->Type();
392       SUPERV::Port_var aPastePort;
393       if (aPort->IsInput()) {
394         //check if port with such name is alredy exists
395         QStringList aNames = node->getPortsNamesIN(aNode, true);
396         if (aNames.contains(aName))
397           QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_PORT_EXIST") );
398         else {
399           aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
400           aPastePort = aNode->InPort(aName.latin1(), aType.latin1());
401         }
402       }
403       else {
404         //check if port with such name is already exists
405         QStringList aNames = node->getPortsNamesIN(aNode, false);
406         if (aNames.contains(aName))
407           QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_PORT_EXIST") );
408         else {
409           aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
410           aPastePort = aNode->OutPort(aName.latin1(), aType.latin1());
411         }
412       }
413       if ( !CORBA::is_nil(aPastePort) )
414         node->createPort( aPastePort.in() );
415     }
416   }
417 }