Salome HOME
Icons are shown in the object browser tree.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeoreferencementDlg.cxx
index 534ba65917329cdd0df7c544bd956c50205e7815..57bfafb6f4df207c3210b54852eb9be1f1d6f736 100644 (file)
 #include <QItemDelegate>
 #include <QHeaderView>
 #include <QRadioButton>
+#include <QPushButton>
 #include <QLineEdit>
 #include <QButtonGroup>
 #include <QGroupBox>
 #include <QLayout>
 #include <QMouseEvent>
 
+//! Profile data structre constructor
+HYDROGUI_GeoreferencementDlg::ProfileGeoData::ProfileGeoData( 
+  const QString& theXg, const QString& theYg, 
+  const QString& theXd, const QString& theYd)
+{
+  this->isEmpty = theXg.isEmpty() && theYg.isEmpty() &&
+                 theXd.isEmpty() && theYd.isEmpty();
+  this->isIncomplete = !isEmpty;
+
+  if ( isIncomplete ) {
+    bool isOk = false;
+
+    this->Xg= theXg.toDouble( &isOk );
+    if ( isOk ) {
+      this->Yg = theYg.toDouble( &isOk );
+      if ( isOk ) {
+        this->Xd = theXd.toDouble( &isOk );
+        if ( isOk ) {
+          this->Yd = theYd.toDouble( &isOk );
+          this->isIncomplete = !isOk;
+        }
+      }
+    }
+  }
+}
+
 //! Custom item delegate (line edit with double validator)
 class HYDROGUI_GeoreferencementDlg::Delegate : public QItemDelegate
 {
@@ -65,16 +92,10 @@ QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor(
   QWidget* anEditor = 0;
 
   if ( theIndex.column() > 0 ) {
-    QVariant aData = theIndex.data( Qt::DisplayRole );
-    
     QLineEdit* aLineEdit = new QLineEdit( theParent );
     aLineEdit->setValidator( new QDoubleValidator( aLineEdit ) );
-    aLineEdit->setText( aData.toString() );
-
     anEditor = aLineEdit;
-  }
-
-  if ( !anEditor) {
+  } else {
     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
   }
 
@@ -84,23 +105,25 @@ QWidget* HYDROGUI_GeoreferencementDlg::Delegate::createEditor(
 void HYDROGUI_GeoreferencementDlg::Delegate::setEditorData( 
   QWidget* theEditor, const QModelIndex& theIndex ) const
 {
-  QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
-  if ( anEditor ) {
-    anEditor->setText( theIndex.data( Qt::EditRole ).toString() );
+  if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
+    aLineEdit->setText( theIndex.data( Qt::EditRole ).toString() );
+  } else {
+    QItemDelegate::setEditorData( theEditor, theIndex );
   }
 }
 
 void HYDROGUI_GeoreferencementDlg::Delegate::setModelData( 
   QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
 {
-  QLineEdit* anEditor = qobject_cast<QLineEdit*>( theEditor );
-  if ( anEditor ) {
-    theModel->setData( theIndex, anEditor->text() );
+  if ( QLineEdit* aLineEdit = dynamic_cast<QLineEdit*>( theEditor ) ) {
+    theModel->setData( theIndex, aLineEdit->text() );
+  } else {
+    QItemDelegate::setModelData( theEditor, theModel, theIndex );
   }
 }
 
 HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* theModule, const QString& theTitle )
-: HYDROGUI_InputPanel( theModule, theTitle )
+: HYDROGUI_InputPanel( theModule, theTitle ), myIsModified( false )
 {
   // Mode selector (all/selected)
   QGroupBox* aModeGroup = new QGroupBox( tr( "PROFILES" ), this );
@@ -118,6 +141,13 @@ HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* the
   aModeSelectorLayout->addWidget( anAllRB );
   aModeSelectorLayout->addWidget( aSelectedRB );
 
+  // Update selection button
+  myUpdateSelBtn = new QPushButton( mainFrame() );
+  myUpdateSelBtn->setText( tr("UPDATE_SELECTION") );
+  QBoxLayout* anUpdateSelLayout = new QHBoxLayout( mainFrame() );
+  anUpdateSelLayout->addWidget( myUpdateSelBtn );
+  anUpdateSelLayout->addStretch();
+
   // Table
   myTable = new QTableWidget( mainFrame() );
   myTable->setItemDelegate( new Delegate( this ) );
@@ -132,10 +162,14 @@ HYDROGUI_GeoreferencementDlg::HYDROGUI_GeoreferencementDlg( HYDROGUI_Module* the
 
   // Layout
   addWidget( aModeGroup );
+  addLayout( anUpdateSelLayout );
   addWidget( myTable );
 
   // Connect signals and slots
   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
+  connect( myUpdateSelBtn, SIGNAL( clicked() ), this, SIGNAL( updateSelection() ) );
+  connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
+           this, SLOT( onDataChanged() ) );
 }
 
 HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
@@ -144,6 +178,7 @@ HYDROGUI_GeoreferencementDlg::~HYDROGUI_GeoreferencementDlg()
 
 void HYDROGUI_GeoreferencementDlg::onModeActivated( int theMode )
 {
+  myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
   emit modeActivated( theMode );
 }
 
@@ -165,11 +200,16 @@ void HYDROGUI_GeoreferencementDlg::setMode( const int theMode )
     aModeButton->setChecked( true );
   }
 
+  myUpdateSelBtn->setEnabled( theMode == SelectedProfiles );
+
   myModeButtons->blockSignals( isBlocked );
 }
 
 void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
 {
+  disconnect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
+              this, SLOT( onDataChanged() ) );
+
   myTable->setRowCount( 0 );
 
   foreach ( const QString& aProfileName, theMap.keys() ) {
@@ -181,7 +221,7 @@ void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
     // Get georeferencement data for the current profile
     ProfileGeoData aGeoData = theMap.value( aProfileName );
     QString aXg, anYg, aXd, anYd;
-    if ( aGeoData.isValid ) {
+    if ( !aGeoData.isEmpty ) {
       aXg = getString( aGeoData.Xg );
       anYg = getString( aGeoData.Yg );
       aXd = getString( aGeoData.Xd );
@@ -214,6 +254,12 @@ void HYDROGUI_GeoreferencementDlg::setData( const ProfilesGeoDataMap& theMap )
   }
 
   myTable->resizeColumnToContents( 0 );
+  myTable->resizeRowsToContents();
+
+  myIsModified = false;
+  
+  connect( myTable->model(), SIGNAL( dataChanged ( const QModelIndex&, const QModelIndex& ) ), 
+           this, SLOT( onDataChanged() ) );
 }
 
 void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
@@ -223,26 +269,16 @@ void HYDROGUI_GeoreferencementDlg::getData( ProfilesGeoDataMap& theMap )
 
   // Fill the map
   bool isOk = false;
+  QString aXg, anYg, aXd, anYd;
   for ( int aRow = 0; aRow < myTable->rowCount(); aRow++ ) {
     QString aProfileName = myTable->item( aRow, 0 )->text();
 
-    theMap.insert( aProfileName, ProfileGeoData() );
-
-    double aXg, anYg, aXd, anYd;
-      
-    aXg = myTable->item( aRow, 1 )->text().toDouble( &isOk );
-    if ( !isOk ) continue;
-
-    anYg = myTable->item( aRow, 2 )->text().toDouble( &isOk );
-    if ( !isOk ) continue;
-
-    aXd = myTable->item( aRow, 3 )->text().toDouble( &isOk );
-    if ( !isOk ) continue;
-
-    anYd = myTable->item( aRow, 4 )->text().toDouble( &isOk );
-    if ( !isOk ) continue;
+    aXg = myTable->item( aRow, 1 )->text();
+    anYg = myTable->item( aRow, 2 )->text();
+    aXd = myTable->item( aRow, 3 )->text();
+    anYd = myTable->item( aRow, 4 )->text();
    
-    theMap[aProfileName] = ProfileGeoData( aXg, anYg, aXd, anYd );
+    theMap.insert( aProfileName, ProfileGeoData( aXg, anYg, aXd, anYd ) );
   }
 }
 
@@ -279,4 +315,14 @@ void HYDROGUI_GeoreferencementDlg::onMousePress(
 QString HYDROGUI_GeoreferencementDlg::getString( const double theNumber ) const
 {
   return QString::number( theNumber, 'g', 12 );
+}
+
+void HYDROGUI_GeoreferencementDlg::onDataChanged()
+{
+  myIsModified = true;
+}
+
+bool HYDROGUI_GeoreferencementDlg::isModified() const
+{
+  return myIsModified;
 }
\ No newline at end of file