Salome HOME
Update copyright
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_FixedPointsParamWdg.cxx
index 51685c37054b55dfdfbf477cceabdfc7c6979de8..b4243f1d0c63fc30d1a1e4f6d50594440a4f436c 100644 (file)
@@ -1,30 +1,32 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-//
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // File   : StdMeshersGUI_FixedPointsParamWdg.cxx
 // Author : Open CASCADE S.A.S.
 // SMESH includes
 //
 #include "StdMeshersGUI_FixedPointsParamWdg.h"
 
+#include <SMESHGUI_SpinBox.h>
+
+#include <SalomeApp_IntSpinBox.h>
+
 // Qt includes
 #include <QPushButton>
 #include <QIntValidator>
@@ -36,7 +38,6 @@
 #include <QTreeWidgetItem>
 #include <QCheckBox>
 #include <QLineEdit>
-#include <QDoubleSpinBox>
 #include <QItemDelegate>
 #include <QKeyEvent>
 
 #define MARGIN 0
 #define SAME_TEXT "-/-"
 
+#define TOLERANCE 1e-7
+#define EQUAL_DBL(a,b) (fabs(a-b)<TOLERANCE)
+#define LT_DBL(a,b) ((a<b)&&!EQUAL_DBL(a,b))
+#define GT_DBL(a,b) ((a>b)&&!EQUAL_DBL(a,b))
+
 /*
- * function : Tree Widget Item Delegate
+ * class : Tree Widget Item Delegate
  * purpose  : Custom item delegate
  */
 
@@ -69,12 +75,13 @@ StdMeshersGUI_FixedPointsParamWdg::LineDelegate::LineDelegate( QTreeWidget* pare
 }
 
 QWidget* StdMeshersGUI_FixedPointsParamWdg::LineDelegate::createEditor( QWidget* parent,
-                                                                       const QStyleOptionViewItem& option,
-                                                                       const QModelIndex& index ) const
+                                                                        const QStyleOptionViewItem& option,
+                                                                        const QModelIndex& index ) const
 {
   QWidget* w = 0;
   if ( (index.column() == 1 ) ) {
-    QSpinBox* sb = new QSpinBox( parent );
+    SalomeApp_IntSpinBox* sb = new SalomeApp_IntSpinBox( parent );
+    sb->setAcceptNames( false ); // No Notebook variables allowed
     sb->setFrame( false );
     sb->setRange( 1, 999);
     w = sb;
@@ -84,11 +91,11 @@ QWidget* StdMeshersGUI_FixedPointsParamWdg::LineDelegate::createEditor( QWidget*
 }
 
 void StdMeshersGUI_FixedPointsParamWdg::LineDelegate::setModelData( QWidget* editor, 
-                                                                   QAbstractItemModel* model, 
-                                                                   const QModelIndex& index ) const
+                                                                    QAbstractItemModel* model, 
+                                                                    const QModelIndex& index ) const
 {
-  model->setData( index, qobject_cast<QSpinBox*>( editor )->value(), Qt::EditRole );
-  model->setData( index, qobject_cast<QSpinBox*>( editor )->value(), Qt::UserRole );
+  model->setData( index, qobject_cast<SalomeApp_IntSpinBox*>( editor )->value(), Qt::EditRole );
+  model->setData( index, qobject_cast<SalomeApp_IntSpinBox*>( editor )->value(), Qt::UserRole );
 }
 
 //================================================================================
@@ -107,7 +114,7 @@ StdMeshersGUI_FixedPointsParamWdg
   
   myListWidget   = new QListWidget( this );
   myTreeWidget   = new QTreeWidget( this );
-  mySpinBox      = new QDoubleSpinBox( this );
+  mySpinBox      = new SMESHGUI_SpinBox( this );
   myAddButton    = new QPushButton( tr( "SMESH_BUT_ADD" ),    this );
   myRemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), this );      
   mySameValues   = new QCheckBox( tr("SMESH_SAME_NB_SEGMENTS"), this);
@@ -134,12 +141,15 @@ StdMeshersGUI_FixedPointsParamWdg
   myListWidget->setMinimumWidth( 80 );
   myTreeWidget->setMinimumWidth( 200 );
 
-  mySpinBox->setRange( 0, 1 );
-  mySpinBox->setSingleStep( 0.1 );
+  mySpinBox->setAcceptNames( false ); // No Notebook variables allowed
+  mySpinBox->RangeStepAndValidator( 0., 1., .1, "parametric_precision" );
+  myListWidget->setMinimumWidth( 70 );
 
-  connect( myAddButton,    SIGNAL(clicked()), SLOT(onAdd()));
-  connect( myRemoveButton, SIGNAL(clicked()), SLOT(onRemove()));
-  connect( mySameValues,   SIGNAL(stateChanged(int)), SLOT(onCheckBoxChanged()));
+  connect( myAddButton,    SIGNAL( clicked() ),              SLOT( onAdd() ) );
+  connect( myRemoveButton, SIGNAL( clicked() ),              SLOT( onRemove() ) );
+  connect( mySameValues,   SIGNAL( stateChanged( int ) ),    SLOT( onCheckBoxChanged() ) );
+  connect( mySpinBox,      SIGNAL( valueChanged( double ) ), SLOT( updateState() ) );
+  connect( myListWidget,   SIGNAL( itemSelectionChanged() ), SLOT( updateState() ) );
   myListWidget->installEventFilter( this );
 
   clear();
