Salome HOME
refs #550: fix crash when myObject is NULL
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationDlg.cxx
index ece8150415041034e80a48e899dcd9832dcd49c3..c23683e9ac1c16eccefef716e6131012acc106fe 100644 (file)
@@ -1,12 +1,8 @@
-// Copyright (C) 2007-2013  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
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // 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.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_DataBrowser.h"
 #include "HYDROGUI_DataModel.h"
+#include "HYDROGUI_ListSelector.h"
 #include "HYDROGUI_Module.h"
 #include "HYDROGUI_DataObject.h"
 #include "HYDROGUI_NameValidator.h"
 #include "HYDROGUI_Region.h"
 #include "HYDROGUI_Zone.h"
+#include "HYDROGUI_OrderedListWidget.h"
+#include "HYDROGUI_PriorityWidget.h"
 
 #include <HYDROData_Document.h>
 #include <HYDROData_Entity.h>
 #include <SUIT_Study.h>
 
 #include <LightApp_Application.h>
+#include <LightApp_SelectionMgr.h>
 #include <SUIT_Desktop.h>
 #include <SUIT_MessageBox.h>
 
+#include <QButtonGroup>
+#include <QComboBox>
 #include <QGroupBox>
 #include <QLabel>
 #include <QLayout>
 #include <QListWidget>
 #include <QPicture>
 #include <QPushButton>
+#include <QRadioButton>
+#include <QSplitter>
+#include <QTableWidget>
 #include <QToolButton>
+#include <QTreeView>
 #include <QWizardPage>
-#include <QComboBox>
-#include <QStackedWidget>
+
 
 HYDROGUI_CalculationDlg::HYDROGUI_CalculationDlg( HYDROGUI_Module* theModule, const QString& theTitle )
 : HYDROGUI_Wizard( theModule, theTitle )
@@ -75,16 +80,30 @@ HYDROGUI_CalculationDlg::~HYDROGUI_CalculationDlg()
 void HYDROGUI_CalculationDlg::reset()
 {
   myObjectName->clear();
-  myGeomObjects->clear();
+  HYDROGUI_ListModel::Object2VisibleList anObject2VisibleList;
+  myGeomObjects->setObjects(anObject2VisibleList);
   myPolylineName->clear();
   myAvailableGeomObjects->clear();
+
+  // Activate the automatic mode
+  setMode( HYDROData_CalculationCase::AUTOMATIC );
+
+  // Reset the priority widget state
+  QList<Handle(HYDROData_Object)> anObjects;
+  myPriorityWidget->setObjects( anObjects );
 }
 
 QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
   QWizardPage* aPage = new QWizardPage( mainFrame() );
   QFrame* aFrame = new QFrame( aPage );
 
-  // Calculation name
+  // Splitter
+  mySplitter = new QSplitter(Qt::Vertical);
+
+  // Top of the page
+  QWidget* aTopContainer = new QWidget;
+   
+  // calculation name
   myObjectName = new QLineEdit( aPage );
   myValidator = new HYDROGUI_NameValidator(module(), myObjectName);
   myObjectName->setValidator( myValidator );
@@ -92,32 +111,49 @@ QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
   connect( myValidator, SIGNAL( emptyName() ), SLOT( onEmptyName() ) );
   connect( myValidator, SIGNAL( alreadyExists( QString ) ), SLOT( onAlreadyExists( QString ) ) );
 
+  // polyline name
   myPolylineName = new QComboBox( aPage );
   connect( myPolylineName, SIGNAL( activated( const QString & ) ), 
     SIGNAL( boundarySelected( const QString & ) ) );
 
