Salome HOME
Merge branch 'BR_LAND_COVER' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StricklerTableDlg.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_StricklerTableDlg.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_Tool.h"
23 #include "HYDROGUI_LineEditDoubleValidator.h"
24
25 #include <LightApp_Application.h>
26
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>
32
33 #include <QGroupBox>
34 #include <QLineEdit>
35 #include <QToolButton>
36 #include <QLayout>
37 #include <QLabel>
38 #include <QTableWidget>
39 #include <QHeaderView>
40
41 HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle, int theType )
42 : HYDROGUI_InputPanel( theModule, theTitle ),
43   myType( theType )
44 {
45     SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
46
47     QString fileGroupTitle = theType == Export ? tr( "EXPORT_STRICKLER_TABLE_FILE" ) : tr( "IMPORT_STRICKLER_TABLE_FILE" );
48
49     // Import Strickler table from file
50     QGroupBox* aFileNameGroup = new QGroupBox( fileGroupTitle, this );
51     aFileNameGroup->setVisible( theType != Edit );
52
53     QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
54
55     myFileName = new QLineEdit( aFileNameGroup );
56     myFileName->setReadOnly( true );
57
58     QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
59     aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
60
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 );
67
68     // Strickler table name
69     QGroupBox* aNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
70
71     QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), aNameGroup );
72     myName = new QLineEdit( aNameGroup );
73
74     QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
75     anImageNameLayout->setMargin( 5 );
76     anImageNameLayout->setSpacing( 5 );
77     anImageNameLayout->addWidget( anImageNameLabel );
78     anImageNameLayout->addWidget( myName );
79
80     // Strickler table
81     QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
82     aTableGroup->setVisible( theType == Edit );
83
84     // Main layout
85     QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
86     aTableLayout->setMargin( 5 );
87     aTableLayout->setSpacing( 5 );
88
89     // Buttons
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" ) );
96
97     // Table
98     myTable = new QTableWidget( mainFrame() );
99     myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
100     myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
101         QAbstractItemView::SelectedClicked |
102         QAbstractItemView::EditKeyPressed );
103
104     myTable->setColumnCount( 2 );
105     QStringList aColumnNames;
106     aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" );
107     myTable->setHorizontalHeaderLabels( aColumnNames );
108
109     myTable->horizontalHeader()->setStretchLastSection( false);
110     myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
111     myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
112
113     myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
114
115     // Layout
116     // buttons
117     QHBoxLayout* aButtonsLayout = new QHBoxLayout();
118     aButtonsLayout->addWidget( myAddBtn );
119     aButtonsLayout->addWidget( myRemoveBtn );
120     aButtonsLayout->addStretch( 1 );
121     aButtonsLayout->addWidget( myClearBtn );
122
123     // main
124     aTableLayout->addLayout( aButtonsLayout );
125     aTableLayout->addWidget( myTable );
126
127     // Common
128     addWidget( aFileNameGroup );
129     addWidget( aNameGroup );
130     addWidget( aTableGroup );
131
132     // Update controls
133     updateControls();
134
135     // Connections
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() ) );  
142
143     setMinimumWidth( 350 );
144
145     if ( theType != Edit )
146         addStretch();
147 }
148
149 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
150 {
151 }
152
153 void HYDROGUI_StricklerTableDlg::reset()
154 {
155     myFileName->clear();
156
157     myName->clear();
158 }
159
160 QString HYDROGUI_StricklerTableDlg::getFileName() const
161 {
162     return myFileName->text();
163 }
164
165 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
166 {
167     myFileName->setText( theName );
168 }
169
170 void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
171 {
172     myName->setText(theName);
173 }
174
175 QString HYDROGUI_StricklerTableDlg::getTableName() const
176 {
177     return myName->text();
178 }
179
180 bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
181 {
182     return myName->isReadOnly();
183 }
184
185 void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
186 {
187     myName->setReadOnly( on );
188 }
189
190 HYDROGUI_StricklerTableDlg::StricklerCoefficientList HYDROGUI_StricklerTableDlg::getData() const
191 {
192     StricklerCoefficientList aRes;
193     for ( int i = 0; i < myTable->rowCount(); i++ )
194     {
195         QTableWidgetItem* typeItem = myTable->item( i, 0 );
196         QTableWidgetItem* coeffItem = myTable->item( i, 1 );
197         aRes.append( StricklerCoefficient( typeItem->text(), coeffItem->text().toDouble() ) );
198     }
199     return aRes;
200 }
201
202 void HYDROGUI_StricklerTableDlg::setData(const StricklerCoefficientList& theData)
203 {
204     myTable->setRowCount( 0 );
205
206     foreach ( const StricklerCoefficient& aData, theData ) {
207         // Check the current Strickler type
208         if ( aData.myType.isEmpty() ) {
209             continue;
210         }
211
212         // Get Strickler coefficient value for the current Strickler type
213         QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( aData.myCoefficient, false );
214
215         // Insert row with the data
216         int aRow = myTable->rowCount();
217         myTable->insertRow( aRow );
218
219         // "Type" column
220         QTableWidgetItem* aTypeItem = new QTableWidgetItem( aData.myType );    
221         myTable->setItem( aRow, 0, aTypeItem );
222
223         // "Coefficient" column
224         myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
225     }
226
227     myTable->resizeColumnToContents( 0 );
228     myTable->resizeRowsToContents();
229
230     updateControls();
231 }
232
233 void HYDROGUI_StricklerTableDlg::updateControls()
234 {
235     bool isTableNotEmpty = myTable->rowCount() > 0;
236     myClearBtn->setEnabled( isTableNotEmpty );  
237     onSelectionChanged();
238 }
239
240 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
241 {
242     QList<int> aSortedRows = theRows;
243     qSort( aSortedRows );
244
245     int aRowToRemove = -1;
246     int aNbRemoved = 0;
247     foreach ( int aRow, aSortedRows ) {
248         aRowToRemove = aRow - aNbRemoved;
249         if ( myTable->model()->removeRow( aRowToRemove ) ) {
250             aNbRemoved++;
251         }
252     }
253
254     if ( aNbRemoved > 0 )
255         updateControls();
256 }
257
258 void HYDROGUI_StricklerTableDlg::onBrowse()
259 {
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() )
263     {
264         setFileName( aFileName );
265         emit fileSelected( aFileName );
266     }
267 }
268
269 /**
270 Add the new default constructed Strickler coefficient.
271 */
272 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
273 {
274     int aRow = myTable->rowCount();
275     myTable->insertRow( aRow );
276
277     // Set default type (=> generate unique type name) and coefficient
278     //...
279
280     updateControls();  
281 }
282
283 /**
284 Remove the selected Strickler coefficient.
285 */
286 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
287 {
288     QList<int> aRows;
289     QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
290     foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
291         aRows << anIndex.row();
292     }
293
294     removeRows( aRows );
295 }
296
297 /**
298 Clear all Strickler coefficients.
299 */
300 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
301 {
302     QList<int> aRows;
303     for ( int i=0; i< myTable->rowCount(); i++ ) {
304         aRows << i;
305     }
306
307     removeRows( aRows );
308 }
309
310 /**
311 Slot called on table selection change.
312 */
313 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
314 {
315     QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
316     myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );
317 }