1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
40 @param theIconSize the icon size for arrow buttons
42 HYDROGUI_OrderedListWidget::HYDROGUI_OrderedListWidget( QWidget* theParent, int theArrowIconSize )
43 : QWidget( theParent )
46 QHBoxLayout* aMainLayout = new QHBoxLayout( this );
47 aMainLayout->setMargin( 0 );
48 aMainLayout->setSpacing( 5 );
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 );
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 );
67 myList->setModel( aFilteredModel );
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" ) );
90 QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
91 aListButtonsLayout->addWidget( myTop );
92 aListButtonsLayout->addWidget( myUp );
93 aListButtonsLayout->addWidget( myDown );
94 aListButtonsLayout->addWidget( myBottom );
95 aListButtonsLayout->addStretch();
97 aMainLayout->addWidget( myList );
98 aMainLayout->addLayout( aListButtonsLayout );
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 ) ) );
112 connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
113 this, SIGNAL( selectionChanged() ) );
116 setHiddenObjectsShown( true );
117 setVisibilityIconShown( true );
123 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
128 Set the list of objects (which supposed to be ordered by the widget).
129 @param theObjects the list of pairs (object; visibility)
131 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
133 HYDROGUI_ListModel* aModel = getSourceModel();
135 aModel->setObjects( theObjects );
140 Returns the ordered list of objects.
141 @return the list of objects
143 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
145 QList<Handle(HYDROData_Entity)> anObjects;
147 HYDROGUI_ListModel* aModel = getSourceModel();
149 anObjects = aModel->getObjects();
156 Add the object to the end of the list.
157 @param theObjects the pair (object; visibility)
159 void HYDROGUI_OrderedListWidget::addObject( const HYDROGUI_ListModel::Object2Visible& theObject )
161 HYDROGUI_ListModel* aModel = getSourceModel();
163 aModel->addObject(theObject);
168 Remove the object from the list.
169 @param theObjectName the name of the object to remove
171 void HYDROGUI_OrderedListWidget::removeObjectByName( const QString& theObjectName )
173 HYDROGUI_ListModel* aModel = getSourceModel();
175 aModel->removeObjectByName(theObjectName);
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
183 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
185 myIsHiddenObjectsShown = theIsToShow;
187 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
188 QString anExpr = theIsToShow ? "true|false" : "true";
189 aFilterModel->setFilterRegExp( anExpr );
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
196 void HYDROGUI_OrderedListWidget::setVisibilityIconShown( const bool theIsToShow )
198 HYDROGUI_ListModel* aModel = getSourceModel();
200 aModel->setDecorationEnabled( theIsToShow );
205 Get entries of the selected objects.
206 @return the list of entries
208 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
210 QStringList anEntries;
212 QSortFilterProxyModel* aFilterModel =
213 dynamic_cast<QSortFilterProxyModel*>( myList->model() );
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;
231 Set objects with the given entries selected (other objects will deselected).
232 @param theEntries the list of entries
234 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
236 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
237 if( !aFilterModel ) {
240 HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
241 if ( !aSourceModel ) {
244 QItemSelectionModel* aSelectionModel = myList->selectionModel();
245 if ( !aSelectionModel ) {
249 for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
250 QModelIndex anIndex = aSourceModel->index( aRow, 0 );
251 if ( !anIndex.isValid() ) {
255 QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
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 );
266 Get names of the selected objects.
267 @return the list of names
269 QStringList HYDROGUI_OrderedListWidget::getSelectedNames() const
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();
292 Get names of all objects.
293 @return the list of names
295 QStringList HYDROGUI_OrderedListWidget::getAllNames() const
300 foreach ( const Handle(HYDROData_Entity)& anObject, getObjects() ) {
301 aNames << anObject->GetName();
308 Slot called on top, up, down and bottom button click.
309 @param theType the move operation type
311 void HYDROGUI_OrderedListWidget::onMove( int theType )
313 bool isMoved = false;
315 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
317 HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
319 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
320 QModelIndexList aSelectedSourceIndexes;
321 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
322 aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
324 QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
325 isMoved = aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType,
326 !myIsHiddenObjectsShown );
336 Returns the list source model.
337 @return the source model
339 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
341 HYDROGUI_ListModel* aSourceModel = 0;
343 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
345 aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
352 Enable/disable ordering (i.e. enable/disable arrow buttons and drag and drop).
353 @param theIsToEnable if true - ordering will bw enabled
355 void HYDROGUI_OrderedListWidget::setOrderingEnabled( const bool theIsToEnable )
357 // enable/disable arrow buttons
358 myTop->setEnabled( theIsToEnable );
359 myUp->setEnabled( theIsToEnable );
360 myDown->setEnabled( theIsToEnable );
361 myBottom->setEnabled( theIsToEnable );
363 // enable/disable drag and drop
364 myList->setDragEnabled( theIsToEnable );