]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_StricklerTableDlg.cxx
Salome HOME
refs #562: GUI to import/modify Strickler table objects.
[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 )
42 : HYDROGUI_InputPanel( theModule, theTitle ), myName(NULL)
43 {
44   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
45
46   // Import Strickler table from file
47   myFileNameGroup = new QGroupBox( tr( "IMPORT_STRICKLER_TABLE_FROM_FILE" ), this );
48
49   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
50
51   myFileName = new QLineEdit( myFileNameGroup );
52   myFileName->setReadOnly( true );
53
54   myBrowseBtn = new QToolButton( myFileNameGroup );
55   myBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
56
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 );
63
64   // Strickler table name
65   myNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
66
67   QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
68   myName = new QLineEdit( myNameGroup );
69
70   QBoxLayout* anImageNameLayout = new QHBoxLayout( myNameGroup );
71   anImageNameLayout->setMargin( 5 );
72   anImageNameLayout->setSpacing( 5 );
73   anImageNameLayout->addWidget( anImageNameLabel );
74   anImageNameLayout->addWidget( myName );
75
76   // Strickler table
77   myTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
78
79   // Main layout
80   QVBoxLayout* aTableLayout = new QVBoxLayout( myTableGroup );
81   aTableLayout->setMargin( 5 );
82   aTableLayout->setSpacing( 5 );
83
84   // Buttons
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" ) );
91
92   // Table
93   myTable = new QTableWidget( mainFrame() );
94   myTable->setItemDelegate( new HYDROGUI_LineEditDoubleValidator( this ) );
95   myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
96                             QAbstractItemView::SelectedClicked |
97                             QAbstractItemView::EditKeyPressed );
98
99   myTable->setColumnCount( 2 );
100   QStringList aColumnNames;
101   aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" );
102   myTable->setHorizontalHeaderLabels( aColumnNames );
103
104   myTable->horizontalHeader()->setStretchLastSection( false);
105   myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
106   myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
107
108   myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
109
110   // Layout
111   // buttons
112   QHBoxLayout* aButtonsLayout = new QHBoxLayout();
113   aButtonsLayout->addWidget( myAddBtn );
114   aButtonsLayout->addWidget( myRemoveBtn );
115   aButtonsLayout->addStretch( 1 );
116   aButtonsLayout->addWidget( myClearBtn );
117
118   // main
119   aTableLayout->addLayout( aButtonsLayout );
120   aTableLayout->addWidget( myTable );
121   
122   // Common
123   addWidget( myFileNameGroup );
124   addWidget( myNameGroup );
125   addWidget( myTableGroup );
126   
127   // Update controls
128   updateControls();
129
130   // Connections
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() ) );  
137
138   setMinimumWidth( 350 );
139 }
140
141 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
142 {
143 }
144
145 void HYDROGUI_StricklerTableDlg::setIsEdit( const bool theIsEdit )
146 {
147   myFileNameGroup->setVisible( !theIsEdit );
148   myNameGroup->setEnabled( theIsEdit );
149   myTableGroup->setVisible( theIsEdit );
150   if ( !theIsEdit )
151     addStretch();
152 }
153
154 void HYDROGUI_StricklerTableDlg::reset()
155 {
156   myFileName->clear();
157
158   myName->clear();
159   myNameGroup->setEnabled( false );
160 }
161
162 QString HYDROGUI_StricklerTableDlg::getFileName() const
163 {
164   return myFileName->text();
165 }
166
167 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
168 {
169   myFileName->setText( theName );
170
171   if ( !myNameGroup->isEnabled() )
172     myNameGroup->setEnabled( true );
173 }
174
175 void HYDROGUI_StricklerTableDlg::setStricklerTableName( const QString& theName )
176 {
177   myName->setText(theName);
178 }
179
180 QString HYDROGUI_StricklerTableDlg::getStricklerTableName() const
181 {
182   return myName->text();
183 }
184
185 void HYDROGUI_StricklerTableDlg::setData(const StricklerCoefficientList& theData)
186 {
187   myTable->setRowCount( 0 );
188
189   foreach ( const StricklerCoefficient& aData, theData ) {
190     // Check the current Strickler type
191     if ( aData.myType.isEmpty() ) {
192       continue;
193     }
194
195     // Get Strickler coefficient value for the current Strickler type
196     QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( aData.myCoefficient, false );
197     
198     // Insert row with the data
199     int aRow = myTable->rowCount();
200     myTable->insertRow( aRow );
201
202     // "Type" column
203     QTableWidgetItem* aTypeItem = new QTableWidgetItem( aData.myType );    
204     myTable->setItem( aRow, 0, aTypeItem );
205
206     // "Coefficient" column
207     myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
208   }
209
210   myTable->resizeColumnToContents( 0 );
211   myTable->resizeRowsToContents();
212
213   updateControls();
214 }
215
216 void HYDROGUI_StricklerTableDlg::updateControls()
217 {
218   bool isTableNotEmpty = myTable->rowCount() > 0;
219   myClearBtn->setEnabled( isTableNotEmpty );  
220   onSelectionChanged();
221 }
222
223 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
224 {
225   QList<int> aSortedRows = theRows;
226   qSort( aSortedRows );
227
228   int aRowToRemove = -1;
229   int aNbRemoved = 0;
230   foreach ( int aRow, aSortedRows ) {
231     aRowToRemove = aRow - aNbRemoved;
232         if ( myTable->model()->removeRow( aRowToRemove ) ) {
233       aNbRemoved++;
234     }
235   }
236
237   if ( aNbRemoved > 0 )
238     updateControls();
239 }
240
241 void HYDROGUI_StricklerTableDlg::onBrowse()
242 {
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() )
246   {
247     setFileName( aFileName );
248     emit fileSelected( aFileName );
249   }
250 }
251
252 /**
253   Add the new default constructed Strickler coefficient.
254  */
255 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
256 {
257   int aRow = myTable->rowCount();
258   myTable->insertRow( aRow );
259
260   // Set default type (=> generate unique type name) and coefficient
261   //...
262
263   updateControls();  
264 }
265
266 /**
267   Remove the selected Strickler coefficient.
268  */
269 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
270 {
271   QList<int> aRows;
272   QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
273   foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
274     aRows << anIndex.row();
275   }
276
277   removeRows( aRows );
278 }
279
280 /**
281   Clear all Strickler coefficients.
282  */
283 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
284 {
285   QList<int> aRows;
286   for ( int i=0; i< myTable->rowCount(); i++ ) {
287     aRows << i;
288   }
289
290   removeRows( aRows );
291 }
292
293 /**
294  Slot called on table selection change.
295 */
296 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
297 {
298   QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
299   myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );
300 }