//
#include "HYDROGUI_StricklerTableDlg.h"
+
#include "HYDROGUI_Module.h"
+#include "HYDROGUI_Tool.h"
+#include "HYDROGUI_LineEditDoubleValidator.h"
#include <LightApp_Application.h>
#include <QToolButton>
#include <QLayout>
#include <QLabel>
+#include <QTableWidget>
+#include <QHeaderView>
-HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle )
-: HYDROGUI_InputPanel( theModule, theTitle ), myName(NULL)
+HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle, int theType )
+: HYDROGUI_InputPanel( theModule, theTitle ),
+ myType( theType )
{
- SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
+ SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
+
+ QString fileGroupTitle = theType == Export ? tr( "EXPORT_STRICKLER_TABLE_FILE" ) : tr( "IMPORT_STRICKLER_TABLE_FILE" );
+
+ // Import Strickler table from file
+ QGroupBox* aFileNameGroup = new QGroupBox( fileGroupTitle, this );
+ aFileNameGroup->setVisible( theType != Edit );
+
+ QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
+
+ myFileName = new QLineEdit( aFileNameGroup );
+ myFileName->setReadOnly( true );
+
+ QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
+ aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
+
+ QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
+ aFileNameLayout->setMargin( 5 );
+ aFileNameLayout->setSpacing( 5 );
+ aFileNameLayout->addWidget( aFileNameLabel );
+ aFileNameLayout->addWidget( myFileName );
+ aFileNameLayout->addWidget( aBrowseBtn );
+
+ // Strickler table name
+ QGroupBox* aNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
- // Import Strickler table from file
- myFileNameGroup = new QGroupBox( tr( "IMPORT_STRICKLER_TABLE_FROM_FILE" ), this );
+ QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), aNameGroup );
+ myName = new QLineEdit( aNameGroup );
- QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
+ QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
+ anImageNameLayout->setMargin( 5 );
+ anImageNameLayout->setSpacing( 5 );
+ anImageNameLayout->addWidget( anImageNameLabel );
+ anImageNameLayout->addWidget( myName );
- myFileName = new QLineEdit( myFileNameGroup );
- myFileName->setReadOnly( true );
+ // Strickler table
+ QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
+ aTableGroup->setVisible( theType == Edit );
- myBrowseBtn = new QToolButton( myFileNameGroup );
- myBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
+ // Main layout
+ QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
+ aTableLayout->setMargin( 5 );
+ aTableLayout->setSpacing( 5 );
- QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
- aFileNameLayout->setMargin( 5 );
- aFileNameLayout->setSpacing( 5 );
- aFileNameLayout->addWidget( aFileNameLabel );
- aFileNameLayout->addWidget( myFileName );
- aFileNameLayout->addWidget( myBrowseBtn );
+ // Buttons
+ myAddBtn = new QToolButton;
+ myAddBtn->setText( tr( "ADD" ) );
+ myRemoveBtn = new QToolButton;
+ myRemoveBtn->setText( tr( "REMOVE" ) );
+ myClearBtn = new QToolButton;
+ myClearBtn->setText( tr( "CLEAR_ALL" ) );
- // Strickler table name
- myNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
+ // Table
+ myTable = new QTableWidget( mainFrame() );
+ myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
+ myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
+ QAbstractItemView::SelectedClicked |
+ QAbstractItemView::EditKeyPressed );
- QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
- myName = new QLineEdit( myNameGroup );
+ myTable->setColumnCount( 2 );
+ QStringList aColumnNames;
+ aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" );
+ myTable->setHorizontalHeaderLabels( aColumnNames );
- QBoxLayout* anImageNameLayout = new QHBoxLayout( myNameGroup );
- anImageNameLayout->setMargin( 5 );
- anImageNameLayout->setSpacing( 5 );
- anImageNameLayout->addWidget( anImageNameLabel );
- anImageNameLayout->addWidget( myName );
+ myTable->horizontalHeader()->setStretchLastSection( false);
+ myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
+ myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
- // Strickler table
- //...
+ myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
- // Common
- addWidget( myFileNameGroup );
- addWidget( myNameGroup );
- //addWidget( myTableGroup );
- addStretch();
+ // Layout
+ // buttons
+ QHBoxLayout* aButtonsLayout = new QHBoxLayout();
+ aButtonsLayout->addWidget( myAddBtn );
+ aButtonsLayout->addWidget( myRemoveBtn );
+ aButtonsLayout->addStretch( 1 );
+ aButtonsLayout->addWidget( myClearBtn );
- connect( myBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
+ // main
+ aTableLayout->addLayout( aButtonsLayout );
+ aTableLayout->addWidget( myTable );
- setMinimumWidth( 350 );
+ // Common
+ addWidget( aFileNameGroup );
+ addWidget( aNameGroup );
+ addWidget( aTableGroup );
+
+ // Update controls
+ updateControls();
+
+ // Connections
+ connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
+ connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( onAddCoefficient() ) );
+ connect( myRemoveBtn, SIGNAL( clicked() ), this, SLOT( onRemoveCoefficient() ) );
+ connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( onClearCoefficients() ) );
+ connect( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
+ this, SLOT( onSelectionChanged() ) );
+
+ setMinimumWidth( 350 );
+
+ if ( theType != Edit )
+ addStretch();
}
HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
{
}
-void HYDROGUI_StricklerTableDlg::setIsEdit( const bool theIsEdit )
+void HYDROGUI_StricklerTableDlg::reset()
{
- myFileNameGroup->setVisible( !theIsEdit );
- myNameGroup->setEnabled( theIsEdit );
- //myTableGroup->setEnabled( theIsEdit );
+ myFileName->clear();
+
+ myName->clear();
}
-void HYDROGUI_StricklerTableDlg::reset()
+QString HYDROGUI_StricklerTableDlg::getFileName() const
{
- myFileName->clear();
+ return myFileName->text();
+}
- myName->clear();
- myNameGroup->setEnabled( false );
-
- //myTableGroup->setEnabled( false );
+void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
+{
+ myFileName->setText( theName );
+}
- //myIsInitialized = false;
+void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
+{
+ myName->setText(theName);
}
-QString HYDROGUI_StricklerTableDlg::getFileName() const
+QString HYDROGUI_StricklerTableDlg::getTableName() const
{
- return myFileName->text();
+ return myName->text();
}
-void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
+bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
{
- myFileName->setText( theName );
+ return myName->isReadOnly();
}
-void HYDROGUI_StricklerTableDlg::setStricklerTableName( const QString& theName )
+void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
{
- myName->setText(theName);
+ myName->setReadOnly( on );
}
-QString HYDROGUI_StricklerTableDlg::getStricklerTableName() const
+HYDROGUI_StricklerTableDlg::StricklerCoefficientList HYDROGUI_StricklerTableDlg::getData() const
{
- return myName->text();
+ StricklerCoefficientList aRes;
+ for ( int i = 0; i < myTable->rowCount(); i++ )
+ {
+ QTableWidgetItem* typeItem = myTable->item( i, 0 );
+ QTableWidgetItem* coeffItem = myTable->item( i, 1 );
+ aRes.append( StricklerCoefficient( typeItem->text(), coeffItem->text().toDouble() ) );
+ }
+ return aRes;
+}
+
+void HYDROGUI_StricklerTableDlg::setData(const StricklerCoefficientList& theData)
+{
+ myTable->setRowCount( 0 );
+
+ foreach ( const StricklerCoefficient& aData, theData ) {
+ // Check the current Strickler type
+ if ( aData.myType.isEmpty() ) {
+ continue;
+ }
+
+ // Get Strickler coefficient value for the current Strickler type
+ QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( aData.myCoefficient, false );
+
+ // Insert row with the data
+ int aRow = myTable->rowCount();
+ myTable->insertRow( aRow );
+
+ // "Type" column
+ QTableWidgetItem* aTypeItem = new QTableWidgetItem( aData.myType );
+ myTable->setItem( aRow, 0, aTypeItem );
+
+ // "Coefficient" column
+ myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
+ }
+
+ myTable->resizeColumnToContents( 0 );
+ myTable->resizeRowsToContents();
+
+ updateControls();
+}
+
+void HYDROGUI_StricklerTableDlg::updateControls()
+{
+ bool isTableNotEmpty = myTable->rowCount() > 0;
+ myClearBtn->setEnabled( isTableNotEmpty );
+ onSelectionChanged();
+}
+
+void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
+{
+ QList<int> aSortedRows = theRows;
+ qSort( aSortedRows );
+
+ int aRowToRemove = -1;
+ int aNbRemoved = 0;
+ foreach ( int aRow, aSortedRows ) {
+ aRowToRemove = aRow - aNbRemoved;
+ if ( myTable->model()->removeRow( aRowToRemove ) ) {
+ aNbRemoved++;
+ }
+ }
+
+ if ( aNbRemoved > 0 )
+ updateControls();
}
void HYDROGUI_StricklerTableDlg::onBrowse()
{
- QString aFilter( tr( "STRICKLER_TABLE_FILTER" ) );
- QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_STRICKLER_TABLE_FROM_FILE" ), true );
- if( !aFileName.isEmpty() )
- {
- setFileName( aFileName );
- emit fileSelected( aFileName );
- }
+ QString aFilter( tr( "STRICKLER_TABLE_FILTER" ) );
+ QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, myFileName->parent()->property( "title" ).toString(), myType == Import );
+ if ( !aFileName.isEmpty() )
+ {
+ setFileName( aFileName );
+ emit fileSelected( aFileName );
+ }
+}
+
+/**
+Add the new default constructed Strickler coefficient.
+*/
+void HYDROGUI_StricklerTableDlg::onAddCoefficient()
+{
+ int aRow = myTable->rowCount();
+ myTable->insertRow( aRow );
+
+ // Set default type (=> generate unique type name) and coefficient
+ //...
+
+ updateControls();
+}
+
+/**
+Remove the selected Strickler coefficient.
+*/
+void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
+{
+ QList<int> aRows;
+ QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
+ foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
+ aRows << anIndex.row();
+ }
+
+ removeRows( aRows );
+}
+
+/**
+Clear all Strickler coefficients.
+*/
+void HYDROGUI_StricklerTableDlg::onClearCoefficients()
+{
+ QList<int> aRows;
+ for ( int i=0; i< myTable->rowCount(); i++ ) {
+ aRows << i;
+ }
+
+ removeRows( aRows );
+}
+
+/**
+Slot called on table selection change.
+*/
+void HYDROGUI_StricklerTableDlg::onSelectionChanged()
+{
+ QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
+ myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );
}