@@ -182,6 +192,7 @@ void StdMeshersGUI_FixedPointsParamWdg::clear()
   myTreeWidget->addTopLevelItem( newTreeItem( 0, 1 ) );
   mySpinBox->setValue( 0. );
   onCheckBoxChanged();
+  updateState();
 }
 
 //=================================================================================
@@ -192,7 +203,7 @@ void StdMeshersGUI_FixedPointsParamWdg::onAdd()
 {
   addPoint( mySpinBox->value() );
 }
-        
+         
 //=================================================================================
 // function : onRemove()
 // purpose  : Called when Remove Button Clicked
@@ -244,14 +255,14 @@ QString StdMeshersGUI_FixedPointsParamWdg::treeItemText( double v1, double v2 )
 //=================================================================================
 void StdMeshersGUI_FixedPointsParamWdg::addPoint( double v)
 {
-  if ( v > 0 && v < 1) {
+  if ( GT_DBL(v, 0.0) && LT_DBL(v, 1.0)) {
     bool toInsert = true;
     int idx = myTreeWidget->topLevelItemCount()-1;
     for ( int i = 0 ; i < myListWidget->count(); i++ ) {
       double lv = point( i );
-      if ( lv == v ) { toInsert = false; break; }
-      else if ( lv > v ) {
-       idx = i; break;
+      if ( EQUAL_DBL(lv, v) ) { toInsert = false; break; }
+      else if ( GT_DBL(lv, v) ) {
+        idx = i; break;
       }
     }
     if ( toInsert ) {
@@ -263,6 +274,7 @@ void StdMeshersGUI_FixedPointsParamWdg::addPoint( double v)
       onCheckBoxChanged();
     }
   }
+  updateState();
 }
 
 //=================================================================================
@@ -278,9 +290,10 @@ void StdMeshersGUI_FixedPointsParamWdg::removePoints()
     delete myTreeWidget->topLevelItem( idx );
     delete item;
     myTreeWidget->topLevelItem( idx )->setText( 0, treeItemText( idx == 0 ? 0 : point( idx-1 ),
-                                                                idx > myListWidget->count()-1 ? 1 : point( idx ) ) );
+                                                                 idx > myListWidget->count()-1 ? 1 : point( idx ) ) );
   }
   onCheckBoxChanged();
+  updateState();
 }
 
 double StdMeshersGUI_FixedPointsParamWdg::point( int idx ) const
@@ -314,6 +327,17 @@ void StdMeshersGUI_FixedPointsParamWdg::onCheckBoxChanged()
   }
 }
 
+//=================================================================================
+// function : updateState()
+// purpose  : Update widgets state
+//=================================================================================
+void StdMeshersGUI_FixedPointsParamWdg::updateState()
+{
+  double v = mySpinBox->value();
+  myAddButton->setEnabled( GT_DBL(v, 0.0) && LT_DBL(v, 1.0) );
+  myRemoveButton->setEnabled( myListWidget->selectedItems().count() > 0 );
+}
+
 //=================================================================================
 // function : GetListOfPoints
 // purpose  : Called to get the list of Edges IDs
@@ -325,7 +349,6 @@ SMESH::double_array_var StdMeshersGUI_FixedPointsParamWdg::GetListOfPoints()
   anArray->length( size );
   for (int i = 0; i < size; i++) {
     anArray[i] = point(i);
-    //    printf ("Point %f \n", anArray[i]);
   }
   return anArray;
 }
@@ -339,7 +362,6 @@ void StdMeshersGUI_FixedPointsParamWdg::SetListOfPoints( SMESH::double_array_var
   clear();
   for ( int i = 0; i < thePoints->length(); i++ ) {
     addPoint( thePoints[ i ] );
-    //    printf ("Add Point %f \n", thePoints[ i ]);
   }
 }
 
@@ -354,7 +376,6 @@ SMESH::long_array_var StdMeshersGUI_FixedPointsParamWdg::GetListOfSegments()
   anArray->length( size );
   for (int i = 0; i < size; i++) {
     anArray[i] = nbSegments( i );
-    //    printf ("Segments %d \n", anArray[i] );
   }
   return anArray;
 }
@@ -369,6 +390,5 @@ void StdMeshersGUI_FixedPointsParamWdg::SetListOfSegments( SMESH::long_array_var
     mySameValues->setChecked(true);
   for ( int i = 0; i < theSegments->length(); i++ ) {
     setNbSegments( i, theSegments[i] );
-    //    printf ("\nadd Segment = %d\n", theSegments[i]);
   }
 }