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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_OrderedListWidget.h"
21 #include "HYDROGUI_ListModel.h"
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
28 #include <QPushButton>
29 #include <QSignalMapper>
30 #include <QSortFilterProxyModel>
35 @param theParent the parent widget
36 @param theIconSize the icon size for arrow buttons
38 HYDROGUI_OrderedListWidget::HYDROGUI_OrderedListWidget( QWidget* theParent, int theArrowIconSize )
39 : QWidget( theParent )
42 QHBoxLayout* aMainLayout = new QHBoxLayout( this );
43 aMainLayout->setMargin( 0 );
44 aMainLayout->setSpacing( 5 );
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 );
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 );
63 myList->setModel( aFilteredModel );
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" ) );
86 QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
87 aListButtonsLayout->addWidget( myTop );
88 aListButtonsLayout->addWidget( myUp );
89 aListButtonsLayout->addWidget( myDown );
90 aListButtonsLayout->addWidget( myBottom );
91 aListButtonsLayout->addStretch();
93 aMainLayout->addWidget( myList );
94 aMainLayout->addLayout( aListButtonsLayout );
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 ) ) );
108 connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
109 this, SIGNAL( selectionChanged() ) );
112 setHiddenObjectsShown( true );
113 setVisibilityIconShown( true );
119 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
124 Set the list of objects (which supposed to be ordered by the widget).
125 @param theObjects the list of pairs (object; visibility)
127 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
129 HYDROGUI_ListModel* aModel = getSourceModel();
131 aModel->setObjects( theObjects );
136 Returns the ordered list of objects.
137 @return the list of objects
139 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
141 QList<Handle(HYDROData_Entity)> anObjects;
143 HYDROGUI_ListModel* aModel = getSourceModel();
145 anObjects = aModel->getObjects();
152 Add the object to the end of the list.
153 @param theObjects the pair (object; visibility)
155 void HYDROGUI_OrderedListWidget::addObject( const HYDROGUI_ListModel::Object2Visible& theObject )
157 HYDROGUI_ListModel* aModel = getSourceModel();
159 aModel->addObject(theObject);
164 Remove the object from the list.
165 @param theObjectName the name of the object to remove
167 void HYDROGUI_OrderedListWidget::removeObjectByName( const QString& theObjectName )
169 HYDROGUI_ListModel* aModel = getSourceModel();
171 aModel->removeObjectByName(theObjectName);
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
179 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
181 myIsHiddenObjectsShown = theIsToShow;
183 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
184 QString anExpr = theIsToShow ? "true|false" : "true";
185 aFilterModel->setFilterRegExp( anExpr );
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
192 void HYDROGUI_OrderedListWidget::setVisibilityIconShown( const bool theIsToShow )
194 HYDROGUI_ListModel* aModel = getSourceModel();
196 aModel->setDecorationEnabled( theIsToShow );
201 Get entries of the selected objects.
202 @return the list of entries
204 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
206 QStringList anEntries;
208 QSortFilterProxyModel* aFilterModel =
209 dynamic_cast<QSortFilterProxyModel*>( myList->model() );
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;
227 Set objects with the given entries selected (other objects will deselected).
228 @param theEntries the list of entries
230 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
232 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
233 if( !aFilterModel ) {
236 HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
237 if ( !aSourceModel ) {
240 QItemSelectionModel* aSelectionModel = myList->selectionModel();
241 if ( !aSelectionModel ) {
245 for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
246 QModelIndex anIndex = aSourceModel->index( aRow, 0 );
247 if ( !anIndex.isValid() ) {
251 QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
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 );
262 Get names of the selected objects.
263 @return the list of names
265 QStringList HYDROGUI_OrderedListWidget::getSelectedNames() const
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();
288 Get names of all objects.
289 @return the list of names
291 QStringList HYDROGUI_OrderedListWidget::getAllNames() const
296 foreach ( const Handle(HYDROData_Entity)& anObject, getObjects() ) {
297 aNames << anObject->GetName();
304 Slot called on top, up, down and bottom button click.
305 @param theType the move operation type
307 void HYDROGUI_OrderedListWidget::onMove( int theType )
309 bool isMoved = false;
311 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
313 HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
315 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
316 QModelIndexList aSelectedSourceIndexes;
317 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
318 aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
320 QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
321 isMoved = aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType,
322 !myIsHiddenObjectsShown );
333 Returns the list source model.
334 @return the source model
336 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
338 HYDROGUI_ListModel* aSourceModel = 0;
340 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
342 aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
349 Enable/disable ordering (i.e. enable/disable arrow buttons and drag and drop).
350 @param theIsToEnable if true - ordering will bw enabled
352 void HYDROGUI_OrderedListWidget::setOrderingEnabled( const bool theIsToEnable )
354 // enable/disable arrow buttons
355 myTop->setEnabled( theIsToEnable );
356 myUp->setEnabled( theIsToEnable );
357 myDown->setEnabled( theIsToEnable );
358 myBottom->setEnabled( theIsToEnable );
360 // enable/disable drag and drop
361 myList->setDragEnabled( theIsToEnable );
364 void HYDROGUI_OrderedListWidget::undoLastMove()
366 HYDROGUI_ListModel* aModel = getSourceModel();
368 aModel->undoLastMove();