Salome HOME
719503bb7d33627155db387c7c6fc05926ccfde2
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PriorityWidget.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_PriorityWidget.h"
24
25 #include "HYDROGUI_PriorityTableModel.h"
26
27 #include "HYDROData_PriorityQueue.h"
28
29 #include <QComboBox>
30 #include <QLayout>
31 #include <QStyledItemDelegate>
32 #include <QTableView>
33 #include <QToolButton>
34
35
36 /**
37   Custom item delegate (combobox)
38  */
39 class HYDROGUI_PriorityWidget::Delegate : public QStyledItemDelegate
40 {
41 public:
42   Delegate( QObject* theParent = 0 );
43   
44   QWidget* createEditor( QWidget*, const QStyleOptionViewItem&,
45                          const QModelIndex& ) const;
46   
47   void setEditorData( QWidget*, const QModelIndex& ) const;
48   void setModelData( QWidget*, QAbstractItemModel*, const QModelIndex& ) const;
49 };
50
51 /**
52   Constructor.
53   @param theParent the parent object
54  */
55 HYDROGUI_PriorityWidget::Delegate::Delegate( QObject* theParent )
56  : QStyledItemDelegate( theParent )
57 {
58 }
59
60 /**
61  */
62 QWidget* HYDROGUI_PriorityWidget::Delegate::createEditor( 
63   QWidget* theParent, const QStyleOptionViewItem& theOption,
64   const QModelIndex& theIndex ) const
65 {
66   QComboBox* aComboBox = new QComboBox( theParent );
67
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() );
72     }
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 );
77   }
78   
79   return aComboBox;
80 }
81
82 /**
83  */
84 void HYDROGUI_PriorityWidget::Delegate::setEditorData( 
85   QWidget* theEditor, const QModelIndex& theIndex ) const
86 {
87   QComboBox* aComboBox = dynamic_cast<QComboBox*>( theEditor );
88
89   if ( aComboBox ) {
90     int anIndex = -1;
91
92     if ( theIndex.column() == 0 || theIndex.column() == 2 ) {
93       QString aText = theIndex.data( Qt::EditRole ).toString();
94       anIndex = aComboBox->findText( aText );
95     } else {
96       QVariant aData = theIndex.data( Qt::EditRole ).toInt();
97       anIndex = aComboBox->findData( aData );
98     }
99
100     if ( anIndex >= 0 ) {
101       aComboBox->setCurrentIndex( anIndex );
102     }
103   }
104 }
105
106 /**
107  */
108 void HYDROGUI_PriorityWidget::Delegate::setModelData( 
109   QWidget* theEditor, QAbstractItemModel* theModel, const QModelIndex& theIndex) const
110 {
111   QComboBox* aComboBox = dynamic_cast<QComboBox*>( theEditor );
112
113   if ( aComboBox ) {
114     int aColumn = theIndex.column();
115     if ( aColumn == 0 || aColumn == 2 ) {
116       theModel->setData( theIndex, aComboBox->currentText() );
117     } else {
118       theModel->setData( theIndex, aComboBox->itemData( aComboBox->currentIndex() ) );
119     }
120   }
121 }
122
123
124 /**
125   Constructor.
126   @param theParent the parent widget
127  */
128 HYDROGUI_PriorityWidget::HYDROGUI_PriorityWidget( QWidget* theParent )
129 : QWidget( theParent )
130 {
131   // Main layout
132   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
133   aMainLayout->setMargin( 0 );
134   aMainLayout->setSpacing( 5 );
135
136   // Buttons
137   myAdd = new QToolButton;
138   myAdd->setText( tr( "ADD" ) );
139   myRemove = new QToolButton;
140   myRemove->setText( tr( "REMOVE" ) );
141   myRemove->setEnabled( false );
142
143   // Table view
144   myTable = new QTableView( this );
145   myTable->setItemDelegate( new Delegate( this ) );
146   myTable->setEditTriggers( QAbstractItemView::DoubleClicked    | QAbstractItemView::SelectedClicked );
147
148   // Set the custom model
149   myTable->setModel( new HYDROGUI_PriorityTableModel() );
150
151   // Layout
152   // buttons
153   QHBoxLayout* aButtonsLayout = new QHBoxLayout();
154   aButtonsLayout->addWidget( myAdd );
155   aButtonsLayout->addWidget( myRemove );
156   aButtonsLayout->addStretch( 1 );
157
158   // main
159   aMainLayout->addLayout( aButtonsLayout );
160   aMainLayout->addWidget( myTable );
161   
162   // Connections
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() ) );
167 }
168
169 /**
170   Destructor.
171  */
172 HYDROGUI_PriorityWidget::~HYDROGUI_PriorityWidget()
173 {
174 }
175
176 /**
177   Adds the new rule.
178  */
179 void HYDROGUI_PriorityWidget::onAddRule()
180 {
181   HYDROGUI_PriorityTableModel* aModel = 
182     dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
183   if( aModel ) {
184     aModel->createNewRule();
185   }
186   myTable->resizeColumnsToContents();
187   onSelectionChanged();
188 }
189
190 /**
191   Removes the selected rule.
192  */
193 void HYDROGUI_PriorityWidget::onRemoveRule()
194 {
195   HYDROGUI_PriorityTableModel* aModel = 
196     dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
197
198   if (aModel) {
199     QList<int> aRows;
200
201     QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
202     foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
203       aRows << anIndex.row();
204     }
205     aModel->removeRows( aRows );
206   }
207   myTable->resizeColumnsToContents();
208   onSelectionChanged();
209 }
210
211 /**
212   Set objects.
213  */
214 void HYDROGUI_PriorityWidget::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
215 {
216   HYDROGUI_PriorityTableModel* aModel = 
217     dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
218   if( aModel ) {
219     aModel->setObjects( theObjects );
220   }
221 }
222
223 /**
224   Get rules.
225   @return the list of rules
226  */
227 HYDROData_ListOfRules HYDROGUI_PriorityWidget::getRules() const
228 {
229   HYDROData_ListOfRules aRules;
230
231   HYDROGUI_PriorityTableModel* aModel = 
232     dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
233   if( aModel ) {
234     aRules = aModel->getRules();
235   }
236
237   return aRules;
238 }
239
240 /**
241   Set rules.
242   @param theRules the list of rules
243 */
244 void HYDROGUI_PriorityWidget::setRules( const HYDROData_ListOfRules& theRules ) const
245 {
246   HYDROGUI_PriorityTableModel* aModel = 
247     dynamic_cast<HYDROGUI_PriorityTableModel*>( myTable->model() );
248   if( aModel ) {
249     aModel->setRules( theRules );
250   }
251   myTable->resizeColumnsToContents();
252 }
253
254 /**
255  Slot called on table selection change.
256 */
257 void HYDROGUI_PriorityWidget::onSelectionChanged()
258 {
259   QModelIndexList aSelectedIndexes = myTable->selectionModel()->selectedRows();
260   myRemove->setEnabled( aSelectedIndexes.count() > 0 );
261 }