-  myGeomObjects = new QListWidget( aPage );
-  myGeomObjects->setSelectionMode( QListWidget::ExtendedSelection );
-  myGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
-  myGeomObjects->setViewMode( QListWidget::ListMode );
-  myGeomObjects->setSortingEnabled( true );
+  // names labels
+  QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
+  QLabel* aLimitsLabel = new QLabel( tr( "LIMITS" ), aPage );
+
+  // mode selector (auto/manual)
+  QGroupBox* aModeGroup = new QGroupBox( tr( "MODE" ) );
+
+  QRadioButton* aManualRB = new QRadioButton( tr( "MANUAL" ), mainFrame() );
+  QRadioButton* anAutoRB = new QRadioButton( tr( "AUTO" ), mainFrame() );
 
+  myModeButtons = new QButtonGroup( mainFrame() );
+  myModeButtons->addButton( anAutoRB, HYDROData_CalculationCase::AUTOMATIC );
+  myModeButtons->addButton( aManualRB, HYDROData_CalculationCase::MANUAL );
+  
+  QBoxLayout* aModeSelectorLayout = new QHBoxLayout;
+  aModeSelectorLayout->setMargin( 5 );
+  aModeSelectorLayout->setSpacing( 5 );
+  aModeSelectorLayout->addWidget( anAutoRB );
+  aModeSelectorLayout->addWidget( aManualRB );
+  aModeGroup->setLayout( aModeSelectorLayout );
+
+  // geometry objects
+  QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ) );
+  myGeomObjects = new HYDROGUI_OrderedListWidget( aPage, 16 );
+  myGeomObjects->setHiddenObjectsShown(true);
+  myGeomObjects->setVisibilityIconShown(false);
+  myGeomObjects->setContentsMargins(QMargins());
+  // included geometry objects
+  QLabel* anIncludedLabel = new QLabel( tr( "INCLUDED_OBJECTS" ) );
   myAvailableGeomObjects = new QListWidget( aPage );
   myAvailableGeomObjects->setSelectionMode( QListWidget::ExtendedSelection );
   myAvailableGeomObjects->setEditTriggers( QListWidget::NoEditTriggers );
   myAvailableGeomObjects->setViewMode( QListWidget::ListMode );
   myAvailableGeomObjects->setSortingEnabled( true );
 
-  connect( myGeomObjects, SIGNAL( itemSelectionChanged() ), 
-    SIGNAL( objectsSelected() ) );
-
-  QFrame* anObjectsFrame = new QFrame( aPage );
-  QGridLayout* anObjsLayout = new QGridLayout( anObjectsFrame );
-  anObjsLayout->setMargin( 5 );
-  anObjsLayout->setSpacing( 5 );
-  anObjectsFrame->setLayout( anObjsLayout );
-
-  QFrame* aBtnsFrame = new QFrame( anObjectsFrame );
+  // buttons
+  QFrame* aBtnsFrame = new QFrame;
   QVBoxLayout* aBtnsLayout = new QVBoxLayout( aBtnsFrame );
   aBtnsLayout->setMargin( 5 );
   aBtnsLayout->setSpacing( 5 );
