1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_StricklerTableDlg.h"
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_Tool.h"
23 #include "HYDROGUI_LineEditDoubleValidator.h"
25 #include <LightApp_Application.h>
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_FileDlg.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_MessageBox.h>
35 #include <QToolButton>
38 #include <QTableWidget>
39 #include <QHeaderView>
41 HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle )
42 : HYDROGUI_InputPanel( theModule, theTitle ), myName(NULL)
44 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
46 // Import Strickler table from file
47 myFileNameGroup = new QGroupBox( tr( "IMPORT_STRICKLER_TABLE_FROM_FILE" ), this );
49 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
51 myFileName = new QLineEdit( myFileNameGroup );
52 myFileName->setReadOnly( true );
54 myBrowseBtn = new QToolButton( myFileNameGroup );
55 myBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
57 QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
58 aFileNameLayout->setMargin( 5 );
59 aFileNameLayout->setSpacing( 5 );
60 aFileNameLayout->addWidget( aFileNameLabel );
61 aFileNameLayout->addWidget( myFileName );
62 aFileNameLayout->addWidget( myBrowseBtn );
64 // Strickler table name
65 myNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
67 QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
68 myName = new QLineEdit( myNameGroup );
70 QBoxLayout* anImageNameLayout = new QHBoxLayout( myNameGroup );
71 anImageNameLayout->setMargin( 5 );
72 anImageNameLayout->setSpacing( 5 );
73 anImageNameLayout->addWidget( anImageNameLabel );
74 anImageNameLayout->addWidget( myName );
77 myTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
80 QVBoxLayout* aTableLayout = new QVBoxLayout( myTableGroup );
81 aTableLayout->setMargin( 5 );
82 aTableLayout->setSpacing( 5 );
85 myAddBtn = new QToolButton;
86 myAddBtn->setText( tr( "ADD" ) );
87 myRemoveBtn = new QToolButton;
88 myRemoveBtn->setText( tr( "REMOVE" ) );
89 myClearBtn = new QToolButton;
90 myClearBtn->setText( tr( "CLEAR_ALL" ) );
93 myTable = new QTableWidget( mainFrame() );
94 myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
95 myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
96 QAbstractItemView::SelectedClicked |
97 QAbstractItemView::EditKeyPressed );
99 myTable->setColumnCount( 2 );
100 QStringList aColumnNames;
101 aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" );
102 myTable->setHorizontalHeaderLabels( aColumnNames );
104 myTable->horizontalHeader()->setStretchLastSection( false);
105 myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
106 myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
108 myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
112 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
113 aButtonsLayout->addWidget( myAddBtn );
114 aButtonsLayout->addWidget( myRemoveBtn );
115 aButtonsLayout->addStretch( 1 );
116 aButtonsLayout->addWidget( myClearBtn );
119 aTableLayout->addLayout( aButtonsLayout );
120 aTableLayout->addWidget( myTable );
123 addWidget( myFileNameGroup );
124 addWidget( myNameGroup );
125 addWidget( myTableGroup );
131 connect( myBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
132 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( onAddCoefficient() ) );
133 connect( myRemoveBtn, SIGNAL( clicked() ), this, SLOT( onRemoveCoefficient() ) );
134 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( onClearCoefficients() ) );
135 connect( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
136 this, SLOT( onSelectionChanged() ) );
138 setMinimumWidth( 350 );
141 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
145 void HYDROGUI_StricklerTableDlg::setIsEdit( const bool theIsEdit )
147 myFileNameGroup->setVisible( !theIsEdit );
148 myNameGroup->setEnabled( theIsEdit );
149 myTableGroup->setVisible( theIsEdit );
154 void HYDROGUI_StricklerTableDlg::reset()
159 myNameGroup->setEnabled( false );
162 QString HYDROGUI_StricklerTableDlg::getFileName() const
164 return myFileName->text();
167 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
169 myFileName->setText( theName );
171 if ( !myNameGroup->isEnabled() )
172 myNameGroup->setEnabled( true );
175 void HYDROGUI_StricklerTableDlg::setStricklerTableName( const QString& theName )
177 myName->setText(theName);
180 QString HYDROGUI_StricklerTableDlg::getStricklerTableName() const
182 return myName->text();
185 void HYDROGUI_StricklerTableDlg::setData(const StricklerCoefficientList& theData)
187 myTable->setRowCount( 0 );
189 foreach ( const StricklerCoefficient& aData, theData ) {
190 // Check the current Strickler type
191 if ( aData.myType.isEmpty() ) {
195 // Get Strickler coefficient value for the current Strickler type
196 QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( aData.myCoefficient, false );
198 // Insert row with the data
199 int aRow = myTable->rowCount();
200 myTable->insertRow( aRow );
203 QTableWidgetItem* aTypeItem = new QTableWidgetItem( aData.myType );
204 myTable->setItem( aRow, 0, aTypeItem );
206 // "Coefficient" column
207 myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
210 myTable->resizeColumnToContents( 0 );
211 myTable->resizeRowsToContents();
216 void HYDROGUI_StricklerTableDlg::updateControls()
218 bool isTableNotEmpty = myTable->rowCount() > 0;
219 myClearBtn->setEnabled( isTableNotEmpty );
220 onSelectionChanged();
223 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
225 QList<int> aSortedRows = theRows;
226 qSort( aSortedRows );
228 int aRowToRemove = -1;
230 foreach ( int aRow, aSortedRows ) {
231 aRowToRemove = aRow - aNbRemoved;
232 if ( myTable->model()->removeRow( aRowToRemove ) ) {
237 if ( aNbRemoved > 0 )
241 void HYDROGUI_StricklerTableDlg::onBrowse()
243 QString aFilter( tr( "STRICKLER_TABLE_FILTER" ) );
244 QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_STRICKLER_TABLE_FROM_FILE" ), true );
245 if( !aFileName.isEmpty() )
247 setFileName( aFileName );
248 emit fileSelected( aFileName );
253 Add the new default constructed Strickler coefficient.
255 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
257 int aRow = myTable->rowCount();
258 myTable->insertRow( aRow );
260 // Set default type (=> generate unique type name) and coefficient
267 Remove the selected Strickler coefficient.
269 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
272 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
273 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
274 aRows << anIndex.row();
281 Clear all Strickler coefficients.
283 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
286 for ( int i=0; i< myTable->rowCount(); i++ ) {
294 Slot called on table selection change.
296 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
298 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
299 myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );