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