1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : StdMeshersGUI_BlockRenumberCreator.cxx
23 // Author : Open CASCADE S.A.S.
25 #include "StdMeshersGUI_BlockRenumberCreator.h"
28 #include "SMESHGUI_Utils.h"
29 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_HypothesesUtils.h"
32 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
35 #include <LightApp_SelectionMgr.h>
36 #include <SALOMEDSClient_SObject.hxx>
37 #include <SALOMEDSClient_Study.hxx>
38 #include <SALOME_InteractiveObject.hxx>
39 #include <SALOME_ListIO.hxx>
40 #include <SUIT_ResourceMgr.h>
41 #include <SalomeApp_Tools.h>
43 #include <QApplication>
44 #include <QButtonGroup>
45 #include <QGridLayout>
46 #include <QGridLayout>
48 #include <QHeaderView>
51 #include <QPushButton>
53 #include <QTreeWidget>
54 #include <QTreeWidgetItem>
61 //================================================================================
63 * \brief Textual representation of GEOM_Object in QTreeWidget
65 //================================================================================
67 QString toText( const GEOM::GEOM_Object_var& theGO,
68 const QString & /*theLabel*/,
72 if ( !theGO->_is_nil() )
74 CORBA::String_var name = theGO->GetName();
75 CORBA::String_var entry = theGO->GetStudyEntry();
76 theEntry = entry.in();
79 //txt = theLabel + name.in() + " [" + theEntry + "]";
87 //================================================================================
89 * \brief Find GEOM_Object by study entry
91 //================================================================================
93 GEOM::GEOM_Object_var toGeom( const QString& entry )
95 return SMESH::EntryToInterface<GEOM::GEOM_Object>( entry );
99 //================================================================================
101 * \brief Find GEOM_Object in the tree
103 //================================================================================
105 QTreeWidgetItem* findSolidInTree( const GEOM::GEOM_Object_var& go, QTreeWidget* tree )
107 if ( go->_is_nil() || !tree )
110 for ( int i = 0; i < tree->topLevelItemCount(); ++i )
112 QTreeWidgetItem* item = tree->topLevelItem( i );
113 if ( item->data( 1, Qt::UserRole ).toString() == SMESH::toQStr( go->GetStudyEntry() ))
120 //================================================================================
122 * \brief StdMeshersGUI_BlockRenumberCreator constructor
124 //================================================================================
126 StdMeshersGUI_BlockRenumberCreator::StdMeshersGUI_BlockRenumberCreator(const QString& aHypType)
127 : StdMeshersGUI_StdHypothesisCreator( aHypType ),
128 mySolidFilter( "", TopAbs_SOLID, 1, TopAbs_SOLID ),
129 myVertexFilter( "", TopAbs_VERTEX, 1, TopAbs_VERTEX )
133 //================================================================================
135 * \brief StdMeshersGUI_BlockRenumberCreator destructor
137 //================================================================================
139 StdMeshersGUI_BlockRenumberCreator::~StdMeshersGUI_BlockRenumberCreator()
141 SMESHGUI::selectionMgr()->clearFilters();
144 //================================================================================
146 * \brief Create widgets
148 //================================================================================
150 QFrame* StdMeshersGUI_BlockRenumberCreator::buildFrame()
152 QFrame* fr = new QFrame();
153 //fr->setMinimumWidth(460);
155 QGridLayout* frLayout = new QGridLayout( fr );
156 frLayout->setSpacing( SPACING );
157 frLayout->setMargin( MARGIN );
164 myName = new QLineEdit( fr );
165 frLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), fr ), row, 0 );
166 frLayout->addWidget( myName, row, 1 );
170 QGroupBox* groupBox = new QGroupBox( tr( "BLOCK_CS_GROUPBOX" ), fr );
171 frLayout->addWidget( groupBox, row, 0, 1, 2 );
173 QGridLayout* layout = new QGridLayout( groupBox );
174 layout->setSpacing( SPACING );
175 layout->setMargin( MARGIN );
176 layout->setColumnStretch( 0, 1 );
180 myBlockTree = new QTreeWidget( groupBox );
181 myBlockTree->setColumnCount( 2 );
182 myBlockTree->header()->hide();
183 // myBlockTree->setHeaderLabels( QStringList()
184 // << tr("COLUMN_BLOCK") << tr("COLUMN_V000") << tr("COLUMN_V001"));
185 layout->addWidget( myBlockTree, row, 0, row + 7, 1 );
188 myButGroup = new QButtonGroup( groupBox );
189 myButGroup->setExclusive( true );
190 QLabel* label[3] = { new QLabel( tr( "SOLID" ), groupBox ),
191 new QLabel( tr( "V000" ), groupBox ),
192 new QLabel( tr( "V001" ), groupBox ) };
193 QIcon icon( SMESHGUI::resourceMgr()->loadPixmap("SMESH", tr("ICON_SELECT")));
194 for ( int i = 0; i < 3; ++i )
196 myShapeSelectBut[i] = new QPushButton( icon, "", groupBox );
197 myLineEdit [i] = new QLineEdit( groupBox );
198 layout->addWidget( label[i], row, 1 );
199 layout->addWidget( myShapeSelectBut[i], row, 2 );
200 layout->addWidget( myLineEdit[i], row, 3 );
201 myShapeSelectBut[i]->setCheckable( true );
202 myButGroup->addButton( myShapeSelectBut[i], i );
207 myAddBut = new QPushButton( tr("ADD"), groupBox );
208 myModifBut = new QPushButton( tr("MODIFY"), groupBox );
209 myRemoveBut = new QPushButton( tr("SMESH_REMOVE"), groupBox );
210 layout->addWidget( myAddBut, row, 1, 1, 3 );
212 layout->addWidget( myModifBut, row, 1, 1, 3 );
214 layout->addWidget( myRemoveBut, row, 1, 1, 3 );
216 layout->setRowStretch( row, 1 );
218 LightApp_SelectionMgr* selMgr = SMESH::GetSelectionMgr( SMESHGUI::GetSMESHGUI() );
220 connect( selMgr, SIGNAL( currentSelectionChanged()), SLOT( onSelectionChange()));
221 connect( myBlockTree, SIGNAL( itemSelectionChanged() ), SLOT( onTreeSelectionChange()));
222 connect( myShapeSelectBut[0], SIGNAL( clicked(bool)), SLOT( onSelectBtnClick() ));
223 connect( myShapeSelectBut[1], SIGNAL( clicked(bool)), SLOT( onSelectBtnClick() ));
224 connect( myShapeSelectBut[2], SIGNAL( clicked(bool)), SLOT( onSelectBtnClick() ));
225 connect( myAddBut, SIGNAL( clicked() ), SLOT( onAddBtnClick() ));
226 connect( myModifBut, SIGNAL( clicked() ), SLOT( onModifBtnClick() ));
227 connect( myRemoveBut, SIGNAL( clicked() ), SLOT( onRemoveBtnClick() ));
232 //================================================================================
234 * \brief Set myGO's to the tree
236 //================================================================================
238 void StdMeshersGUI_BlockRenumberCreator::setBlockToTree(QTreeWidgetItem* solidItem)
240 if ( !myGO[0]->_is_nil() && !myGO[1]->_is_nil() && !myGO[2]->_is_nil() )
242 if ( !solidItem ) solidItem = new QTreeWidgetItem( myBlockTree );
243 solidItem->setExpanded( true );
244 QTreeWidgetItem* item = solidItem;
246 QFont boldFont = item->font( 0 );
247 boldFont.setBold( true );
249 QString entry, label[3] = { tr("BLOCK_LABEL"), tr("V000_LABEL"), tr("V001_LABEL") };
250 for ( int i = 0; i < 3; ++i )
252 if ( i > 0 && ! (item = solidItem->child( i - 1 )))
253 item = new QTreeWidgetItem( solidItem );
255 item->setText( 0, label[i] );
256 item->setText( 1, toText( myGO[i], label[i], entry ));
257 item->setData( 1, Qt::UserRole, entry );
258 item->setFont( 1, boldFont );
259 item->setToolTip( 1, entry );
261 myBlockTree->resizeColumnToContents( 0 );
262 myBlockTree->resizeColumnToContents( 1 );
264 for ( int i = 0; i < 3; ++i )
266 myGO[i] = GEOM::GEOM_Object::_nil();
267 myLineEdit[i]->setText("");
271 //================================================================================
273 * \brief Transfer parameters from hypothesis to widgets
275 //================================================================================
277 void StdMeshersGUI_BlockRenumberCreator::retrieveParams() const
279 StdMeshersGUI_BlockRenumberCreator* me = const_cast<StdMeshersGUI_BlockRenumberCreator*>( this );
281 StdMeshers::StdMeshers_BlockRenumber_var h =
282 StdMeshers::StdMeshers_BlockRenumber::_narrow( initParamsHypothesis() );
285 me->myName->setText( hypName() );
287 me->myBlockTree->clear();
289 StdMeshers::blockcs_array_var blkArray = h->GetBlocksOrientation();
290 for ( CORBA::ULong i = 0; i < blkArray->length(); ++i )
292 me->myGO[0] = GEOM::GEOM_Object::_duplicate( blkArray[i].solid.in() );
293 me->myGO[1] = GEOM::GEOM_Object::_duplicate( blkArray[i].vertex000.in() );
294 me->myGO[2] = GEOM::GEOM_Object::_duplicate( blkArray[i].vertex001.in() );
295 me->setBlockToTree();
298 me->myShapeSelectBut[0]->click();
303 //================================================================================
305 * \brief Transfer parameters from widgets to hypothesis
307 //================================================================================
309 QString StdMeshersGUI_BlockRenumberCreator::storeParams() const
311 StdMeshers::StdMeshers_BlockRenumber_var h =
312 StdMeshers::StdMeshers_BlockRenumber::_narrow( hypothesis() );
317 SMESH::SetName( SMESH::FindSObject( h ), myName->text().toUtf8().constData() );
319 StdMeshers::blockcs_array_var array = new StdMeshers::blockcs_array();
320 array->length( myBlockTree->topLevelItemCount() );
322 for ( int i = 0; i < myBlockTree->topLevelItemCount(); ++i )
324 StdMeshers::BlockCS& bcs = array[i];
325 QTreeWidgetItem* item0 = myBlockTree->topLevelItem( i );
326 QTreeWidgetItem* item1 = item0->child( 0 );
327 QTreeWidgetItem* item2 = item0->child( 1 );
329 bcs.solid = toGeom( item0->data( 1, Qt::UserRole ).toString() )._retn();
330 bcs.vertex000 = toGeom( item1->data( 1, Qt::UserRole ).toString() )._retn();
331 bcs.vertex001 = toGeom( item2->data( 1, Qt::UserRole ).toString() )._retn();
333 h->SetBlocksOrientation( array );
336 catch(const SALOME::SALOME_Exception& ex)
338 SalomeApp_Tools::QtCatchCorbaException(ex);
343 //================================================================================
345 * \brief Take selected object
347 //================================================================================
349 void StdMeshersGUI_BlockRenumberCreator::onSelectionChange()
352 SMESHGUI::GetSMESHGUI()->selectionMgr()->selectedObjects( list );
354 int shapeID = myButGroup->checkedId();
355 if ( shapeID < 0 || shapeID > 2 )
358 myGO[ shapeID ] = GEOM::GEOM_Object::_nil();
359 myLineEdit[ shapeID ]->clear();
361 if ( list.IsEmpty() )
364 Handle(SALOME_InteractiveObject) io = list.First();
365 if ( !io->hasEntry() )
368 myGO[ shapeID ] = SMESH::IObjectToInterface<GEOM::GEOM_Object>( io );
370 if ( !myGO[ shapeID ]->_is_nil() )
371 myLineEdit[ shapeID ]->setText( SMESH::toQStr( myGO[ shapeID ]->GetName() ));
376 //================================================================================
378 * \brief Display selected block CS in myLineEdit's
380 //================================================================================
382 void StdMeshersGUI_BlockRenumberCreator::onTreeSelectionChange()
384 QList<QTreeWidgetItem *> items = myBlockTree->selectedItems();
385 for ( QTreeWidgetItem* item : items )
387 if ( item->parent() )
388 item = item->parent();
390 QTreeWidgetItem* items[3] = { item, item->child( 0 ), item->child( 1 ) };
391 if ( items[1] && items[2] )
392 for ( int i = 0; i < 3; ++i )
394 myGO[i] = toGeom( items[i]->data( 1, Qt::UserRole ).toString() );
395 myLineEdit[i]->setText( items[i]->text( 1 ));
404 //================================================================================
406 * \brief Activate/deactivate buttons
408 //================================================================================
410 void StdMeshersGUI_BlockRenumberCreator::updateButtons()
412 bool isSolidInTree = findSolidInTree( myGO[0], myBlockTree );
413 myAddBut ->setEnabled( !isSolidInTree &&
414 !myGO[0]->_is_nil() && !myGO[1]->_is_nil() && !myGO[2]->_is_nil() );
415 myModifBut ->setEnabled( isSolidInTree );
416 myRemoveBut->setEnabled( !myBlockTree->selectedItems().isEmpty() ||
417 myBlockTree->topLevelItemCount() == 1 );
420 //================================================================================
422 * \brief Install filter upon activation of another object selection
424 //================================================================================
426 void StdMeshersGUI_BlockRenumberCreator::onSelectBtnClick()
428 int shapeID = myButGroup->checkedId();
429 LightApp_SelectionMgr* selMgr = SMESHGUI::selectionMgr();
430 selMgr->clearFilters();
431 selMgr->installFilter( shapeID > 0 ? &myVertexFilter : &mySolidFilter );
434 //================================================================================
436 * \brief Add shapes to the tree
438 //================================================================================
440 void StdMeshersGUI_BlockRenumberCreator::onAddBtnClick()
446 //================================================================================
448 * \brief Modify a current block
450 //================================================================================
452 void StdMeshersGUI_BlockRenumberCreator::onModifBtnClick()
454 if ( QTreeWidgetItem* item = findSolidInTree( myGO[0], myBlockTree ))
456 setBlockToTree( item );
461 //================================================================================
463 * \brief Remove selected block from the tree
465 //================================================================================
467 void StdMeshersGUI_BlockRenumberCreator::onRemoveBtnClick()
469 QList<QTreeWidgetItem *> items = myBlockTree->selectedItems();
470 if ( items.isEmpty() && myBlockTree->topLevelItemCount() == 1 )
471 items.push_back( myBlockTree->topLevelItem( 0 ));
473 for ( QTreeWidgetItem* item : items )
475 if ( item->parent() )
476 item = item->parent();
484 //================================================================================
486 * \brief Validate parameters
488 //================================================================================
490 bool StdMeshersGUI_BlockRenumberCreator::checkParams( QString& /*msg*/ ) const
495 //================================================================================
497 * \brief Returns a name of help page
499 //================================================================================
501 QString StdMeshersGUI_BlockRenumberCreator::helpPage() const
503 return "3d_meshing_hypo.html#renumber-hypothesis";
506 //================================================================================
510 //================================================================================
512 QString StdMeshersGUI_BlockRenumberCreator::hypTypeName( const QString& ) const
514 return "BLOCK_RENUMBER";