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>
20 #include <HYDROGUI_LineEditDoubleValidator.h>
21 #include <HYDROGUI_Tool.h>
22 #include <HYDROData_StricklerTable.h>
23 #include <SUIT_FileDlg.h>
25 #include <SUIT_ResourceMgr.h>
26 #include <SUIT_Session.h>
29 #include <QColorDialog>
31 #include <QHeaderView>
35 #include <QTableWidget>
36 #include <QToolButton>
38 HYDROGUI_ColorDelegate::HYDROGUI_ColorDelegate( QWidget* theParent )
42 HYDROGUI_ColorDelegate::~HYDROGUI_ColorDelegate()
46 void HYDROGUI_ColorDelegate::paint( QPainter* thePainter, const QStyleOptionViewItem& theOption,
47 const QModelIndex& theIndex ) const
49 QColor aColor = qVariantValue<QColor>( theIndex.data( Qt::BackgroundColorRole ) );
50 thePainter->fillRect( theOption.rect, aColor );
53 QWidget* HYDROGUI_ColorDelegate::createEditor( QWidget* theParent,
54 const QStyleOptionViewItem& theOption,
55 const QModelIndex& theIndex ) const
57 QColor aColor = qVariantValue<QColor>( theIndex.data( Qt::BackgroundColorRole ) );
58 QColor aNewColor = QColorDialog::getColor( aColor );
59 if( aNewColor.isValid() )
61 QAbstractItemModel* aModel = const_cast<QAbstractItemModel*>( theIndex.model() );
62 QVariant aValue = qVariantFromValue( aNewColor );
63 aModel->setData( theIndex, aValue, Qt::BackgroundColorRole );
68 void HYDROGUI_ColorDelegate::setEditorData( QWidget* theEditor, const QModelIndex& theIndex ) const
72 void HYDROGUI_ColorDelegate::setModelData( QWidget* theEditor, QAbstractItemModel* theModel,
73 const QModelIndex& theIndex ) const
77 QSize HYDROGUI_ColorDelegate::sizeHint( const QStyleOptionViewItem& theOption, const QModelIndex& theIndex ) const
79 return theOption.rect.size();
84 const int COLUMNS_COUNT = 4;
87 HYDROGUI_StricklerTableDlg::HYDROGUI_StricklerTableDlg( HYDROGUI_Module* theModule, const QString& theTitle, int theType )
88 : HYDROGUI_InputPanel( theModule, theTitle ),
91 QString fileGroupTitle = theType == Export ? tr( "EXPORT_STRICKLER_TABLE_FILE" ) : tr( "IMPORT_STRICKLER_TABLE_FILE" );
93 // Import Strickler table from file
94 QGroupBox* aFileNameGroup = new QGroupBox( fileGroupTitle, this );
95 aFileNameGroup->setVisible( theType != Edit );
97 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
99 myFileName = new QLineEdit( aFileNameGroup );
100 myFileName->setReadOnly( true );
102 QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
104 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
105 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
115 // Strickler table name
116 QGroupBox* aNameGroup = new QGroupBox( tr( "STRICKLER_TABLE_NAME" ), this );
118 QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), aNameGroup );
119 myName = new QLineEdit( aNameGroup );
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 );
126 QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
127 anImageNameLayout->setMargin( 5 );
128 anImageNameLayout->setSpacing( 5 );
129 anImageNameLayout->addWidget( anImageNameLabel );
130 anImageNameLayout->addWidget( myName );
132 QBoxLayout* anAttrNameLayout = new QHBoxLayout( aAttrNameGroup );
133 anAttrNameLayout->setMargin( 5 );
134 anAttrNameLayout->setSpacing( 5 );
135 anAttrNameLayout->addWidget( aAttrNameLabel );
136 anAttrNameLayout->addWidget( myAttrName );
139 QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
140 aTableGroup->setVisible( theType == Edit );
143 QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
144 aTableLayout->setMargin( 5 );
145 aTableLayout->setSpacing( 5 );
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" ) );
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 );
163 myTable->setColumnCount( COLUMNS_COUNT );
164 QStringList aColumnNames;
165 aColumnNames << tr( "STRICKLER_TYPE" ) << tr( "STRICKLER_COEFFICIENT" ) << tr( "ATTR_VALUE" ) << tr( "COLOR" );
166 myTable->setHorizontalHeaderLabels( aColumnNames );
168 myTable->horizontalHeader()->setStretchLastSection( false );
169 myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::ResizeToContents );
170 myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
171 myTable->horizontalHeader()->setResizeMode( 2, QHeaderView::ResizeToContents );
172 myTable->horizontalHeader()->setResizeMode( 3, QHeaderView::Stretch );
173 myTable->horizontalHeader()->setMinimumSectionSize( 50 );
175 myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
179 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
180 aButtonsLayout->addWidget( myAddBtn );
181 aButtonsLayout->addWidget( myRemoveBtn );
182 aButtonsLayout->addStretch( 1 );
183 aButtonsLayout->addWidget( myClearBtn );
186 aTableLayout->addLayout( aButtonsLayout );
187 aTableLayout->addWidget( myTable );
190 addWidget( aFileNameGroup );
191 addWidget( aNameGroup );
192 addWidget( aAttrNameGroup );
193 addWidget( aTableGroup );
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() ) );
206 setMinimumWidth( 450 );
208 if ( theType != Edit )
212 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
216 void HYDROGUI_StricklerTableDlg::reset()
223 QString HYDROGUI_StricklerTableDlg::getFileName() const
225 return myFileName->text();
228 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
230 myFileName->setText( theName );
233 void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
235 myName->setText(theName);
238 QString HYDROGUI_StricklerTableDlg::getTableName() const
240 return myName->text();
243 bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
245 return myName->isReadOnly();
248 void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
250 myName->setReadOnly( on );
253 void HYDROGUI_StricklerTableDlg::getGuiData( Handle_HYDROData_StricklerTable& theTable ) const
255 if( theTable.IsNull() )
258 theTable->SetAttrName( myAttrName->text() );
260 for ( int i = 0; i < myTable->rowCount(); i++ )
262 QTableWidgetItem* typeItem = myTable->item( i, 0 );
263 QString aType = typeItem->data( Qt::DisplayRole ).toString();
265 QTableWidgetItem* coeffItem = myTable->item( i, 1 );
266 QString aCoeffStr = coeffItem->data( Qt::DisplayRole ).toString();
267 double aCoeff = aCoeffStr.toDouble();
269 QTableWidgetItem* attrValueItem = myTable->item( i, 2 );
270 QString anAttrValue = attrValueItem->data( Qt::DisplayRole ).toString();
272 QTableWidgetItem* colorItem = myTable->item( i, 3 );
273 QColor aColor = colorItem->backgroundColor();
275 theTable->Set( aType, aCoeff );
276 theTable->SetAttrValue( aType, anAttrValue );
277 theTable->SetColor( aType, aColor );
281 void HYDROGUI_StricklerTableDlg::setGuiData( const Handle_HYDROData_StricklerTable& theTable )
283 myAttrName->setText( theTable->GetAttrName() );
285 if( theTable.IsNull() )
287 myTable->setRowCount( 0 );
291 QStringList aTypes = theTable->GetTypes();
292 myTable->setRowCount( 0 );
293 foreach( QString aType, aTypes )
295 // Check the current Strickler type
296 if( aType.isEmpty() )
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 );
304 // Insert row with the data
305 int aRow = myTable->rowCount();
306 myTable->insertRow( aRow );
309 QTableWidgetItem* aTypeItem = new QTableWidgetItem( aType );
310 myTable->setItem( aRow, 0, aTypeItem );
312 // "Coefficient" column
313 myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
315 // "Attribute value" column
316 myTable->setItem( aRow, 2, new QTableWidgetItem( anAttrValue ) );
319 QTableWidgetItem* anItem = new QTableWidgetItem();
320 anItem->setBackgroundColor( aColor );
321 myTable->setItem( aRow, 3, anItem );
324 myTable->resizeColumnToContents( 0 );
325 myTable->resizeRowsToContents();
330 void HYDROGUI_StricklerTableDlg::updateControls()
332 bool isTableNotEmpty = myTable->rowCount() > 0;
333 myClearBtn->setEnabled( isTableNotEmpty );
334 onSelectionChanged();
337 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
339 QList<int> aSortedRows = theRows;
340 qSort( aSortedRows );
342 int aRowToRemove = -1;
344 foreach ( int aRow, aSortedRows ) {
345 aRowToRemove = aRow - aNbRemoved;
346 if ( myTable->model()->removeRow( aRowToRemove ) ) {
351 if ( aNbRemoved > 0 )
355 void HYDROGUI_StricklerTableDlg::onBrowse()
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() )
361 setFileName( aFileName );
362 emit fileSelected( aFileName );
367 Add the new default constructed Strickler coefficient.
369 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
371 int aRow = myTable->rowCount();
372 myTable->insertRow( aRow );
374 for( int i=0; i<COLUMNS_COUNT; i++ )
376 myTable->setItem( aRow, i, new QTableWidgetItem() );
379 // Set default type (=> generate unique type name) and coefficient
386 Remove the selected Strickler coefficient.
388 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
391 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedIndexes();
392 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
393 int aRowIndex = anIndex.row();
394 if ( !aRows.contains( aRowIndex ) )
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()->selectedIndexes();
420 myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );