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