]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_StricklerTableDlg.cxx
Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_LAND_COVER_MAP' into BR_SHP_FORMAT2
[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 #include "HYDROData_StricklerTable.h"
25
26 #include <LightApp_Application.h>
27
28 #include <SUIT_Session.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_FileDlg.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_MessageBox.h>
33
34 #include <QGroupBox>
35 #include <QLineEdit>
36 #include <QToolButton>
37 #include <QLayout>
38 #include <QLabel>
39 #include <QTableWidget>
40 #include <QHeaderView>
41 #include <QColorDialog>
42
43 HYDROGUI_ColorDelegate::HYDROGUI_ColorDelegate( QWidget* theParent )
44 {
45 }
46
47 HYDROGUI_ColorDelegate::~HYDROGUI_ColorDelegate()
48 {
49 }
50
51 void HYDROGUI_ColorDelegate::paint( QPainter* thePainter, const QStyleOptionViewItem& theOption,
52                                     const QModelIndex& theIndex ) const
53 {
54   QColor aColor = qVariantValue<QColor>( theIndex.data( Qt::BackgroundColorRole ) );
55   thePainter->fillRect( theOption.rect, aColor );
56 }
57
58 QWidget* HYDROGUI_ColorDelegate::createEditor( QWidget* theParent,
59                                                const QStyleOptionViewItem& theOption,
60                                                const QModelIndex& theIndex ) const
61 {
62   QColor aColor = qVariantValue<QColor>( theIndex.data( Qt::BackgroundColorRole ) );
63   QColor aNewColor = QColorDialog::getColor( aColor );
64   if( aNewColor.isValid() )
65   {
66     QAbstractItemModel* aModel = const_cast<QAbstractItemModel*>( theIndex.model() );
67     QVariant aValue = qVariantFromValue( aNewColor );
68     aModel->setData( theIndex, aValue, Qt::BackgroundColorRole );
69   }
70   return 0;
71 }
72
73 void HYDROGUI_ColorDelegate::setEditorData( QWidget* theEditor, const QModelIndex& theIndex ) const
74 {
75 }
76
77 void HYDROGUI_ColorDelegate::setModelData( QWidget* theEditor, QAbstractItemModel* theModel,
78                                            const QModelIndex& theIndex ) const
79 {
80 }
81
82 QSize   HYDROGUI_ColorDelegate::sizeHint( const QStyleOptionViewItem& theOption, const QModelIndex& theIndex ) const
83 {
84   return theOption.rect.size();
85 }
86
87
88
89
90 HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle, int theType )
91 : HYDROGUI_InputPanel( theModule, theTitle ),
92   myType( theType )
93 {
94     SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
95
96     QString fileGroupTitle = theType == Export ? tr( "EXPORT_STRICKLER_TABLE_FILE" ) : tr( "IMPORT_STRICKLER_TABLE_FILE" );
97
98     // Import Strickler table from file
99     QGroupBox* aFileNameGroup = new QGroupBox( fileGroupTitle, this );
100     aFileNameGroup->setVisible( theType != Edit );
101
102     QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
103
104     myFileName = new QLineEdit( aFileNameGroup );
105     myFileName->setReadOnly( true );
106
107     QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
108     aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
109
110     QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
111     aFileNameLayout->setMargin( 5 );
112     aFileNameLayout->setSpacing( 5 );
113     aFileNameLayout->addWidget( aFileNameLabel );
114     aFileNameLayout->addWidget( myFileName );
115     aFileNameLayout->addWidget( aBrowseBtn );
116
117     // Strickler table name
118     QGroupBox* aNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
119
120     QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), aNameGroup );
121     myName = new QLineEdit( aNameGroup );
122
123     QGroupBox* aAttrNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_ATTR_NAME" ), this );
124     QLabel* aAttrNameLabel = new QLabel( tr( "ATTR_NAME" ), aAttrNameGroup );
125     myAttrName = new QLineEdit( aAttrNameGroup );
126
127     QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
128     anImageNameLayout->setMargin( 5 );
129     anImageNameLayout->setSpacing( 5 );
130     anImageNameLayout->addWidget( anImageNameLabel );
131     anImageNameLayout->addWidget( myName );
132
133     QBoxLayout* anAttrNameLayout = new QHBoxLayout( aAttrNameGroup );
134     anAttrNameLayout->setMargin( 5 );
135     anAttrNameLayout->setSpacing( 5 );
136     anAttrNameLayout->addWidget( aAttrNameLabel );
137     anAttrNameLayout->addWidget( myAttrName );
138
139     // Strickler table
140     QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
141     aTableGroup->setVisible( theType == Edit );
142
143     // Main layout
144     QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
145     aTableLayout->setMargin( 5 );
146     aTableLayout->setSpacing( 5 );
147
148     // Buttons
149     myAddBtn = new QToolButton;
150     myAddBtn->setText( tr( "ADD" ) );
151     myRemoveBtn = new QToolButton;
152     myRemoveBtn->setText( tr( "REMOVE" ) );
153     myClearBtn = new QToolButton;
154     myClearBtn->setText( tr( "CLEAR_ALL" ) );
155
156     // Table
157     myTable = new QTableWidget( mainFrame() );
158     myTable->setItemDelegateForColumn( 1, new HYDROGUI_LineEditDoubleValidator( this ) );
159     myTable->setItemDelegateForColumn( 3, new HYDROGUI_ColorDelegate( this ) );
160     myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
161         QAbstractItemView::SelectedClicked |
162         QAbstractItemView::EditKeyPressed );
163
164     myTable->setColumnCount( 4 );
165     QStringList aColumnNames;
166     aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" ) << tr( "ATTR_VALUE" ) << tr( "COLOR" );
167     myTable->setHorizontalHeaderLabels( aColumnNames );
168
169     myTable->horizontalHeader()->setStretchLastSection( false );
170     myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::ResizeToContents );
171     myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
172     myTable->horizontalHeader()->setResizeMode( 2, QHeaderView::ResizeToContents );
173     myTable->horizontalHeader()->setResizeMode( 3, QHeaderView::Stretch );
174
175     myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
176
177     // Layout
178     // buttons
179     QHBoxLayout* aButtonsLayout = new QHBoxLayout();
180     aButtonsLayout->addWidget( myAddBtn );
181     aButtonsLayout->addWidget( myRemoveBtn );
182     aButtonsLayout->addStretch( 1 );
183     aButtonsLayout->addWidget( myClearBtn );
184
185     // main
186     aTableLayout->addLayout( aButtonsLayout );
187     aTableLayout->addWidget( myTable );
188
189     // Common
190     addWidget( aFileNameGroup );
191     addWidget( aNameGroup );
192     addWidget( aAttrNameGroup );
193     addWidget( aTableGroup );
194
195     // Update controls
196     updateControls();
197
198     // Connections
199     connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
200     connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( onAddCoefficient() ) );
201     connect( myRemoveBtn, SIGNAL( clicked() ), this, SLOT( onRemoveCoefficient() ) );
202     connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( onClearCoefficients() ) );
203     connect( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), 
204         this, SLOT( onSelectionChanged() ) );  
205
206     setMinimumWidth( 450 );
207
208     if ( theType != Edit )
209         addStretch();
210 }
211
212 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
213 {
214 }
215
216 void HYDROGUI_StricklerTableDlg::reset()
217 {
218     myFileName->clear();
219
220     myName->clear();
221 }
222
223 QString HYDROGUI_StricklerTableDlg::getFileName() const
224 {
225     return myFileName->text();
226 }
227
228 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
229 {
230     myFileName->setText( theName );
231 }
232
233 void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
234 {
235     myName->setText(theName);
236 }
237
238 QString HYDROGUI_StricklerTableDlg::getTableName() const
239 {
240     return myName->text();
241 }
242
243 bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
244 {
245     return myName->isReadOnly();
246 }
247
248 void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
249 {
250     myName->setReadOnly( on );
251 }
252
253 void HYDROGUI_StricklerTableDlg::getGuiData( Handle_HYDROData_StricklerTable& theTable ) const
254 {
255   if( theTable.IsNull() )
256     return;
257
258   theTable->SetAttrName( myAttrName->text() );
259   theTable->Clear();
260   for ( int i = 0; i < myTable->rowCount(); i++ )
261   {
262     QTableWidgetItem* typeItem = myTable->item( i, 0 );
263     QString aType = typeItem->data( Qt::DisplayRole ).toString();
264
265     QTableWidgetItem* coeffItem = myTable->item( i, 1 );
266     QString aCoeffStr = coeffItem->data( Qt::DisplayRole ).toString();
267     double aCoeff = aCoeffStr.toDouble();
268
269     QTableWidgetItem* attrValueItem = myTable->item( i, 2 );
270     QString anAttrValue = attrValueItem->data( Qt::DisplayRole ).toString();
271
272     QTableWidgetItem* colorItem = myTable->item( i, 3 );
273     QColor aColor = colorItem->backgroundColor();
274
275     theTable->Set( aType, aCoeff );
276     theTable->SetAttrValue( aType, anAttrValue );
277     theTable->SetColor( aType, aColor );
278   }
279 }
280
281 void HYDROGUI_StricklerTableDlg::setGuiData( const Handle_HYDROData_StricklerTable& theTable )
282 {
283   myAttrName->setText( theTable->GetAttrName() );
284
285   if( theTable.IsNull() )
286   {
287     myTable->setRowCount( 0 );
288     return;
289   }
290
291   QStringList aTypes = theTable->GetTypes();
292   myTable->setRowCount( 0 );
293   foreach( QString aType, aTypes )
294   {
295     // Check the current Strickler type
296     if( aType.isEmpty() )
297       continue;
298
299     // Get Strickler data for the current Strickler type
300     QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( theTable->Get( aType, 0.0 ), false );
301     QString anAttrValue = theTable->GetAttrValue( aType );
302     QColor aColor = theTable->GetColor( aType );
303
304     // Insert row with the data
305     int aRow = myTable->rowCount();
306     myTable->insertRow( aRow );
307
308     // "Type" column
309     QTableWidgetItem* aTypeItem = new QTableWidgetItem( aType );    
310     myTable->setItem( aRow, 0, aTypeItem );
311
312     // "Coefficient" column
313     myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
314
315     // "Attribute value" column
316     myTable->setItem( aRow, 2, new QTableWidgetItem( anAttrValue ) );
317
318     // "Color" column
319     QTableWidgetItem* anItem = new QTableWidgetItem();
320     anItem->setBackgroundColor( aColor );
321     myTable->setItem( aRow, 3, anItem );
322   }
323
324   myTable->resizeColumnToContents( 0 );
325   myTable->resizeRowsToContents();
326
327   updateControls();
328 }
329
330 void HYDROGUI_StricklerTableDlg::updateControls()
331 {
332     bool isTableNotEmpty = myTable->rowCount() > 0;
333     myClearBtn->setEnabled( isTableNotEmpty );  
334     onSelectionChanged();
335 }
336
337 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
338 {
339     QList<int> aSortedRows = theRows;
340     qSort( aSortedRows );
341
342     int aRowToRemove = -1;
343     int aNbRemoved = 0;
344     foreach ( int aRow, aSortedRows ) {
345         aRowToRemove = aRow - aNbRemoved;
346         if ( myTable->model()->removeRow( aRowToRemove ) ) {
347             aNbRemoved++;
348         }
349     }
350
351     if ( aNbRemoved > 0 )
352         updateControls();
353 }
354
355 void HYDROGUI_StricklerTableDlg::onBrowse()
356 {
357     QString aFilter( tr( "STRICKLER_TABLE_FILTER" ) );
358     QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, myFileName->parent()->property( "title" ).toString(), myType == Import );
359     if ( !aFileName.isEmpty() )
360     {
361         setFileName( aFileName );
362         emit fileSelected( aFileName );
363     }
364 }
365
366 /**
367 Add the new default constructed Strickler coefficient.
368 */
369 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
370 {
371     int aRow = myTable->rowCount();
372     myTable->insertRow( aRow );
373
374     // Set default type (=> generate unique type name) and coefficient
375     //...
376
377     updateControls();  
378 }
379
380 /**
381 Remove the selected Strickler coefficient.
382 */
383 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
384 {
385     QList<int> aRows;
386     QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
387     foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
388         aRows << anIndex.row();
389     }
390
391     removeRows( aRows );
392 }
393
394 /**
395 Clear all Strickler coefficients.
396 */
397 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
398 {
399     QList<int> aRows;
400     for ( int i=0; i< myTable->rowCount(); i++ ) {
401         aRows << i;
402     }
403
404     removeRows( aRows );
405 }
406
407 /**
408 Slot called on table selection change.
409 */
410 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
411 {
412     QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
413     myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );
414 }