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