1 // Copyright (C) 2007-2011 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.
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
23 // File : StdMeshersGUI_CartesianParamCreator.cxx
24 // Author : Open CASCADE S.A.S.
27 #include "StdMeshersGUI_CartesianParamCreator.h"
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32 #include <SMESHGUI_SpinBox.h>
35 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
37 // SALOME GUI includes
38 #include <SalomeApp_Tools.h>
39 #include <SalomeApp_IntSpinBox.h>
40 #include <QtxComboBox.h>
43 #include <QAbstractItemModel>
44 #include <QApplication>
45 #include <QButtonGroup>
46 #include <QGridLayout>
48 #include <QHBoxLayout>
51 #include <QListWidget>
52 #include <QModelIndex>
53 #include <QRadioButton>
55 #include <QStyleOptionViewItem>
56 #include <QTreeWidget>
57 #include <QTreeWidgetItem>
58 #include <QVBoxLayout>
59 #include <QPushButton>
65 namespace StdMeshersGUI
67 enum { COORD_BUT = 0, SPACING_BUT };
69 //================================================================================
71 * \brief get spacing definition from a tree item
73 //================================================================================
75 void getFromItem(QTreeWidgetItem * item, double& t0, double& t1, QString& fun )
79 t0 = item->text( 0 ).split(' ')[0].toDouble();
80 t1 = item->data( 0, Qt::UserRole ).toDouble();
81 fun = item->text( 1 );
85 //================================================================================
87 * \brief set spacing definition to a tree item
89 //================================================================================
91 QTreeWidgetItem* setToItem(double t0, double t1, const QString& fun, QTreeWidgetItem * item)
93 if ( !item ) item = new QTreeWidgetItem;
94 item->setText( 0, QString( "%1 - %2" ).arg( t0 ).arg( t1 ));
95 item->setData( 0, Qt::UserRole, t1 );
96 item->setText( 1, fun );
97 item->setFlags( item->flags() | Qt::ItemIsEditable );
101 //================================================================================
103 * \brief Retrieves coordinate value from a list item
105 //================================================================================
107 double coordFromItem( QListWidgetItem * item )
109 return item ? item->data( Qt::UserRole ).toDouble() : 0;
112 //================================================================================
114 * \brief Sets coordinate value to a list item
116 //================================================================================
118 QListWidgetItem* coordToItem( double coord, QListWidgetItem * item )
120 if ( !item ) item = new QListWidgetItem;
121 item->setText( QString::number( coord ));
122 item->setData( Qt::UserRole, coord );
123 item->setFlags( item->flags() | Qt::ItemIsEditable );
127 //================================================================================
130 * \param theParent - Parent widget for this tab
132 * Makes tab's look and feel
134 //================================================================================
136 GridAxisTab::GridAxisTab( QWidget* theParent,const int axisIndex ):
137 QFrame( theParent ), myAxisIndex( axisIndex )
139 // 1) Grid definition mode
140 myModeGroup = new QButtonGroup( this );
141 QGroupBox* modeBox = new QGroupBox( tr( "GRID_DEF_MODE" ), this );
142 QHBoxLayout* modeLay = new QHBoxLayout( modeBox );
143 modeLay->setMargin( MARGIN );
144 modeLay->setSpacing( SPACING );
146 QRadioButton* coordModeBtn = new QRadioButton( tr( "SMESH_COORDINATES" ), modeBox );
147 QRadioButton* spacModeBtn = new QRadioButton( tr( "SPACING" ), modeBox );
149 modeLay->addWidget( coordModeBtn );
150 modeLay->addWidget( spacModeBtn );
151 myModeGroup->addButton( coordModeBtn, COORD_BUT );
152 myModeGroup->addButton( spacModeBtn, SPACING_BUT );
155 myInsertBtn = new QPushButton( tr("INSERT"), this);
156 myDeleteBtn = new QPushButton( tr("SMESH_BUT_DELETE"), this);
158 myStepLabel = new QLabel( tr("COORD_STEP"));
159 myStepSpin = new SMESHGUI_SpinBox( this );
160 myStepSpin->setAcceptNames( false ); // No Notebook variables allowed
161 myStepSpin->RangeStepAndValidator();
162 myStepSpin->SetStep( 1. );
163 myStepSpin->SetValue( myStep = 1. );
165 // 3) Coodrinates/Spacing group
166 QFrame* csFrame = new QFrame( this );
167 QVBoxLayout* scLay = new QVBoxLayout( csFrame );
168 scLay->setMargin( 0 );
169 scLay->setSpacing( SPACING );
172 mySpacingTreeWdg = new QTreeWidget( csFrame );
173 mySpacingTreeWdg->setColumnCount(2);
174 mySpacingTreeWdg->setHeaderLabels( QStringList() << tr( "SMESH_RANGE" ) << QString( "f(t)" ));
175 mySpacingTreeWdg->setColumnWidth( 1, 40 );
176 mySpacingTreeWdg->setColumnWidth( 2, 30 );
177 mySpacingTreeWdg->setItemDelegate( new LineDelegate( mySpacingTreeWdg ));
178 scLay->addWidget( mySpacingTreeWdg );
181 myCoordList = new QListWidget( csFrame );
182 myCoordList->setItemDelegate( new LineDelegate( myCoordList ));
183 scLay->addWidget( myCoordList );
187 QGridLayout* axisTabLayout = new QGridLayout( this );
188 axisTabLayout->setMargin( MARGIN );
189 axisTabLayout->setSpacing( SPACING );
191 axisTabLayout->addWidget( modeBox , 0, 0, 1, 3 );
192 axisTabLayout->addWidget( myInsertBtn , 1, 0, 1, 2 );
193 axisTabLayout->addWidget( myDeleteBtn , 2, 0, 1, 2 );
194 axisTabLayout->addWidget( myStepLabel, 3, 0 );
195 axisTabLayout->addWidget( myStepSpin , 3, 1 );
196 axisTabLayout->addWidget( csFrame , 1, 2, 4, 1 );
198 axisTabLayout->setRowStretch( 4, 1 );
201 connect( myInsertBtn, SIGNAL( clicked() ), SLOT( onInsert() ));
202 connect( myDeleteBtn, SIGNAL( clicked() ), SLOT( onDelete() ));
203 connect( myModeGroup, SIGNAL( buttonClicked ( int )), SLOT( onMode(int)));
204 connect( mySpacingTreeWdg, SIGNAL( itemSelectionChanged()), SLOT( updateButtons() ));
205 connect( myCoordList, SIGNAL( itemSelectionChanged()), SLOT( updateButtons() ));
206 connect( myStepSpin, SIGNAL( valueChanged(double)), SLOT( onStepChange() ));
209 void GridAxisTab::onInsert()
211 if ( isGridBySpacing() )
213 QTreeWidgetItem * item = mySpacingTreeWdg->currentItem();
214 if ( !item ) item = mySpacingTreeWdg->topLevelItem( 0 );
215 int i = mySpacingTreeWdg->indexOfTopLevelItem( item );
217 double t0, t1; QString fun;
218 getFromItem( item, t0, t1, fun );
219 double t = 0.5 * ( t0 + t1 );
220 setToItem( t0, t, fun, item );
222 item = setToItem( t, t1, fun );
223 if ( i == mySpacingTreeWdg->topLevelItemCount()-1 )
224 mySpacingTreeWdg->addTopLevelItem( item );
226 mySpacingTreeWdg->insertTopLevelItem( i+1, item );
227 mySpacingTreeWdg->setCurrentItem( item );
231 if ( myCoordList->count() == 0 )
233 myCoordList->addItem( coordToItem( 0 ));
237 double coord = coordFromItem( myCoordList->currentItem() ) + myStep;
238 int i = myCoordList->currentRow();
239 while ( i > 0 && coordFromItem( myCoordList->item( i-1 )) > coord )
241 while ( i < myCoordList->count() && coordFromItem( myCoordList->item( i )) < coord )
243 const double tol = 1e-6;
245 ( i < myCoordList->count() && coordFromItem( myCoordList->item( i )) - coord < tol ) ||
246 ( i > 0 && coord - coordFromItem( myCoordList->item( i-1 )) < tol );
248 myCoordList->insertItem( i, coordToItem( coord ));
249 else if ( myStep < 0 )
251 myCoordList->setCurrentRow( i );
257 void GridAxisTab::onDelete()
259 if ( isGridBySpacing() )
261 QList<QTreeWidgetItem *> selItems = mySpacingTreeWdg->selectedItems();
262 QTreeWidgetItem * item;
263 foreach ( item, selItems )
265 int i = mySpacingTreeWdg->indexOfTopLevelItem( item );
266 if ( i == 0 ) continue;
267 QTreeWidgetItem* prevItem = mySpacingTreeWdg->topLevelItem( i-1 );
269 double t0, t1, t2; QString fun;
270 getFromItem( item, t1, t2, fun );
271 getFromItem( prevItem, t0, t1, fun );
274 setToItem( t0, t2, fun, prevItem );
279 if ( myCoordList->count() > 2 )
280 if ( QListWidgetItem * item = myCoordList->currentItem() )
286 void GridAxisTab::onMode(int isSpacing)
288 mySpacingTreeWdg->setShown( isSpacing );
289 myCoordList->setShown( !isSpacing );
290 myStepSpin->setShown( !isSpacing );
291 myStepLabel->setShown( !isSpacing );
294 if ( mySpacingTreeWdg->topLevelItemCount() == 0 )
296 QString spacing( "1" );
297 if ( myCoordList->count() > 1 )
299 double c1 = coordFromItem( myCoordList->item( 1 ));
300 double c0 = coordFromItem( myCoordList->item( 0 ));
301 spacing = QString::number( c1 - c0 );
303 mySpacingTreeWdg->addTopLevelItem( setToItem( 0., 1., spacing ) );
305 myCoordList->clear();
309 mySpacingTreeWdg->clear();
310 if ( myCoordList->count() == 0 )
311 myCoordList->addItem( coordToItem( 0 ));
316 void GridAxisTab::onStepChange()
318 if ( fabs( myStepSpin->GetValue() ) < 1e-100 )
320 double delta = myStepSpin->singleStep() * ( myStep > myStepSpin->GetValue() ? -1 : +1 );
321 myStepSpin->SetValue( myStepSpin->GetValue() + delta );
323 myStep = myStepSpin->GetValue();
326 void GridAxisTab::updateButtons()
328 bool insertEnable = false, deleteEnable = false;
329 if ( isGridBySpacing() )
332 const int nbSelected = mySpacingTreeWdg->selectedItems().count();
333 if ( nbSelected > 0 )
335 // we delete a current range by uniting it with the previous
336 int i = mySpacingTreeWdg->indexOfTopLevelItem( mySpacingTreeWdg->currentItem() );
337 deleteEnable = ( i > 0 );
342 const int nbSelected = myCoordList->selectedItems().count();
343 insertEnable = ( nbSelected || myCoordList->count() < 2 );
344 deleteEnable = ( nbSelected && myCoordList->count() > 2 );
346 myInsertBtn->setEnabled( insertEnable );
347 myDeleteBtn->setEnabled( deleteEnable );
350 void GridAxisTab::setCoordinates( SMESH::double_array_var coords )
352 myCoordList->clear();
353 for ( size_t i = 0; i < coords->length(); ++i )
354 myCoordList->addItem( coordToItem( coords[i] ));
356 myModeGroup->button( COORD_BUT )->setChecked( true );
360 void GridAxisTab::setSpacing( SMESH::string_array_var funs, SMESH::double_array_var points )
362 mySpacingTreeWdg->clear();
363 if ( funs->length() == points->length() - 1 )
365 for ( size_t i = 1; i < points->length(); ++i )
366 mySpacingTreeWdg->addTopLevelItem
367 ( setToItem( points[i-1], points[i], (const char*) funs[i-1] ));
369 myModeGroup->button( SPACING_BUT )->setChecked( true );
370 onMode( SPACING_BUT );
373 bool GridAxisTab::isGridBySpacing() const
375 return ( myModeGroup->checkedId() == SPACING_BUT );
378 SMESH::double_array* GridAxisTab::getCoordinates()
380 SMESH::double_array_var coords = new SMESH::double_array;
381 coords->length( myCoordList->count() );
382 for ( size_t i = 0; i < coords->length(); ++i )
383 coords[i] = coordFromItem( myCoordList->item( i ) );
385 return coords._retn();
388 void GridAxisTab::getSpacing(SMESH::string_array_out funs,
389 SMESH::double_array_out points) const
391 funs = new SMESH::string_array();
392 points = new SMESH::double_array();
393 funs->length( mySpacingTreeWdg->topLevelItemCount() );
394 points->length( mySpacingTreeWdg->topLevelItemCount() + 1 );
395 double t0, t1; QString fun;
396 for ( size_t i = 0; i < funs->length(); ++i )
398 QTreeWidgetItem* item = mySpacingTreeWdg->topLevelItem( i );
399 getFromItem( item, t0, t1, fun );
401 funs[i] = fun.toLatin1().constData();
403 points[ points->length()-1 ] = 1.0;
407 bool GridAxisTab::checkParams(QString& msg, SMESH::SMESH_Hypothesis_var& hyp) const
409 if ( isGridBySpacing() )
411 if ( mySpacingTreeWdg->topLevelItemCount() == 0 )
412 return false; // how could it be?
413 StdMeshers::StdMeshers_CartesianParameters3D_var h =
414 StdMeshers::StdMeshers_CartesianParameters3D::_narrow( hyp );
415 SMESH::string_array_var funs;
416 SMESH::double_array_var points;
417 getSpacing( funs.out(), points.out() );
419 const char* axisName[3] = { "X", "Y", "Z" };
420 SMESH::double_array_var coords =
421 h->ComputeCoordinates(0.,1., funs, points, axisName[ myAxisIndex ]);
423 catch ( const SALOME::SALOME_Exception& ex ) {
424 msg = (const char*) ex.details.text;
430 return myCoordList->count() > 1;
435 LineDelegate::LineDelegate( QWidget* parent ):
436 QItemDelegate( parent ),
437 mySpacingTreeWdg( qobject_cast<QTreeWidget*>( parent )),
438 myCoordList( qobject_cast<QListWidget*>( parent ))
442 QWidget* LineDelegate::createEditor( QWidget* parent,
443 const QStyleOptionViewItem& opt,
444 const QModelIndex& index) const
447 if ( mySpacingTreeWdg )
449 if ( index.column() == 0 &&
450 index.row() != mySpacingTreeWdg->topLevelItemCount()-1 )
452 SMESHGUI_SpinBox* sb = new SMESHGUI_SpinBox( parent );
453 sb->setAcceptNames( false ); // No Notebook variables allowed
454 sb->setFrame( false );
457 if ( index.column() == 1 ) {
458 w = new QLineEdit( parent );
463 SMESHGUI_SpinBox* sb = new SMESHGUI_SpinBox( parent );
464 sb->setAcceptNames( false ); // No Notebook variables allowed
465 sb->setFrame( false );
466 const double tol = 1e-5;
467 double from = index.row() ? coordFromItem( myCoordList->item( index.row()-1 ))+tol : -1e+6;
468 double to = index.row() == myCoordList->count()-1 ? 1e+6 : coordFromItem( myCoordList->item( index.row()+1 ))-tol;
469 sb->RangeStepAndValidator( from, to, 0.01 );
475 void LineDelegate::setEditorData ( QWidget * editor, const QModelIndex & index ) const
477 if ( mySpacingTreeWdg && index.column() == 0 )
479 double t0, t1, t2=1.0; QString fun;
480 QTreeWidgetItem* item = mySpacingTreeWdg->topLevelItem( index.row() );
481 getFromItem( item, t0, t1, fun );
482 if ( index.row() != mySpacingTreeWdg->topLevelItemCount()-1 )
484 item = mySpacingTreeWdg->topLevelItem( index.row()+1 );
485 getFromItem( item, t1, t2, fun );
487 const double tol = 1e-3;
488 SMESHGUI_SpinBox* sb = qobject_cast<SMESHGUI_SpinBox*>( editor );
489 sb->RangeStepAndValidator( t0 + tol, t2 - tol, 0.01 );
494 QItemDelegate::setEditorData( editor, index );
497 void LineDelegate::setModelData( QWidget* editor,
498 QAbstractItemModel* model,
499 const QModelIndex& index ) const
501 if ( mySpacingTreeWdg )
503 if ( index.column() == 0 )
505 if ( index.row() != mySpacingTreeWdg->topLevelItemCount()-1 )
507 SMESHGUI_SpinBox* sb = qobject_cast<SMESHGUI_SpinBox*>( editor );
508 double t0, t1, t = sb->GetValue(); QString fun;
510 QTreeWidgetItem* item = mySpacingTreeWdg->topLevelItem( index.row() );
511 getFromItem( item, t0, t1, fun );
512 setToItem( t0, t, fun, item );
514 item = mySpacingTreeWdg->topLevelItem( index.row() + 1 );
515 getFromItem( item, t0, t1, fun );
516 setToItem( t, t1, fun, item );
519 else if ( !qobject_cast<QLineEdit*>(editor)->text().trimmed().isEmpty() )
521 QItemDelegate::setModelData( editor, model, index );
526 SMESHGUI_SpinBox* sb = qobject_cast<SMESHGUI_SpinBox*>( editor );
527 coordToItem( sb->GetValue(), myCoordList->item( index.row() ));
531 } // namespace StdMeshersGUI
534 StdMeshersGUI_CartesianParamCreator::StdMeshersGUI_CartesianParamCreator(const QString& aHypType)
535 : StdMeshersGUI_StdHypothesisCreator( aHypType ),
543 StdMeshersGUI_CartesianParamCreator::~StdMeshersGUI_CartesianParamCreator()
545 if ( myAxisTabs[0] ) delete myAxisTabs[0];
546 if ( myAxisTabs[1] ) delete myAxisTabs[1];
547 if ( myAxisTabs[2] ) delete myAxisTabs[2];
553 bool StdMeshersGUI_CartesianParamCreator::checkParams( QString& msg ) const
555 if( !SMESHGUI_GenericHypothesisCreator::checkParams( msg ) )
558 if ( myName && myName->text().trimmed().isEmpty() )
560 msg = tr("SMESH_WRN_EMPTY_NAME");
563 if ( ! myThreshold->isValid( msg, true ))
566 SMESH::SMESH_Hypothesis_var hyp = hypothesis();
567 if ( !myAxisTabs[0]->checkParams( msg, hyp )) return false;
568 if ( !myAxisTabs[1]->checkParams( msg, hyp )) return false;
569 if ( !myAxisTabs[2]->checkParams( msg, hyp )) return false;
574 QFrame* StdMeshersGUI_CartesianParamCreator::buildFrame()
576 QFrame* fr = new QFrame();
577 //fr->setMinimumWidth(460);
579 QVBoxLayout* lay = new QVBoxLayout( fr );
581 lay->setSpacing( SPACING );
583 QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
584 lay->addWidget( GroupC1 );
586 StdMeshers::StdMeshers_NumberOfSegments_var h =
587 StdMeshers::StdMeshers_NumberOfSegments::_narrow( hypothesis() );
589 QGridLayout* argGroupLayout = new QGridLayout( GroupC1 );
590 argGroupLayout->setSpacing( SPACING );
591 argGroupLayout->setMargin( MARGIN );
592 argGroupLayout->setColumnStretch( 0, 0 );
593 argGroupLayout->setColumnStretch( 1, 1 );
600 myName = new QLineEdit( GroupC1 );
601 argGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
602 argGroupLayout->addWidget( myName, row, 1 );
607 argGroupLayout->addWidget( new QLabel( tr( "THRESHOLD" ), GroupC1 ), row, 0 );
608 myThreshold = new SMESHGUI_SpinBox( GroupC1 );
609 myThreshold->setAcceptNames( false ); // No Notebook variables allowed
610 myThreshold->RangeStepAndValidator( 1.1, 1e+10, 1., "length_precision" );
611 argGroupLayout->addWidget( myThreshold, row, 1 );
614 // 2) Grid definition
615 QTabWidget* tabWdg = new QTabWidget( fr );
616 myAxisTabs[ 0 ] = new StdMeshersGUI::GridAxisTab( tabWdg, 0 );
617 myAxisTabs[ 1 ] = new StdMeshersGUI::GridAxisTab( tabWdg, 1 );
618 myAxisTabs[ 2 ] = new StdMeshersGUI::GridAxisTab( tabWdg, 2 );
619 tabWdg->addTab( myAxisTabs[ 0 ], tr( "AXIS_X" ) );
620 tabWdg->addTab( myAxisTabs[ 1 ], tr( "AXIS_Y" ) );
621 tabWdg->addTab( myAxisTabs[ 2 ], tr( "AXIS_Z" ) );
622 argGroupLayout->addWidget( tabWdg, row, 0, 1, 2 );
627 void StdMeshersGUI_CartesianParamCreator::retrieveParams() const
629 StdMeshers::StdMeshers_CartesianParameters3D_var h =
630 StdMeshers::StdMeshers_CartesianParameters3D::_narrow( initParamsHypothesis() );
633 myName->setText( hypName() );
635 myThreshold->setValue( h->GetSizeThreshold() );
637 for ( int ax = 0; ax < 3; ++ax )
639 if ( h->IsGridBySpacing( ax ))
641 SMESH::string_array_var funs;
642 SMESH::double_array_var intPoints;
643 h->GetGridSpacing( funs.out(), intPoints.out(), ax );
644 myAxisTabs[ax]->setSpacing( funs, intPoints );
648 SMESH::double_array_var coords = h->GetGrid( ax );
649 myAxisTabs[ax]->setCoordinates( coords );
653 dlg()->setMinimumSize( dlg()->minimumSizeHint().width(), dlg()->minimumSizeHint().height() );
656 QString StdMeshersGUI_CartesianParamCreator::storeParams() const
658 StdMeshers::StdMeshers_CartesianParameters3D_var h =
659 StdMeshers::StdMeshers_CartesianParameters3D::_narrow( hypothesis() );
664 SMESH::SetName( SMESH::FindSObject( h ), myName->text().toLatin1().constData() );
666 h->SetSizeThreshold( myThreshold->text().toDouble() );
668 for ( int ax = 0; ax < 3; ++ax )
670 if ( myAxisTabs[ax]->isGridBySpacing())
672 SMESH::double_array_var intPoints;
673 SMESH::string_array_var funs;
674 myAxisTabs[ax]->getSpacing( funs.out(), intPoints.out() );
675 h->SetGridSpacing( funs, intPoints, ax );
679 SMESH::double_array_var coords = myAxisTabs[ax]->getCoordinates();
680 h->SetGrid( coords, ax );
684 catch(const SALOME::SALOME_Exception& ex)
686 SalomeApp_Tools::QtCatchCorbaException(ex);
691 QString StdMeshersGUI_CartesianParamCreator::helpPage() const
693 return "cartesian_algo_page.html#cartesian_hyp_anchor";