1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : StdMeshersGUI_FixedPointsParamWdg.cxx
21 // Author : Open CASCADE S.A.S.
24 #include "StdMeshersGUI_FixedPointsParamWdg.h"
26 #include <SMESHGUI_SpinBox.h>
28 #include <SalomeApp_IntSpinBox.h>
31 #include <QPushButton>
32 #include <QIntValidator>
33 #include <QGridLayout>
34 #include <QListWidget>
35 #include <QListWidgetItem>
36 #include <QItemDelegate>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItem>
41 #include <QItemDelegate>
46 #define SAME_TEXT "-/-"
48 #define TOLERANCE 1e-7
49 #define EQUAL_DBL(a,b) (fabs(a-b)<TOLERANCE)
50 #define LT_DBL(a,b) ((a<b)&&!EQUAL_DBL(a,b))
51 #define GT_DBL(a,b) ((a>b)&&!EQUAL_DBL(a,b))
54 * class : Tree Widget Item Delegate
55 * purpose : Custom item delegate
58 class StdMeshersGUI_FixedPointsParamWdg::LineDelegate : public QItemDelegate
61 LineDelegate( QTreeWidget* );
64 QWidget* createEditor( QWidget*, const QStyleOptionViewItem&, const QModelIndex& ) const;
65 void setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const;
68 QTreeWidget* myTreeWidget;
71 StdMeshersGUI_FixedPointsParamWdg::LineDelegate::LineDelegate( QTreeWidget* parent )
72 : QItemDelegate( parent ),
73 myTreeWidget( parent )
77 QWidget* StdMeshersGUI_FixedPointsParamWdg::LineDelegate::createEditor( QWidget* parent,
78 const QStyleOptionViewItem& option,
79 const QModelIndex& index ) const
82 if ( (index.column() == 1 ) ) {
83 SalomeApp_IntSpinBox* sb = new SalomeApp_IntSpinBox( parent );
84 sb->setAcceptNames( false ); // No Notebook variables allowed
85 sb->setFrame( false );
86 sb->setRange( 1, 999);
93 void StdMeshersGUI_FixedPointsParamWdg::LineDelegate::setModelData( QWidget* editor,
94 QAbstractItemModel* model,
95 const QModelIndex& index ) const
97 model->setData( index, qobject_cast<SalomeApp_IntSpinBox*>( editor )->value(), Qt::EditRole );
98 model->setData( index, qobject_cast<SalomeApp_IntSpinBox*>( editor )->value(), Qt::UserRole );
101 //================================================================================
105 //================================================================================
107 StdMeshersGUI_FixedPointsParamWdg
108 ::StdMeshersGUI_FixedPointsParamWdg( QWidget * parent ):
111 QGridLayout* edgesLayout = new QGridLayout( this );
112 edgesLayout->setMargin( MARGIN );
113 edgesLayout->setSpacing( SPACING );
115 myListWidget = new QListWidget( this );
116 myTreeWidget = new QTreeWidget( this );
117 mySpinBox = new SMESHGUI_SpinBox( this );
118 myAddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), this );
119 myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );
120 mySameValues = new QCheckBox( tr("SMESH_SAME_NB_SEGMENTS"), this);
122 myListWidget->setSelectionMode( QListWidget::ExtendedSelection );
124 myTreeWidget->setColumnCount(2);
125 myTreeWidget->setHeaderLabels( QStringList() << tr( "SMESH_RANGE" ) << tr( "SMESH_NB_SEGMENTS" ) );
126 myTreeWidget->setColumnWidth( 1, 40 );
127 myTreeWidget->setColumnWidth( 2, 30 );
128 myTreeWidget->setItemDelegate( new LineDelegate( myTreeWidget ) );
130 edgesLayout->addWidget(myListWidget, 0, 0, 4, 1);
131 edgesLayout->addWidget(mySpinBox, 0, 1);
132 edgesLayout->addWidget(myAddButton, 1, 1);
133 edgesLayout->addWidget(myRemoveButton, 2, 1);
134 edgesLayout->addWidget(myTreeWidget, 0, 2, 4, 1);
135 edgesLayout->addWidget(mySameValues, 4, 0, 1, 3);
136 edgesLayout->setRowStretch( 3, 5 );
137 edgesLayout->setColumnStretch(0, 1);
138 edgesLayout->setColumnStretch(1, 0);
139 edgesLayout->setColumnStretch(2, 2);
141 myListWidget->setMinimumWidth( 80 );
142 myTreeWidget->setMinimumWidth( 200 );
144 mySpinBox->setAcceptNames( false ); // No Notebook variables allowed
145 mySpinBox->RangeStepAndValidator( 0., 1., .1, "parametric_precision" );
146 myListWidget->setMinimumWidth( 70 );
148 connect( myAddButton, SIGNAL( clicked() ), SLOT( onAdd() ) );
149 connect( myRemoveButton, SIGNAL( clicked() ), SLOT( onRemove() ) );
150 connect( mySameValues, SIGNAL( stateChanged( int ) ), SLOT( onCheckBoxChanged() ) );
151 connect( mySpinBox, SIGNAL( valueChanged( double ) ), SLOT( updateState() ) );
152 connect( myListWidget, SIGNAL( itemSelectionChanged() ), SLOT( updateState() ) );
153 myListWidget->installEventFilter( this );
158 //================================================================================
162 //================================================================================
164 StdMeshersGUI_FixedPointsParamWdg::~StdMeshersGUI_FixedPointsParamWdg()
168 //================================================================================
172 //================================================================================
173 bool StdMeshersGUI_FixedPointsParamWdg::eventFilter( QObject* o, QEvent* e )
175 if ( o == myListWidget && e->type() == QEvent::KeyPress ) {
176 QKeyEvent* ke = (QKeyEvent*)e;
177 if ( ke->key() == Qt::Key_Delete )
180 return QWidget::eventFilter( o, e );
183 //================================================================================
187 //================================================================================
188 void StdMeshersGUI_FixedPointsParamWdg::clear()
190 myTreeWidget->clear();
191 myListWidget->clear();
192 myTreeWidget->addTopLevelItem( newTreeItem( 0, 1 ) );
193 mySpinBox->setValue( 0. );
198 //=================================================================================
199 // function : onAdd()
200 // purpose : Called when Add Button Clicked
201 //=================================================================================
202 void StdMeshersGUI_FixedPointsParamWdg::onAdd()
204 addPoint( mySpinBox->value() );
207 //=================================================================================
208 // function : onRemove()
209 // purpose : Called when Remove Button Clicked
210 //=================================================================================
211 void StdMeshersGUI_FixedPointsParamWdg::onRemove()
216 //=================================================================================
217 // function : newTreeItem()
218 // purpose : Called to create TreeItem
219 //=================================================================================
221 QTreeWidgetItem* StdMeshersGUI_FixedPointsParamWdg::newTreeItem( double v1, double v2 )
223 QTreeWidgetItem* anItem = new QTreeWidgetItem();
224 anItem->setText( 0, treeItemText( v1, v2 ) );
225 anItem->setText( 1, QString::number( 1 ) );
226 anItem->setData( 1, Qt::UserRole, 1 );
230 //=================================================================================
231 // function : newListItem()
232 // purpose : Called to create ListItem
233 //=================================================================================
235 QListWidgetItem* StdMeshersGUI_FixedPointsParamWdg::newListItem( double v )
237 QListWidgetItem* anItem = new QListWidgetItem( QString::number( v ) );
238 anItem->setData( Qt::UserRole, v );
242 //=================================================================================
243 // function : itemText()
244 // purpose : Called to convert Values to Text
245 //=================================================================================
247 QString StdMeshersGUI_FixedPointsParamWdg::treeItemText( double v1, double v2 )
249 return QString( "%1 - %2" ).arg( v1 ).arg( v2 );
252 //=================================================================================
253 // function : addPoint()
254 // purpose : Called to Add new Point
255 //=================================================================================
256 void StdMeshersGUI_FixedPointsParamWdg::addPoint( double v)
258 if ( GT_DBL(v, 0.0) && LT_DBL(v, 1.0)) {
259 bool toInsert = true;
260 int idx = myTreeWidget->topLevelItemCount()-1;
261 for ( int i = 0 ; i < myListWidget->count(); i++ ) {
262 double lv = point( i );
263 if ( EQUAL_DBL(lv, v) ) { toInsert = false; break; }
264 else if ( GT_DBL(lv, v) ) {
269 double v1 = idx == 0 ? 0 : point( idx-1 );
270 double v2 = idx == myTreeWidget->topLevelItemCount()-1 ? 1 : point( idx );
271 myTreeWidget->insertTopLevelItem( idx, newTreeItem( v1, v ) );
272 myTreeWidget->topLevelItem( idx+1 )->setText( 0, treeItemText( v, v2 ) );
273 myListWidget->insertItem( idx, newListItem( v ) );
280 //=================================================================================
281 // function : removePoints()
282 // purpose : Called to remove selected points
283 //=================================================================================
284 void StdMeshersGUI_FixedPointsParamWdg::removePoints()
286 QList<QListWidgetItem*> selItems = myListWidget->selectedItems();
287 QListWidgetItem* item;
288 foreach ( item, selItems ) {
289 int idx = myListWidget->row( item );
290 delete myTreeWidget->topLevelItem( idx );
292 myTreeWidget->topLevelItem( idx )->setText( 0, treeItemText( idx == 0 ? 0 : point( idx-1 ),
293 idx > myListWidget->count()-1 ? 1 : point( idx ) ) );
299 double StdMeshersGUI_FixedPointsParamWdg::point( int idx ) const
301 return idx >= 0 && idx < myListWidget->count() ? myListWidget->item( idx )->data( Qt::UserRole ).toDouble() : 0.;
304 void StdMeshersGUI_FixedPointsParamWdg::setNbSegments( int idx, int val )
306 if ( idx >= 0 && idx < myTreeWidget->topLevelItemCount() ) {
307 myTreeWidget->topLevelItem( idx )->setData( 1, Qt::UserRole, val );
308 myTreeWidget->topLevelItem( idx )->setText( 1, idx > 0 && mySameValues->isChecked() ? QString( SAME_TEXT ) : QString::number( val ) );
312 int StdMeshersGUI_FixedPointsParamWdg::nbSegments( int idx ) const
314 return idx >= 0 && idx < myTreeWidget->topLevelItemCount() ? myTreeWidget->topLevelItem( idx )->data( 1, Qt::UserRole ).toInt() : 1;
317 //=================================================================================
318 // function : onCheckBoxChanged()
319 // purpose : Called when Check Box Clicked
320 //=================================================================================
321 void StdMeshersGUI_FixedPointsParamWdg::onCheckBoxChanged()
323 for ( int i = 0; i < myTreeWidget->topLevelItemCount(); i++ ) {
324 QTreeWidgetItem* anItem = myTreeWidget->topLevelItem(i);
325 setNbSegments( i, nbSegments( i ) );
326 anItem->setFlags( mySameValues->isChecked() && i > 0 ? anItem->flags() & ~Qt::ItemIsEditable : anItem->flags() | Qt::ItemIsEditable );
330 //=================================================================================
331 // function : updateState()
332 // purpose : Update widgets state
333 //=================================================================================
334 void StdMeshersGUI_FixedPointsParamWdg::updateState()
336 double v = mySpinBox->value();
337 myAddButton->setEnabled( GT_DBL(v, 0.0) && LT_DBL(v, 1.0) );
338 myRemoveButton->setEnabled( myListWidget->selectedItems().count() > 0 );
341 //=================================================================================
342 // function : GetListOfPoints
343 // purpose : Called to get the list of Edges IDs
344 //=================================================================================
345 SMESH::double_array_var StdMeshersGUI_FixedPointsParamWdg::GetListOfPoints()
347 SMESH::double_array_var anArray = new SMESH::double_array;
348 int size = myListWidget->count();
349 anArray->length( size );
350 for (int i = 0; i < size; i++) {
351 anArray[i] = point(i);
356 //=================================================================================
357 // function : SetListOfPoints
358 // purpose : Called to set the list of Points
359 //=================================================================================
360 void StdMeshersGUI_FixedPointsParamWdg::SetListOfPoints( SMESH::double_array_var thePoints)
363 for ( int i = 0; i < thePoints->length(); i++ ) {
364 addPoint( thePoints[ i ] );
368 //=================================================================================
369 // function : GetListOfSegments
370 // purpose : Called to get the list Number of Segments
371 //=================================================================================
372 SMESH::long_array_var StdMeshersGUI_FixedPointsParamWdg::GetListOfSegments()
374 SMESH::long_array_var anArray = new SMESH::long_array;
375 int size = mySameValues->isChecked() ? 1 : myTreeWidget->topLevelItemCount();
376 anArray->length( size );
377 for (int i = 0; i < size; i++) {
378 anArray[i] = nbSegments( i );
383 //=================================================================================
384 // function : SetListOfPoints
385 // purpose : Called to set the list of Points
386 //=================================================================================
387 void StdMeshersGUI_FixedPointsParamWdg::SetListOfSegments( SMESH::long_array_var theSegments)
389 if ( myListWidget->count() > 0 && theSegments->length() == 1)
390 mySameValues->setChecked(true);
391 for ( int i = 0; i < theSegments->length(); i++ ) {
392 setNbSegments( i, theSegments[i] );