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 "LightApp_NameDlg.h"
22 #include "SUIT_MessageBox.h"
23 #include "LogWindow.h"
24 #include "SUIT_Session.h"
27 SUPERVGUI_CanvasNode::SUPERVGUI_CanvasNode( SUIT_ResourceMgr* mgr, QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode, bool theIsCell):
40 Trace("SUPERVGUI_CanvasNode::SUPERVGUI_CanvasNode");
42 setName(myNode->Name());
44 QString aComment(myNode->Comment());
45 QString aNewComment = aComment;
46 if (getNodeType() == SUPERV::FactoryNode) {
47 aNewComment = QString(myNode->Service()->ServiceName) +
48 QString(tr("COMMENT_FROM")) +
49 QString(getFactoryNode()->GetComponentName());
51 else if (myNode->IsMacro()) {
52 aNewComment = tr("COMMENT_MNODE");
55 aNewComment = tr("COMMENT_CNODE");
57 if (aComment.isEmpty()) {
58 myNode->SetComment(aNewComment.latin1());
61 myLabelText = aNewComment;
63 // mkr : if the SUPERVGUI_CavasCellNode created (for CANVASTABLE view),
64 // we don't create the ports
68 SUPERV_Ports aPortList = myNode->Ports();
69 for (int i = 0; i < aPortList->length(); i++)
70 createPort(aPortList[i].in());
72 SUPERV_StreamPorts aStreamPortList = myNode->StreamPorts();
73 for (int i = 0; i < aStreamPortList->length(); i++) {
74 createStreamPort(aStreamPortList[i].in());
81 SUPERVGUI_CanvasNode::~SUPERVGUI_CanvasNode()
91 void SUPERVGUI_CanvasNode::setDestroyed()
97 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasNode::getPrs()
105 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasNode::createPrs() const
107 return new SUPERVGUI_CanvasNodePrs( myMgr, myMain->getCanvas(), (SUPERVGUI_CanvasNode*)this);
110 void SUPERVGUI_CanvasNode::createPort(SUPERV::Port_ptr thePort)
112 SUPERVGUI_CanvasPort* aPort = 0;
113 if (thePort->IsInput())
114 aPort = new SUPERVGUI_CanvasPortIn((SUPERVGUI_CanvasNode*)this, myMain, thePort);
116 aPort = new SUPERVGUI_CanvasPortOut((SUPERVGUI_CanvasNode*)this, myMain, thePort);
119 connect(aPort, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
120 if (!isIgnore) getPrs()->updatePorts();
124 void SUPERVGUI_CanvasNode::createStreamPort(SUPERV::StreamPort_ptr thePort)
126 SUPERVGUI_CanvasPort* aPort = 0;
127 if (thePort->IsInput())
128 aPort = new SUPERVGUI_CanvasStreamPortIn((SUPERVGUI_CanvasNode*)this, myMain, thePort);
130 aPort = new SUPERVGUI_CanvasStreamPortOut((SUPERVGUI_CanvasNode*)this, myMain, thePort);
133 connect(aPort, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
134 if (!isIgnore) getPrs()->updatePorts();
138 void SUPERVGUI_CanvasNode::onDestroyed(QObject* theObject)
141 removeChild(theObject); // signal is sent before removing the object
142 getPrs()->updatePorts();
146 QPopupMenu* SUPERVGUI_CanvasNode::getPopupMenu(QWidget* theParent)
148 QPopupMenu* popup = new QPopupMenu(theParent);
150 if (myMain->getDataflow()->IsExecuting()) { // Execution time
151 popup->insertItem((myNode->IsSuspended()?tr("MSG_RESUME"):tr("MSG_SUSPEND")), this, SLOT(suspendResume()));
152 popup->insertItem(tr("MSG_KILL"), this, SLOT(kill()));
154 else { // Edition time
155 const int type = getNodeType();
157 // for all nodes except EndLoop and EndSwitch : Rename, Delete, Copy
158 if ( myMain->isEditable() && type != SUPERV::EndLoopNode && type != SUPERV::EndSwitchNode &&
159 // asv 26.01.05 : don't allow nodes "edition" commands in Table view
160 myMain->getViewType() != CANVASTABLE ) {
161 popup->insertItem(tr("MSG_RENAME"), this, SLOT(rename()));
162 popup->insertItem(tr("MSG_DELETE"), this, SLOT(remove()));
165 // tmp: Copy temporary does not work for Macro nodes...
166 if ( type != SUPERV::MacroNode )
167 popup->insertItem(tr("ITM_COPY_NODE"), this, SLOT(copy())); // Copy Node functionality
168 popup->insertSeparator();
170 // for all nodes : Browse
171 popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
173 // for all nodes except EndLoop and EndSwitch : Change info
174 if (type != SUPERV::EndLoopNode && type != SUPERV::EndSwitchNode)
175 popup->insertItem(tr("MSG_CHANGE_INFO"), this, SLOT(changeInformation()));
177 // for all InLine nodes
178 if ( type != SUPERV::FactoryNode && type != SUPERV::ComputingNode && type != SUPERV::MacroNode ) {
179 if ( myMain->isEditable() ) {
180 popup->insertSeparator();
181 popup->insertItem( tr( "MNU_EDIT_FUNC" ), this, SLOT(editFunction()));
184 // for all InLine, regardless isEditable() : Export to Library
185 popup->insertItem( tr( "MNU_EXPORT" ), this, SLOT( exportToLib()));
187 // for all InLine except EndLoop : Add Ports menu, Paste, Manage Ports
188 if ( myMain->isEditable() && type != SUPERV::EndLoopNode ) {
189 // mkr : IPAL9815 : commented the following code
190 /*QPopupMenu* addPortMenu = new QPopupMenu(theParent);
191 addPortMenu->insertItem( tr( "MNU_INPUT" ), this, SLOT(addInputPort()));
192 if (getNodeType() != SUPERV::LoopNode)
193 addPortMenu->insertItem( tr( "MNU_OUTPUT" ), this, SLOT(addOutputPort()));
195 popup->insertItem( tr( "MNU_ADD_PORT" ), addPortMenu);*/
196 popup->insertItem( tr( "MNU_MANAGE_PORTS" ), this, SLOT(managePorts()));
198 // Paste Port functionality
199 int aPasteItem = popup->insertItem(tr("ITM_PASTE_PORT"), this, SLOT(pastePort()));
200 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
201 if ( !aCB->isCopyPort() || (type == SUPERV::LoopNode && !aCB->getCopyPort()->IsInput()) )
202 popup->setItemEnabled( aPasteItem, false );
209 void SUPERVGUI_CanvasNode::show()
214 void SUPERVGUI_CanvasNode::showAll()
219 void SUPERVGUI_CanvasNode::hideAll()
224 void SUPERVGUI_CanvasNode::switchLabel()
226 getPrs()->setLabelVisible(!getPrs()->isLabelVisible());
229 void SUPERVGUI_CanvasNode::switchPorts()
231 getPrs()->setPortVisible(!getPrs()->isPortVisible());
234 void SUPERVGUI_CanvasNode::move(int x, int y)
236 // myNode->Coords(x, y); // <<<- done inside Prs to reflect item movement by mouse press
237 if (x > GRAPH_MAX) x = (int) getPrs()->x();
238 if (y > GRAPH_MAX) y = (int) getPrs()->y();
239 getPrs()->move(x, y);
242 void SUPERVGUI_CanvasNode::merge()
244 // synchronize port list
247 SUPERVGUI_CanvasPort* aPort;
248 QObjectList* aPortList = queryList("SUPERVGUI_CanvasPort");
249 SUPERV_Ports aPorts = myNode->Ports();
250 for (int i = 0; i < aPorts->length(); i++) {
251 aPort = (SUPERVGUI_CanvasPort*)
252 child(myMain->getCanvas()->getPortName(aPorts[i].in()), "SUPERVGUI_CanvasPort");
254 aPortList->removeRef(aPort);
258 createPort(aPorts[i].in());
263 SUPERV_StreamPorts aStreamPorts = myNode->StreamPorts();
264 for (int i = 0; i < aStreamPorts->length(); i++) {
265 aPort = (SUPERVGUI_CanvasPort*)
266 child(myMain->getCanvas()->getPortName(aStreamPorts[i].in()), "SUPERVGUI_CanvasPort");
268 aPortList->removeRef(aPort);
272 createPort(aStreamPorts[i].in());
277 QObjectListIt it(*aPortList);
278 while ((aPort=(SUPERVGUI_CanvasPort*)it.current()) != 0) {
280 aPortList->removeRef(aPort);
286 if (update) getPrs()->updatePorts();
288 sync(); // update node state also
291 void SUPERVGUI_CanvasNode::sync() {
292 const bool isExecuting = myMain->getDataflow()->IsExecuting();
293 if ( myNode->IsMacro() && isExecuting ) {
294 // get SubGraph from MacroNode
295 SUPERV_Graph aMacro = SUPERV::Graph::_narrow(myNode);
296 if (!SUPERV_isNull(aMacro)) {
298 if (aMacro->IsStreamMacro())
299 aGraph = aMacro->StreamObjRef();
301 aGraph = aMacro->FlowObjRef();
302 if (!SUPERV_isNull(aGraph)) {
303 if (aGraph->State() != SUPERV::UndefinedState && aGraph->State() != SUPERV::NoState)
304 getPrs()->setState(aGraph->State());
306 getPrs()->setState(myNode->State());
311 SUPERV::GraphState aState = myNode->State();
313 // asv : 18.11.04 : fix for 6170 : after execution is finished, nodes' status must be reset.
314 // asv : 13.12.04 : commented out setting EditingState. See 2.21.
315 //if ( !isExecuting && myMain->IsGUIEventLoopFinished() && (aState == SUPERV::DoneState ||
316 // aState == SUPERV::KillState || aState == SUPERV::StopState ) )
317 // aState = SUPERV::EditingState;
318 getPrs()->setState(aState);
321 // update child ports
322 const QObjectList* list = children();
324 QObjectListIt it(*list);
325 while (QObject* obj = it.current()) {
327 if (obj->inherits("SUPERVGUI_CanvasPort")) {
328 ((SUPERVGUI_CanvasPort*)obj)->sync();
334 void SUPERVGUI_CanvasNode::syncOnEvent(SUPERV::GraphState theStateFromEvent)
336 getPrs()->setState(theStateFromEvent);
339 bool SUPERVGUI_CanvasNode::setNodeName(QString aName) {
340 bool result = myNode->SetName(aName.latin1());
342 setName(myNode->Name());
343 getPrs()->updateInfo();
344 // TODO: update name of all the links to this node
347 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr( "ERROR" ), tr( "MSG_CANT_RENAMENODE" ) );
352 void SUPERVGUI_CanvasNode::rename() {
353 QString aName = LightApp_NameDlg::getName( SUIT_Session::session()->activeApplication()->desktop(), myNode->Name() );
354 //mkr : modifications for fixing bug IPAL9972
355 if (!aName.isEmpty() && aName.compare( myNode->Name() ) != 0) {
360 void SUPERVGUI_CanvasNode::remove() {
361 Trace("SUPERVGUI_CanvasNode::remove");
362 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
364 //set myCopyPort from Main object to empty if engine of this port is deleted
365 //check if any port of this deleted node has been copied
366 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
367 if ( aCB->isCopyPort() )
368 if ( QString(myNode->Name()) == QString(aCB->getCopyPort()->Node()->Name()) )
369 aCB->setCopyPort( 0 );
371 //set myCopyNode from Main object to empty if engine of this node is deleted
372 //check if myCopyNode and this node engine is equal
373 if ( aCB->isCopyNode() )
374 if ( QString(myNode->Name()) == QString(aCB->getCopyNode()->Name()) )
375 aCB->setCopyNode( 0 );
377 // mkr: since the deletion of the node allow only in CANVAS view,
378 // it is necessary to remove the CanvasArray's children, which
379 // have the same CNode engine as deleting node
380 myMain->removeArrayChild(myNode);
382 SUPERVGUI_Canvas* aCanvas = myMain->getCanvas();
389 void SUPERVGUI_CanvasNode::copy() {
390 Trace("SUPERVGUI_CanvasNode::copy()");
391 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
392 const int aKind = getNodeType();
393 if ( aKind == SUPERV::EndLoopNode || aKind == SUPERV::EndSwitchNode )
394 aCB->setCopyNode( SUPERV::CNode::_duplicate(SUPERV::GNode::_narrow(getEngine())->Coupled()) );
396 aCB->setCopyNode( SUPERV::CNode::_duplicate( getEngine() ) );
399 void SUPERVGUI_CanvasNode::suspendResume() {
400 Trace("SUPERVGUI_CanvasNode::suspendResume");
401 int n = queryList("SUPERVGUI_CanvasNode")->count();
402 if (myNode->IsSuspended()) {
403 if (!((n==1)? myMain->getDataflow()->Resume() : myNode->Resume())) {
404 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_RESUMENODE") );
408 myMain->getMyThread()->startThread(tr("MSG_NODE_RESUMED1")+myNode->Name()+tr("MSG_NODE_RESUMED2"));
411 if (!((n==1)? myMain->getDataflow()->Suspend() : myNode->Suspend())) {
412 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_SUSPENDNODE") );
414 syncOnEvent(SUPERV::SuspendReadyState);
415 myMain->getMessage()->putMessage(tr("MSG_NODE_SUSPENDED1")+myNode->Name()+tr("MSG_NODE_SUSPENDED2"));
420 void SUPERVGUI_CanvasNode::kill() {
421 Trace("SUPERVGUI_CanvasNode::kill");
422 int n = queryList("SUPERVGUI_CanvasNode")->count();
423 if (!((n==1)? myMain->getDataflow()->Kill() : myNode->Kill())) {
424 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_KILLNODE") );
426 syncOnEvent(SUPERV_Kill);
427 myMain->getMessage()->putMessage(tr("MSG_NODE_KILLED1")+myNode->Name()+tr("MSG_NODE_KILLED2"));
430 /* asv : 15.12.04 : commented out stopRestart() in Main and CanvasNode because it's not called from anywhere,
431 the comment from kloss (in Main.cxx) may be explaining it, but it's in French and I do not understand it..
432 void SUPERVGUI_CanvasNode::stopRestart() {
433 Trace("SUPERVGUI_CanvasNode::stopRestart");
435 int n = queryList("SUPERVGUI_CanvasNode")->count();
436 if ((myNode->State() == SUPERV_Stop) || (myNode->State() == SUPERV_Kill)) {
437 if (!((n==1)? myMain->getDataflow()->Run() : myNode->ReStart())) {
438 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_RESTARTNODE") );
441 if (!((n==1)? myMain->getDataflow()->Stop() : myNode->Stop())) {
442 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_STOPNODE") );
447 void SUPERVGUI_CanvasNode::changeInformation() {
448 SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(myNode, !myMain->isEditable());
451 QString aName = myNode->Name();
452 if (!aName.isEmpty() && myMain->isEditable()) {
453 //mkr : modifications for fixing bug IPAL9972
454 //setNodeName(aName);
456 setName(myNode->Name());
457 getPrs()->updateInfo();
458 // TODO: update name of all the links to this node
465 /* asv : 13.12.04 : The functions below are not called from anywhere, so commenting them out...
466 void SUPERVGUI_CanvasNode::configure()
468 Trace("SUPERVGUI_CanvasNode::configure");
469 QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_NOT_IMPLEMENTED") ); // kloss : a faire : lancer l'ihm DATA
472 void SUPERVGUI_CanvasNode::showPython()
474 Trace("SUPERVGUI_CanvasNode::showPython");
475 SUPERVGUI_Python cp(myMain->getStudy()->get_PyInterp(), !myMain->isEditable());
480 bool SUPERVGUI_CanvasNode::isWarning()
482 Trace("SUPERVGUI_CanvasNode::isWarning");
486 bool SUPERVGUI_CanvasNode::isStep()
488 Trace("SUPERVGUI_CanvasNode::isStep");
492 bool SUPERVGUI_CanvasNode::isTrace()
494 Trace("SUPERVGUI_CanvasNode::isTrace");
498 bool SUPERVGUI_CanvasNode::isVerbose()
500 Trace("SUPERVGUI_CanvasNode::isVerbose");
504 void SUPERVGUI_CanvasNode::setWarning(bool b)
506 Trace("SUPERVGUI_CanvasNode::setWarning");
510 void SUPERVGUI_CanvasNode::setStep(bool b)
512 Trace("SUPERVGUI_CanvasNode::setStep");
516 void SUPERVGUI_CanvasNode::setTrace(bool b)
518 Trace("SUPERVGUI_CanvasNode::setTrace");
522 void SUPERVGUI_CanvasNode::setVerbose(bool b)
524 Trace("SUPERVGUI_CanvasNode::setVerbose");
529 void SUPERVGUI_CanvasNode::browse()
531 // asv 28.01.05 : set "Editing" flag only on "OK" pressed in BrowseDlg
532 //myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
534 myBrowseDlg = new SUPERVGUI_BrowseNodeDlg(this);
535 myBrowseDlg->installEventFilter(this);
537 if (!myBrowseDlg->isVisible())
540 myBrowseDlg->raise();
541 myBrowseDlg->setActiveWindow();
542 myBrowseDlg->setFocus();
546 bool SUPERVGUI_CanvasNode::eventFilter( QObject* o, QEvent* e )
548 if (o == myBrowseDlg && e->type() == QEvent::Close)
550 return QObject::eventFilter(o, e);
553 QStringList SUPERVGUI_CanvasNode::getPortsNamesIN(SUPERV_INode theNode, bool theInputPorts)
556 if (!SUPERV_isNull(theNode)) {
557 SUPERV_Ports aPorts = theNode->Ports();
558 for (int i=0; i<aPorts->length(); i++) {
560 if (aPorts[i]->IsInput()) aPNList.append(QString(aPorts[i]->Name()));
563 if (!aPorts[i]->IsInput()) aPNList.append(QString(aPorts[i]->Name()));
569 SUPERV_Port SUPERVGUI_CanvasNode::createInPort()
571 SUPERV_INode aNode = getInlineNode();
572 if (SUPERV_isNull(aNode)) {
573 MESSAGE("SUPERVGUI_CanvasNode::createInPort: Node is wrong type");
576 SUPERVGUI_PortParamsDlg* aDlg = new SUPERVGUI_PortParamsDlg(getPortsNamesIN(aNode,true));
578 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
579 SUPERV_Port aPort = aNode->InPort(aDlg->getName().latin1(),
580 aDlg->getType().latin1());
588 SUPERV_Port SUPERVGUI_CanvasNode::createOutPort()
590 SUPERV_INode aNode = getInlineNode();
591 if (SUPERV_isNull(aNode)) {
592 MESSAGE("SUPERVGUI_CanvasNode::createOutPort: Node is wrong type");
596 SUPERVGUI_PortParamsDlg* aDlg = new SUPERVGUI_PortParamsDlg(getPortsNamesIN(aNode,false));
598 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
599 SUPERV_Port aPort = aNode->OutPort(aDlg->getName().latin1(),
600 aDlg->getType().latin1());
608 // mkr : IPAL9815 : commented the following code
609 /*void SUPERVGUI_CanvasNode::addInputPort() {
610 SUPERV_Port aPort = createInPort();
611 if (aPort == NULL || CORBA::is_nil( aPort ) ) return;
613 createPort(aPort.in());
617 void SUPERVGUI_CanvasNode::addOutputPort() {
618 SUPERV_Port aPort = createOutPort();
619 if (aPort == NULL || CORBA::is_nil( aPort ) ) return;
621 createPort(aPort.in());
625 void SUPERVGUI_CanvasNode::editFunction() {
626 if (getNodeType() == SUPERV::LoopNode) {
627 SUPERVGUI_EditPythonDlg* aDlg = new SUPERVGUI_EditPythonDlg(true);
628 SUPERV_LNode aLNode = getLoopNode();
629 aDlg->setInitFunction(aLNode->PyInit());
630 aDlg->setMoreFunction(aLNode->PyMore());
631 aDlg->setNextFunction(aLNode->PyNext());
633 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
634 aLNode->SetPyInit(aDlg->getInitFuncName().latin1(), (aDlg->getInitFunction()).in());
635 aLNode->SetPyMore(aDlg->getMoreFuncName().latin1(), (aDlg->getMoreFunction()).in());
636 aLNode->SetPyNext(aDlg->getNextFuncName().latin1(), (aDlg->getNextFunction()).in());
641 SUPERVGUI_EditPythonDlg* aDlg = new SUPERVGUI_EditPythonDlg();
642 SUPERV_INode aINode = getInlineNode();
643 aDlg->setFunction(aINode->PyFunction());
645 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
646 aINode->SetPyFunction(aDlg->getFuncName().latin1(), (aDlg->getFunction()).in());
653 * Called on "Paste port" command of popup menu
655 void SUPERVGUI_CanvasNode::pastePort() {
656 SUPERVGUI_Clipboard::getClipboard()->pastePort( this );
660 * Called on "Edit ports" popup menu command. See SUPERVGUI_ManagePortsDlg.h
661 * for detailed description of the functionality
663 void SUPERVGUI_CanvasNode::managePorts() {
664 SUPERVGUI_ManagePortsDlg* aDlg = new SUPERVGUI_ManagePortsDlg( this );
670 * Called on "Export to Library" popup menu command. See SUPERVGUI_Library.h
671 * for details on InLine nodes library functionality
673 void SUPERVGUI_CanvasNode::exportToLib() {
674 SUPERV::INode_var anINode = SUPERV::INode::_narrow( getEngine() );
675 if ( !CORBA::is_nil( anINode ) )
676 SUPERVGUI_Library::getLibrary()->Export( anINode );
678 SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(), tr( "ERROR" ), tr( "MSG_BAD_INODE" ), tr( "OK" ) );