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