Salome HOME
getting nb of intersections (non-gui) + add of profiles of stream to tree
[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   // Add list to the main layout
66   aMainLayout->addWidget( myList );
67
68   // Buttons top, up, down, bottom
69   if ( theArrowIconSize > 0 ) {
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     // Add buttons to the main layout
89     QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
90     aListButtonsLayout->addWidget( myTop );
91     aListButtonsLayout->addWidget( myUp );
92     aListButtonsLayout->addWidget( myDown );
93     aListButtonsLayout->addWidget( myBottom );
94     aListButtonsLayout->addStretch();
95     
96     aMainLayout->addLayout( aListButtonsLayout );
97
98     // Buttons connections
99     QSignalMapper* aSignalMapper = new QSignalMapper( this );
100     aSignalMapper->setMapping( myTop, HYDROGUI_ListModel::Top );
101     aSignalMapper->setMapping( myUp, HYDROGUI_ListModel::Up );
102     aSignalMapper->setMapping( myDown, HYDROGUI_ListModel::Down );
103     aSignalMapper->setMapping( myBottom, HYDROGUI_ListModel::Bottom );
104     connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
105     connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
106     connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
107     connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
108     connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
109   } else {
110     myTop = myUp = myDown = myBottom = 0;
111   }
112
113   connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), 
114             this, SIGNAL( selectionChanged() ) );
115
116   // Initialize
117   setHiddenObjectsShown( true );
118   setVisibilityIconShown( true );
119 }
120
121 /**
122   Destructor.
123 */
124 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
125 {
126 }
127
128 /**
129   Set the list of objects (which supposed to be ordered by the widget).
130   @param theObjects the list of pairs (object; visibility)
131 */
132 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
133 {
134   HYDROGUI_ListModel* aModel = getSourceModel();
135   if( aModel ) {
136     aModel->setObjects( theObjects );
137   }
138 }
139
140 /**
141   Returns the ordered list of objects.
142   @return the list of objects
143 */
144 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
145 {
146   QList<Handle(HYDROData_Entity)> anObjects;
147
148   HYDROGUI_ListModel* aModel = getSourceModel();
149   if( aModel ) {
150     anObjects = aModel->getObjects();
151   }
152
153   return anObjects;
154 }
155
156 /**
157   Add the object to the end of the list.
158   @param theObjects the pair (object; visibility)
159 */
160 void HYDROGUI_OrderedListWidget::addObject( const HYDROGUI_ListModel::Object2Visible& theObject )
161 {
162   HYDROGUI_ListModel* aModel = getSourceModel();
163   if( aModel ) {
164     aModel->addObject(theObject);
165   }
166 }
167
168 /**
169   Remove the object from the list.
170   @param theObjectName the name of the object to remove
171 */
172 void HYDROGUI_OrderedListWidget::removeObjectByName( const QString& theObjectName )
173 {
174   HYDROGUI_ListModel* aModel = getSourceModel();
175   if( aModel ) {
176     aModel->removeObjectByName(theObjectName);
177   }
178 }
179
180 /**
181   Set whether the hidden objects are presented in the list.
182   @param theIsToShow if true - the hidden objects will be shown in the list
183 */
184 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
185 {
186   myIsHiddenObjectsShown = theIsToShow; 
187
188   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
189   QString anExpr = theIsToShow ? "true|false" : "true";
190   aFilterModel->setFilterRegExp( anExpr );
191 }
192
193 /**
194   Set whether the visibility icon (eye icon) are presented in the list.
195   @param theIsToShow if true - the eye icon will be shown in the list
196 */
197 void HYDROGUI_OrderedListWidget::setVisibilityIconShown( const bool theIsToShow )
198 {
199   HYDROGUI_ListModel* aModel = getSourceModel();
200   if( aModel ) {
201     aModel->setDecorationEnabled( theIsToShow );
202   }
203 }
204
205 /**
206   Get entries of the selected objects.
207   @return the list of entries
208 */
209 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
210
211   QStringList anEntries;
212
213   QSortFilterProxyModel* aFilterModel = 
214     dynamic_cast<QSortFilterProxyModel*>( myList->model() );
215   if( aFilterModel ) {
216     HYDROGUI_ListModel* aSourceModel = 
217       dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
218     if ( aSourceModel ) {
219       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
220       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
221         QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
222         QString anEntry = aSourceModel->data( aSourceIndex, HYDROGUI_EntryRole ).toString();
223         anEntries << anEntry;
224       }
225     }
226   }
227
228   return anEntries;
229 }
230
231 /**
232   Set objects with the given entries selected (other objects will deselected).
233   @param theEntries the list of entries
234 */
235 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
236 {
237   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
238   if( !aFilterModel ) {
239     return;
240   }
241   HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
242   if ( !aSourceModel ) {
243     return;
244   }
245   QItemSelectionModel* aSelectionModel = myList->selectionModel();
246   if ( !aSelectionModel ) {
247     return;
248   }
249   
250   for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
251     QModelIndex anIndex = aSourceModel->index( aRow, 0 );
252     if ( !anIndex.isValid() ) {
253       continue;
254     }
255
256     QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
257
258     bool isToSelect = theEntries.contains( anEntry );
259     QModelIndex aProxyModelIndex = aFilterModel->mapFromSource( anIndex );
260     QItemSelectionModel::SelectionFlags aSelectionFlags =
261       isToSelect ? QItemSelectionModel::Select : QItemSelectionModel::Deselect;
262     aSelectionModel->select( aProxyModelIndex, aSelectionFlags );
263   }
264 }
265
266 /**
267   Get names of the selected objects.
268   @return the list of names
269 */
270 QStringList HYDROGUI_OrderedListWidget::getSelectedNames() const
271 {
272   QStringList aNames;
273
274   QSortFilterProxyModel* aFilterModel = 
275     dynamic_cast<QSortFilterProxyModel*>( myList->model() );
276   if ( aFilterModel ) {
277     HYDROGUI_ListModel* aSourceModel = 
278       dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
279     if ( aSourceModel ) {
280       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
281       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
282         QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
283         QString anEntry = aSourceModel->data( aSourceIndex, Qt::DisplayRole ).toString();
284         aNames << anEntry;
285       }
286     }
287   }
288
289   return aNames;
290 }
291
292 /**
293   Get names of all objects.
294   @return the list of names
295 */
296 QStringList HYDROGUI_OrderedListWidget::getAllNames() const
297 {
298   QStringList aNames;
299
300
301   foreach ( const Handle(HYDROData_Entity)& anObject, getObjects() ) {
302     aNames << anObject->GetName();
303   }
304
305   return aNames;
306 }
307
308 /**
309   Slot called on top, up, down and bottom button click.
310   @param theType the move operation type
311 */
312 void HYDROGUI_OrderedListWidget::onMove( int theType )
313 {
314   bool isMoved = false;
315
316   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
317   if( aFilterModel ) {
318     HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
319     if( aModel ) {
320       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
321       QModelIndexList aSelectedSourceIndexes;
322       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
323         aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
324       }
325       QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
326       isMoved = aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType, 
327                               !myIsHiddenObjectsShown );
328     }
329   }
330
331   if ( isMoved )
332   {
333     emit orderChanged();
334   }
335 }
336
337 /**
338   Returns the list source model.
339   @return the source model
340 */
341 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
342 {
343   HYDROGUI_ListModel* aSourceModel = 0;
344
345   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
346   if( aFilterModel ) {
347     aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
348   }
349
350   return aSourceModel;
351 }
352
353 /**
354   Enable/disable ordering (i.e. enable/disable arrow buttons and drag and drop).
355   @param theIsToEnable if true - ordering will bw enabled
356 */
357 void HYDROGUI_OrderedListWidget::setOrderingEnabled( const bool theIsToEnable )
358 {
359   // enable/disable arrow buttons
360   if ( myTop && myUp && myDown && myBottom ) {
361     myTop->setEnabled( theIsToEnable );
362     myUp->setEnabled( theIsToEnable );
363     myDown->setEnabled( theIsToEnable );
364     myBottom->setEnabled( theIsToEnable );
365   }
366
367   // enable/disable drag and drop
368   myList->setDragEnabled( theIsToEnable );
369 }
370
371 void HYDROGUI_OrderedListWidget::undoLastMove()
372 {
373   HYDROGUI_ListModel* aModel = getSourceModel();
374   if( aModel )
375     aModel->undoLastMove();
376 }