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 QLabel* aAttrNameLabel = new QLabel( tr( "ATTR_NAME" ), aAttrNameGroup );
123 myAttrName = new QLineEdit( aAttrNameGroup );
125 QBoxLayout* anImageNameLayout = new QHBoxLayout( aNameGroup );
126 anImageNameLayout->setMargin( 5 );
127 anImageNameLayout->setSpacing( 5 );
128 anImageNameLayout->addWidget( anImageNameLabel );
129 anImageNameLayout->addWidget( myName );
131 QBoxLayout* anAttrNameLayout = new QHBoxLayout( aAttrNameGroup );
132 anAttrNameLayout->setMargin( 5 );
133 anAttrNameLayout->setSpacing( 5 );
134 anAttrNameLayout->addWidget( aAttrNameLabel );
135 anAttrNameLayout->addWidget( myAttrName );
138 QGroupBox* aTableGroup = new QGroupBox( tr( "STRICKLER_TABLE_TABLE" ), this );
139 aTableGroup->setVisible( theType == Edit );
142 QVBoxLayout* aTableLayout = new QVBoxLayout( aTableGroup );
143 aTableLayout->setMargin( 5 );
144 aTableLayout->setSpacing( 5 );
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" ) );
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 );
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 );
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 );
173 myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
177 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
178 aButtonsLayout->addWidget( myAddBtn );
179 aButtonsLayout->addWidget( myRemoveBtn );
180 aButtonsLayout->addStretch( 1 );
181 aButtonsLayout->addWidget( myClearBtn );
184 aTableLayout->addLayout( aButtonsLayout );
185 aTableLayout->addWidget( myTable );
188 addWidget( aFileNameGroup );
189 addWidget( aNameGroup );
190 addWidget( aAttrNameGroup );
191 addWidget( aTableGroup );
197 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
198 connect( myAddBtn, SIGNAL( clicked() ), this, SLOT( onAddCoefficient() ) );
199 connect( myRemoveBtn, SIGNAL( clicked() ), this, SLOT( onRemoveCoefficient() ) );
200 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( onClearCoefficients() ) );
201 connect( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
202 this, SLOT( onSelectionChanged() ) );
204 setMinimumWidth( 450 );
206 if ( theType != Edit )
210 HYDROGUI_StricklerTableDlg::~HYDROGUI_StricklerTableDlg()
214 void HYDROGUI_StricklerTableDlg::reset()
221 QString HYDROGUI_StricklerTableDlg::getFileName() const
223 return myFileName->text();
226 void HYDROGUI_StricklerTableDlg::setFileName( const QString& theName )
228 myFileName->setText( theName );
231 void HYDROGUI_StricklerTableDlg::setTableName( const QString& theName )
233 myName->setText(theName);
236 QString HYDROGUI_StricklerTableDlg::getTableName() const
238 return myName->text();
241 bool HYDROGUI_StricklerTableDlg::isTableNameReadOnly() const
243 return myName->isReadOnly();
246 void HYDROGUI_StricklerTableDlg::setTableNameReadOnly( bool on )
248 myName->setReadOnly( on );
251 void HYDROGUI_StricklerTableDlg::getGuiData( Handle_HYDROData_StricklerTable& theTable ) const
253 if( theTable.IsNull() )
256 theTable->SetAttrName( myAttrName->text() );
258 for ( int i = 0; i < myTable->rowCount(); i++ )
260 QTableWidgetItem* typeItem = myTable->item( i, 0 );
261 QString aType = typeItem->data( Qt::DisplayRole ).toString();
263 QTableWidgetItem* coeffItem = myTable->item( i, 1 );
264 QString aCoeffStr = coeffItem->data( Qt::DisplayRole ).toString();
265 double aCoeff = aCoeffStr.toDouble();
267 QTableWidgetItem* attrValueItem = myTable->item( i, 2 );
268 QString anAttrValue = attrValueItem->data( Qt::DisplayRole ).toString();
270 QTableWidgetItem* colorItem = myTable->item( i, 3 );
271 QColor aColor = colorItem->backgroundColor();
273 theTable->Set( aType, aCoeff );
274 theTable->SetAttrValue( aType, anAttrValue );
275 theTable->SetColor( aType, aColor );
279 void HYDROGUI_StricklerTableDlg::setGuiData( const Handle_HYDROData_StricklerTable& theTable )
281 myAttrName->setText( theTable->GetAttrName() );
283 if( theTable.IsNull() )
285 myTable->setRowCount( 0 );
289 QStringList aTypes = theTable->GetTypes();
290 myTable->setRowCount( 0 );
291 foreach( QString aType, aTypes )
293 // Check the current Strickler type
294 if( aType.isEmpty() )
297 // Get Strickler data for the current Strickler type
298 QString aCoefficient = HYDROGUI_Tool::GetCoordinateString( theTable->Get( aType, 0.0 ), false );
299 QString anAttrValue = theTable->GetAttrValue( aType );
300 QColor aColor = theTable->GetColor( aType );
302 // Insert row with the data
303 int aRow = myTable->rowCount();
304 myTable->insertRow( aRow );
307 QTableWidgetItem* aTypeItem = new QTableWidgetItem( aType );
308 myTable->setItem( aRow, 0, aTypeItem );
310 // "Coefficient" column
311 myTable->setItem( aRow, 1, new QTableWidgetItem( aCoefficient ) );
313 // "Attribute value" column
314 myTable->setItem( aRow, 2, new QTableWidgetItem( anAttrValue ) );
317 QTableWidgetItem* anItem = new QTableWidgetItem();
318 anItem->setBackgroundColor( aColor );
319 myTable->setItem( aRow, 3, anItem );
322 myTable->resizeColumnToContents( 0 );
323 myTable->resizeRowsToContents();
328 void HYDROGUI_StricklerTableDlg::updateControls()
330 bool isTableNotEmpty = myTable->rowCount() > 0;
331 myClearBtn->setEnabled( isTableNotEmpty );
332 onSelectionChanged();
335 void HYDROGUI_StricklerTableDlg::removeRows( const QList<int> theRows )
337 QList<int> aSortedRows = theRows;
338 qSort( aSortedRows );
340 int aRowToRemove = -1;
342 foreach ( int aRow, aSortedRows ) {
343 aRowToRemove = aRow - aNbRemoved;
344 if ( myTable->model()->removeRow( aRowToRemove ) ) {
349 if ( aNbRemoved > 0 )
353 void HYDROGUI_StricklerTableDlg::onBrowse()
355 QString aFilter( tr( "STRICKLER_TABLE_FILTER" ) );
356 QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, myFileName->parent()->property( "title" ).toString(), myType == Import );
357 if ( !aFileName.isEmpty() )
359 setFileName( aFileName );
360 emit fileSelected( aFileName );
365 Add the new default constructed Strickler coefficient.
367 void HYDROGUI_StricklerTableDlg::onAddCoefficient()
369 int aRow = myTable->rowCount();
370 myTable->insertRow( aRow );
372 for( int i=0; i<COLUMNS_COUNT; i++ )
374 myTable->setItem( aRow, i, new QTableWidgetItem() );
377 // Set default type (=> generate unique type name) and coefficient
384 Remove the selected Strickler coefficient.
386 void HYDROGUI_StricklerTableDlg::onRemoveCoefficient()
389 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
390 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
391 aRows << anIndex.row();
398 Clear all Strickler coefficients.
400 void HYDROGUI_StricklerTableDlg::onClearCoefficients()
403 for ( int i=0; i< myTable->rowCount(); i++ ) {
411 Slot called on table selection change.
413 void HYDROGUI_StricklerTableDlg::onSelectionChanged()
415 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
416 myRemoveBtn->setEnabled( aSelectedIndexes.count() > 0 );