@@ -125,41 +161,82 @@ QWizardPage* HYDROGUI_CalculationDlg::createObjectsPage() {
   QPushButton* anAddBtn = new QPushButton( tr("INCLUDE"), aBtnsFrame );
   QPushButton* aRemoveBtn = new QPushButton( tr("EXCLUDE"), aBtnsFrame );
 
-  // Fill the butons frame with two buttons
+  // fill the butons frame with two buttons
   aBtnsLayout->addWidget( anAddBtn );
   aBtnsLayout->addWidget( aRemoveBtn );
   aBtnsLayout->addStretch( 1 );
-
-  QLabel* anIncludedLabel = new QLabel( tr( "INCLUDED_OBJECTS" ), anObjectsFrame );
-  QLabel* anObjectsLabel = new QLabel( tr( "CALCULATION_REFERENCE_OBJECTS" ), anObjectsFrame );
-
-  // Fill the objects frame with two lists, two labels and with buttons frame
+  
+  // top of the page layout
+  
+  // objects frame
+  QFrame* anObjectsFrame = new QFrame( aPage );
+  anObjectsFrame->setFrameStyle( QFrame::Panel | QFrame::Raised );
+  QGridLayout* anObjsLayout = new QGridLayout( anObjectsFrame );
+  anObjsLayout->setMargin( 5 );
+  anObjsLayout->setSpacing( 5 );
+  anObjectsFrame->setLayout( anObjsLayout );
+  
+  // fill the objects frame with two lists, two labels and with buttons frame
   anObjsLayout->addWidget( anObjectsLabel, 0, 0, Qt::AlignHCenter );
   anObjsLayout->addWidget( anIncludedLabel, 0, 2, Qt::AlignHCenter );
-  anObjsLayout->addWidget( myAvailableGeomObjects, 1, 0, Qt::AlignHCenter );
+  anObjsLayout->addWidget( myAvailableGeomObjects, 1, 0 );
   anObjsLayout->addWidget( aBtnsFrame, 1, 1, Qt::AlignHCenter );
-  anObjsLayout->addWidget( myGeomObjects, 1, 2, Qt::AlignHCenter );
-
-
-  QLabel* aNameLabel = new QLabel( tr( "NAME" ), aPage );
-  QLabel* aLimitsLabel = new QLabel( tr( "LIMITS" ), aPage );
-
-  // Fill the page
-  QGridLayout* aPageLayout = new QGridLayout( aPage );
+  anObjsLayout->addWidget( myGeomObjects, 1, 2 );
+  
+  // fill the top of the page
+  QGridLayout* aTopLayout = new QGridLayout;
+  aTopLayout->setMargin( 5 );
+  aTopLayout->setSpacing( 5 );
+  aTopLayout->setVerticalSpacing( 10 );
+  aTopLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
+  aTopLayout->addWidget( myObjectName,   0, 1 );
+  aTopLayout->addWidget( aLimitsLabel,   1, 0, Qt::AlignHCenter );
+  aTopLayout->addWidget( myPolylineName, 1, 1 );
+  aTopLayout->addWidget( aModeGroup, 2, 0, 1, 2 );
+  aTopLayout->addWidget( anObjectsFrame, 3, 0, 1, 2 );
+
+  aTopContainer->setLayout( aTopLayout );
+
+  // add the top of the page to the splitter
+  mySplitter->insertWidget(0, aTopContainer);
+  mySplitter->setStretchFactor(0, 2);
+
+  // Bottom of the page
+  myPriorityWidget = new HYDROGUI_PriorityWidget( mainFrame() );
+
+  QGroupBox* aPriorityGroup = new QGroupBox( tr( "PRIORITY" ) );
+  QBoxLayout* aPriorityLayout = new QHBoxLayout;
+  aPriorityLayout->setMargin( 5 );
+  aPriorityLayout->setSpacing( 5 );
+  aPriorityLayout->addWidget( myPriorityWidget );
+  aPriorityGroup->setLayout( aPriorityLayout );
+
+  // add the bottom of the page to the splitter
+  mySplitter->insertWidget(1, aPriorityGroup);
+  mySplitter->setStretchFactor(1, 1);
+
+  // Page layout
+  QVBoxLayout* aPageLayout = new QVBoxLayout;
   aPageLayout->setMargin( 5 );
   aPageLayout->setSpacing( 5 );
-  aPageLayout->setVerticalSpacing( 10 );
-  aPageLayout->addWidget( aNameLabel,     0, 0, Qt::AlignHCenter );
-  aPageLayout->addWidget( myObjectName,   0, 1 );
-  aPageLayout->addWidget( aLimitsLabel,   1, 0, Qt::AlignHCenter );
-  aPageLayout->addWidget( myPolylineName, 1, 1 );
-  aPageLayout->addWidget( anObjectsFrame, 2, 0, 1, 2, Qt::AlignHCenter );
+  aPageLayout->addWidget( mySplitter );
 
   aPage->setLayout( aPageLayout );
 
+  // Create selector
+  if ( module() ) {
+    HYDROGUI_ListSelector* aListSelector = 
+      new HYDROGUI_ListSelector( myGeomObjects, module()->getApp()->selectionMgr() );
+    aListSelector->setAutoBlock( true );
+  }
+
+  // Connections
+  connect( myModeButtons, SIGNAL( buttonClicked( int ) ), SIGNAL( changeMode( int ) ) );
   connect( anAddBtn, SIGNAL( clicked() ), SIGNAL( addObjects() ) );
   connect( aRemoveBtn, SIGNAL( clicked() ), SIGNAL( removeObjects() ) );
 
+  connect( myGeomObjects, SIGNAL( orderChanged() ), SLOT( onOrderChanged() ) );
+
   return aPage;
 }
 
