1 // SMESH SMESHGUI : GUI for SMESH component
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESHGUI_aParameter.cxx
28 #include "SMESHGUI_aParameter.h"
31 #include <qvalidator.h>
32 #include <qlineedit.h>
33 #include <qtextedit.h>
34 #include <qcombobox.h>
35 #include <qcheckbox.h>
37 #include <qvalidator.h>
38 #include <qpushbutton.h>
41 #include <QtxDblSpinBox.h>
43 SMESHGUI_aParameter::~SMESHGUI_aParameter() {}
45 QString SMESHGUI_aParameter::sigValueChanged() const
50 //=================================================================================
51 // class : SMESHGUI_intParameter
53 //=================================================================================
54 SMESHGUI_intParameter::SMESHGUI_intParameter (const int theInitValue,
55 const QString& theLabel,
58 :SMESHGUI_aParameter(theLabel),
59 _top(theTop), _bottom(theBottom), _initValue(theInitValue),
60 _newValue( theInitValue )
63 SMESHGUI_aParameter::Type SMESHGUI_intParameter::GetType() const
65 return SMESHGUI_aParameter::INT;
67 bool SMESHGUI_intParameter::GetNewInt (int & theValue) const
70 return _newValue != _initValue;
72 bool SMESHGUI_intParameter::GetNewDouble (double & Value) const
76 bool SMESHGUI_intParameter::GetNewText (QString & Value) const
81 QWidget* SMESHGUI_intParameter::CreateWidget( QWidget* parent ) const
83 return new QSpinBox( parent );
86 void SMESHGUI_intParameter::InitializeWidget (QWidget* theQWidget) const
88 QSpinBox * aSpin = dynamic_cast< QSpinBox *>(theQWidget);
90 aSpin->setMinValue(_bottom);
91 aSpin->setMaxValue(_top);
92 aSpin->setValue(_initValue);
95 void SMESHGUI_intParameter::TakeValue (QWidget* theQWidget)
97 QSpinBox * aSpin = dynamic_cast< QSpinBox *>(theQWidget);
99 _newValue = aSpin->value();
102 QString SMESHGUI_intParameter::sigValueChanged() const
104 return SIGNAL( valueChanged( int ) );
107 //=================================================================================
108 // class : SMESHGUI_doubleParameter
110 //=================================================================================
111 SMESHGUI_doubleParameter::SMESHGUI_doubleParameter (const double theInitValue,
112 const QString& theLabel,
113 const double theBottom,
115 const double theStep,
116 const int theDecimals)
117 :SMESHGUI_aParameter(theLabel),
118 _top(theTop), _bottom(theBottom), _step(theStep),
119 _initValue(theInitValue), _decimals(theDecimals)
122 SMESHGUI_aParameter::Type SMESHGUI_doubleParameter::GetType() const
124 return SMESHGUI_aParameter::DOUBLE;
126 bool SMESHGUI_doubleParameter::GetNewInt (int & theValue) const
130 bool SMESHGUI_doubleParameter::GetNewDouble (double & Value) const
133 return _newValue != _initValue;
135 bool SMESHGUI_doubleParameter::GetNewText (QString & Value) const
140 QWidget* SMESHGUI_doubleParameter::CreateWidget( QWidget* parent ) const
142 return new QtxDblSpinBox( parent );
145 void SMESHGUI_doubleParameter::InitializeWidget (QWidget* theQWidget) const
147 QtxDblSpinBox* aSpin = dynamic_cast<QtxDblSpinBox*>(theQWidget);
149 aSpin->setPrecision(_decimals);
151 aSpin->setDblPrecision(_bottom);
153 aSpin->setRange(_bottom, _top);
154 aSpin->setValue(_initValue);
155 aSpin->setLineStep(_step);
158 void SMESHGUI_doubleParameter::TakeValue (QWidget* theQWidget)
160 QtxDblSpinBox* aSpin = dynamic_cast<QtxDblSpinBox*>(theQWidget);
162 _newValue = aSpin->value();
165 QString SMESHGUI_doubleParameter::sigValueChanged() const
167 return SIGNAL( valueChanged( double ) );
170 //=================================================================================
171 // class : SMESHGUI_strParameter
173 //=================================================================================
174 SMESHGUI_strParameter::SMESHGUI_strParameter (const QString& theInitValue,
175 const QString& theLabel)
176 :SMESHGUI_aParameter(theLabel),
177 _initValue(theInitValue)
180 SMESHGUI_aParameter::Type SMESHGUI_strParameter::GetType() const
182 return SMESHGUI_aParameter::STRING;
184 bool SMESHGUI_strParameter::GetNewInt (int & theValue) const
188 bool SMESHGUI_strParameter::GetNewDouble (double & Value) const
192 bool SMESHGUI_strParameter::GetNewText (QString & theValue) const
194 theValue = _newValue;
195 return _newValue != _initValue;
198 QWidget* SMESHGUI_strParameter::CreateWidget( QWidget* parent ) const
200 return new QLineEdit( parent );
203 void SMESHGUI_strParameter::InitializeWidget (QWidget* theQWidget) const
205 QLineEdit* anEdit = dynamic_cast< QLineEdit* >(theQWidget);
207 anEdit->setText(_initValue);
210 void SMESHGUI_strParameter::TakeValue (QWidget* theQWidget)
212 QLineEdit* anEdit = dynamic_cast< QLineEdit* >(theQWidget);
214 _newValue = anEdit->text();
217 QString SMESHGUI_strParameter::sigValueChanged() const
219 return SIGNAL( textChanged( const QString& ) );
224 //=================================================================================
225 // class : SMESHGUI_dependParameter
227 //=================================================================================
228 SMESHGUI_dependParameter::SMESHGUI_dependParameter( const QString& label )
229 : SMESHGUI_aParameter( label )
233 const SMESHGUI_dependParameter::ShownMap& SMESHGUI_dependParameter::shownMap() const
238 SMESHGUI_dependParameter::ShownMap& SMESHGUI_dependParameter::shownMap()
247 //=================================================================================
248 // class : SMESHGUI_enumParameter
250 //=================================================================================
251 SMESHGUI_enumParameter::SMESHGUI_enumParameter( const QStringList& values,
253 const QString& label )
254 : SMESHGUI_dependParameter( label ),
255 myInitValue( initValue ),
256 myValue( initValue ),
261 SMESHGUI_enumParameter::~SMESHGUI_enumParameter()
265 SMESHGUI_aParameter::Type SMESHGUI_enumParameter::GetType() const
267 return SMESHGUI_aParameter::ENUM;
270 bool SMESHGUI_enumParameter::GetNewInt( int& v ) const
273 return myValue!=myInitValue;
276 bool SMESHGUI_enumParameter::GetNewDouble( double& ) const
281 bool SMESHGUI_enumParameter::GetNewText( QString& v ) const
283 bool res = myValue>=0 && myValue<Count();
286 v = myValues[ myValue ];
288 return res && v!=myInitValue;
291 QWidget* SMESHGUI_enumParameter::CreateWidget( QWidget* parent ) const
293 return new QComboBox( parent );
296 void SMESHGUI_enumParameter::InitializeWidget( QWidget* w ) const
298 if( w && w->inherits( "QComboBox" ) )
300 QComboBox* c = ( QComboBox* ) w;
302 c->insertStringList( myValues );
303 c->setCurrentItem( myInitValue );
307 void SMESHGUI_enumParameter::TakeValue( QWidget* w )
309 if( w && w->inherits( "QComboBox" ) )
311 QComboBox* c = ( QComboBox* ) w;
312 myValue = c->currentItem();
316 int SMESHGUI_enumParameter::Count() const
318 return myValues.count();
321 QString SMESHGUI_enumParameter::sigValueChanged() const
323 return SIGNAL( activated( int ) );
327 //=================================================================================
328 // class : SMESHGUI_boolParameter
330 //=================================================================================
331 SMESHGUI_boolParameter::SMESHGUI_boolParameter( const bool initValue,
332 const QString& label )
333 : SMESHGUI_dependParameter( label ),
334 myInitValue( initValue ),
335 myValue( myInitValue )
339 SMESHGUI_boolParameter::~SMESHGUI_boolParameter()
343 SMESHGUI_aParameter::Type SMESHGUI_boolParameter::GetType() const
348 bool SMESHGUI_boolParameter::GetNewInt( int& v ) const
354 return v!=myInitValue;
357 bool SMESHGUI_boolParameter::GetNewDouble( double& ) const
362 bool SMESHGUI_boolParameter::GetNewText( QString& ) const
367 QWidget* SMESHGUI_boolParameter::CreateWidget( QWidget* parent ) const
369 return new QCheckBox( parent );
372 void SMESHGUI_boolParameter::InitializeWidget( QWidget* w ) const
374 if( w && w->inherits( "QCheckBox" ) )
376 QCheckBox* box = ( QCheckBox* )w;
377 box->setChecked( myInitValue );
381 void SMESHGUI_boolParameter::TakeValue( QWidget* w )
383 if( w && w->inherits( "QCheckBox" ) )
385 QCheckBox* box = ( QCheckBox* )w;
386 myValue = box->isChecked();
390 QString SMESHGUI_boolParameter::sigValueChanged() const
392 return SIGNAL( stateChanged( int ) );
398 //=================================================================================
399 // class : SMESHGUI_doubleItem
400 // purpose : Custom table item which contains double and has double validator
401 //=================================================================================
402 class SMESHGUI_doubleItem: public QTableItem
405 SMESHGUI_doubleItem( QTable*, EditType, const double );
406 virtual ~SMESHGUI_doubleItem();
408 void setValidator( const double, const double, const int );
409 void validator( double&, double&, int& );
410 virtual QWidget* createEditor() const;
413 QDoubleValidator* myValidator;
416 SMESHGUI_doubleItem::SMESHGUI_doubleItem( QTable* t, EditType e, const double num )
417 : QTableItem( t, e, QString( "%1" ).arg( num ) ),
418 myValidator( new QDoubleValidator( 0.0, 1.0, 3, t ) )
422 SMESHGUI_doubleItem::~SMESHGUI_doubleItem()
426 void SMESHGUI_doubleItem::setValidator( const double bot, const double top, const int dec )
428 myValidator->setBottom( bot );
429 myValidator->setTop( top );
430 myValidator->setDecimals( dec );
433 void SMESHGUI_doubleItem::validator( double& bot, double& top, int& dec )
435 bot = myValidator->bottom();
436 top = myValidator->top();
437 dec = myValidator->decimals();
440 QWidget* SMESHGUI_doubleItem::createEditor() const
442 QWidget* res = QTableItem::createEditor();
443 if( res && res->inherits( "QLineEdit" ) )
445 QLineEdit* l = ( QLineEdit* )res;
446 l->setValidator( myValidator );
452 //=================================================================================
453 // class : SMESHGUI_Table
455 //=================================================================================
456 SMESHGUI_Table::SMESHGUI_Table( int numRows, int numCols, QWidget* parent, const char* name )
457 : QTable( numRows, numCols, parent, name )
461 SMESHGUI_Table::~SMESHGUI_Table()
465 QSize SMESHGUI_Table::sizeHint() const
467 if( cachedSizeHint().isValid() )
468 return cachedSizeHint();
472 QSize sh = QScrollView::sizeHint();
475 if( sh.height()<200 )
478 setCachedSizeHint( sh );
482 void SMESHGUI_Table::stopEditing()
484 endEdit( currEditRow(), currEditCol(), false, false );
487 void SMESHGUI_Table::validator( const int row, const int col, double& minV, double& maxV, int& dec )
489 SMESHGUI_doubleItem* it = dynamic_cast<SMESHGUI_doubleItem*>( item( row, col ) );
491 it->validator( minV, maxV, dec );
494 void SMESHGUI_Table::setValidator( const double minV, const double maxV, const int dec,
495 const int rmin, const int rmax,
496 const int cmin, const int cmax )
498 int r1 = rmin>=0 ? rmin : 0,
499 r2 = rmax>=0 ? rmax : numRows(),
500 c1 = cmin>=0 ? cmin : 0,
501 c2 = cmax>=0 ? cmax : numCols();
503 for( int i=r1; i<=r2; i++ )
504 for( int j=c1; j<=c2; j++ )
506 SMESHGUI_doubleItem* it = dynamic_cast<SMESHGUI_doubleItem*>( item( i, j ) );
508 it->setValidator( minV, maxV, dec );
514 //=================================================================================
515 // class : SMESHGUI_TableFrame
517 //=================================================================================
518 SMESHGUI_TableFrame::SMESHGUI_TableFrame( QWidget* parent )
521 QVBoxLayout* main = new QVBoxLayout( this, 0, 0 );
523 myTable = new SMESHGUI_Table( 1, 1, this );
525 QFrame* aButFrame = new QFrame( this );
526 QHBoxLayout* butLay = new QHBoxLayout( aButFrame, 5, 5 );
528 myAddColumn = new QPushButton( "Add column", aButFrame );
530 myRemoveColumn = new QPushButton( "Remove column", aButFrame );
532 myAddRow = new QPushButton( "Add row", aButFrame );
534 myRemoveRow = new QPushButton( "Remove row", aButFrame );
536 butLay->addWidget( myAddColumn, 0 );
537 butLay->addWidget( myRemoveColumn, 0 );
538 butLay->addWidget( myAddRow, 0 );
539 butLay->addWidget( myRemoveRow, 0 );
540 butLay->addStretch( 1 );
542 main->addWidget( myTable, 1 );
543 main->addWidget( aButFrame, 0 );
545 connect( myAddColumn, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
546 connect( myRemoveColumn, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
547 connect( myAddRow, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
548 connect( myRemoveRow, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
551 SMESHGUI_TableFrame::~SMESHGUI_TableFrame()
555 SMESHGUI_Table* SMESHGUI_TableFrame::table() const
560 void SMESHGUI_TableFrame::setShown( const Button b, const bool sh )
563 button( b )->setShown( sh );
566 bool SMESHGUI_TableFrame::isShown( const Button b ) const
570 res = button( b )->isShown();
574 QButton* SMESHGUI_TableFrame::button( const Button b ) const
584 res = myRemoveColumn;
598 void SMESHGUI_TableFrame::onButtonClicked()
600 if( sender()==button( ADD_COLUMN ) )
601 emit toEdit( ADD_COLUMN, table()->currentColumn() );
603 else if( sender()==button( REMOVE_COLUMN ) )
604 emit toEdit( REMOVE_COLUMN, table()->currentColumn() );
606 else if( sender()==button( ADD_ROW ) )
607 emit toEdit( ADD_ROW, table()->currentRow() );
609 else if( sender()==button( REMOVE_ROW ) )
610 emit toEdit( REMOVE_ROW, table()->currentRow() );
614 //=================================================================================
615 // class : SMESHGUI_tableParameter
617 //=================================================================================
618 SMESHGUI_tableParameter::SMESHGUI_tableParameter( const double init,
619 const QString& label )
620 : SMESHGUI_aParameter( label ),
629 SMESHGUI_tableParameter::~SMESHGUI_tableParameter()
633 SMESHGUI_aParameter::Type SMESHGUI_tableParameter::GetType() const
638 bool SMESHGUI_tableParameter::GetNewInt( int& ) const
643 bool SMESHGUI_tableParameter::GetNewDouble( double& ) const
648 bool SMESHGUI_tableParameter::GetNewText( QString& ) const
653 QWidget* SMESHGUI_tableParameter::CreateWidget( QWidget* par ) const
655 SMESHGUI_TableFrame* t = new SMESHGUI_TableFrame( par );
656 connect( t, SIGNAL( toEdit( SMESHGUI_TableFrame::Button, int ) ),
657 this, SLOT ( onEdit( SMESHGUI_TableFrame::Button, int ) ) );
663 void SMESHGUI_tableParameter::setItems( QWidget* w,
664 int old_row, int new_row,
665 int old_col, int new_col ) const
667 if( w && w->inherits( "SMESHGUI_TableFrame" ) )
669 QTable* tab = ( ( SMESHGUI_TableFrame* )w )->table();
674 new_row = tab->numRows();
678 new_col = tab->numCols();
680 for( int i=old_row, m=new_row; i<m; i++ )
681 for( int j=0, n=new_col; j<n; j++ )
682 tab->setItem( i, j, new SMESHGUI_doubleItem( tab, QTableItem::WhenCurrent, myInitValue ) );
684 for( int i=0, m=new_row; i<m; i++ )
685 for( int j=old_col, n=new_col; j<n; j++ )
686 tab->setItem( i, j, new SMESHGUI_doubleItem( tab, QTableItem::WhenCurrent, myInitValue ) );
688 for( int j=old_col; j<new_col; j++ )
689 tab->setColumnWidth( j, 50 );
693 void SMESHGUI_tableParameter::InitializeWidget( QWidget* w ) const
697 if( w && w->inherits( "SMESHGUI_TableFrame" ) )
699 SMESHGUI_Table* tab = ( ( SMESHGUI_TableFrame* )w )->table();
702 int col = tab->numCols(),
703 row = tab->numRows();
705 for( int i=0, m=row; i<m; i++ )
706 for( int j=0, n=col; j<n; j++ )
707 if( row*j+i<myData.length() )
708 tab->item( i, j )->setText( QString( "%1" ).arg( myData[row*j+i] ) );
712 void SMESHGUI_tableParameter::TakeValue( QWidget* w )
714 if( w && w->inherits( "SMESHGUI_TableFrame" ) )
716 QTable* tab = ( ( SMESHGUI_TableFrame* )w )->table();
718 int col = tab->numCols(),
719 row = tab->numRows();
721 myData.length( col*row );
722 for( int i=0; i<row; i++ )
723 for( int j=0; j<col; j++ )
724 myData[ col*i+j ] = tab->text( i, j ).toDouble();
728 void SMESHGUI_tableParameter::data( SMESH::double_array& v ) const
733 void SMESHGUI_tableParameter::setData( const SMESH::double_array& d )
738 void SMESHGUI_tableParameter::update( QWidget* w ) const
740 if( w && w->inherits( "SMESHGUI_TableFrame" ) )
742 SMESHGUI_TableFrame* tabfr = ( SMESHGUI_TableFrame* ) w;
743 SMESHGUI_Table* tab = tabfr->table();
745 int old_col = tab->numCols(),
746 old_row = tab->numRows();
748 int col = myColsInt, row = myRowsInt;
750 myCols->GetNewInt( col );
753 myRows->GetNewInt( row );
760 if( col!=tab->numCols() )
761 tab->setNumCols( col );
763 if( row!=tab->numRows() )
764 tab->setNumRows( row );
766 tabfr->setShown( SMESHGUI_TableFrame::ADD_COLUMN, myEditCols );
767 tabfr->setShown( SMESHGUI_TableFrame::REMOVE_COLUMN, myEditCols );
768 tabfr->setShown( SMESHGUI_TableFrame::ADD_ROW, myEditRows );
769 tabfr->setShown( SMESHGUI_TableFrame::REMOVE_ROW, myEditRows );
771 setItems( w, old_row, row, old_col, col );
773 QMap< int, QString >::const_iterator aNIt = myColNames.begin(),
774 aNLast = myColNames.end();
775 for( ; aNIt!=aNLast; aNIt++ )
776 tab->horizontalHeader()->setLabel( aNIt.key(), aNIt.data() );
778 ValidatorsMap::const_iterator anIt = myValidators.begin(),
779 aLast = myValidators.end();
780 for( ; anIt!=aLast; anIt++ )
782 int row = anIt.key(), dec;
784 validator( row, minV, maxV, dec );
785 tab->setValidator( minV, maxV, dec, -1, -1, col, col );
788 QSize s = tab->sizeHint();
789 tab->resize( s.width(), s.height() );
793 void SMESHGUI_tableParameter::setColCount( const int c, QWidget* w )
799 void SMESHGUI_tableParameter::setRowCount( const int c, QWidget* w )
805 void SMESHGUI_tableParameter::setColCount( const SMESHGUI_aParameterPtr p, QWidget* w )
814 void SMESHGUI_tableParameter::setRowCount( const SMESHGUI_aParameterPtr p, QWidget* w )
823 QString SMESHGUI_tableParameter::sigValueChanged() const
825 return SIGNAL( valueChanged( int, int ) );
828 void SMESHGUI_tableParameter::setValidator( const int ind, const double minV, const double maxV, const int dec )
833 inf.myDecimals = dec;
834 myValidators[ ind ] = inf;
837 void SMESHGUI_tableParameter::validator( const int ind, double& minV, double& maxV, int& dec ) const
839 if( myValidators.contains( ind ) )
841 const ValidatorInfo& inf = myValidators[ ind ];
844 dec = inf.myDecimals;
848 void SMESHGUI_tableParameter::setEditCols( const bool ed )
853 void SMESHGUI_tableParameter::setEditRows( const bool ed )
858 void SMESHGUI_tableParameter::setColName( const int ind, const QString& name )
860 myColNames.insert( ind, name );
863 QString SMESHGUI_tableParameter::colName( const int ind ) const
865 if( myColNames.contains( ind ) )
866 return myColNames[ ind ];
868 return QString::null;
871 void SMESHGUI_tableParameter::onEdit( SMESHGUI_TableFrame::Button b, int n )
873 if( sender() && sender()->inherits( "SMESHGUI_TableFrame" ) )
875 SMESHGUI_TableFrame* fr = ( SMESHGUI_TableFrame* )sender();
876 SMESHGUI_Table* tab = fr->table();
880 case SMESHGUI_TableFrame::ADD_COLUMN:
882 if( !myEditCols || myCols.get() )
885 myColsInt++; update( fr );
887 for( int i=0; i<myRowsInt; i++ )
888 for( int j=myColsInt-1; j>=n; j-- )
890 tab->setText( i, j, QString( "%1" ).arg( myInitValue ) );
892 tab->setText( i, j, tab->text( i, j-1 ) );
896 case SMESHGUI_TableFrame::REMOVE_COLUMN:
898 if( !myEditCols || myCols.get() || myColsInt<=1 )
902 for( int i=0; i<myRowsInt; i++ )
903 for( int j=n; j<myColsInt-1; j++ )
904 tab->setText( i, j, tab->text( i, j+1 ) );
905 myColsInt--; update( fr );
910 case SMESHGUI_TableFrame::ADD_ROW:
912 if( !myEditRows || myRows.get() )
915 myRowsInt++; update( fr );
917 for( int i=myRowsInt-1; i>=n; i-- )
918 for( int j=0; j<myColsInt; j++ )
920 tab->setText( i, j, QString( "%1" ).arg( myInitValue ) );
922 tab->setText( i, j, tab->text( i-1, j ) );
926 case SMESHGUI_TableFrame::REMOVE_ROW:
928 if( !myEditRows || myRows.get() || myRowsInt<=1 )
932 for( int i=n; i<myRowsInt-1; i++ )
933 for( int j=0; j<myColsInt; j++ )
934 tab->setText( i, j, tab->text( i+1, j ) );
935 myRowsInt--; update( fr );