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()->setSectionResizeMode( 0, QHeaderView::Stretch );
158 myTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::ResizeToContents );
159 myTable->horizontalHeader()->setSectionResizeMode( 2, QHeaderView::Stretch );
160 myTable->horizontalHeader()->setSectionResizeMode( 3, QHeaderView::ResizeToContents );
162 myTable->verticalHeader()->setSectionResizeMode( 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& ) ) );
188 connect( aModel, SIGNAL( ruleChanged() ), this, SIGNAL( ruleChanged() ) );
194 HYDROGUI_PriorityWidget::~HYDROGUI_PriorityWidget()
199 Add the new default constructed rule.
201 void HYDROGUI_PriorityWidget::onAddRule()
203 HYDROGUI_PriorityTableModel* aModel =
204 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
206 if (aModel->createNewRule()) {
214 Remove the selected rule.
216 void HYDROGUI_PriorityWidget::onRemoveRule()
218 HYDROGUI_PriorityTableModel* aModel =
219 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
224 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedIndexes();
225 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
226 int aRowIndex = anIndex.row();
227 if ( !aRows.contains( aRowIndex ) )
231 if ( aModel->removeRows( aRows ) ) {
241 void HYDROGUI_PriorityWidget::onClearRules()
243 HYDROGUI_PriorityTableModel* aModel =
244 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
245 if ( aModel && aModel->removeAll() ) {
252 Set objects which could be used for rules definition.
253 @param theObjects the ordered list of objects
255 void HYDROGUI_PriorityWidget::setObjects( const QList<Handle(HYDROData_Entity)>& theObjects )
257 HYDROGUI_PriorityTableModel* aModel =
258 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
260 aModel->setObjects( theObjects );
267 @return the list of rules
269 HYDROData_ListOfRules HYDROGUI_PriorityWidget::getRules() const
271 HYDROData_ListOfRules aRules;
273 HYDROGUI_PriorityTableModel* aModel =
274 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
276 aRules = aModel->getRules();
284 @param theRules the list of rules
286 void HYDROGUI_PriorityWidget::setRules( const HYDROData_ListOfRules& theRules )
288 HYDROGUI_PriorityTableModel* aModel =
289 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
291 aModel->setRules( theRules );
298 @return the table view
300 QTableView* HYDROGUI_PriorityWidget::getTable() const
306 Slot called on table selection change.
308 void HYDROGUI_PriorityWidget::onSelectionChanged()
310 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedIndexes();
311 myRemove->setEnabled( aSelectedIndexes.count() > 0 );
315 Update GUI controls state.
317 void HYDROGUI_PriorityWidget::updateControls()
319 HYDROGUI_PriorityTableModel* aModel =
320 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
322 myAdd->setEnabled( aModel->canCreateNewRule() );
323 bool isTableNotEmpty = aModel->rowCount() > 0;
324 myClear->setEnabled( isTableNotEmpty );
326 onSelectionChanged();
332 void HYDROGUI_PriorityWidget::onShowError( const QString& theMsg ) {
333 SUIT_MessageBox::warning( this, tr( "INCORRECT_INPUT" ), theMsg );
337 Undo last change in priority rules table.
339 void HYDROGUI_PriorityWidget::undoLastChange()
341 HYDROGUI_PriorityTableModel* aModel =
342 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
344 aModel->undoLastChange();