@@ -207,16 +284,16 @@ QWizardPage* HYDROGUI_CalculationDlg::createGroupsPage() {
   // Fill the objects frame with two lists, two labels and with buttons frame
   aGroupsLayout->addWidget( anAvailableLabel, 0, 0, Qt::AlignHCenter );
   aGroupsLayout->addWidget( anIncludedLabel, 0, 2, Qt::AlignHCenter );
-  aGroupsLayout->addWidget( myAvailableGroups, 1, 0, Qt::AlignHCenter );
+  aGroupsLayout->addWidget( myAvailableGroups, 1, 0 );
   aGroupsLayout->addWidget( aBtnsFrame, 1, 1, Qt::AlignHCenter );
-  aGroupsLayout->addWidget( myGroups, 1, 2, Qt::AlignHCenter );
+  aGroupsLayout->addWidget( myGroups, 1, 2 );
 
   // Fill the page
   QGridLayout* aPageLayout = new QGridLayout( aPage );
   aPageLayout->setMargin( 5 );
   aPageLayout->setSpacing( 5 );
   aPageLayout->setVerticalSpacing( 10 );
-  aPageLayout->addWidget( aGroupsFrame, 0, 0, Qt::AlignHCenter );
+  aPageLayout->addWidget( aGroupsFrame, 0, 0 );
 
   aPage->setLayout( aPageLayout );
 
@@ -239,13 +316,13 @@ QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
 
   aLayout->addWidget( myBrowser, 0, 0, 1, 2 );
 
-  myBatimetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
+  myBathymetryLabel = new QLabel( tr( "BATHYMETRY" ), aFrame );
   myBathymetryChoice = new QComboBox( aFrame );
 
   myBathymetryChoice->setVisible( false );
-  myBatimetryLabel->setVisible( false );
+  myBathymetryLabel->setVisible( false );
 
-  aLayout->addWidget( myBatimetryLabel, 1, 0 );
+  aLayout->addWidget( myBathymetryLabel, 1, 0 );
   aLayout->addWidget( myBathymetryChoice, 1, 1 );
 
   aPage->setLayout( aLayout );
@@ -257,6 +334,7 @@ QWizardPage* HYDROGUI_CalculationDlg::createZonesPage() {
   connect( myBrowser, 
       SIGNAL( dropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ),
       SLOT( onZonesDropped( const QList<SUIT_DataObject*>&, SUIT_DataObject*, int, Qt::DropAction ) ) );
+  connect( myBrowser, SIGNAL( newRegion() ), this, SLOT( OnNewRegion() ) );
 
   return aPage;
 }
@@ -266,7 +344,7 @@ bool HYDROGUI_CalculationDlg::acceptCurrent() const
 {
   QString anErrorMsg;
 
-  if ( myGeomObjects->count() == 0 )
+  if ( false /*myGeomObjects->count() == 0*/ )
   {
     anErrorMsg = tr( "EMPTY_GEOMETRY_OBJECTS" );
   }
@@ -331,6 +409,11 @@ void HYDROGUI_CalculationDlg::onZonesDropped( const QList<SUIT_DataObject*>& the
   }
 }
 
+void HYDROGUI_CalculationDlg::OnNewRegion()
+{
+  emit createRegion( myBrowser->getSelected() );
+}
+
 void HYDROGUI_CalculationDlg::onMergeTypeSelected( int theIndex )
 {
   int aType = myBathymetryChoice->itemData( theIndex ).toInt();
@@ -356,7 +439,7 @@ void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
     myBathymetryChoice->addItem( tr("MERGE_UNKNOWN"), HYDROData_Zone::Merge_UNKNOWN );
     myBathymetryChoice->addItem( tr("MERGE_ZMIN"), HYDROData_Zone::Merge_ZMIN );
     myBathymetryChoice->addItem( tr("MERGE_ZMAX"), HYDROData_Zone::Merge_ZMAX );
-    QStringList aList = aZone->getBathymetries();
+    QStringList aList = aZone->getAltitudes();
     for ( int i = 0; i < aList.length(); i++ )
     {
       myBathymetryChoice->addItem( aList.at( i ), HYDROData_Zone::Merge_Object );
@@ -372,7 +455,7 @@ void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
         aCurIndex = 2;
         break;
       case HYDROData_Zone::Merge_Object:
-        aCurIndex = 3 + aList.indexOf( aZone->text( HYDROGUI_DataObject::BathymetryId ) );
+        aCurIndex = 3 + aList.indexOf( aZone->text( HYDROGUI_DataObject::AltitudeObjId ) );
         break;
       default:
         aCurIndex = 0; // Select unknown by default
@@ -382,7 +465,8 @@ void HYDROGUI_CalculationDlg::onSelected( SUIT_DataObject* theObject )
   }
 
   myBathymetryChoice->setVisible( doShow );
-  myBatimetryLabel->setVisible( doShow );
+  myBathymetryChoice->setEnabled( getMode() == HYDROData_CalculationCase::MANUAL );
+  myBathymetryLabel->setVisible( doShow );
 }
 
 void HYDROGUI_CalculationDlg::setObjectName( const QString& theName )
@@ -418,12 +502,39 @@ void moveItems( QListWidget* theSource, QListWidget* theDest, const QStringList&
 
 void HYDROGUI_CalculationDlg::includeGeomObjects( const QStringList& theObjects )
 {
-  moveItems( myAvailableGeomObjects, myGeomObjects, theObjects );
+  HYDROGUI_ListModel::Object2VisibleList anObjectsToInclude;
+  QList<QListWidgetItem*> aFoundItems;
+  foreach ( const QString& anObjName, theObjects ) {
+    // Hide the object in the available objects list
+    aFoundItems = myAvailableGeomObjects->findItems( anObjName, Qt::MatchExactly );
+    foreach ( QListWidgetItem* anItem, aFoundItems ) {
+      anItem->setHidden( true );
+    }
+
+    // Add the object to the list of included objects
+    Handle(HYDROData_Entity) anObject = 
+      HYDROGUI_Tool::FindObjectByName( module(), anObjName );
+    myGeomObjects->addObject( HYDROGUI_ListModel::Object2Visible( anObject, true ) );
+  }
+
+  myPriorityWidget->setObjects( getGeometryObjects() );
 }
 
 void HYDROGUI_CalculationDlg::excludeGeomObjects( const QStringList& theObjects )
 {
-  moveItems( myGeomObjects, myAvailableGeomObjects, theObjects );
+  QList<QListWidgetItem*> aFoundItems;
+  foreach ( const QString& anObjName, theObjects ) {
+    // Set visible the object in the available objects list
+    aFoundItems = myAvailableGeomObjects->findItems( anObjName, Qt::MatchExactly );
+    foreach ( QListWidgetItem* anItem, aFoundItems ) {
+      anItem->setHidden( false );
+    }
+
+    // Remove the object from the list of included objects
+    myGeomObjects->removeObjectByName( anObjName );
+  }
+
+  myPriorityWidget->setObjects( getGeometryObjects() );
 }
 
 void HYDROGUI_CalculationDlg::setBoundary( const QString& theObjName )
@@ -470,7 +581,12 @@ QStringList getSelected( QListWidget* theWidget )
 
 QStringList HYDROGUI_CalculationDlg::getSelectedGeomObjects() const
 {
-  return getSelected( myGeomObjects );
+  return myGeomObjects->getSelectedNames();
+}
+
+QStringList HYDROGUI_CalculationDlg::getAllGeomObjects() const
+{
+  return myGeomObjects->getAllNames();
 }
 
 QStringList HYDROGUI_CalculationDlg::getSelectedAvailableGeomObjects() const
@@ -491,7 +607,6 @@ void HYDROGUI_CalculationDlg::setEditedObject( const Handle(HYDROData_Calculatio
   myBrowser->adjustColumnsWidth();
   myBrowser->setAutoUpdate( true );
   myBrowser->setUpdateModified( true );
-
 }
 
 HYDROGUI_Zone* HYDROGUI_CalculationDlg::getCurrentZone() const
@@ -541,3 +656,92 @@ void HYDROGUI_CalculationDlg::excludeGroups( const QStringList& theObjects )
 {
   moveItems( myGroups, myAvailableGroups, theObjects );
 }
+
+/**
+  Get creation mode.
+  @param theMode the mode
+*/
+int HYDROGUI_CalculationDlg::getMode() const
+{
+  return myModeButtons->checkedId();
+}
+
+/**
+  Set creation mode.
+  @param theMode the mode
+*/
+void HYDROGUI_CalculationDlg::setMode( int theMode )
+{
+  bool isBlocked = myModeButtons->blockSignals( true );
+  myModeButtons->button( theMode )->setChecked( true );
+  myModeButtons->blockSignals( isBlocked );
+
+  bool isAuto = ( theMode == HYDROData_CalculationCase::AUTOMATIC );
+
+  myGeomObjects->setOrderingEnabled( isAuto );
+  QWidget* aWidget = mySplitter->widget( 1 );
+  if ( aWidget ) {
+    aWidget->setVisible( isAuto );
+  }
+}
+
+/**
+  Enable/disable zones drag'n'drop and renaming.
+  @param theIsEnabled if true - zones drag'n'drop and renaming will be enabled
+*/
+void HYDROGUI_CalculationDlg::setEditZonesEnabled( const bool theIsEnabled )
+{
+  myBrowser->setReadOnly( !theIsEnabled );
+}
+
+/**
+  Get included geometry objects.
+  @return the list of geometry objects
+ */
+QList<Handle(HYDROData_Object)> HYDROGUI_CalculationDlg::getGeometryObjects()
+{
+  QList<Handle(HYDROData_Entity)> anEntities = myGeomObjects->getObjects();
+  QList<Handle(HYDROData_Object)> anObjects;
+
+  foreach ( Handle(HYDROData_Entity) anEntity, anEntities ) {
+    Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( anEntity );
+    if ( anObj.IsNull() ) {
+      continue;
+    }
+
+    anObjects << anObj;
+  }
+
+  return anObjects;
+}
+
+/**
+  Get rules.
+  @return the list of rules
+ */
+HYDROData_ListOfRules HYDROGUI_CalculationDlg::getRules() const
+{
+  return myPriorityWidget->getRules();
+}
+
+/**
+  Set rules.
+  @param theRules the list of rules
+ */
+void  HYDROGUI_CalculationDlg::setRules( const HYDROData_ListOfRules& theRules ) const
+{
+  myPriorityWidget->setRules( theRules );
+}
+
+/**
+  Slot called when objects order is changed.
+ */
+void HYDROGUI_CalculationDlg::onOrderChanged()
+{
+  bool isConfirmed = true;
+  emit orderChanged( isConfirmed );
+  if( isConfirmed )
+    myPriorityWidget->setObjects( getGeometryObjects() );
+  else
+    myGeomObjects->undoLastMove();
+}