1 // SUPERV SUPERVGUI : GUI for Supervisor component
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SUPERVGUI_ManagePortsDlg.cxx
8 // Author : Alexander SLADKOV
13 #include "SUPERVGUI_ManagePortsDlg.h"
14 #include "SUPERVGUI_CanvasNode.h"
15 #include "SUPERVGUI_CanvasPort.h"
16 #include "SUPERVGUI_CanvasControlNode.h"
17 #include "SUPERVGUI.h"
21 #include <qpushbutton.h>
23 #include <qgroupbox.h>
25 static const char* gTypes[] =
26 {"string", "boolean", "char", "short", "int", "long", "float", "double", "objref"};
28 static const char* const add_pix_data[] = {
34 "....................",
35 "....................",
36 "....................",
37 "....................",
38 "........##a.........",
39 "........##b.........",
40 "........##b.........",
41 "........##b.........",
42 "........##b.........",
43 "...############a....",
44 "...############b....",
45 "...abbbb##bbbbbb....",
46 "........##b.........",
47 "........##b.........",
48 "........##b.........",
49 "........##b.........",
50 "........abb.........",
51 "....................",
52 "....................",
53 "...................."};
55 static const char* const del_pix_data[] = {
77 static const char* const up_pix_data[] = {
101 static const char* const down_pix_data[] = {
129 SUPERVGUI_ManagePortsDlg::SUPERVGUI_ManagePortsDlg( SUPERVGUI_CanvasNode* theNode )
130 : QDialog( QAD_Application::getDesktop(), 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
137 void SUPERVGUI_ManagePortsDlg::init()
139 setName( "SUPERVGUI_ManagePortsDlg" );
141 SUPERV_CNode aCNode = myNode->getEngine();
142 setCaption( tr( "TIT_MANAGE_PORTS" ) + aCNode->Name() );
144 QGridLayout* aBaseLayout = new QGridLayout( this );
145 aBaseLayout->setMargin( 11 );
146 aBaseLayout->setSpacing( 6 );
148 // -- name and type upper line widgets --
149 QLabel* aNameLbl = new QLabel( tr( "NAME_LBL") , this );
150 myNameEdt = new QLineEdit( this );
151 QLabel* aTypeLbl = new QLabel( tr( "TYPE_LBL" ), this );
152 myTypeCombo = new QComboBox( false, this ); // input field not editable
154 aBaseLayout->addWidget( aNameLbl, 0, 0 );
155 aBaseLayout->addWidget( myNameEdt, 0, 1 );
156 aBaseLayout->addWidget( aTypeLbl, 0, 2 );
157 aBaseLayout->addWidget( myTypeCombo, 0, 3 );
158 aBaseLayout->setColStretch( 1, 1 );
159 aBaseLayout->setColStretch( 3, 1 );
161 // -- common Input/Output variables --
162 QPixmap adPix( (const char**) add_pix_data );
163 QPixmap rmPix( (const char**) del_pix_data );
164 QPixmap upPix( (const char**) up_pix_data );
165 QPixmap dnPix( (const char**) down_pix_data );
167 // -- Input ports group box --
168 QGroupBox* anInputGr = new QGroupBox( 1, Qt::Vertical, tr( "INPUT" ), this );
169 anInputGr->layout()->setSpacing( 6 );
170 anInputGr->layout()->setMargin( 11 );
172 myInList = new QListBox( anInputGr );
173 myInList->setSelectionMode( QListBox::Single );
175 QGroupBox* anInputBtnGr = new QGroupBox( 0, Qt::Vertical, anInputGr );
176 anInputBtnGr->setFrameStyle( QFrame::NoFrame );
177 anInputBtnGr->layout()->setSpacing( 0 );
178 anInputBtnGr->layout()->setMargin( 0 );
180 QVBoxLayout* anInputBtnLay = new QVBoxLayout( anInputBtnGr->layout() );
181 anInputBtnLay->layout()->setSpacing( 6 );
182 anInputBtnLay->layout()->setMargin( 0 );
184 QPushButton* anAddInputBtn = new QPushButton( anInputBtnGr );
185 anAddInputBtn->setPixmap( adPix );
186 QPushButton* aDeleteInputBtn = new QPushButton( anInputBtnGr );
187 aDeleteInputBtn->setPixmap( rmPix );
188 QPushButton* anUpInputBtn = new QPushButton( anInputBtnGr );
189 anUpInputBtn->setPixmap( upPix );
190 QPushButton* aDownInputBtn = new QPushButton( anInputBtnGr );
191 aDownInputBtn->setPixmap( dnPix );
193 connect( anAddInputBtn, SIGNAL( clicked() ), this, SLOT( addInput() ) );
194 connect( aDeleteInputBtn, SIGNAL( clicked() ), this, SLOT( removeInput() ) );
195 connect( anUpInputBtn, SIGNAL( clicked() ), this, SLOT( upInput() ) );
196 connect( aDownInputBtn, SIGNAL( clicked() ), this, SLOT( downInput() ) );
198 anInputBtnLay->addWidget( anAddInputBtn );
199 anInputBtnLay->addWidget( aDeleteInputBtn );
200 anInputBtnLay->addItem( new QSpacerItem( 1, 16, QSizePolicy::Maximum, QSizePolicy::MinimumExpanding ) );
201 anInputBtnLay->addWidget( anUpInputBtn );
202 anInputBtnLay->addWidget( aDownInputBtn );
203 anInputBtnLay->addItem( new QSpacerItem( 1, 16, QSizePolicy::Maximum, QSizePolicy::MinimumExpanding ) );
205 aBaseLayout->addMultiCellWidget( anInputGr, 1, 1, 0, 1 );
207 // -- Output ports group box --
208 QGroupBox* anOutputGr = new QGroupBox( 1, Qt::Vertical, tr( "OUTPUT" ), this );
209 anOutputGr->layout()->setSpacing( 6 );
210 anOutputGr->layout()->setMargin( 11 );
212 myOutList = new QListBox( anOutputGr );
213 myOutList->setSelectionMode( QListBox::Single );
215 QGroupBox* anOutputBtnGr = new QGroupBox( 0, Qt::Vertical, anOutputGr );
216 anOutputBtnGr->setFrameStyle( QFrame::NoFrame );
217 anOutputBtnGr->layout()->setSpacing( 0 );
218 anOutputBtnGr->layout()->setMargin( 0 );
220 QVBoxLayout* anOutputBtnLay = new QVBoxLayout( anOutputBtnGr->layout() );
221 anOutputBtnLay->layout()->setSpacing( 6 );
222 anOutputBtnLay->layout()->setMargin( 0 );
224 QPushButton* anAddOutputBtn = new QPushButton( anOutputBtnGr );
225 anAddOutputBtn->setPixmap( adPix );
226 QPushButton* aDeleteOutputBtn = new QPushButton( anOutputBtnGr );
227 aDeleteOutputBtn->setPixmap( rmPix );
228 QPushButton* anUpOutputBtn = new QPushButton( anOutputBtnGr );
229 anUpOutputBtn->setPixmap( upPix );
230 QPushButton* aDownOutputBtn = new QPushButton( anOutputBtnGr );
231 aDownOutputBtn->setPixmap( dnPix );
233 connect( anAddOutputBtn, SIGNAL( clicked() ), this, SLOT( addOutput() ) );
234 connect( aDeleteOutputBtn, SIGNAL( clicked() ), this, SLOT( removeOutput() ) );
235 connect( anUpOutputBtn, SIGNAL( clicked() ), this, SLOT( upOutput() ) );
236 connect( aDownOutputBtn, SIGNAL( clicked() ), this, SLOT( downOutput() ) );
238 anOutputBtnLay->addWidget( anAddOutputBtn );
239 anOutputBtnLay->addWidget( aDeleteOutputBtn );
240 anOutputBtnLay->addItem( new QSpacerItem( 1, 16, QSizePolicy::Maximum, QSizePolicy::MinimumExpanding ) );
241 anOutputBtnLay->addWidget( anUpOutputBtn );
242 anOutputBtnLay->addWidget( aDownOutputBtn );
243 anOutputBtnLay->addItem( new QSpacerItem( 1, 16, QSizePolicy::Maximum, QSizePolicy::MinimumExpanding ) );
245 aBaseLayout->addMultiCellWidget( anOutputGr, 1, 1, 2, 3 );
247 // -- Ok / Cancel buttons line at the bottom of dialog --
248 QGroupBox* aBtnBox = new QGroupBox( this );
249 aBtnBox->setColumnLayout( 0, Qt::Vertical );
250 aBtnBox->layout()->setSpacing( 0 );
251 aBtnBox->layout()->setMargin( 0 );
253 QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
254 aBtnLayout->setAlignment( Qt::AlignTop );
255 aBtnLayout->setSpacing( 6 );
256 aBtnLayout->setMargin( 11 );
258 QPushButton* aOkBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
259 QPushButton* aCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), aBtnBox );
260 connect( aOkBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
261 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
263 aBtnLayout->addWidget( aOkBtn );
264 aBtnLayout->addStretch();
265 aBtnLayout->addWidget( aCancelBtn );
267 aBaseLayout->addMultiCellWidget( aBtnBox, 2, 2, 0, 3 );
270 connect( myInList,SIGNAL(currentChanged(QListBoxItem*)), this, SLOT(itemChanged(QListBoxItem*)));
271 connect( myOutList, SIGNAL(currentChanged(QListBoxItem*)), this, SLOT(itemChanged(QListBoxItem*)));
272 connect( myNameEdt, SIGNAL(textChanged(const QString&)), this, SLOT(nameChanged(const QString&)));
273 connect( myTypeCombo, SIGNAL(activated(const QString&)), this, SLOT(typeChanged(const QString&)));
275 myNode->getMain()->lockedGraph( true );
277 // asv : 11.01.05 : if a node is a loop node, then only INPUT ports can be added/removed
278 if ( myNode->getNodeType() == SUPERV::LoopNode ) {
279 myOutList->setEnabled( false );
280 anAddOutputBtn->setEnabled( false );
281 aDeleteOutputBtn->setEnabled( false );
282 anUpOutputBtn->setEnabled( false );
283 aDownOutputBtn->setEnabled( false );
291 SUPERVGUI_ManagePortsDlg::~SUPERVGUI_ManagePortsDlg() {
295 * Set the ports as entered by user (order, name/type, etc.) and close the dialog
297 void SUPERVGUI_ManagePortsDlg::accept() {
298 // 1. set the ports to Engine's CNode
299 SUPERV_INode aINode = myNode->getInlineNode();
300 if ( !SUPERV_isNull( aINode ) ) {
302 const bool isLoop = ( myNode->getNodeType() == SUPERV::LoopNode );
304 // 1.1 delete all ports (delete CanvasPorts, they delete SUPERV_Ports)
305 QObjectList* oldPorts = myNode->queryList("SUPERVGUI_CanvasPort");
306 QObjectListIt it( *oldPorts ); // iterate over the old ports
309 // asv : 11.01.05 : fix for a bug: for Loop node, removal of an Input Port
310 // automatically removes the corresponding Output Port. So for Loops the
311 // oldPorts list should be filtered to include only Input Ports.
312 // But I'll filter Gate-s as well..
313 QObjectList portsToRemove;
314 while ( (obj = it.current()) != 0 ) {
316 SUPERV::Port_var aPort = ((SUPERVGUI_CanvasPort*)obj)->getEngine();
317 if ( !aPort->IsGate() && ( !isLoop || aPort->IsInput() ) )
318 portsToRemove.append( obj );
320 delete oldPorts; // delete the list, not the objects
322 // portsToRemove list contains:
323 // for Loop node: all INPUT ports except Gates
324 // for other Inline: all INPUT and OUTPUT ports except Gates
325 it = QObjectListIt( portsToRemove );
326 while ( (obj = it.current()) != 0 ) {
328 ((SUPERVGUI_CanvasPort*)obj)->remove();
331 // 1.2 create new ports in INode and CanvasPort in myNode
334 for ( i = 0; i < myInList->count(); i++ ) {
335 item = (PortListItem*)myInList->item( i );
336 aPort = aINode->InPort( item->PortName.latin1(), item->PortType.latin1() );
337 myNode->createPort( aPort.in() );
338 // asv : 11.01.05 : for Loop nodes do the same as in SUPERVGUI_CanvasStartNode::addInputPort()
340 SUPERVGUI_CanvasStartNode* aStartLoopNode = (SUPERVGUI_CanvasStartNode*)myNode;
341 aStartLoopNode->merge();
342 aStartLoopNode->getCoupled()->merge();
345 // creating Out-ports, except LoopNode-s
346 for ( i = 0; i < myOutList->count() && !isLoop; i++ ) {
347 item = (PortListItem*)myOutList->item( i );
348 aPort = aINode->OutPort( item->PortName.latin1(), item->PortType.latin1() );
349 myNode->createPort( aPort.in() );
352 // 2. update the node's presentation
353 myNode->getMain()->getCanvas()->update();
356 // 3. close the dialog
357 myNode->getMain()->lockedGraph( false );
363 * Simply close the dialog
365 void SUPERVGUI_ManagePortsDlg::reject() {
366 myNode->getMain()->lockedGraph( false );
372 * Update current values on show event
374 void SUPERVGUI_ManagePortsDlg::showEvent( QShowEvent* theEvent ) {
376 QDialog::showEvent( theEvent );
380 * Initialize In/Out tables with values from the given node
382 void SUPERVGUI_ManagePortsDlg::init( const SUPERVGUI_CanvasNode* theNode ) {
384 MESSAGE("ERROR: SUPERVGUI_ManagePortsDlg: the CanvasNode is nil!");
388 SUPERV_INode aINode = myNode->getInlineNode();
389 if ( SUPERV_isNull( aINode ) ) {
390 MESSAGE("ERROR: SUPERVGUI_ManagePortsDlg: the node of wrong type is found (not InLine node)!");
394 // 1. fill myTypeCombo with all available types
395 myTypeCombo->insertStrList( gTypes );
397 // 2. fill Input and Output listboxes with Ports' names/types
399 SUPERV_Ports aPorts = aINode->Ports();
400 for ( int i=0; i < aPorts->length(); i++ ) {
401 if ( aPorts[i]->IsGate() )
403 if ( aPorts[i]->IsInput() )
404 new PortListItem( myInList, aPorts[i]->Name(), aPorts[i]->Type() );
406 new PortListItem( myOutList, aPorts[i]->Name(), aPorts[i]->Type() );
410 void SUPERVGUI_ManagePortsDlg::addPort( QListBox* theLB ) {
413 QString name = myNameEdt->text();
414 if ( !name.isNull() && name.length() ) {
415 bool found = false; // check if already exists -- and don't allow if yes
416 for ( int i = 0; i < theLB->count(); i++ ) {
417 if ( ((PortListItem*)theLB->item(i))->PortName == name ) {
423 QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_PORT_EXIST") );
425 new PortListItem( theLB, name, myTypeCombo->currentText() );
430 * called on 'X' button press - remove selected port item
432 void SUPERVGUI_ManagePortsDlg::removePort( QListBox* theLB ) {
436 const int i = theLB->currentItem();
438 delete theLB->item( i );
442 * called on up/down key press - move the selected list box item from position 'from' to position 'to'
444 void SUPERVGUI_ManagePortsDlg::moveItem( QListBox* theLB, const int from, const int to ) {
448 // disconnect itemChanged - it leads to crash if there was only 1 item left
449 disconnect( theLB, SIGNAL(currentChanged(QListBoxItem*)), this, SLOT(itemChanged(QListBoxItem*)));
451 if ( from >= 0 && from < theLB->count() && to >=0 && to < theLB->count() ) {
452 QListBoxItem* item = theLB->item( from );
453 theLB->takeItem( item );
454 theLB->insertItem( item, to );
455 theLB->setCurrentItem( item );
456 theLB->setSelected( item, true );
460 connect( theLB, SIGNAL(currentChanged(QListBoxItem*)), this, SLOT(itemChanged(QListBoxItem*)));
464 * move the selected item UP
466 void SUPERVGUI_ManagePortsDlg::moveUp( QListBox* theLB ) {
469 const int i = theLB->currentItem();
470 moveItem( theLB, i, i-1 );
474 * move the selected item DOWN
476 void SUPERVGUI_ManagePortsDlg::moveDown( QListBox* theLB ) {
479 const int i = theLB->currentItem();
480 moveItem( theLB, i, i+1 );
485 * returns true if there is a current and selected item in the given listbox and it
486 * equals to the given item.
488 bool isEditingItem( const QListBoxItem* item, const QListBox* theLB ) {
489 return item && item == theLB->item( theLB->currentItem() ) && theLB->isSelected( item );
493 * called when name is changed. Tries to change name of the item, which was
494 * the last selected by user item in myInList or myOutList (myLastItem)
496 void SUPERVGUI_ManagePortsDlg::nameChanged( const QString& name ) {
497 if ( isEditingItem( myLastItem, myInList ) || isEditingItem( myLastItem, myOutList ) ) {
498 ((PortListItem*)myLastItem)->PortName = name;
499 //myLastItem->listBox()->repaint( true );
500 // it would be much better to change the item and repaint it -- but -- repaint()
501 // is done only after a while, which is not good.. so -- "cheating" around
502 const int i = myLastItem->listBox()->index( myLastItem );
503 moveItem( myLastItem->listBox(), i, i );
508 * called when type item in myTypeCombo is changed. Tries to change type
509 * of the last selected by user item in myInList or myOutList (myLastItem)
511 void SUPERVGUI_ManagePortsDlg::typeChanged( const QString& type ) {
512 if ( isEditingItem( myLastItem, myInList ) || isEditingItem( myLastItem, myOutList ) ) {
513 ((PortListItem*)myLastItem)->PortType = type;
514 //myLastItem->listBox()->repaint( true );
515 // it would be much better to change the item and repaint it -- but -- repaint()
516 // is done only after a while, which is not good.. so -- "cheating" around
517 const int i = myLastItem->listBox()->index( myLastItem );
518 moveItem( myLastItem->listBox(), i, i );
523 * called when new item became "currentItem" in any myInList or myOutList listboxes.
525 void SUPERVGUI_ManagePortsDlg::itemChanged( QListBoxItem * item ) {
526 if ( !item ) // after remove, etc..
529 // disable nameChanged, typeChanged callbacks
530 disconnect( myNameEdt, SIGNAL(textChanged(const QString&)), this, SLOT(nameChanged(const QString&)));
531 disconnect( myTypeCombo, SIGNAL(activated(const QString&)), this, SLOT(typeChanged(const QString&)));
534 myNameEdt->setText( ((PortListItem*)item)->PortName );
535 for ( int i = 0; i < myTypeCombo->count(); i++ )
536 if ( myTypeCombo->text( i ) == ((PortListItem*)item)->PortType ) {
537 myTypeCombo->setCurrentItem( i );
541 // connect back nameChanged, typeChanged
542 connect( myNameEdt, SIGNAL(textChanged(const QString&)), this, SLOT(nameChanged(const QString&)));
543 connect( myTypeCombo, SIGNAL(activated(const QString&)), this, SLOT(typeChanged(const QString&)));
550 * Port parameters dialog definition (taken from SUPERVGUI_Node.cxx without change)
552 SUPERVGUI_PortParamsDlg::SUPERVGUI_PortParamsDlg(const QStringList& thePortsNames)
553 : QDialog(QAD_Application::getDesktop(), 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
554 myPortsNames( thePortsNames )
556 setSizeGripEnabled( true );
557 setCaption("Port parameters");
559 QGridLayout* aBaseLayout = new QGridLayout( this );
560 aBaseLayout->setMargin( 11 ); aBaseLayout->setSpacing( 6 );
562 QLabel* aNameLbl = new QLabel("Port Name", this );
563 aBaseLayout->addWidget(aNameLbl, 0, 0);
565 myNameTxt = new QLineEdit( this );
566 aNameLbl->setBuddy( myNameTxt );
567 aBaseLayout->addWidget(myNameTxt, 0, 1);
569 QLabel* aTypeLbl = new QLabel("Value Type", this );
570 aBaseLayout->addWidget(aTypeLbl, 1, 0);
572 myTypeTxt = new QComboBox( this );
573 aTypeLbl->setBuddy( myTypeTxt );
574 myTypeTxt->insertStrList( gTypes );
575 aBaseLayout->addWidget(myTypeTxt, 1, 1);
577 QGroupBox* aBtnBox = new QGroupBox( this );
578 aBtnBox->setColumnLayout( 0, Qt::Vertical );
579 aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
580 QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
581 aBtnLayout->setAlignment( Qt::AlignTop );
582 aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
584 aBaseLayout->addMultiCellWidget( aBtnBox, 2, 2, 0, 1 );
586 QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
587 connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( clickOnOk() ) );
588 aBtnLayout->addWidget( aOKBtn );
590 aBtnLayout->addStretch();
592 QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
593 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
594 aBtnLayout->addWidget( aCancelBtn );
597 void SUPERVGUI_PortParamsDlg::clickOnOk()
599 if (getName().isEmpty() || getType().isEmpty())
600 QMessageBox::warning( QAD_Application::getDesktop(), tr( "ERROR" ), tr( "MSG_CANT_CREATE_PORT" ) );
601 else if (myPortsNames.contains(getName()))
602 QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_PORT_EXIST") );