+//=======================================================================
+//function : onAutoSew
+//purpose : SLOT called when Auto Sewing check box is checked
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onAutoSew( int isAuto )
+{
+ GroupCoincidentWidget->setVisible( !isAuto );
+
+ QApplication::instance()->processEvents();
+
+ SewFreeBordersWidget->hide();
+ if ( ModeButGrp->checkedId() == MODE_AUTO )
+ SewFreeBordersWidget->show();
+
+ if ( isAuto )
+ restoreDisplayMode();
+ else
+ setDisplayMode();
+ SMESH::RepaintCurrentView();
+
+ UpdateButtons();
+
+ updateGeometry();
+ resize(minimumSizeHint());
+}
+
+//=======================================================================
+//function : haveBorders
+//purpose : Returns true if myBorders have been initialized
+//=======================================================================
+
+bool SMESHGUI_SewingDlg::haveBorders()
+{
+ return ( & myBorders.in() &&
+ myBorders->borders.length() &&
+ myBorders->coincidentGroups.length() );
+}
+
+//=======================================================================
+//function : getGroupText
+//purpose : Returns a text of a given group of coincident free borders
+//=======================================================================
+
+QString SMESHGUI_SewingDlg::getPartText(const SMESH::FreeBorderPart& aPART)
+{
+ typedef CORBA::Long TInt;
+ QString text;
+ if ( 0 <= aPART.border && aPART.border < (TInt)myBorders->borders.length() )
+ {
+ const SMESH::FreeBorder& aBRD = myBorders->borders[ aPART.border ];
+ if ( 0 <= aPART.node1 && aPART.node1 < (TInt)aBRD.nodeIDs.length() &&
+ 0 <= aPART.nodeLast && aPART.nodeLast < (TInt)aBRD.nodeIDs.length() )
+ {
+ text += QString("( %1 %2 %3 ) ")
+ .arg( aBRD.nodeIDs[ aPART.node1 ] )
+ .arg( aBRD.nodeIDs[ aPART.node2 ] )
+ .arg( aBRD.nodeIDs[ aPART.nodeLast ] );
+ }
+ }
+ return text;
+}
+
+//=======================================================================
+//function : getGroupText
+//purpose : Returns a text of a given group of coincident free borders
+//=======================================================================
+
+QString SMESHGUI_SewingDlg::getGroupText(int groupIndex)
+{
+ QString text;
+
+ if ( haveBorders() &&
+ groupIndex >= 0 &&
+ groupIndex < (int)myBorders->coincidentGroups.length() )
+ {
+ const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ groupIndex ];
+
+ for ( CORBA::ULong iP = 0; iP < aGRP.length(); ++iP )
+ {
+ QString partText = getPartText( aGRP[ iP ]);
+ if ( partText.isEmpty() )
+ return "";
+ text += partText;
+ }
+ }
+ return text;
+}
+
+//=======================================================================
+//function : onDetectClicked
+//purpose : SLOT called when [Detect] is clicked
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onDetectClicked()
+{
+ myBusy = true;
+ ListCoincident->clear();
+
+ if ( myMesh->_is_nil() )
+ return;
+
+ SUIT_OverrideCursor wc;
+
+ SMESH::SMESH_MeshEditor_var editor = myMesh->GetMeshEditor();
+ myBorders = editor->FindCoincidentFreeBorders( SpinBoxTolerance->GetValue() );
+ if ( haveBorders() )
+ {
+ for ( size_t i = 0; i < myBorderDisplayers.size(); ++i )
+ {
+ delete myBorderDisplayers[ i ];
+ myBorderDisplayers[ i ] = 0;
+ }
+ myBorderDisplayers.resize( myBorders->coincidentGroups.length(), 0 );
+
+ for ( uint i = 0; i < myBorders->coincidentGroups.length(); ++i )
+ {
+ QString groupText = getGroupText( i );
+ if ( groupText.isEmpty() )
+ continue;
+
+ QColor groupColor;
+ groupColor.setHsvF( float(i) / myBorders->coincidentGroups.length(), 1., 1. );
+ QPixmap icon( QSize( 20, 20 ));
+ icon.fill( groupColor );
+
+ QListWidgetItem * item = new QListWidgetItem( icon, groupText, ListCoincident );
+ item->setData( GROUP_COLOR, groupColor );
+ item->setData( GROUP_INDEX, i );
+ }
+ }
+ myBusy = false;
+
+ onSelectGroup();
+
+ UpdateButtons();
+}
+
+//=======================================================================
+//function : onRemoveGroupClicked
+//purpose :
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onRemoveGroupClicked()
+{
+ myBusy = true;
+ QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
+ for ( int i = 0; i < selItems.count(); ++i )
+ {
+ QListWidgetItem* item = selItems[ i ];
+ item->setSelected( false );
+ int groupIndex = item->data( GROUP_INDEX ).toInt();
+ delete item;
+ if ( myBorderDisplayers[ groupIndex ])
+ myBorderDisplayers[ groupIndex ]->Hide();
+ SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ groupIndex ];
+ aGRP.length( 0 );
+ }
+ myBusy = false;
+
+ onSelectGroup();
+ UpdateButtons();
+}
+
+//=======================================================================
+//function : showGroup
+//purpose : display a group of coincident free borders in the Viewer
+//=======================================================================
+
+void SMESHGUI_SewingDlg::showGroup( QListWidgetItem* item )
+{
+ if ( !item ||
+ item->listWidget() != ListCoincident ||
+ !haveBorders())
+ return;
+
+ int groupIndex = item->data( GROUP_INDEX ).toInt();
+ QColor groupColor = item->data( GROUP_COLOR ).value<QColor>();
+ if ( groupIndex >= 0 &&
+ groupIndex < (int)myBorders->coincidentGroups.length() )
+ {
+ if ( !myBorderDisplayers[ groupIndex ] && SMESH::GetCurrentVtkView())
+ myBorderDisplayers[ groupIndex ] = new BorderGroupDisplayer( myBorders, groupIndex, groupColor, myMesh );
+ bool wholeBorders = setCurrentGroup();
+ if ( myBorderDisplayers[ groupIndex ])
+ myBorderDisplayers[ groupIndex ]->ShowGroup( wholeBorders );
+ }
+}
+
+//=======================================================================
+//function : setCurrentGroup
+//purpose : set index of a current free border group to myCurGroupIndex
+//=======================================================================
+
+bool SMESHGUI_SewingDlg::setCurrentGroup()
+{
+ if ( !haveBorders() )
+ return false;
+
+ QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
+ if ( selItems.count() != 1 )
+ return false;
+
+ myCurGroupIndex = selItems[0]->data( GROUP_INDEX ).toInt();
+
+ return ( myCurGroupIndex >= 0 && myCurGroupIndex < (int)myBorders->coincidentGroups.length() );
+}
+
+//=======================================================================
+//function : setCurrentPart
+//purpose : set index of a current free border of a current group to myCurPartIndex
+//=======================================================================
+
+bool SMESHGUI_SewingDlg::setCurrentPart()
+{
+ if ( !setCurrentGroup() )
+ return false;
+
+ if ( ListEdit->selectedItems().count() != 1 )
+ return false;
+
+ myCurPartIndex = ListEdit->currentRow();
+ const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+
+ return ( myCurPartIndex >= 0 && myCurPartIndex < (int)aGRP.length() );
+}
+
+//=======================================================================
+//function : onSelectGroup
+//purpose : SLOT called when selection of coincident free borders change
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onSelectGroup()
+{
+ if ( myBusy )
+ return;
+ ListEdit->clear();
+ BorderEndLine[0]->clear();
+ BorderEndLine[1]->clear();
+ for ( size_t i = 0; i < myBorderDisplayers.size(); ++i )
+ if ( myBorderDisplayers[ i ])
+ myBorderDisplayers[ i ]->Hide();
+
+ QList<QListWidgetItem*> selItems = ListCoincident->selectedItems();
+
+ RemoveGroupButton->setEnabled( selItems.count() > 0 );
+
+ onSelectBorderPartFromGroup(); // enable buttons
+
+ if ( !haveBorders() )
+ return;
+
+ SelectAllCheck->blockSignals( true );
+ if ( ListCoincident->count() != selItems.count() )
+ SelectAllCheck->setChecked( false );
+ SelectAllCheck->blockSignals( false );
+
+ if ( selItems.empty() ) // nothing selected - show all
+ for ( int i = 0; i < ListCoincident->count(); ++i )
+ showGroup( ListCoincident->item( i ));
+ else
+ for ( int i = 0; i < selItems.count(); ++i )
+ showGroup( selItems[ i ]);
+
+ if ( setCurrentGroup() ) // edit a selected group
+ {
+ const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+ for ( CORBA::ULong iP = 0; iP < aGRP.length(); ++iP )
+ new QListWidgetItem( getPartText( aGRP[ iP ]), ListEdit );
+ }
+ SMESH::RepaintCurrentView();
+}
+
+//=======================================================================
+//function : onSelectAll
+//purpose : SLOT called when Select All is checked
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onSelectAll(int isOn)
+{
+ if ( isOn )
+ ListCoincident->selectAll();
+ else
+ ListCoincident->clearSelection();
+}
+
+//=======================================================================
+//function : onSelectBorderPartFromGroup
+//purpose : SLOT called when selection of borders in an edited group changes
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onSelectBorderPartFromGroup()
+{
+ if ( myBusy ) return;
+ BorderEndLine[0]->setText("");
+ BorderEndLine[1]->setText("");
+ MoveBorderEndsButGrp->button( MOVE_LEFT_1 )->setEnabled( false );
+ MoveBorderEndsButGrp->button( MOVE_RIGHT_1 )->setEnabled( false );
+ MoveBorderEndsButGrp->button( MOVE_LEFT_2 )->setEnabled( false );
+ MoveBorderEndsButGrp->button( MOVE_RIGHT_2 )->setEnabled( false );
+ SwapBut->setEnabled( false );
+ SetFirstButton->setEnabled( false );
+ RemoveElemButton->setEnabled ( ListEdit->count() > 2 );
+
+ if ( !setCurrentGroup() )
+ return;
+
+ if ( !myBorderDisplayers[ myCurGroupIndex ]) return;
+ myBorderDisplayers[ myCurGroupIndex ]->Hide();
+
+ QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
+ bool editPart = ( setCurrentPart() );
+ for ( int i = 0; i < selItems.count(); ++i )
+ myBorderDisplayers[ myCurGroupIndex ]->ShowPart( ListEdit->row( selItems[i] ), editPart );
+
+ if ( selItems.isEmpty() )
+ myBorderDisplayers[ myCurGroupIndex ]->ShowGroup( /*wholeBorders=*/ true );
+
+ if ( editPart )
+ {
+ SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+ SMESH::FreeBorderPart& aPRT = aGRP[ myCurPartIndex ];
+ SMESH::FreeBorder& aBRD = myBorders->borders[ aPRT.border ];
+
+ BorderEndLine[0]->setText( QString::number( aBRD.nodeIDs[ aPRT.node1 ]));
+ BorderEndLine[1]->setText( QString::number( aBRD.nodeIDs[ aPRT.nodeLast ]));
+ SwapBut->setEnabled( true );
+ SetFirstButton->setEnabled( myCurPartIndex > 0 );
+
+ int size = (int) aBRD.nodeIDs.length();
+ bool isClosed = ( aBRD.nodeIDs[0] == aBRD.nodeIDs[ size-1 ]);
+ if ( !isClosed )
+ {
+ bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
+ int dn = ( isFwd ? +1 : -1 ) * StepSpin->value();
+ MoveBorderEndsButGrp->button( MOVE_LEFT_1 )->
+ setEnabled( 0 <= aPRT.node1-dn && aPRT.node1-dn < size );
+ MoveBorderEndsButGrp->button( MOVE_RIGHT_1 )->
+ setEnabled( 0 <= aPRT.node1+dn && aPRT.node1+dn < size );
+ MoveBorderEndsButGrp->button( MOVE_LEFT_2 )->
+ setEnabled( 0 <= aPRT.nodeLast-dn && aPRT.nodeLast-dn < size );
+ MoveBorderEndsButGrp->button( MOVE_RIGHT_2 )->
+ setEnabled( 0 <= aPRT.nodeLast+dn && aPRT.nodeLast+dn < size );
+ }
+ else
+ {
+ MoveBorderEndsButGrp->button( MOVE_LEFT_1 )->setEnabled( true );
+ MoveBorderEndsButGrp->button( MOVE_RIGHT_1 )->setEnabled( true );
+ MoveBorderEndsButGrp->button( MOVE_LEFT_2 )->setEnabled( true );
+ MoveBorderEndsButGrp->button( MOVE_RIGHT_2 )->setEnabled( true );
+ }
+ }
+ SMESH::RepaintCurrentView();
+}
+
+//=======================================================================
+//function : onGroupChange
+//purpose : Update after modification of a current group by the user
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onGroupChange( bool partChange )
+{
+ ListCoincident->currentItem()->setText( getGroupText( myCurGroupIndex ));
+
+ const SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+ for ( int i = 0; i < ListEdit->count(); ++i )
+ ListEdit->item( i )->setText( getPartText( aGRP[ i ]));
+
+ if ( myBorderDisplayers[ myCurGroupIndex ])
+ myBorderDisplayers[ myCurGroupIndex ]->Update();
+
+ if ( partChange )
+ onSelectBorderPartFromGroup();
+}
+
+//=======================================================================
+//function : onSetFirstClicked
+//purpose : STOL called when |<< is clicked
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onSetFirstClicked()
+{
+ if ( !setCurrentPart() || myCurPartIndex == 0 || ListEdit->count() == 0 )
+ return;
+
+ SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+
+ SMESH::FreeBorderPart new1st = aGRP[ myCurPartIndex ];
+ for ( ; myCurPartIndex > 0; --myCurPartIndex )
+ aGRP[ myCurPartIndex ] = aGRP[ myCurPartIndex - 1 ];
+
+ aGRP[ 0 ] = new1st;
+
+ onGroupChange();
+
+ myBusy = true;
+ ListEdit->clearSelection();
+ myBusy = false;
+ ListEdit->setCurrentItem( ListEdit->item(0) );//ListEdit->item(0)->setSelected(true);
+}
+
+//=======================================================================
+//function : onRemoveElemClicked
+//purpose :
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onRemoveElemClicked()
+{
+ if ( !setCurrentGroup() )
+ return;
+
+ SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+
+ myBusy = true;
+ QList<QListWidgetItem*> selItems = ListEdit->selectedItems();
+ for ( int i = 0; i < selItems.count(); ++i )
+ {
+ int part = ListEdit->row( selItems[i] );
+ for ( ; part + 1 < (int)aGRP.length(); ++part )
+ aGRP[ part ] = aGRP[ part + 1 ];
+ if ( aGRP.length() > 0 )
+ aGRP.length( aGRP.length() - 1 );
+ delete selItems[i];
+ }
+ myBusy = false;
+
+ if ( aGRP.length() == 0 )
+ onRemoveGroupClicked();
+ else
+ onGroupChange( /*partChange=*/true );
+}
+
+//=======================================================================
+//function : onMoveBorderEnd
+//purpose :
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onMoveBorderEnd(int button)
+{
+ if ( !setCurrentPart() )
+ return;
+
+ SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+ SMESH::FreeBorderPart& aPRT = aGRP[ myCurPartIndex ];
+ SMESH::FreeBorder& aBRD = myBorders->borders[ aPRT.border ];
+ int size = (int) aBRD.nodeIDs.length();
+
+ bool isClosed = ( aBRD.nodeIDs[0] == aBRD.nodeIDs[ size-1 ]);
+ if ( isClosed ) --size;
+
+ bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
+ int dn = ( isFwd ? +1 : -1 ) * StepSpin->value();
+ if ( button == MOVE_LEFT_1 || button == MOVE_LEFT_2 )
+ dn *= -1;
+
+ switch ( button ) {
+ case MOVE_LEFT_1:
+ case MOVE_RIGHT_1:
+ if (( isClosed ) ||
+ ( 0 <= aPRT.node1+dn && aPRT.node1+dn < size ))
+ {
+ aPRT.node1 = ( aPRT.node1 + size + dn ) % size;
+ aPRT.node2 = ( aPRT.node2 + size + dn ) % size;
+ break;
+ }
+ case MOVE_LEFT_2:
+ case MOVE_RIGHT_2:
+ if (( isClosed ) ||
+ ( 0 <= aPRT.nodeLast+dn && aPRT.nodeLast+dn < size ))
+ {
+ aPRT.nodeLast = ( aPRT.nodeLast + size + dn ) % size;
+ break;
+ }
+ default:
+ return; // impossible to move
+ }
+
+ onGroupChange( /*partChange=*/true );
+}
+
+//=======================================================================
+//function : onSwapClicked
+//purpose : SLOT called when <-> is clicked
+//=======================================================================
+
+void SMESHGUI_SewingDlg::onSwapClicked()
+{
+ if ( !setCurrentPart() )
+ return;
+
+ SMESH::FreeBordersGroup& aGRP = myBorders->coincidentGroups[ myCurGroupIndex ];
+ SMESH::FreeBorderPart& aPRT = aGRP[ myCurPartIndex ];
+ SMESH::FreeBorder& aBRD = myBorders->borders[ aPRT.border ];
+ int size = (int) aBRD.nodeIDs.length();
+
+ bool isClosed = ( aBRD.nodeIDs[0] == aBRD.nodeIDs[ size-1 ]);
+ if ( isClosed ) --size;
+
+ bool isFwd = ( Abs( aPRT.node2 - aPRT.node1 ) == 1 ) ? aPRT.node2 > aPRT.node1 : aPRT.node2 < aPRT.node1;
+
+ std::swap( aPRT.nodeLast, aPRT.node1 );
+
+ aPRT.node2 = ( aPRT.node1 + ( isFwd ? -1 : +1 ) + size ) % size;
+
+ onGroupChange( /*partChange=*/true );
+}
+