1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_PriorityWidget.h"
25 #include "HYDROGUI_PriorityTableModel.h"
27 #include "HYDROData_PriorityQueue.h"
31 #include <QStyledItemDelegate>
33 #include <QToolButton>
37 Custom item delegate (combobox)
39 class HYDROGUI_PriorityWidget::Delegate : public QStyledItemDelegate
42 Delegate( QObject* theParent = 0 );
44 QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
45 const QModelIndex& ) const;
47 void setEditorData( QWidget*, const QModelIndex& ) const;
48 void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
53 @param theParent the parent object
55 HYDROGUI_PriorityWidget::Delegate::Delegate( QObject* theParent )
56 : QStyledItemDelegate( theParent )
62 QWidget* HYDROGUI_PriorityWidget::Delegate::createEditor(
63 QWidget* theParent, const QStyleOptionViewItem& theOption,
64 const QModelIndex& theIndex ) const
66 QComboBox* aComboBox = new QComboBox( theParent );
68 if ( theIndex.column() == 1 || theIndex.column() == 3 ) {
69 QMap<QString, QVariant> aMap = theIndex.data( Qt::UserRole ).toMap();
70 foreach ( QString aText, aMap.keys() ) {
71 aComboBox->addItem( aText, aMap.value( aText ).toInt() );
73 } else if ( theIndex.column() == 0 || theIndex.column() == 2 ) {
74 QStringList anItems = theIndex.data( Qt::UserRole ).toStringList();
75 QStringList anObjNames = theIndex.data( Qt::UserRole ).toStringList();
76 aComboBox->addItems( anObjNames );
84 void HYDROGUI_PriorityWidget::Delegate::setEditorData(
85 QWidget* theEditor, const QModelIndex& theIndex ) const
87 QComboBox* aComboBox = dynamic_cast<QComboBox*>( theEditor );
92 if ( theIndex.column() == 0 || theIndex.column() == 2 ) {
93 QString aText = theIndex.data( Qt::EditRole ).toString();
94 anIndex = aComboBox->findText( aText );
96 QVariant aData = theIndex.data( Qt::EditRole ).toInt();
97 anIndex = aComboBox->findData( aData );
100 if ( anIndex >= 0 ) {
101 aComboBox->setCurrentIndex( anIndex );
108 void HYDROGUI_PriorityWidget::Delegate::setModelData(
109 QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
111 QComboBox* aComboBox = dynamic_cast<QComboBox*>( theEditor );
114 int aColumn = theIndex.column();
115 if ( aColumn == 0 || aColumn == 2 ) {
116 theModel->setData( theIndex, aComboBox->currentText() );
118 theModel->setData( theIndex, aComboBox->itemData( aComboBox->currentIndex() ) );
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 myRemove->setEnabled( false );
144 myTable = new QTableView( this );
145 myTable->setItemDelegate( new Delegate( this ) );
146 myTable->setEditTriggers( QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked );
148 // Set the custom model
149 myTable->setModel( new HYDROGUI_PriorityTableModel() );
153 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
154 aButtonsLayout->addWidget( myAdd );
155 aButtonsLayout->addWidget( myRemove );
156 aButtonsLayout->addStretch( 1 );
159 aMainLayout->addLayout( aButtonsLayout );
160 aMainLayout->addWidget( myTable );
163 connect( myAdd, SIGNAL( clicked() ), this, SLOT( onAddRule() ) );
164 connect( myRemove, SIGNAL( clicked() ), this, SLOT( onRemoveRule() ) );
165 connect ( myTable->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
166 this, SLOT( onSelectionChanged() ) );
172 HYDROGUI_PriorityWidget::~HYDROGUI_PriorityWidget()
179 void HYDROGUI_PriorityWidget::onAddRule()
181 HYDROGUI_PriorityTableModel* aModel =
182 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
184 aModel->createNewRule();
186 myTable->resizeColumnsToContents();
187 onSelectionChanged();
191 Removes the selected rule.
193 void HYDROGUI_PriorityWidget::onRemoveRule()
195 HYDROGUI_PriorityTableModel* aModel =
196 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
201 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
202 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
203 aRows << anIndex.row();
205 aModel->removeRows( aRows );
207 myTable->resizeColumnsToContents();
208 onSelectionChanged();
214 void HYDROGUI_PriorityWidget::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
216 HYDROGUI_PriorityTableModel* aModel =
217 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
219 aModel->setObjects( theObjects );
225 @return the list of rules
227 HYDROData_ListOfRules HYDROGUI_PriorityWidget::getRules() const
229 HYDROData_ListOfRules aRules;
231 HYDROGUI_PriorityTableModel* aModel =
232 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
234 aRules = aModel->getRules();
242 @param theRules the list of rules
244 void HYDROGUI_PriorityWidget::setRules( const HYDROData_ListOfRules& theRules ) const
246 HYDROGUI_PriorityTableModel* aModel =
247 dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
249 aModel->setRules( theRules );
251 myTable->resizeColumnsToContents();
255 Slot called on table selection change.
257 void HYDROGUI_PriorityWidget::onSelectionChanged()
259 QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
260 myRemove->setEnabled( aSelectedIndexes.count() > 0 );