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 // Add list to the main layout
66 aMainLayout->addWidget( myList );
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" ) );
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();
96 aMainLayout->addLayout( aListButtonsLayout );
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 ) ) );
110 myTop = myUp = myDown = myBottom = 0;
113 connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
114 this, SIGNAL( selectionChanged() ) );
116 connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
117 this, SLOT( onSelectionChanged() ) );
120 setHiddenObjectsShown( true );
121 setVisibilityIconShown( true );
124 void HYDROGUI_OrderedListWidget::onSelectionChanged()
126 QStringList selectedEntries = getSelectedEntries();
133 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
138 Set the list of objects (which supposed to be ordered by the widget).
139 @param theObjects the list of pairs (object; visibility)
141 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
143 HYDROGUI_ListModel* aModel = getSourceModel();
145 aModel->setObjects( theObjects );
149 void HYDROGUI_OrderedListWidget::setBackgroundColor (int theInd, QColor theColor)
151 HYDROGUI_ListModel* aModel = getSourceModel();
153 aModel->setBackgroundColor( theInd, theColor );
157 void HYDROGUI_OrderedListWidget::clearAllBackgroundColors ()
159 HYDROGUI_ListModel* aModel = getSourceModel();
161 aModel->clearAllBackgroundColors( );
166 QColor HYDROGUI_OrderedListWidget::getBackgroundColor (int theInd) const
168 HYDROGUI_ListModel* aModel = getSourceModel();
170 return aModel->getBackgroundColor( theInd );
175 Returns the ordered list of objects.
176 @return the list of objects
178 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
180 QList<Handle(HYDROData_Entity)> anObjects;
182 HYDROGUI_ListModel* aModel = getSourceModel();
184 anObjects = aModel->getObjects();
191 Add the object to the end of the list.
192 @param theObjects the pair (object; visibility)
194 void HYDROGUI_OrderedListWidget::addObject( const HYDROGUI_ListModel::Object2Visible& theObject )
196 HYDROGUI_ListModel* aModel = getSourceModel();
198 aModel->addObject(theObject);
203 Remove the object from the list.
204 @param theObjectName the name of the object to remove
206 void HYDROGUI_OrderedListWidget::removeObjectByName( const QString& theObjectName )
208 HYDROGUI_ListModel* aModel = getSourceModel();
210 aModel->removeObjectByName(theObjectName);
215 Set whether the hidden objects are presented in the list.
216 @param theIsToShow if true - the hidden objects will be shown in the list
218 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
220 myIsHiddenObjectsShown = theIsToShow;
222 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
223 QString anExpr = theIsToShow ? "true|false" : "true";
224 aFilterModel->setFilterRegExp( anExpr );
228 Set whether the visibility icon (eye icon) are presented in the list.
229 @param theIsToShow if true - the eye icon will be shown in the list
231 void HYDROGUI_OrderedListWidget::setVisibilityIconShown( const bool theIsToShow )
233 HYDROGUI_ListModel* aModel = getSourceModel();
235 aModel->setDecorationEnabled( theIsToShow );
240 Get entries of the selected objects.
241 @return the list of entries
243 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
245 QStringList anEntries;
247 QSortFilterProxyModel* aFilterModel =
248 dynamic_cast<QSortFilterProxyModel*>( myList->model() );
250 HYDROGUI_ListModel* aSourceModel =
251 dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
252 if ( aSourceModel ) {
253 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
254 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
255 QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
256 QString anEntry = aSourceModel->data( aSourceIndex, HYDROGUI_EntryRole ).toString();
257 anEntries << anEntry;
266 Set objects with the given entries selected (other objects will deselected).
267 @param theEntries the list of entries
269 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
271 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
272 if( !aFilterModel ) {
275 HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
276 if ( !aSourceModel ) {
279 QItemSelectionModel* aSelectionModel = myList->selectionModel();
280 if ( !aSelectionModel ) {
284 for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
285 QModelIndex anIndex = aSourceModel->index( aRow, 0 );
286 if ( !anIndex.isValid() ) {
290 QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
292 bool isToSelect = theEntries.contains( anEntry );
293 QModelIndex aProxyModelIndex = aFilterModel->mapFromSource( anIndex );
294 QItemSelectionModel::SelectionFlags aSelectionFlags =
295 isToSelect ? QItemSelectionModel::Select : QItemSelectionModel::Deselect;
296 aSelectionModel->select( aProxyModelIndex, aSelectionFlags );
301 Get names of the selected objects.
302 @return the list of names
304 QStringList HYDROGUI_OrderedListWidget::getSelectedNames() const
308 QSortFilterProxyModel* aFilterModel =
309 dynamic_cast<QSortFilterProxyModel*>( myList->model() );
310 if ( aFilterModel ) {
311 HYDROGUI_ListModel* aSourceModel =
312 dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
313 if ( aSourceModel ) {
314 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
315 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
316 QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
317 QString anEntry = aSourceModel->data( aSourceIndex, Qt::DisplayRole ).toString();
327 Get names of all objects.
328 @return the list of names
330 QStringList HYDROGUI_OrderedListWidget::getAllNames() const
335 foreach ( const Handle(HYDROData_Entity)& anObject, getObjects() ) {
336 aNames << anObject->GetName();
343 Slot called on top, up, down and bottom button click.
344 @param theType the move operation type
346 void HYDROGUI_OrderedListWidget::onMove( int theType )
348 bool isMoved = false;
350 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
352 HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
354 QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
355 QModelIndexList aSelectedSourceIndexes;
356 foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
357 aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
359 QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
360 isMoved = aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType,
361 !myIsHiddenObjectsShown );
372 Returns the list source model.
373 @return the source model
375 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
377 HYDROGUI_ListModel* aSourceModel = 0;
379 QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
381 aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
388 Enable/disable ordering (i.e. enable/disable arrow buttons and drag and drop).
389 @param theIsToEnable if true - ordering will bw enabled
391 void HYDROGUI_OrderedListWidget::setOrderingEnabled( const bool theIsToEnable )
393 // enable/disable arrow buttons
394 if ( myTop && myUp && myDown && myBottom ) {
395 myTop->setEnabled( theIsToEnable );
396 myUp->setEnabled( theIsToEnable );
397 myDown->setEnabled( theIsToEnable );
398 myBottom->setEnabled( theIsToEnable );
401 // enable/disable drag and drop
402 myList->setDragEnabled( theIsToEnable );
405 void HYDROGUI_OrderedListWidget::undoLastMove()
407 HYDROGUI_ListModel* aModel = getSourceModel();
409 aModel->undoLastMove();