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, int theType )
42 : HYDROGUI_InputPanel( theModule, theTitle ),
45 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
47 QString fileGroupTitle = theType == Export ? tr( "EXPORT_STRICKLER_TABLE_FILE" ) : tr( "IMPORT_STRICKLER_TABLE_FILE" );
49 // Import Strickler table from file
50 QGroupBox* aFileNameGroup = new QGroupBox( fileGroupTitle, this );
51 aFileNameGroup->setVisible( theType != Edit );
53 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
55 myFileName = new QLineEdit( aFileNameGroup );
56 myFileName->setReadOnly( true );
58 QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
59 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
61 QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
62 aFileNameLayout->setMargin( 5 );
63 aFileNameLayout->setSpacing( 5 );
64 aFileNameLayout->addWidget( aFileNameLabel );
65 aFileNameLayout->addWidget( myFileName );
66 aFileNameLayout->addWidget( aBrowseBtn );
68 // Strickler table name
69 QGroupBox* aNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
71 QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), aNameGroup );
72 myName = new QLineEdit( aNameGroup );
74 QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
75 anImageNameLayout->setMargin( 5 );
76 anImageNameLayout->setSpacing( 5 );
77 anImageNameLayout->addWidget( anImageNameLabel );
78 anImageNameLayout->addWidget( myName );
81 QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
82 aTableGroup->setVisible( theType == Edit );
85 QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
86 aTableLayout->setMargin( 5 );
87 aTableLayout->setSpacing( 5 );
90 myAddBtn = new QToolButton;
91 myAddBtn->setText( tr( "ADD" ) );
92 myRemoveBtn = new QToolButton;
93 myRemoveBtn->setText( tr( "REMOVE" ) );
94 myClearBtn = new QToolButton;
95 myClearBtn->setText( tr( "CLEAR_ALL" ) );
98 myTable = new QTableWidget( mainFrame() );
99 myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
100 myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
101 QAbstractItemView::SelectedClicked |
102 QAbstractItemView::EditKeyPressed );
104 myTable->setColumnCount( 2 );
105 QStringList aColumnNames;
106 aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" );
107 myTable->setHorizontalHeaderLabels( aColumnNames );
109 myTable->horizontalHeader()->setStretchLastSection( false);
110 myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
111 myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
113 myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
117 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
118 aButtonsLayout->addWidget( myAddBtn );
119 aButtonsLayout->addWidget( myRemoveBtn );
120 aButtonsLayout->addStretch( 1 );
121 aButtonsLayout->addWidget( myClearBtn );
124 aTableLayout->addLayout( aButtonsLayout );
125 aTableLayout->addWidget( myTable );
128 addWidget( aFileNameGroup );
129 addWidget( aNameGroup );
130 addWidget( aTableGroup );
136 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
137 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( onAddCoefficient() ) );
138 connect( myRemoveBtn, SIGNAL( clicked() ), this, SLOT( onRemoveCoefficient() ) );
139 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( onClearCoefficients() ) );
140 connect( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
141 this, SLOT( onSelectionChanged() ) );
143 setMinimumWidth( 350 );
145 if ( theType != Edit )
149 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
153 void HYDROGUI_StricklerTableDlg::reset()
160 QString HYDROGUI_StricklerTableDlg::getFileName() const
162 return myFileName->text();
165 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
167 myFileName->setText( theName );
170 void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
172 myName->setText(theName);
175 QString HYDROGUI_StricklerTableDlg::getTableName() const
177 return myName->text();
180 bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
182 return myName->isReadOnly();
185 void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
187 myName->setReadOnly( on );
190 HYDROGUI_StricklerTableDlg::StricklerCoefficientList HYDROGUI_StricklerTableDlg::getData() const
192 StricklerCoefficientList aRes;
193 for ( int i = 0; i < myTable->rowCount(); i++ )
195 QTableWidgetItem* typeItem = myTable->item( i, 0 );
196 QTableWidgetItem* coeffItem = myTable->item( i, 1 );
197 aRes.append( StricklerCoefficient( typeItem->text(), coeffItem->text().toDouble() ) );
202 void HYDROGUI_StricklerTableDlg::setData(const StricklerCoefficientList& theData)
204 myTable->setRowCount( 0 );
206 foreach ( const StricklerCoefficient& aData, theData ) {
207 // Check the current Strickler type
208 if ( aData.myType.isEmpty() ) {
212 // Get Strickler coefficient value for the current Strickler type
213 QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( aData.myCoefficient, false );
215 // Insert row with the data
216 int aRow = myTable->rowCount();
217 myTable->insertRow( aRow );
220 QTableWidgetItem* aTypeItem = new QTableWidgetItem( aData.myType );
221 myTable->setItem( aRow, 0, aTypeItem );
223 // "Coefficient" column
224 myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
227 myTable->resizeColumnToContents( 0 );
228 myTable->resizeRowsToContents();
233 void HYDROGUI_StricklerTableDlg::updateControls()
235 bool isTableNotEmpty = myTable->rowCount() > 0;
236 myClearBtn->setEnabled( isTableNotEmpty );
237 onSelectionChanged();
240 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
242 QList<int> aSortedRows = theRows;
243 qSort( aSortedRows );
245 int aRowToRemove = -1;
247 foreach ( int aRow, aSortedRows ) {
248 aRowToRemove = aRow - aNbRemoved;
249 if ( myTable->model()->removeRow( aRowToRemove ) ) {
254 if ( aNbRemoved > 0 )
258 void HYDROGUI_StricklerTableDlg::onBrowse()
260 QString aFilter( tr( "STRICKLER_TABLE_FILTER" ) );
261 QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, myFileName->parent()->property( "title" ).toString(), myType == Import );
262 if ( !aFileName.isEmpty() )
264 setFileName( aFileName );
265 emit fileSelected( aFileName );
270 Add the new default constructed Strickler coefficient.
272 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
274 int aRow = myTable->rowCount();
275 myTable->insertRow( aRow );
277 // Set default type (=> generate unique type name) and coefficient
284 Remove the selected Strickler coefficient.
286 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
289 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
290 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
291 aRows << anIndex.row();
298 Clear all Strickler coefficients.
300 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
303 for ( int i=0; i< myTable->rowCount(); i++ ) {
311 Slot called on table selection change.
313 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
315 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
316 myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );