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_PriorityWidget.h"
21 #include "HYDROGUI_PriorityTableModel.h"
23 #include "HYDROData_PriorityQueue.h"
25 #include <SUIT_MessageBox.h>
28 #include <QHeaderView>
31 #include <QToolButton>
36 @param theParent the parent object
38 HYDROGUI_PriorityWidget::Delegate::Delegate( QObject* theParent )
39 : QStyledItemDelegate( theParent )
45 QWidget* HYDROGUI_PriorityWidget::Delegate::createEditor(
46 QWidget* theParent, const QStyleOptionViewItem& theOption,
47 const QModelIndex& theIndex ) const
49 QComboBox* aComboBox = new QComboBox( theParent );
51 if ( theIndex.column() == 1 || theIndex.column() == 3 ) {
52 QMap<QString, QVariant> aMap = theIndex.data( Qt::UserRole ).toMap();
53 foreach ( QString aText, aMap.keys() ) {
54 aComboBox->addItem( aText, aMap.value( aText ).toInt() );
56 } else if ( theIndex.column() == 0 || theIndex.column() == 2 ) {
57 QStringList anObjNames = theIndex.data( Qt::UserRole ).toStringList();
58 aComboBox->addItems( anObjNames );
61 connect( aComboBox, SIGNAL( activated( int ) ), this, SLOT( finishEditing() ) );
68 void HYDROGUI_PriorityWidget::Delegate::setEditorData(
69 QWidget* theEditor, const QModelIndex& theIndex ) const
71 QComboBox* aComboBox = dynamic_cast<QComboBox*>( theEditor );
76 if ( theIndex.column() == 0 || theIndex.column() == 2 ) {
77 QString aText = theIndex.data( Qt::EditRole ).toString();
78 anIndex = aComboBox->findText( aText );
80 QVariant aData = theIndex.data( Qt::EditRole ).toInt();
81 anIndex = aComboBox->findData( aData );
85 aComboBox->setCurrentIndex( anIndex );
92 void HYDROGUI_PriorityWidget::Delegate::setModelData(
93 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
95 QComboBox* aComboBox = dynamic_cast<QComboBox*>( theEditor );
98 int aColumn = theIndex.column();
99 if ( aColumn == 0 || aColumn == 2 ) {
100 QString aCurrentText = theIndex.data( Qt::EditRole ).toString();
101 QString aNewText = aComboBox->currentText();
102 if ( aNewText != aCurrentText ) {
103 theModel->setData( theIndex, aNewText );
106 theModel->setData( theIndex, aComboBox->itemData( aComboBox->currentIndex() ) );
112 Emit signal indicating that the user has finished editing.
114 void HYDROGUI_PriorityWidget::Delegate::finishEditing()
116 QWidget* anEditor = qobject_cast<QWidget*>( sender() );
118 emit commitData( anEditor );
119 emit closeEditor( anEditor );
126 @param theParent the parent widget
128 HYDROGUI_PriorityWidget::HYDROGUI_PriorityWidget( QWidget* theParent )
129 : QWidget( theParent )
132 QVBoxLayout* aMainLayout = new QVBoxLayout( this );
133 aMainLayout->setMargin( 0 );
134 aMainLayout->setSpacing( 5 );
137 myAdd = new QToolButton;
138 myAdd->setText( tr( "ADD" ) );
139 myRemove = new QToolButton;
140 myRemove->setText( tr( "REMOVE" ) );
141 myClear = new QToolButton;
142 myClear->setText( tr( "CLEAR_ALL" ) );
145 myTable = new QTableView( this );
146 myTable->setItemDelegate( new Delegate( this ) );
147 myTable->setEditTriggers( QAbstractItemView::DoubleClicked |
148 QAbstractItemView::SelectedClicked |
149 QAbstractItemView::EditKeyPressed );
151 // Set the custom model
152 HYDROGUI_PriorityTableModel* aModel = new HYDROGUI_PriorityTableModel();
153 myTable->setModel( aModel );
156 myTable->horizontalHeader()->setStretchLastSection( false);
157 myTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
158 myTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
159 myTable->horizontalHeader()->setResizeMode( 2, QHeaderView::Stretch );
160 myTable->horizontalHeader()->setResizeMode( 3, QHeaderView::ResizeToContents );
162 myTable->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
166 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
167 aButtonsLayout->addWidget( myAdd );
168 aButtonsLayout->addWidget( myRemove );
169 aButtonsLayout->addStretch( 1 );
170 aButtonsLayout->addWidget( myClear );
173 aMainLayout->addLayout( aButtonsLayout );
174 aMainLayout->addWidget( myTable );
180 connect( myAdd, SIGNAL( clicked() ), this, SLOT( onAddRule() ) );
181 connect( myRemove, SIGNAL( clicked() ), this, SLOT( onRemoveRule() ) );
182 connect( myClear, SIGNAL( clicked() ), this, SLOT( onClearRules() ) );
184 connect ( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
185 this, SLOT( onSelectionChanged() ) );
187 connect( aModel, SIGNAL( showError( const QString& ) ), this, SLOT( onShowError( const QString& ) ) );
193 HYDROGUI_PriorityWidget::~HYDROGUI_PriorityWidget()
198 Add the new default constructed rule.
200 void HYDROGUI_PriorityWidget::onAddRule()
202 HYDROGUI_PriorityTableModel* aModel =
203 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
205 if (aModel->createNewRule()) {
212 Remove the selected rule.
214 void HYDROGUI_PriorityWidget::onRemoveRule()
216 HYDROGUI_PriorityTableModel* aModel =
217 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
222 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
223 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
224 aRows << anIndex.row();
227 if ( aModel->removeRows( aRows ) ) {
236 void HYDROGUI_PriorityWidget::onClearRules()
238 HYDROGUI_PriorityTableModel* aModel =
239 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
240 if ( aModel && aModel->removeAll() ) {
246 Set objects which could be used for rules definition.
247 @param theObjects the ordered list of objects
249 void HYDROGUI_PriorityWidget::setObjects( const QList<Handle(HYDROData_Entity)>& theObjects )
251 HYDROGUI_PriorityTableModel* aModel =
252 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
254 aModel->setObjects( theObjects );
261 @return the list of rules
263 HYDROData_ListOfRules HYDROGUI_PriorityWidget::getRules() const
265 HYDROData_ListOfRules aRules;
267 HYDROGUI_PriorityTableModel* aModel =
268 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
270 aRules = aModel->getRules();
278 @param theRules the list of rules
280 void HYDROGUI_PriorityWidget::setRules( const HYDROData_ListOfRules& theRules )
282 HYDROGUI_PriorityTableModel* aModel =
283 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
285 aModel->setRules( theRules );
292 @return the table view
294 QTableView* HYDROGUI_PriorityWidget::getTable() const
300 Slot called on table selection change.
302 void HYDROGUI_PriorityWidget::onSelectionChanged()
304 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
305 myRemove->setEnabled( aSelectedIndexes.count() > 0 );
309 Update GUI controls state.
311 void HYDROGUI_PriorityWidget::updateControls()
313 HYDROGUI_PriorityTableModel* aModel =
314 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
316 myAdd->setEnabled( aModel->canCreateNewRule() );
317 bool isTableNotEmpty = aModel->rowCount() > 0;
318 myClear->setEnabled( isTableNotEmpty );
320 onSelectionChanged();
326 void HYDROGUI_PriorityWidget::onShowError( const QString& theMsg ) {
327 SUIT_MessageBox::warning( this, tr( "INCORRECT_INPUT" ), theMsg );