1 // SUPERV SUPERVGUI : GUI for Supervisor component
3 // Copyright (C) 2003 OPEN CASCADE
5 // File : SUPERVGUI_CanvasNode.cxx
6 // Author : Natalia KOPNOVA
10 #include "SUPERVGUI_CanvasNode.h"
11 #include "SUPERVGUI_CanvasNodePrs.h"
12 #include "SUPERVGUI_CanvasPort.h"
13 #include "SUPERVGUI_Clipboard.h"
14 #include "SUPERVGUI_Main.h"
15 #include "SUPERVGUI.h"
16 #include "SUPERVGUI_BrowseNodeDlg.h"
17 #include "SUPERVGUI_ManagePortsDlg.h"
18 #include "SUPERVGUI_Information.h"
19 #include "SUPERVGUI_Library.h"
21 #include "SalomeApp_NameDlg.h"
22 #include "SUIT_MessageBox.h"
23 #include "LogWindow.h"
24 #include "SUIT_Session.h"
27 SUPERVGUI_CanvasNode::SUPERVGUI_CanvasNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode, bool theIsCell):
39 Trace("SUPERVGUI_CanvasNode::SUPERVGUI_CanvasNode");
41 setName(myNode->Name());
43 QString aComment(myNode->Comment());
44 QString aNewComment = aComment;
45 if (getNodeType() == SUPERV::FactoryNode) {
46 aNewComment = QString(myNode->Service()->ServiceName) +
47 QString(tr("COMMENT_FROM")) +
48 QString(getFactoryNode()->GetComponentName());
50 else if (myNode->IsMacro()) {
51 aNewComment = tr("COMMENT_MNODE");
54 aNewComment = tr("COMMENT_CNODE");
56 if (aComment.isEmpty()) {
57 myNode->SetComment(aNewComment.latin1());
60 myLabelText = aNewComment;
62 // mkr : if the SUPERVGUI_CavasCellNode created (for CANVASTABLE view),
63 // we don't create the ports
67 SUPERV_Ports aPortList = myNode->Ports();
68 for (int i = 0; i < aPortList->length(); i++)
69 createPort(aPortList[i].in());
71 SUPERV_StreamPorts aStreamPortList = myNode->StreamPorts();
72 for (int i = 0; i < aStreamPortList->length(); i++) {
73 createStreamPort(aStreamPortList[i].in());
80 SUPERVGUI_CanvasNode::~SUPERVGUI_CanvasNode()
90 void SUPERVGUI_CanvasNode::setDestroyed()
96 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasNode::getPrs()
104 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasNode::createPrs() const
106 return new SUPERVGUI_CanvasNodePrs(myMain->getCanvas(), (SUPERVGUI_CanvasNode*)this);
109 void SUPERVGUI_CanvasNode::createPort(SUPERV::Port_ptr thePort)
111 SUPERVGUI_CanvasPort* aPort = 0;
112 if (thePort->IsInput())
113 aPort = new SUPERVGUI_CanvasPortIn((SUPERVGUI_CanvasNode*)this, myMain, thePort);
115 aPort = new SUPERVGUI_CanvasPortOut((SUPERVGUI_CanvasNode*)this, myMain, thePort);
118 connect(aPort, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
119 if (!isIgnore) getPrs()->updatePorts();
123 void SUPERVGUI_CanvasNode::createStreamPort(SUPERV::StreamPort_ptr thePort)
125 SUPERVGUI_CanvasPort* aPort = 0;
126 if (thePort->IsInput())
127 aPort = new SUPERVGUI_CanvasStreamPortIn((SUPERVGUI_CanvasNode*)this, myMain, thePort);
129 aPort = new SUPERVGUI_CanvasStreamPortOut((SUPERVGUI_CanvasNode*)this, myMain, thePort);
132 connect(aPort, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
133 if (!isIgnore) getPrs()->updatePorts();
137 void SUPERVGUI_CanvasNode::onDestroyed(QObject* theObject)
140 removeChild(theObject); // signal is sent before removing the object
141 getPrs()->updatePorts();
145 QPopupMenu* SUPERVGUI_CanvasNode::getPopupMenu(QWidget* theParent)
147 QPopupMenu* popup = new QPopupMenu(theParent);
149 if (myMain->getDataflow()->IsExecuting()) { // Execution time
150 popup->insertItem((myNode->IsSuspended()?tr("MSG_RESUME"):tr("MSG_SUSPEND")), this, SLOT(suspendResume()));
151 popup->insertItem(tr("MSG_KILL"), this, SLOT(kill()));
153 else { // Edition time
154 const int type = getNodeType();
156 // for all nodes except EndLoop and EndSwitch : Rename, Delete, Copy
157 if ( myMain->isEditable() && type != SUPERV::EndLoopNode && type != SUPERV::EndSwitchNode &&
158 // asv 26.01.05 : don't allow nodes "edition" commands in Table view
159 myMain->getViewType() != CANVASTABLE ) {
160 popup->insertItem(tr("MSG_RENAME"), this, SLOT(rename()));
161 popup->insertItem(tr("MSG_DELETE"), this, SLOT(remove()));
164 // tmp: Copy temporary does not work for Macro nodes...
165 if ( type != SUPERV::MacroNode )
166 popup->insertItem(tr("ITM_COPY_NODE"), this, SLOT(copy())); // Copy Node functionality
167 popup->insertSeparator();
169 // for all nodes : Browse
170 popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
172 // for all nodes except EndLoop and EndSwitch : Change info
173 if (type != SUPERV::EndLoopNode && type != SUPERV::EndSwitchNode)
174 popup->insertItem(tr("MSG_CHANGE_INFO"), this, SLOT(changeInformation()));
176 // for all InLine nodes
177 if ( type != SUPERV::FactoryNode && type != SUPERV::ComputingNode && type != SUPERV::MacroNode ) {
178 if ( myMain->isEditable() ) {
179 popup->insertSeparator();
180 popup->insertItem( tr( "MNU_EDIT_FUNC" ), this, SLOT(editFunction()));
183 // for all InLine, regardless isEditable() : Export to Library
184 popup->insertItem( tr( "MNU_EXPORT" ), this, SLOT( exportToLib()));
186 // for all InLine except EndLoop : Add Ports menu, Paste, Manage Ports
187 if ( myMain->isEditable() && type != SUPERV::EndLoopNode ) {
188 // mkr : IPAL9815 : commented the following code
189 /*QPopupMenu* addPortMenu = new QPopupMenu(theParent);
190 addPortMenu->insertItem( tr( "MNU_INPUT" ), this, SLOT(addInputPort()));
191 if (getNodeType() != SUPERV::LoopNode)
192 addPortMenu->insertItem( tr( "MNU_OUTPUT" ), this, SLOT(addOutputPort()));
194 popup->insertItem( tr( "MNU_ADD_PORT" ), addPortMenu);*/
195 popup->insertItem( tr( "MNU_MANAGE_PORTS" ), this, SLOT(managePorts()));
197 // Paste Port functionality
198 int aPasteItem = popup->insertItem(tr("ITM_PASTE_PORT"), this, SLOT(pastePort()));
199 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
200 if ( !aCB->isCopyPort() || (type == SUPERV::LoopNode && !aCB->getCopyPort()->IsInput()) )
201 popup->setItemEnabled( aPasteItem, false );
208 void SUPERVGUI_CanvasNode::show()
213 void SUPERVGUI_CanvasNode::showAll()
218 void SUPERVGUI_CanvasNode::hideAll()
223 void SUPERVGUI_CanvasNode::switchLabel()
225 getPrs()->setLabelVisible(!getPrs()->isLabelVisible());
228 void SUPERVGUI_CanvasNode::switchPorts()
230 getPrs()->setPortVisible(!getPrs()->isPortVisible());
233 void SUPERVGUI_CanvasNode::move(int x, int y)
235 // myNode->Coords(x, y); // <<<- done inside Prs to reflect item movement by mouse press
236 if (x > GRAPH_MAX) x = (int) getPrs()->x();
237 if (y > GRAPH_MAX) y = (int) getPrs()->y();
238 getPrs()->move(x, y);
241 void SUPERVGUI_CanvasNode::merge()
243 // synchronize port list
246 SUPERVGUI_CanvasPort* aPort;
247 QObjectList* aPortList = queryList("SUPERVGUI_CanvasPort");
248 SUPERV_Ports aPorts = myNode->Ports();
249 for (int i = 0; i < aPorts->length(); i++) {
250 aPort = (SUPERVGUI_CanvasPort*)
251 child(myMain->getCanvas()->getPortName(aPorts[i].in()), "SUPERVGUI_CanvasPort");
253 aPortList->removeRef(aPort);
257 createPort(aPorts[i].in());
262 SUPERV_StreamPorts aStreamPorts = myNode->StreamPorts();
263 for (int i = 0; i < aStreamPorts->length(); i++) {
264 aPort = (SUPERVGUI_CanvasPort*)
265 child(myMain->getCanvas()->getPortName(aStreamPorts[i].in()), "SUPERVGUI_CanvasPort");
267 aPortList->removeRef(aPort);
271 createPort(aStreamPorts[i].in());
276 QObjectListIt it(*aPortList);
277 while ((aPort=(SUPERVGUI_CanvasPort*)it.current()) != 0) {
279 aPortList->removeRef(aPort);
285 if (update) getPrs()->updatePorts();
287 sync(); // update node state also
290 void SUPERVGUI_CanvasNode::sync() {
291 const bool isExecuting = myMain->getDataflow()->IsExecuting();
292 if ( myNode->IsMacro() && isExecuting ) {
293 // get SubGraph from MacroNode
294 SUPERV_Graph aMacro = SUPERV::Graph::_narrow(myNode);
295 if (!SUPERV_isNull(aMacro)) {
297 if (aMacro->IsStreamMacro())
298 aGraph = aMacro->StreamObjRef();
300 aGraph = aMacro->FlowObjRef();
301 if (!SUPERV_isNull(aGraph)) {
302 if (aGraph->State() != SUPERV::UndefinedState && aGraph->State() != SUPERV::NoState)
303 getPrs()->setState(aGraph->State());
305 getPrs()->setState(myNode->State());
310 SUPERV::GraphState aState = myNode->State();
312 // asv : 18.11.04 : fix for 6170 : after execution is finished, nodes' status must be reset.
313 // asv : 13.12.04 : commented out setting EditingState. See 2.21.
314 //if ( !isExecuting && myMain->IsGUIEventLoopFinished() && (aState == SUPERV::DoneState ||
315 // aState == SUPERV::KillState || aState == SUPERV::StopState ) )
316 // aState = SUPERV::EditingState;
317 getPrs()->setState(aState);
320 // update child ports
321 const QObjectList* list = children();
323 QObjectListIt it(*list);
324 while (QObject* obj = it.current()) {
326 if (obj->inherits("SUPERVGUI_CanvasPort")) {
327 ((SUPERVGUI_CanvasPort*)obj)->sync();
333 void SUPERVGUI_CanvasNode::syncOnEvent(SUPERV::GraphState theStateFromEvent)
335 getPrs()->setState(theStateFromEvent);
338 bool SUPERVGUI_CanvasNode::setNodeName(QString aName) {
339 bool result = myNode->SetName(aName.latin1());
341 setName(myNode->Name());
342 getPrs()->updateInfo();
343 // TODO: update name of all the links to this node
346 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr( "ERROR" ), tr( "MSG_CANT_RENAMENODE" ) );
351 void SUPERVGUI_CanvasNode::rename() {
352 QString aName = SalomeApp_NameDlg::getName( SUIT_Session::session()->activeApplication()->desktop(), myNode->Name() );
353 //mkr : modifications for fixing bug IPAL9972
354 if (!aName.isEmpty() && aName.compare( myNode->Name() ) != 0) {
359 void SUPERVGUI_CanvasNode::remove() {
360 Trace("SUPERVGUI_CanvasNode::remove");
361 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
363 //set myCopyPort from Main object to empty if engine of this port is deleted
364 //check if any port of this deleted node has been copied
365 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
366 if ( aCB->isCopyPort() )
367 if ( QString(myNode->Name()) == QString(aCB->getCopyPort()->Node()->Name()) )
368 aCB->setCopyPort( 0 );
370 //set myCopyNode from Main object to empty if engine of this node is deleted
371 //check if myCopyNode and this node engine is equal
372 if ( aCB->isCopyNode() )
373 if ( QString(myNode->Name()) == QString(aCB->getCopyNode()->Name()) )
374 aCB->setCopyNode( 0 );
376 // mkr: since the deletion of the node allow only in CANVAS view,
377 // it is necessary to remove the CanvasArray's children, which
378 // have the same CNode engine as deleting node
379 myMain->removeArrayChild(myNode);
381 SUPERVGUI_Canvas* aCanvas = myMain->getCanvas();
388 void SUPERVGUI_CanvasNode::copy() {
389 Trace("SUPERVGUI_CanvasNode::copy()");
390 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
391 const int aKind = getNodeType();
392 if ( aKind == SUPERV::EndLoopNode || aKind == SUPERV::EndSwitchNode )
393 aCB->setCopyNode( SUPERV::CNode::_duplicate(SUPERV::GNode::_narrow(getEngine())->Coupled()) );
395 aCB->setCopyNode( SUPERV::CNode::_duplicate( getEngine() ) );
398 void SUPERVGUI_CanvasNode::suspendResume() {
399 Trace("SUPERVGUI_CanvasNode::suspendResume");
400 int n = queryList("SUPERVGUI_CanvasNode")->count();
401 if (myNode->IsSuspended()) {
402 if (!((n==1)? myMain->getDataflow()->Resume() : myNode->Resume())) {
403 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_RESUMENODE") );
407 myMain->getMyThread()->startThread(tr("MSG_NODE_RESUMED1")+myNode->Name()+tr("MSG_NODE_RESUMED2"));
410 if (!((n==1)? myMain->getDataflow()->Suspend() : myNode->Suspend())) {
411 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_SUSPENDNODE") );
413 syncOnEvent(SUPERV::SuspendReadyState);
414 myMain->getMessage()->putMessage(tr("MSG_NODE_SUSPENDED1")+myNode->Name()+tr("MSG_NODE_SUSPENDED2"));
419 void SUPERVGUI_CanvasNode::kill() {
420 Trace("SUPERVGUI_CanvasNode::kill");
421 int n = queryList("SUPERVGUI_CanvasNode")->count();
422 if (!((n==1)? myMain->getDataflow()->Kill() : myNode->Kill())) {
423 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_KILLNODE") );
425 syncOnEvent(SUPERV_Kill);
426 myMain->getMessage()->putMessage(tr("MSG_NODE_KILLED1")+myNode->Name()+tr("MSG_NODE_KILLED2"));
429 /* asv : 15.12.04 : commented out stopRestart() in Main and CanvasNode because it's not called from anywhere,
430 the comment from kloss (in Main.cxx) may be explaining it, but it's in French and I do not understand it..
431 void SUPERVGUI_CanvasNode::stopRestart() {
432 Trace("SUPERVGUI_CanvasNode::stopRestart");
434 int n = queryList("SUPERVGUI_CanvasNode")->count();
435 if ((myNode->State() == SUPERV_Stop) || (myNode->State() == SUPERV_Kill)) {
436 if (!((n==1)? myMain->getDataflow()->Run() : myNode->ReStart())) {
437 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_RESTARTNODE") );
440 if (!((n==1)? myMain->getDataflow()->Stop() : myNode->Stop())) {
441 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_STOPNODE") );
446 void SUPERVGUI_CanvasNode::changeInformation() {
447 SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(myNode, !myMain->isEditable());
450 QString aName = myNode->Name();
451 if (!aName.isEmpty() && myMain->isEditable()) {
452 //mkr : modifications for fixing bug IPAL9972
453 //setNodeName(aName);
455 setName(myNode->Name());
456 getPrs()->updateInfo();
457 // TODO: update name of all the links to this node
464 /* asv : 13.12.04 : The functions below are not called from anywhere, so commenting them out...
465 void SUPERVGUI_CanvasNode::configure()
467 Trace("SUPERVGUI_CanvasNode::configure");
468 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_NOT_IMPLEMENTED") ); // kloss : a faire : lancer l'ihm DATA
471 void SUPERVGUI_CanvasNode::showPython()
473 Trace("SUPERVGUI_CanvasNode::showPython");
474 SUPERVGUI_Python cp(myMain->getStudy()->get_PyInterp(), !myMain->isEditable());
479 bool SUPERVGUI_CanvasNode::isWarning()
481 Trace("SUPERVGUI_CanvasNode::isWarning");
485 bool SUPERVGUI_CanvasNode::isStep()
487 Trace("SUPERVGUI_CanvasNode::isStep");
491 bool SUPERVGUI_CanvasNode::isTrace()
493 Trace("SUPERVGUI_CanvasNode::isTrace");
497 bool SUPERVGUI_CanvasNode::isVerbose()
499 Trace("SUPERVGUI_CanvasNode::isVerbose");
503 void SUPERVGUI_CanvasNode::setWarning(bool b)
505 Trace("SUPERVGUI_CanvasNode::setWarning");
509 void SUPERVGUI_CanvasNode::setStep(bool b)
511 Trace("SUPERVGUI_CanvasNode::setStep");
515 void SUPERVGUI_CanvasNode::setTrace(bool b)
517 Trace("SUPERVGUI_CanvasNode::setTrace");
521 void SUPERVGUI_CanvasNode::setVerbose(bool b)
523 Trace("SUPERVGUI_CanvasNode::setVerbose");
528 void SUPERVGUI_CanvasNode::browse()
530 // asv 28.01.05 : set "Editing" flag only on "OK" pressed in BrowseDlg
531 //myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
533 myBrowseDlg = new SUPERVGUI_BrowseNodeDlg(this);
534 myBrowseDlg->installEventFilter(this);
536 if (!myBrowseDlg->isVisible())
539 myBrowseDlg->raise();
540 myBrowseDlg->setActiveWindow();
541 myBrowseDlg->setFocus();
545 bool SUPERVGUI_CanvasNode::eventFilter( QObject* o, QEvent* e )
547 if (o == myBrowseDlg && e->type() == QEvent::Close)
549 return QObject::eventFilter(o, e);
552 QStringList SUPERVGUI_CanvasNode::getPortsNamesIN(SUPERV_INode theNode, bool theInputPorts)
555 if (!SUPERV_isNull(theNode)) {
556 SUPERV_Ports aPorts = theNode->Ports();
557 for (int i=0; i<aPorts->length(); i++) {
559 if (aPorts[i]->IsInput()) aPNList.append(QString(aPorts[i]->Name()));
562 if (!aPorts[i]->IsInput()) aPNList.append(QString(aPorts[i]->Name()));
568 SUPERV_Port SUPERVGUI_CanvasNode::createInPort()
570 SUPERV_INode aNode = getInlineNode();
571 if (SUPERV_isNull(aNode)) {
572 MESSAGE("SUPERVGUI_CanvasNode::createInPort: Node is wrong type");
575 SUPERVGUI_PortParamsDlg* aDlg = new SUPERVGUI_PortParamsDlg(getPortsNamesIN(aNode,true));
577 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
578 SUPERV_Port aPort = aNode->InPort(aDlg->getName().latin1(),
579 aDlg->getType().latin1());
587 SUPERV_Port SUPERVGUI_CanvasNode::createOutPort()
589 SUPERV_INode aNode = getInlineNode();
590 if (SUPERV_isNull(aNode)) {
591 MESSAGE("SUPERVGUI_CanvasNode::createOutPort: Node is wrong type");
595 SUPERVGUI_PortParamsDlg* aDlg = new SUPERVGUI_PortParamsDlg(getPortsNamesIN(aNode,false));
597 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
598 SUPERV_Port aPort = aNode->OutPort(aDlg->getName().latin1(),
599 aDlg->getType().latin1());
607 // mkr : IPAL9815 : commented the following code
608 /*void SUPERVGUI_CanvasNode::addInputPort() {
609 SUPERV_Port aPort = createInPort();
610 if (aPort == NULL || CORBA::is_nil( aPort ) ) return;
612 createPort(aPort.in());
616 void SUPERVGUI_CanvasNode::addOutputPort() {
617 SUPERV_Port aPort = createOutPort();
618 if (aPort == NULL || CORBA::is_nil( aPort ) ) return;
620 createPort(aPort.in());
624 void SUPERVGUI_CanvasNode::editFunction() {
625 if (getNodeType() == SUPERV::LoopNode) {
626 SUPERVGUI_EditPythonDlg* aDlg = new SUPERVGUI_EditPythonDlg(true);
627 SUPERV_LNode aLNode = getLoopNode();
628 aDlg->setInitFunction(aLNode->PyInit());
629 aDlg->setMoreFunction(aLNode->PyMore());
630 aDlg->setNextFunction(aLNode->PyNext());
632 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
633 aLNode->SetPyInit(aDlg->getInitFuncName().latin1(), (aDlg->getInitFunction()).in());
634 aLNode->SetPyMore(aDlg->getMoreFuncName().latin1(), (aDlg->getMoreFunction()).in());
635 aLNode->SetPyNext(aDlg->getNextFuncName().latin1(), (aDlg->getNextFunction()).in());
640 SUPERVGUI_EditPythonDlg* aDlg = new SUPERVGUI_EditPythonDlg();
641 SUPERV_INode aINode = getInlineNode();
642 aDlg->setFunction(aINode->PyFunction());
644 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
645 aINode->SetPyFunction(aDlg->getFuncName().latin1(), (aDlg->getFunction()).in());
652 * Called on "Paste port" command of popup menu
654 void SUPERVGUI_CanvasNode::pastePort() {
655 SUPERVGUI_Clipboard::getClipboard()->pastePort( this );
659 * Called on "Edit ports" popup menu command. See SUPERVGUI_ManagePortsDlg.h
660 * for detailed description of the functionality
662 void SUPERVGUI_CanvasNode::managePorts() {
663 SUPERVGUI_ManagePortsDlg* aDlg = new SUPERVGUI_ManagePortsDlg( this );
669 * Called on "Export to Library" popup menu command. See SUPERVGUI_Library.h
670 * for details on InLine nodes library functionality
672 void SUPERVGUI_CanvasNode::exportToLib() {
673 SUPERV::INode_var anINode = SUPERV::INode::_narrow( getEngine() );
674 if ( !CORBA::is_nil( anINode ) )
675 SUPERVGUI_Library::getLibrary()->Export( anINode );
677 SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(), tr( "ERROR" ), tr( "MSG_BAD_INODE" ), tr( "OK" ) );