1 // Copyright (C) 2007-2008 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
22 // SMESH StdMeshersGUI
23 // File : StdMeshersGUI_DistrTable.cxx
27 #include "StdMeshersGUI_DistrTable.h"
28 #include <QtxDblValidator.h>
31 #include <qpushbutton.h>
32 #include <qlineedit.h>
34 //=================================================================================
35 // class : StdMeshersGUI_DistrTable
37 //=================================================================================
38 StdMeshersGUI_DistrTable::StdMeshersGUI_DistrTable( const int rows, QWidget* parent, const char* name )
39 : QTable( rows, 2, parent, name )
41 horizontalHeader()->setLabel( 0, "t" );
42 horizontalHeader()->setLabel( 1, "f(t)" );
43 myArgV = new QtxDblValidator( 0.0, 1.0, 3, this );
44 myFuncV = new QtxDblValidator( 0.0, 1E20, 3, this );
47 StdMeshersGUI_DistrTable::~StdMeshersGUI_DistrTable()
51 QSize StdMeshersGUI_DistrTable::sizeHint() const
53 if( cachedSizeHint().isValid() )
54 return cachedSizeHint();
58 QSize sh = QScrollView::sizeHint();
64 setCachedSizeHint( sh );
68 void StdMeshersGUI_DistrTable::stopEditing( const bool accept )
70 endEdit( currEditRow(), currEditCol(), accept, false );
73 QWidget* StdMeshersGUI_DistrTable::beginEdit( int row, int col, bool replace )
75 QWidget* w = QTable::beginEdit( row, col, replace );
76 if( w && w->inherits( "QLineEdit" ) )
77 ( ( QLineEdit* )w )->selectAll();
81 void StdMeshersGUI_DistrTable::edit( const int r, const int c )
84 endEdit( currEditRow(), currEditCol(), true, false );
86 setCurrentCell( r, c );
87 if( beginEdit( r, c, false ) )
88 setEditMode( Editing, r, c );
95 bool StdMeshersGUI_DistrTable::eventFilter( QObject* o, QEvent* e )
97 if( e && e->type()==QEvent::KeyPress )
99 QKeyEvent* ke = ( QKeyEvent* )e;
101 if( k==Qt::Key_Tab || k==Qt::Key_BackTab || k==Qt::Key_Return || k==Qt::Key_Up || k==Qt::Key_Down )
107 return QTable::eventFilter( o, e );
110 void StdMeshersGUI_DistrTable::keyPressEvent( QKeyEvent* e )
114 int r = currentRow(), c = currentColumn(), nr, nc;
115 bool shift = e->state() & Qt::ShiftButton, cr = false;
123 case Qt::Key_BackTab:
130 nr = shift ? r-1 : r+1;
145 QTable::keyPressEvent( e );
158 if( nr>=numRows() && cr )
161 endEdit( currEditRow(), currEditCol(), true, false );
162 onEdit( INSERT_ROW, nr );
165 else if( nr<0 || nr>=numRows() )
174 QWidget* StdMeshersGUI_DistrTable::createEditor( int r, int c, bool init ) const
176 QWidget* w = QTable::createEditor( r, c, init );
179 //w->installEventFilter( this );
180 if( w->inherits( "QLineEdit" ) )
182 QLineEdit* le = ( QLineEdit* )w;
183 le->setValidator( c==0 ? myArgV : myFuncV );
190 void StdMeshersGUI_DistrTable::onEdit( TableButton b, int cur )
195 setNumRows( numRows()+1 );
196 for( int i=numRows()-1; i>=cur; i-- )
197 for( int j=0; j<numCols(); j++ )
199 setText( i, j, text( i-1, j ) );
201 setText( i, j, "0" );
202 emit( valueChanged( cur, 0 ) );
208 for( int i=cur; i<numRows(); i++ )
209 for( int j=0; j<numCols(); j++ )
210 setText( i, j, text( i+1, j ) );
211 setNumRows( numRows()-1 );
213 emit( valueChanged( cur, 0 ) );
218 void StdMeshersGUI_DistrTable::sortData( SMESH::double_array& arr )
220 QValueList< QPair<double,double> > aData;
221 if( arr.length()%2==1 )
222 arr.length( arr.length()-1 );
224 int aLen = arr.length();
225 for( int i=0; i<aLen/2; i++ )
226 aData.append( QPair<double,double>( arr[2*i], arr[2*i+1] ) );
230 QValueList< QPair<double,double> >::const_iterator anIt = aData.begin(), aLast = aData.end();
231 QValueList<double> unique_values;
232 double prev; int i=0;
233 if( (*anIt).first>0.0 )
235 unique_values.append( 0.0 );
236 unique_values.append( (*anIt).second );
239 for( ; anIt!=aLast; anIt++ )
241 if( i==0 || (*anIt).first>prev )
243 unique_values.append( (*anIt).first );
244 unique_values.append( (*anIt).second );
247 prev = (*anIt).first;
251 unique_values.append( 1.0 );
253 unique_values.append( (*anIt).second );
256 arr.length( unique_values.count() );
257 QValueList<double>::const_iterator anIt1 = unique_values.begin(), aLast1 = unique_values.end();
258 for( int j=0; anIt1!=aLast1; anIt1++, j++ )
262 void StdMeshersGUI_DistrTable::data( SMESH::double_array& v )
265 v.length( 2*numRows() );
266 for( int i=0; i<numRows(); i++ )
267 for( int j=0; j<numCols(); j++ )
268 v[numCols()*i+j] = text( i, j ).toDouble();
272 void StdMeshersGUI_DistrTable::setData( const SMESH::double_array& d )
274 stopEditing( false );
275 setNumRows( d.length()/2 );
277 for( int i=0; i<d.length(); i++ )
279 QtxDblValidator* v = i%2==0 ? myArgV : myFuncV;
280 val = QString::number( d[i] );
282 setText( i/2, i%2, val );
286 QtxDblValidator* StdMeshersGUI_DistrTable::argValidator() const
291 QtxDblValidator* StdMeshersGUI_DistrTable::funcValidator() const
296 //=================================================================================
297 // class : StdMeshersGUI_DistrTableFrame
299 //=================================================================================
300 StdMeshersGUI_DistrTableFrame::StdMeshersGUI_DistrTableFrame( QWidget* parent )
303 QVBoxLayout* main = new QVBoxLayout( this, 0, 0 );
305 myTable = new StdMeshersGUI_DistrTable( 1, this );
306 connect( myTable, SIGNAL( valueChanged( int, int ) ), this, SIGNAL( valueChanged( int, int ) ) );
307 connect( this, SIGNAL( toEdit( TableButton, int ) ), myTable, SLOT( onEdit( TableButton, int ) ) );
309 QFrame* aButFrame = new QFrame( this );
310 QHBoxLayout* butLay = new QHBoxLayout( aButFrame, 5, 5 );
312 myInsertRow = new QPushButton( tr( "SMESH_INSERT_ROW" ), aButFrame );
313 myRemoveRow = new QPushButton( tr( "SMESH_REMOVE_ROW" ), aButFrame );
315 butLay->addWidget( myInsertRow, 0 );
316 butLay->addWidget( myRemoveRow, 0 );
317 butLay->addStretch( 1 );
319 main->addWidget( myTable, 1 );
320 main->addWidget( aButFrame, 0 );
322 connect( myInsertRow, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
323 connect( myRemoveRow, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
326 StdMeshersGUI_DistrTableFrame::~StdMeshersGUI_DistrTableFrame()
330 StdMeshersGUI_DistrTable* StdMeshersGUI_DistrTableFrame::table() const
335 void StdMeshersGUI_DistrTableFrame::setShown( const TableButton b, const bool sh )
338 button( b )->setShown( sh );
341 bool StdMeshersGUI_DistrTableFrame::isShown( const TableButton b ) const
345 res = button( b )->isShown();
349 QButton* StdMeshersGUI_DistrTableFrame::button( const TableButton b ) const
365 void StdMeshersGUI_DistrTableFrame::onButtonClicked()
367 if( sender()==button( INSERT_ROW ) )
368 emit toEdit( INSERT_ROW, table()->currentRow() );
370 else if( sender()==button( REMOVE_ROW ) )
371 emit toEdit( REMOVE_ROW, table()->currentRow() );