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 <QToolButton>
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 QToolButton;
70 myTop->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_TOP_ICO" ) ) );
71 myTop->setIconSize( QSize( 32, 32 ) );
72 myUp = new QToolButton;
73 myUp->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_UP_ICO" ) ) );
74 myUp->setIconSize( myTop->iconSize() );
75 myDown = new QToolButton;
76 myDown->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_DOWN_ICO" ) ) );
77 myDown->setIconSize( myTop->iconSize() );
78 myBottom = new QToolButton;
79 myBottom->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_BOTTOM_ICO" ) ) );
80 myBottom->setIconSize( myTop->iconSize() );
84 QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
85 aListButtonsLayout->addWidget( myTop );
86 aListButtonsLayout->addWidget( myUp );
87 aListButtonsLayout->addWidget( myDown );
88 aListButtonsLayout->addWidget( myBottom );
89 aListButtonsLayout->addStretch();
91 aMainLayout->addWidget( myList );
92 aMainLayout->addLayout( aListButtonsLayout );
95 QSignalMapper* aSignalMapper = new QSignalMapper( this );
96 aSignalMapper->setMapping( myTop, HYDROGUI_ListModel::Top );
97 aSignalMapper->setMapping( myUp, HYDROGUI_ListModel::Up );
98 aSignalMapper->setMapping( myDown, HYDROGUI_ListModel::Down );
99 aSignalMapper->setMapping( myBottom, HYDROGUI_ListModel::Bottom );
100 connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
101 connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
102 connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
103 connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
104 connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
106 connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
107 this, SIGNAL( selectionChanged() ) );
110 setHiddenObjectsShown( true );
116 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
121 Set the list of objects (which supposed to be ordered by the widget).
122 @param theObjects the list of pairs (object; visibility)
124 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
126 HYDROGUI_ListModel* aModel = getSourceModel();
128 aModel->setObjects( theObjects );
133 Returns the ordered list of objects.
134 @return the list of objects
136 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
138 QList<Handle(HYDROData_Entity)> anObjects;
140 HYDROGUI_ListModel* aModel = getSourceModel();
142 anObjects = aModel->getObjects();
149 Set whether the hidden objects are presented in the list.
150 @param theIsToShow if true - the hidden objects will be shown in the list
152 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
154 myIsHiddenObjectsShown = theIsToShow;
156 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
157 QString anExpr = theIsToShow ? "true|false" : "true";
158 aFilterModel->setFilterRegExp( anExpr );
162 Get entries of the selected objects.
163 @return the list of entries
165 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
167 QStringList anEntries;
169 QSortFilterProxyModel* aFilterModel =
170 dynamic_cast<QSortFilterProxyModel*>( myList->model() );
172 HYDROGUI_ListModel* aSourceModel =
173 dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
174 if ( aSourceModel ) {
175 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
176 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
177 QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
178 QString anEntry = aSourceModel->data( aSourceIndex, HYDROGUI_EntryRole ).toString();
179 anEntries << anEntry;
188 Set objects with the given entries selected (other objects will deselected).
189 @param theEntries the list of entries
191 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
193 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
194 if( !aFilterModel ) {
197 HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
198 if ( !aSourceModel ) {
201 QItemSelectionModel* aSelectionModel = myList->selectionModel();
202 if ( !aSelectionModel ) {
206 for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
207 QModelIndex anIndex = aSourceModel->index( aRow, 0 );
208 if ( !anIndex.isValid() ) {
212 QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
214 bool isToSelect = theEntries.contains( anEntry );
215 QModelIndex aProxyModelIndex = aFilterModel->mapFromSource( anIndex );
216 QItemSelectionModel::SelectionFlags aSelectionFlags =
217 isToSelect ? QItemSelectionModel::Select : QItemSelectionModel::Deselect;
218 aSelectionModel->select( aProxyModelIndex, aSelectionFlags );
223 Slot called on top, up, down and bottom button click.
224 @param theType the move operation type
226 void HYDROGUI_OrderedListWidget::onMove( int theType )
228 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
230 HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
232 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
233 QModelIndexList aSelectedSourceIndexes;
234 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
235 aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
237 QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
238 aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType,
239 !myIsHiddenObjectsShown );
245 Returns the list source model.
246 @return the source model
248 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
250 HYDROGUI_ListModel* aSourceModel = 0;
252 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
254 aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );