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 "SALOMEGUI_NameDlg.h"
22 #include "QAD_MessageBox.h"
23 #include "QAD_Message.h"
26 SUPERVGUI_CanvasNode::SUPERVGUI_CanvasNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode, bool theIsCell):
38 Trace("SUPERVGUI_CanvasNode::SUPERVGUI_CanvasNode");
40 setName(myNode->Name());
42 QString aComment(myNode->Comment());
43 QString aNewComment = aComment;
44 if (getNodeType() == SUPERV::FactoryNode) {
45 aNewComment = QString(myNode->Service()->ServiceName) +
46 QString(tr("COMMENT_FROM")) +
47 QString(getFactoryNode()->GetComponentName());
49 else if (myNode->IsMacro()) {
50 aNewComment = tr("COMMENT_MNODE");
53 aNewComment = tr("COMMENT_CNODE");
55 if (aComment.isEmpty()) {
56 myNode->SetComment(aNewComment.latin1());
59 myLabelText = aNewComment;
61 // mkr : if the SUPERVGUI_CavasCellNode created (for CANVASTABLE view),
62 // we don't create the ports
66 SUPERV_Ports aPortList = myNode->Ports();
67 for (int i = 0; i < aPortList->length(); i++)
68 createPort(aPortList[i].in());
70 SUPERV_StreamPorts aStreamPortList = myNode->StreamPorts();
71 for (int i = 0; i < aStreamPortList->length(); i++) {
72 createStreamPort(aStreamPortList[i].in());
79 SUPERVGUI_CanvasNode::~SUPERVGUI_CanvasNode()
89 void SUPERVGUI_CanvasNode::setDestroyed()
95 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasNode::getPrs()
103 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasNode::createPrs() const
105 return new SUPERVGUI_CanvasNodePrs(myMain->getCanvas(), (SUPERVGUI_CanvasNode*)this);
108 void SUPERVGUI_CanvasNode::createPort(SUPERV::Port_ptr thePort)
110 SUPERVGUI_CanvasPort* aPort = 0;
111 if (thePort->IsInput())
112 aPort = new SUPERVGUI_CanvasPortIn((SUPERVGUI_CanvasNode*)this, myMain, thePort);
114 aPort = new SUPERVGUI_CanvasPortOut((SUPERVGUI_CanvasNode*)this, myMain, thePort);
117 connect(aPort, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
118 if (!isIgnore) getPrs()->updatePorts();
122 void SUPERVGUI_CanvasNode::createStreamPort(SUPERV::StreamPort_ptr thePort)
124 SUPERVGUI_CanvasPort* aPort = 0;
125 if (thePort->IsInput())
126 aPort = new SUPERVGUI_CanvasStreamPortIn((SUPERVGUI_CanvasNode*)this, myMain, thePort);
128 aPort = new SUPERVGUI_CanvasStreamPortOut((SUPERVGUI_CanvasNode*)this, myMain, thePort);
131 connect(aPort, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
132 if (!isIgnore) getPrs()->updatePorts();
136 void SUPERVGUI_CanvasNode::onDestroyed(QObject* theObject)
139 removeChild(theObject); // signal is sent before removing the object
140 getPrs()->updatePorts();
144 QPopupMenu* SUPERVGUI_CanvasNode::getPopupMenu(QWidget* theParent)
146 QPopupMenu* popup = new QPopupMenu(theParent);
148 if (myMain->getDataflow()->IsExecuting()) { // Execution time
149 popup->insertItem((myNode->IsSuspended()?tr("MSG_RESUME"):tr("MSG_SUSPEND")), this, SLOT(suspendResume()));
150 popup->insertItem(tr("MSG_KILL"), this, SLOT(kill()));
152 else { // Edition time
153 const int type = getNodeType();
155 // for all nodes except EndLoop and EndSwitch : Rename, Delete, Copy
156 if ( myMain->isEditable() && type != SUPERV::EndLoopNode && type != SUPERV::EndSwitchNode &&
157 // asv 26.01.05 : don't allow nodes "edition" commands in Table view
158 myMain->getViewType() != CANVASTABLE ) {
159 popup->insertItem(tr("MSG_RENAME"), this, SLOT(rename()));
160 popup->insertItem(tr("MSG_DELETE"), this, SLOT(remove()));
163 // tmp: Copy temporary does not work for Macro nodes...
164 if ( type != SUPERV::MacroNode )
165 popup->insertItem(tr("ITM_COPY_NODE"), this, SLOT(copy())); // Copy Node functionality
166 popup->insertSeparator();
168 // for all nodes : Browse
169 popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
171 // for all nodes except EndLoop and EndSwitch : Change info
172 if (type != SUPERV::EndLoopNode && type != SUPERV::EndSwitchNode)
173 popup->insertItem(tr("MSG_CHANGE_INFO"), this, SLOT(changeInformation()));
175 // for all InLine nodes
176 if ( type != SUPERV::FactoryNode && type != SUPERV::ComputingNode && type != SUPERV::MacroNode ) {
177 if ( myMain->isEditable() ) {
178 popup->insertSeparator();
179 popup->insertItem( tr( "MNU_EDIT_FUNC" ), this, SLOT(editFunction()));
182 // for all InLine, regardless isEditable() : Export to Library
183 popup->insertItem( tr( "MNU_EXPORT" ), this, SLOT( exportToLib()));
185 // for all InLine except EndLoop : Add Ports menu, Paste, Manage Ports
186 if ( myMain->isEditable() && type != SUPERV::EndLoopNode ) {
187 QPopupMenu* addPortMenu = new QPopupMenu(theParent);
188 addPortMenu->insertItem( tr( "MNU_INPUT" ), this, SLOT(addInputPort()));
189 if (getNodeType() != SUPERV::LoopNode)
190 addPortMenu->insertItem( tr( "MNU_OUTPUT" ), this, SLOT(addOutputPort()));
192 popup->insertItem( tr( "MNU_ADD_PORT" ), addPortMenu);
193 popup->insertItem( tr( "MNU_MANAGE_PORTS" ), this, SLOT(managePorts()));
195 // Paste Port functionality
196 int aPasteItem = popup->insertItem(tr("ITM_PASTE_PORT"), this, SLOT(pastePort()));
197 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
198 if ( !aCB->isCopyPort() || (type == SUPERV::LoopNode && !aCB->getCopyPort()->IsInput()) )
199 popup->setItemEnabled( aPasteItem, false );
206 void SUPERVGUI_CanvasNode::show()
211 void SUPERVGUI_CanvasNode::showAll()
216 void SUPERVGUI_CanvasNode::hideAll()
221 void SUPERVGUI_CanvasNode::switchLabel()
223 getPrs()->setLabelVisible(!getPrs()->isLabelVisible());
226 void SUPERVGUI_CanvasNode::switchPorts()
228 getPrs()->setPortVisible(!getPrs()->isPortVisible());
231 void SUPERVGUI_CanvasNode::move(int x, int y)
233 // myNode->Coords(x, y); // <<<- done inside Prs to reflect item movement by mouse press
234 if (x > GRAPH_MAX) x = (int) getPrs()->x();
235 if (y > GRAPH_MAX) y = (int) getPrs()->y();
236 getPrs()->move(x, y);
239 void SUPERVGUI_CanvasNode::merge()
241 // synchronize port list
244 SUPERVGUI_CanvasPort* aPort;
245 QObjectList* aPortList = queryList("SUPERVGUI_CanvasPort");
246 SUPERV_Ports aPorts = myNode->Ports();
247 for (int i = 0; i < aPorts->length(); i++) {
248 aPort = (SUPERVGUI_CanvasPort*)
249 child(myMain->getCanvas()->getPortName(aPorts[i].in()), "SUPERVGUI_CanvasPort");
251 aPortList->removeRef(aPort);
255 createPort(aPorts[i].in());
260 SUPERV_StreamPorts aStreamPorts = myNode->StreamPorts();
261 for (int i = 0; i < aStreamPorts->length(); i++) {
262 aPort = (SUPERVGUI_CanvasPort*)
263 child(myMain->getCanvas()->getPortName(aStreamPorts[i].in()), "SUPERVGUI_CanvasPort");
265 aPortList->removeRef(aPort);
269 createPort(aStreamPorts[i].in());
274 QObjectListIt it(*aPortList);
275 while ((aPort=(SUPERVGUI_CanvasPort*)it.current()) != 0) {
277 aPortList->removeRef(aPort);
283 if (update) getPrs()->updatePorts();
285 sync(); // update node state also
288 void SUPERVGUI_CanvasNode::sync() {
289 const bool isExecuting = myMain->getDataflow()->IsExecuting();
290 if ( myNode->IsMacro() && isExecuting ) {
291 // get SubGraph from MacroNode
292 SUPERV_Graph aMacro = SUPERV::Graph::_narrow(myNode);
293 if (!SUPERV_isNull(aMacro)) {
295 if (aMacro->IsStreamMacro())
296 aGraph = aMacro->StreamObjRef();
298 aGraph = aMacro->FlowObjRef();
299 if (!SUPERV_isNull(aGraph)) {
300 if (aGraph->State() != SUPERV::UndefinedState && aGraph->State() != SUPERV::NoState)
301 getPrs()->setState(aGraph->State());
303 getPrs()->setState(myNode->State());
308 SUPERV::GraphState aState = myNode->State();
310 // asv : 18.11.04 : fix for 6170 : after execution is finished, nodes' status must be reset.
311 // asv : 13.12.04 : commented out setting EditingState. See 2.21.
312 //if ( !isExecuting && myMain->IsGUIEventLoopFinished() && (aState == SUPERV::DoneState ||
313 // aState == SUPERV::KillState || aState == SUPERV::StopState ) )
314 // aState = SUPERV::EditingState;
315 getPrs()->setState(aState);
318 // update child ports
319 const QObjectList* list = children();
321 QObjectListIt it(*list);
322 while (QObject* obj = it.current()) {
324 if (obj->inherits("SUPERVGUI_CanvasPort")) {
325 ((SUPERVGUI_CanvasPort*)obj)->sync();
331 void SUPERVGUI_CanvasNode::syncOnEvent(SUPERV::GraphState theStateFromEvent)
333 getPrs()->setState(theStateFromEvent);
336 bool SUPERVGUI_CanvasNode::setNodeName(QString aName) {
337 bool result = myNode->SetName(aName.latin1());
339 setName(myNode->Name());
340 getPrs()->updateInfo();
341 // TODO: update name of all the links to this node
344 QMessageBox::warning( QAD_Application::getDesktop(), tr( "ERROR" ), tr( "MSG_CANT_RENAMENODE" ) );
349 void SUPERVGUI_CanvasNode::rename() {
350 QString aName = SALOMEGUI_NameDlg::getName(QAD_Application::getDesktop(), myNode->Name());
351 if (!aName.isEmpty()) {
356 void SUPERVGUI_CanvasNode::remove() {
357 Trace("SUPERVGUI_CanvasNode::remove");
358 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
360 //set myCopyPort from Main object to empty if engine of this port is deleted
361 //check if any port of this deleted node has been copied
362 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
363 if ( aCB->isCopyPort() )
364 if ( QString(myNode->Name()) == QString(aCB->getCopyPort()->Node()->Name()) )
365 aCB->setCopyPort( 0 );
367 //set myCopyNode from Main object to empty if engine of this node is deleted
368 //check if myCopyNode and this node engine is equal
369 if ( aCB->isCopyNode() )
370 if ( QString(myNode->Name()) == QString(aCB->getCopyNode()->Name()) )
371 aCB->setCopyNode( 0 );
373 // mkr: since the deletion of the node allow only in CANVAS view,
374 // it is necessary to remove the CanvasArray's children, which
375 // have the same CNode engine as deleting node
376 myMain->removeArrayChild(myNode);
378 SUPERVGUI_Canvas* aCanvas = myMain->getCanvas();
385 void SUPERVGUI_CanvasNode::copy() {
386 Trace("SUPERVGUI_CanvasNode::copy()");
387 SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
388 const int aKind = getNodeType();
389 if ( aKind == SUPERV::EndLoopNode || aKind == SUPERV::EndSwitchNode )
390 aCB->setCopyNode( SUPERV::CNode::_duplicate(SUPERV::GNode::_narrow(getEngine())->Coupled()) );
392 aCB->setCopyNode( SUPERV::CNode::_duplicate( getEngine() ) );
395 void SUPERVGUI_CanvasNode::suspendResume() {
396 Trace("SUPERVGUI_CanvasNode::suspendResume");
397 int n = queryList("SUPERVGUI_CanvasNode")->count();
398 if (myNode->IsSuspended()) {
399 if (!((n==1)? myMain->getDataflow()->Resume() : myNode->Resume())) {
400 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESUMENODE"));
404 myMain->getMyThread()->startThread(tr("MSG_NODE_RESUMED1")+myNode->Name()+tr("MSG_NODE_RESUMED2"));
407 if (!((n==1)? myMain->getDataflow()->Suspend() : myNode->Suspend())) {
408 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SUSPENDNODE"));
410 syncOnEvent(SUPERV::SuspendReadyState);
411 myMain->getMessage()->setMessage(tr("MSG_NODE_SUSPENDED1")+myNode->Name()+tr("MSG_NODE_SUSPENDED2"));
416 void SUPERVGUI_CanvasNode::kill() {
417 Trace("SUPERVGUI_CanvasNode::kill");
418 int n = queryList("SUPERVGUI_CanvasNode")->count();
419 if (!((n==1)? myMain->getDataflow()->Kill() : myNode->Kill())) {
420 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_KILLNODE"));
422 syncOnEvent(SUPERV_Kill);
423 myMain->getMessage()->setMessage(tr("MSG_NODE_KILLED1")+myNode->Name()+tr("MSG_NODE_KILLED2"));
426 /* asv : 15.12.04 : commented out stopRestart() in Main and CanvasNode because it's not called from anywhere,
427 the comment from kloss (in Main.cxx) may be explaining it, but it's in French and I do not understand it..
428 void SUPERVGUI_CanvasNode::stopRestart() {
429 Trace("SUPERVGUI_CanvasNode::stopRestart");
431 int n = queryList("SUPERVGUI_CanvasNode")->count();
432 if ((myNode->State() == SUPERV_Stop) || (myNode->State() == SUPERV_Kill)) {
433 if (!((n==1)? myMain->getDataflow()->Run() : myNode->ReStart())) {
434 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESTARTNODE"));
437 if (!((n==1)? myMain->getDataflow()->Stop() : myNode->Stop())) {
438 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_STOPNODE"));
443 void SUPERVGUI_CanvasNode::changeInformation() {
444 SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(myNode, !myMain->isEditable());
446 QString aName = myNode->Name();
447 if (!aName.isEmpty() && myMain->isEditable())
453 /* asv : 13.12.04 : The functions below are not called from anywhere, so commenting them out...
454 void SUPERVGUI_CanvasNode::configure()
456 Trace("SUPERVGUI_CanvasNode::configure");
457 QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_NOT_IMPLEMENTED")); // kloss : a faire : lancer l'ihm DATA
460 void SUPERVGUI_CanvasNode::showPython()
462 Trace("SUPERVGUI_CanvasNode::showPython");
463 SUPERVGUI_Python cp(myMain->getStudy()->get_PyInterp(), !myMain->isEditable());
468 bool SUPERVGUI_CanvasNode::isWarning()
470 Trace("SUPERVGUI_CanvasNode::isWarning");
474 bool SUPERVGUI_CanvasNode::isStep()
476 Trace("SUPERVGUI_CanvasNode::isStep");
480 bool SUPERVGUI_CanvasNode::isTrace()
482 Trace("SUPERVGUI_CanvasNode::isTrace");
486 bool SUPERVGUI_CanvasNode::isVerbose()
488 Trace("SUPERVGUI_CanvasNode::isVerbose");
492 void SUPERVGUI_CanvasNode::setWarning(bool b)
494 Trace("SUPERVGUI_CanvasNode::setWarning");
498 void SUPERVGUI_CanvasNode::setStep(bool b)
500 Trace("SUPERVGUI_CanvasNode::setStep");
504 void SUPERVGUI_CanvasNode::setTrace(bool b)
506 Trace("SUPERVGUI_CanvasNode::setTrace");
510 void SUPERVGUI_CanvasNode::setVerbose(bool b)
512 Trace("SUPERVGUI_CanvasNode::setVerbose");
517 void SUPERVGUI_CanvasNode::browse()
519 // asv 28.01.05 : set "Editing" flag only on "OK" pressed in BrowseDlg
520 //myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
522 myBrowseDlg = new SUPERVGUI_BrowseNodeDlg(this);
523 myBrowseDlg->installEventFilter(this);
525 if (!myBrowseDlg->isVisible())
528 myBrowseDlg->raise();
529 myBrowseDlg->setActiveWindow();
530 myBrowseDlg->setFocus();
534 bool SUPERVGUI_CanvasNode::eventFilter( QObject* o, QEvent* e )
536 if (o == myBrowseDlg && e->type() == QEvent::Close)
538 return QObject::eventFilter(o, e);
541 QStringList SUPERVGUI_CanvasNode::getPortsNamesIN(SUPERV_INode theNode, bool theInputPorts)
544 if (!SUPERV_isNull(theNode)) {
545 SUPERV_Ports aPorts = theNode->Ports();
546 for (int i=0; i<aPorts->length(); i++) {
548 if (aPorts[i]->IsInput()) aPNList.append(QString(aPorts[i]->Name()));
551 if (!aPorts[i]->IsInput()) aPNList.append(QString(aPorts[i]->Name()));
557 SUPERV_Port SUPERVGUI_CanvasNode::createInPort()
559 SUPERV_INode aNode = getInlineNode();
560 if (SUPERV_isNull(aNode)) {
561 MESSAGE("SUPERVGUI_CanvasNode::createInPort: Node is wrong type");
564 SUPERVGUI_PortParamsDlg* aDlg = new SUPERVGUI_PortParamsDlg(getPortsNamesIN(aNode,true));
566 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
567 SUPERV_Port aPort = aNode->InPort(aDlg->getName().latin1(),
568 aDlg->getType().latin1());
576 SUPERV_Port SUPERVGUI_CanvasNode::createOutPort()
578 SUPERV_INode aNode = getInlineNode();
579 if (SUPERV_isNull(aNode)) {
580 MESSAGE("SUPERVGUI_CanvasNode::createOutPort: Node is wrong type");
584 SUPERVGUI_PortParamsDlg* aDlg = new SUPERVGUI_PortParamsDlg(getPortsNamesIN(aNode,false));
586 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
587 SUPERV_Port aPort = aNode->OutPort(aDlg->getName().latin1(),
588 aDlg->getType().latin1());
596 void SUPERVGUI_CanvasNode::addInputPort() {
597 SUPERV_Port aPort = createInPort();
598 if (aPort == NULL || CORBA::is_nil( aPort ) ) return;
600 createPort(aPort.in());
604 void SUPERVGUI_CanvasNode::addOutputPort() {
605 SUPERV_Port aPort = createOutPort();
606 if (aPort == NULL || CORBA::is_nil( aPort ) ) return;
608 createPort(aPort.in());
612 void SUPERVGUI_CanvasNode::editFunction() {
613 if (getNodeType() == SUPERV::LoopNode) {
614 SUPERVGUI_EditPythonDlg* aDlg = new SUPERVGUI_EditPythonDlg(true);
615 SUPERV_LNode aLNode = getLoopNode();
616 aDlg->setInitFunction(aLNode->PyInit());
617 aDlg->setMoreFunction(aLNode->PyMore());
618 aDlg->setNextFunction(aLNode->PyNext());
620 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
621 aLNode->SetPyInit(aDlg->getInitFuncName().latin1(), (aDlg->getInitFunction()).in());
622 aLNode->SetPyMore(aDlg->getMoreFuncName().latin1(), (aDlg->getMoreFunction()).in());
623 aLNode->SetPyNext(aDlg->getNextFuncName().latin1(), (aDlg->getNextFunction()).in());
628 SUPERVGUI_EditPythonDlg* aDlg = new SUPERVGUI_EditPythonDlg();
629 SUPERV_INode aINode = getInlineNode();
630 aDlg->setFunction(aINode->PyFunction());
632 myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag
633 aINode->SetPyFunction(aDlg->getFuncName().latin1(), (aDlg->getFunction()).in());
640 * Called on "Paste port" command of popup menu
642 void SUPERVGUI_CanvasNode::pastePort() {
643 SUPERVGUI_Clipboard::getClipboard()->pastePort( this );
647 * Called on "Edit ports" popup menu command. See SUPERVGUI_ManagePortsDlg.h
648 * for detailed description of the functionality
650 void SUPERVGUI_CanvasNode::managePorts() {
651 SUPERVGUI_ManagePortsDlg* aDlg = new SUPERVGUI_ManagePortsDlg( this );
657 * Called on "Export to Library" popup menu command. See SUPERVGUI_Library.h
658 * for details on InLine nodes library functionality
660 void SUPERVGUI_CanvasNode::exportToLib() {
661 SUPERV::INode_var anINode = SUPERV::INode::_narrow( getEngine() );
662 if ( !CORBA::is_nil( anINode ) )
663 SUPERVGUI_Library::getLibrary()->Export( anINode );
665 QAD_MessageBox::error1( QAD_Application::getDesktop(), tr( "ERROR" ), tr( "MSG_BAD_INODE" ), tr( "OK" ) );