Salome HOME
refs #568: use ordered list view with selection synchronized with object browser...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_OrderedListWidget.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_OrderedListWidget.h"
20
21 #include "HYDROGUI_ListModel.h"
22
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
25
26 #include <QLayout>
27 #include <QListView>
28 #include <QPushButton>
29 #include <QSignalMapper>
30 #include <QSortFilterProxyModel>
31
32
33 /**
34   Constructor.
35   @param theParent the parent widget
36   @param theIconSize the icon size for arrow buttons
37 */
38 HYDROGUI_OrderedListWidget::HYDROGUI_OrderedListWidget( QWidget* theParent, int theArrowIconSize )
39 : QWidget( theParent )
40 {
41   // Main layout
42   QHBoxLayout* aMainLayout = new QHBoxLayout( this );
43   aMainLayout->setMargin( 0 );
44   aMainLayout->setSpacing( 5 );
45
46   // List view
47   myList = new QListView( this );
48   myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
49   // enable drag and drop
50   myList->setDragEnabled( true );
51   myList->setAcceptDrops( true );
52   // configure drag and drop
53   myList->setDropIndicatorShown( true );
54   myList->setDragDropMode( QAbstractItemView::InternalMove );
55
56   // Set the custom model
57   HYDROGUI_ListModel* aModel = new HYDROGUI_ListModel();
58   QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
59   aFilteredModel->setSourceModel( aModel );
60   aFilteredModel->setFilterKeyColumn( 0 );
61   aFilteredModel->setFilterRole( HYDROGUI_VisibleRole );
62
63   myList->setModel( aFilteredModel );
64
65   // Buttons top, up, down, bottom
66   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
67   myTop = new QPushButton( this );
68   myTop->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_TOP_ICO" ) ) );
69   myTop->setIconSize( QSize( theArrowIconSize, theArrowIconSize ) );
70   myTop->setToolTip( tr( "TOP_TLT" ) );
71   myUp = new QPushButton( this );
72   myUp->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_UP_ICO" ) ) );
73   myUp->setIconSize( myTop->iconSize() );
74   myUp->setToolTip( tr( "UP_TLT" ) );
75   myDown = new QPushButton( this );
76   myDown->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_DOWN_ICO" ) ) );
77   myDown->setIconSize( myTop->iconSize() );
78   myDown->setToolTip( tr( "DOWN_TLT" ) );
79   myBottom = new QPushButton( this );
80   myBottom->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_BOTTOM_ICO" ) ) );
81   myBottom->setIconSize( myTop->iconSize() );
82   myBottom->setToolTip( tr( "BOTTOM_TLT" ) );
83
84   // Layout
85   // buttons
86   QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
87   aListButtonsLayout->addWidget( myTop );
88   aListButtonsLayout->addWidget( myUp );
89   aListButtonsLayout->addWidget( myDown );
90   aListButtonsLayout->addWidget( myBottom );
91   aListButtonsLayout->addStretch();
92   // main
93   aMainLayout->addWidget( myList );
94   aMainLayout->addLayout( aListButtonsLayout );
95
96   // Connections
97   QSignalMapper* aSignalMapper = new QSignalMapper( this );
98   aSignalMapper->setMapping( myTop, HYDROGUI_ListModel::Top );
99   aSignalMapper->setMapping( myUp, HYDROGUI_ListModel::Up );
100   aSignalMapper->setMapping( myDown, HYDROGUI_ListModel::Down );
101   aSignalMapper->setMapping( myBottom, HYDROGUI_ListModel::Bottom );
102   connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
103   connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
104   connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
105   connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
106   connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
107
108   connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), 
109             this, SIGNAL( selectionChanged() ) );
110
111   // Initialize
112   setHiddenObjectsShown( true );
113   setVisibilityIconShown( true );
114 }
115
116 /**
117   Destructor.
118 */
119 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
120 {
121 }
122
123 /**
124   Set the list of objects (which supposed to be ordered by the widget).
125   @param theObjects the list of pairs (object; visibility)
126 */
127 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
128 {
129   HYDROGUI_ListModel* aModel = getSourceModel();
130   if( aModel ) {
131     aModel->setObjects( theObjects );
132   }
133 }
134
135 /**
136   Returns the ordered list of objects.
137   @return the list of objects
138 */
139 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
140 {
141   QList<Handle(HYDROData_Entity)> anObjects;
142
143   HYDROGUI_ListModel* aModel = getSourceModel();
144   if( aModel ) {
145     anObjects = aModel->getObjects();
146   }
147
148   return anObjects;
149 }
150
151 /**
152   Add the object to the end of the list.
153   @param theObjects the pair (object; visibility)
154 */
155 void HYDROGUI_OrderedListWidget::addObject( const HYDROGUI_ListModel::Object2Visible& theObject )
156 {
157   HYDROGUI_ListModel* aModel = getSourceModel();
158   if( aModel ) {
159     aModel->addObject(theObject);
160   }
161 }
162
163 /**
164   Remove the object from the list.
165   @param theObjectName the name of the object to remove
166 */
167 void HYDROGUI_OrderedListWidget::removeObjectByName( const QString& theObjectName )
168 {
169   HYDROGUI_ListModel* aModel = getSourceModel();
170   if( aModel ) {
171     aModel->removeObjectByName(theObjectName);
172   }
173 }
174
175 /**
176   Set whether the hidden objects are presented in the list.
177   @param theIsToShow if true - the hidden objects will be shown in the list
178 */
179 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
180 {
181   myIsHiddenObjectsShown = theIsToShow; 
182
183   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
184   QString anExpr = theIsToShow ? "true|false" : "true";
185   aFilterModel->setFilterRegExp( anExpr );
186 }
187
188 /**
189   Set whether the visibility icon (eye icon) are presented in the list.
190   @param theIsToShow if true - the eye icon will be shown in the list
191 */
192 void HYDROGUI_OrderedListWidget::setVisibilityIconShown( const bool theIsToShow )
193 {
194   HYDROGUI_ListModel* aModel = getSourceModel();
195   if( aModel ) {
196     aModel->setDecorationEnabled( theIsToShow );
197   }
198 }
199
200 /**
201   Get entries of the selected objects.
202   @return the list of entries
203 */
204 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
205
206   QStringList anEntries;
207
208   QSortFilterProxyModel* aFilterModel = 
209     dynamic_cast<QSortFilterProxyModel*>( myList->model() );
210   if( aFilterModel ) {
211     HYDROGUI_ListModel* aSourceModel = 
212       dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
213     if ( aSourceModel ) {
214       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
215       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
216         QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
217         QString anEntry = aSourceModel->data( aSourceIndex, HYDROGUI_EntryRole ).toString();
218         anEntries << anEntry;
219       }
220     }
221   }
222
223   return anEntries;
224 }
225
226 /**
227   Set objects with the given entries selected (other objects will deselected).
228   @param theEntries the list of entries
229 */
230 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
231 {
232   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
233   if( !aFilterModel ) {
234     return;
235   }
236   HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
237   if ( !aSourceModel ) {
238     return;
239   }
240   QItemSelectionModel* aSelectionModel = myList->selectionModel();
241   if ( !aSelectionModel ) {
242     return;
243   }
244   
245   for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
246     QModelIndex anIndex = aSourceModel->index( aRow, 0 );
247     if ( !anIndex.isValid() ) {
248       continue;
249     }
250
251     QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
252
253     bool isToSelect = theEntries.contains( anEntry );
254     QModelIndex aProxyModelIndex = aFilterModel->mapFromSource( anIndex );
255     QItemSelectionModel::SelectionFlags aSelectionFlags =
256       isToSelect ? QItemSelectionModel::Select : QItemSelectionModel::Deselect;
257     aSelectionModel->select( aProxyModelIndex, aSelectionFlags );
258   }
259 }
260
261 /**
262   Get names of the selected objects.
263   @return the list of names
264 */
265 QStringList HYDROGUI_OrderedListWidget::getSelectedNames() const
266 {
267   QStringList aNames;
268
269   QSortFilterProxyModel* aFilterModel = 
270     dynamic_cast<QSortFilterProxyModel*>( myList->model() );
271   if ( aFilterModel ) {
272     HYDROGUI_ListModel* aSourceModel = 
273       dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
274     if ( aSourceModel ) {
275       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
276       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
277         QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
278         QString anEntry = aSourceModel->data( aSourceIndex, Qt::DisplayRole ).toString();
279         aNames << anEntry;
280       }
281     }
282   }
283
284   return aNames;
285 }
286
287 /**
288   Get names of all objects.
289   @return the list of names
290 */
291 QStringList HYDROGUI_OrderedListWidget::getAllNames() const
292 {
293   QStringList aNames;
294
295
296   foreach ( const Handle(HYDROData_Entity)& anObject, getObjects() ) {
297     aNames << anObject->GetName();
298   }
299
300   return aNames;
301 }
302
303 /**
304   Slot called on top, up, down and bottom button click.
305   @param theType the move operation type
306 */
307 void HYDROGUI_OrderedListWidget::onMove( int theType )
308 {
309   bool isMoved = false;
310
311   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
312   if( aFilterModel ) {
313     HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
314     if( aModel ) {
315       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
316       QModelIndexList aSelectedSourceIndexes;
317       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
318         aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
319       }
320       QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
321       isMoved = aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType, 
322                               !myIsHiddenObjectsShown );
323     }
324   }
325
326   if ( isMoved )
327   {
328     emit orderChanged();
329   }
330 }
331
332 /**
333   Returns the list source model.
334   @return the source model
335 */
336 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
337 {
338   HYDROGUI_ListModel* aSourceModel = 0;
339
340   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
341   if( aFilterModel ) {
342     aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
343   }
344
345   return aSourceModel;
346 }
347
348 /**
349   Enable/disable ordering (i.e. enable/disable arrow buttons and drag and drop).
350   @param theIsToEnable if true - ordering will bw enabled
351 */
352 void HYDROGUI_OrderedListWidget::setOrderingEnabled( const bool theIsToEnable )
353 {
354   // enable/disable arrow buttons
355   myTop->setEnabled( theIsToEnable );
356   myUp->setEnabled( theIsToEnable );
357   myDown->setEnabled( theIsToEnable );
358   myBottom->setEnabled( theIsToEnable );
359
360   // enable/disable drag and drop
361   myList->setDragEnabled( theIsToEnable );
362 }
363
364 void HYDROGUI_OrderedListWidget::undoLastMove()
365 {
366   HYDROGUI_ListModel* aModel = getSourceModel();
367   if( aModel )
368     aModel->undoLastMove();
369 }