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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_StricklerTableDlg.h"
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_Tool.h"
23 #include "HYDROGUI_LineEditDoubleValidator.h"
24 #include "HYDROData_StricklerTable.h"
26 #include <LightApp_Application.h>
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>
36 #include <QToolButton>
39 #include <QTableWidget>
40 #include <QHeaderView>
41 #include <QColorDialog>
43 HYDROGUI_ColorDelegate::HYDROGUI_ColorDelegate( QWidget* theParent )
47 HYDROGUI_ColorDelegate::~HYDROGUI_ColorDelegate()
51 void HYDROGUI_ColorDelegate::paint( QPainter* thePainter, const QStyleOptionViewItem& theOption,
52 const QModelIndex& theIndex ) const
54 QColor aColor = qVariantValue<QColor>( theIndex.data( Qt::BackgroundColorRole ) );
55 thePainter->fillRect( theOption.rect, aColor );
58 QWidget* HYDROGUI_ColorDelegate::createEditor( QWidget* theParent,
59 const QStyleOptionViewItem& theOption,
60 const QModelIndex& theIndex ) const
62 QColor aColor = qVariantValue<QColor>( theIndex.data( Qt::BackgroundColorRole ) );
63 QColor aNewColor = QColorDialog::getColor( aColor );
64 if( aNewColor.isValid() )
66 QAbstractItemModel* aModel = const_cast<QAbstractItemModel*>( theIndex.model() );
67 QVariant aValue = qVariantFromValue( aNewColor );
68 aModel->setData( theIndex, aValue, Qt::BackgroundColorRole );
73 void HYDROGUI_ColorDelegate::setEditorData( QWidget* theEditor, const QModelIndex& theIndex ) const
77 void HYDROGUI_ColorDelegate::setModelData( QWidget* theEditor, QAbstractItemModel* theModel,
78 const QModelIndex& theIndex ) const
82 QSize HYDROGUI_ColorDelegate::sizeHint( const QStyleOptionViewItem& theOption, const QModelIndex& theIndex ) const
84 return theOption.rect.size();
89 const int COLUMNS_COUNT = 4;
92 HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle, int theType )
93 : HYDROGUI_InputPanel( theModule, theTitle ),
96 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
98 QString fileGroupTitle = theType == Export ? tr( "EXPORT_STRICKLER_TABLE_FILE" ) : tr( "IMPORT_STRICKLER_TABLE_FILE" );
100 // Import Strickler table from file
101 QGroupBox* aFileNameGroup = new QGroupBox( fileGroupTitle, this );
102 aFileNameGroup->setVisible( theType != Edit );
104 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
106 myFileName = new QLineEdit( aFileNameGroup );
107 myFileName->setReadOnly( true );
109 QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
110 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
119 // Strickler table name
120 QGroupBox* aNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
122 QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), aNameGroup );
123 myName = new QLineEdit( aNameGroup );
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 );
129 QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
130 anImageNameLayout->setMargin( 5 );
131 anImageNameLayout->setSpacing( 5 );
132 anImageNameLayout->addWidget( anImageNameLabel );
133 anImageNameLayout->addWidget( myName );
135 QBoxLayout* anAttrNameLayout = new QHBoxLayout( aAttrNameGroup );
136 anAttrNameLayout->setMargin( 5 );
137 anAttrNameLayout->setSpacing( 5 );
138 anAttrNameLayout->addWidget( aAttrNameLabel );
139 anAttrNameLayout->addWidget( myAttrName );
142 QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
143 aTableGroup->setVisible( theType == Edit );
146 QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
147 aTableLayout->setMargin( 5 );
148 aTableLayout->setSpacing( 5 );
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" ) );
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 );
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 );
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 );
177 myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
181 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
182 aButtonsLayout->addWidget( myAddBtn );
183 aButtonsLayout->addWidget( myRemoveBtn );
184 aButtonsLayout->addStretch( 1 );
185 aButtonsLayout->addWidget( myClearBtn );
188 aTableLayout->addLayout( aButtonsLayout );
189 aTableLayout->addWidget( myTable );
192 addWidget( aFileNameGroup );
193 addWidget( aNameGroup );
194 addWidget( aAttrNameGroup );
195 addWidget( aTableGroup );
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() ) );
208 setMinimumWidth( 450 );
210 if ( theType != Edit )
214 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
218 void HYDROGUI_StricklerTableDlg::reset()
225 QString HYDROGUI_StricklerTableDlg::getFileName() const
227 return myFileName->text();
230 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
232 myFileName->setText( theName );
235 void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
237 myName->setText(theName);
240 QString HYDROGUI_StricklerTableDlg::getTableName() const
242 return myName->text();
245 bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
247 return myName->isReadOnly();
250 void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
252 myName->setReadOnly( on );
255 void HYDROGUI_StricklerTableDlg::getGuiData( Handle_HYDROData_StricklerTable& theTable ) const
257 if( theTable.IsNull() )
260 theTable->SetAttrName( myAttrName->text() );
262 for ( int i = 0; i < myTable->rowCount(); i++ )
264 QTableWidgetItem* typeItem = myTable->item( i, 0 );
265 QString aType = typeItem->data( Qt::DisplayRole ).toString();
267 QTableWidgetItem* coeffItem = myTable->item( i, 1 );
268 QString aCoeffStr = coeffItem->data( Qt::DisplayRole ).toString();
269 double aCoeff = aCoeffStr.toDouble();
271 QTableWidgetItem* attrValueItem = myTable->item( i, 2 );
272 QString anAttrValue = attrValueItem->data( Qt::DisplayRole ).toString();
274 QTableWidgetItem* colorItem = myTable->item( i, 3 );
275 QColor aColor = colorItem->backgroundColor();
277 theTable->Set( aType, aCoeff );
278 theTable->SetAttrValue( aType, anAttrValue );
279 theTable->SetColor( aType, aColor );
283 void HYDROGUI_StricklerTableDlg::setGuiData( const Handle_HYDROData_StricklerTable& theTable )
285 myAttrName->setText( theTable->GetAttrName() );
287 if( theTable.IsNull() )
289 myTable->setRowCount( 0 );
293 QStringList aTypes = theTable->GetTypes();
294 myTable->setRowCount( 0 );
295 foreach( QString aType, aTypes )
297 // Check the current Strickler type
298 if( aType.isEmpty() )
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 );
306 // Insert row with the data
307 int aRow = myTable->rowCount();
308 myTable->insertRow( aRow );
311 QTableWidgetItem* aTypeItem = new QTableWidgetItem( aType );
312 myTable->setItem( aRow, 0, aTypeItem );
314 // "Coefficient" column
315 myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
317 // "Attribute value" column
318 myTable->setItem( aRow, 2, new QTableWidgetItem( anAttrValue ) );
321 QTableWidgetItem* anItem = new QTableWidgetItem();
322 anItem->setBackgroundColor( aColor );
323 myTable->setItem( aRow, 3, anItem );
326 myTable->resizeColumnToContents( 0 );
327 myTable->resizeRowsToContents();
332 void HYDROGUI_StricklerTableDlg::updateControls()
334 bool isTableNotEmpty = myTable->rowCount() > 0;
335 myClearBtn->setEnabled( isTableNotEmpty );
336 onSelectionChanged();
339 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
341 QList<int> aSortedRows = theRows;
342 qSort( aSortedRows );
344 int aRowToRemove = -1;
346 foreach ( int aRow, aSortedRows ) {
347 aRowToRemove = aRow - aNbRemoved;
348 if ( myTable->model()->removeRow( aRowToRemove ) ) {
353 if ( aNbRemoved > 0 )
357 void HYDROGUI_StricklerTableDlg::onBrowse()
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() )
363 setFileName( aFileName );
364 emit fileSelected( aFileName );
369 Add the new default constructed Strickler coefficient.
371 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
373 int aRow = myTable->rowCount();
374 myTable->insertRow( aRow );
376 for( int i=0; i<COLUMNS_COUNT; i++ )
378 myTable->setItem( aRow, i, new QTableWidgetItem() );
381 // Set default type (=> generate unique type name) and coefficient
388 Remove the selected Strickler coefficient.
390 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
393 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
394 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
395 aRows << anIndex.row();
402 Clear all Strickler coefficients.
404 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
407 for ( int i=0; i< myTable->rowCount(); i++ ) {
415 Slot called on table selection change.
417 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
419 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
420 myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );