1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_OrderedListWidget.h"
25 #include "HYDROGUI_ListModel.h"
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
32 #include <QPushButton>
33 #include <QSignalMapper>
34 #include <QSortFilterProxyModel>
39 @param theParent the parent widget
41 HYDROGUI_OrderedListWidget::HYDROGUI_OrderedListWidget( QWidget* theParent )
42 : QWidget( theParent )
45 QHBoxLayout* aMainLayout = new QHBoxLayout( this );
46 aMainLayout->setSpacing( 5 );
49 myList = new QListView( this );
50 myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
51 // enable drag and drop
52 myList->setDragEnabled( true );
53 myList->setAcceptDrops( true );
54 // configure drag and drop
55 myList->setDropIndicatorShown( true );
56 myList->setDragDropMode( QAbstractItemView::InternalMove );
58 // Set the custom model
59 HYDROGUI_ListModel* aModel = new HYDROGUI_ListModel();
60 QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
61 aFilteredModel->setSourceModel( aModel );
62 aFilteredModel->setFilterKeyColumn( 0 );
63 aFilteredModel->setFilterRole( HYDROGUI_VisibleRole );
65 myList->setModel( aFilteredModel );
67 // Buttons top, up, down, bottom
68 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
69 myTop = new QPushButton( this );
70 myTop->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_TOP_ICO" ) ) );
71 myTop->setIconSize( QSize( 32, 32 ) );
72 myTop->setToolTip( tr( "TOP_TLT" ) );
73 myUp = new QPushButton( this );
74 myUp->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_UP_ICO" ) ) );
75 myUp->setIconSize( myTop->iconSize() );
76 myUp->setToolTip( tr( "UP_TLT" ) );
77 myDown = new QPushButton( this );
78 myDown->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_DOWN_ICO" ) ) );
79 myDown->setIconSize( myTop->iconSize() );
80 myDown->setToolTip( tr( "DOWN_TLT" ) );
81 myBottom = new QPushButton( this );
82 myBottom->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_BOTTOM_ICO" ) ) );
83 myBottom->setIconSize( myTop->iconSize() );
84 myBottom->setToolTip( tr( "BOTTOM_TLT" ) );
88 QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
89 aListButtonsLayout->addWidget( myTop );
90 aListButtonsLayout->addWidget( myUp );
91 aListButtonsLayout->addWidget( myDown );
92 aListButtonsLayout->addWidget( myBottom );
93 aListButtonsLayout->addStretch();
95 aMainLayout->addWidget( myList );
96 aMainLayout->addLayout( aListButtonsLayout );
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 ) ) );
110 connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
111 this, SIGNAL( selectionChanged() ) );
114 setHiddenObjectsShown( true );
120 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
125 Set the list of objects (which supposed to be ordered by the widget).
126 @param theObjects the list of pairs (object; visibility)
128 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
130 HYDROGUI_ListModel* aModel = getSourceModel();
132 aModel->setObjects( theObjects );
137 Returns the ordered list of objects.
138 @return the list of objects
140 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
142 QList<Handle(HYDROData_Entity)> anObjects;
144 HYDROGUI_ListModel* aModel = getSourceModel();
146 anObjects = aModel->getObjects();
153 Set whether the hidden objects are presented in the list.
154 @param theIsToShow if true - the hidden objects will be shown in the list
156 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
158 myIsHiddenObjectsShown = theIsToShow;
160 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
161 QString anExpr = theIsToShow ? "true|false" : "true";
162 aFilterModel->setFilterRegExp( anExpr );
166 Get entries of the selected objects.
167 @return the list of entries
169 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
171 QStringList anEntries;
173 QSortFilterProxyModel* aFilterModel =
174 dynamic_cast<QSortFilterProxyModel*>( myList->model() );
176 HYDROGUI_ListModel* aSourceModel =
177 dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
178 if ( aSourceModel ) {
179 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
180 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
181 QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
182 QString anEntry = aSourceModel->data( aSourceIndex, HYDROGUI_EntryRole ).toString();
183 anEntries << anEntry;
192 Set objects with the given entries selected (other objects will deselected).
193 @param theEntries the list of entries
195 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
197 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
198 if( !aFilterModel ) {
201 HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
202 if ( !aSourceModel ) {
205 QItemSelectionModel* aSelectionModel = myList->selectionModel();
206 if ( !aSelectionModel ) {
210 for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
211 QModelIndex anIndex = aSourceModel->index( aRow, 0 );
212 if ( !anIndex.isValid() ) {
216 QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
218 bool isToSelect = theEntries.contains( anEntry );
219 QModelIndex aProxyModelIndex = aFilterModel->mapFromSource( anIndex );
220 QItemSelectionModel::SelectionFlags aSelectionFlags =
221 isToSelect ? QItemSelectionModel::Select : QItemSelectionModel::Deselect;
222 aSelectionModel->select( aProxyModelIndex, aSelectionFlags );
227 Slot called on top, up, down and bottom button click.
228 @param theType the move operation type
230 void HYDROGUI_OrderedListWidget::onMove( int theType )
232 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
234 HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
236 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
237 QModelIndexList aSelectedSourceIndexes;
238 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
239 aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
241 QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
242 aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType,
243 !myIsHiddenObjectsShown );
249 Returns the list source model.
250 @return the source model
252 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
254 HYDROGUI_ListModel* aSourceModel = 0;
256 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
258 aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );