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_ListModel.h"
21 #include "HYDROGUI_DataObject.h"
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
28 const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
33 @param theParent the parent object
35 HYDROGUI_ListModel::HYDROGUI_ListModel( QObject* theParent )
36 : QAbstractListModel( theParent ), myIsDecorationEnabled( true )
38 // Get resource manager
39 SUIT_ResourceMgr* aResMgr = 0;
40 SUIT_Session* aSession = SUIT_Session::session();
42 aResMgr = SUIT_Session::session()->resourceMgr();
45 // Define eye icon and empty icon
46 myEmpty = QPixmap( 16, 16 );
47 myEmpty.fill( Qt::transparent );
49 myEye = aResMgr->loadPixmap( "HYDRO", tr( "EYE_ICO" ) );
51 myEye = QPixmap( 16, 16 );
52 myEye.fill( Qt::black );
59 HYDROGUI_ListModel::~HYDROGUI_ListModel()
65 QVariant HYDROGUI_ListModel::data( const QModelIndex &theIndex, int theRole ) const
69 int aRow = theIndex.row();
70 int aColumn = theIndex.column();
76 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
77 return myObjects.at( aRow ).first->GetName();
83 case Qt::DecorationRole:
85 if( myIsDecorationEnabled &&
86 aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
87 bool isVisible = isObjectVisible( aRow );
97 case HYDROGUI_VisibleRole:
99 bool isVisible = isObjectVisible( aRow );
100 return QVariant( isVisible ).toString();
103 case HYDROGUI_EntryRole:
105 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
106 aVariant = HYDROGUI_DataObject::dataObjectEntry( myObjects.at( aRow ).first );
117 int HYDROGUI_ListModel::rowCount( const QModelIndex &theParent ) const
119 return myObjects.count();
124 @param theObjects the list of pairs (object; object visibility)
126 void HYDROGUI_ListModel::setObjects( const Object2VisibleList& theObjects )
129 myObjects = theObjects;
135 @return the list of objects ordered according to the model
137 HYDROGUI_ListModel::ObjectList HYDROGUI_ListModel::getObjects() const
139 ObjectList anObjects;
141 foreach( const Object2Visible& anItem, myObjects ) {
142 anObjects << anItem.first;
149 Add the object to the end of the list.
150 @param theObjects the pair (object; visibility)
152 void HYDROGUI_ListModel::addObject( const Object2Visible& theObject )
155 myObjects << theObject;
160 Remove the object from the list.
161 @param theObjectName the name of the object to remove
163 void HYDROGUI_ListModel::removeObjectByName( const QString& theObjectName )
165 Object2Visible anItem;
166 foreach( anItem, myObjects ) {
167 if ( anItem.first->GetName() == theObjectName ) {
173 myObjects.removeAll(anItem);
179 Check if the object is visible.
180 @param theIndex the object index
181 @return true if the object is visible, false - otherwise
183 bool HYDROGUI_ListModel::isObjectVisible( int theIndex ) const
185 bool isVisible = false;
187 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
188 isVisible = myObjects.at( theIndex ).second;
196 QVariant HYDROGUI_ListModel::headerData( int theSection,
197 Qt::Orientation theOrientation,
200 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
205 return tr( "VISIBLE" );
207 return tr( "OBJECT_NAME" );
215 Qt::ItemFlags HYDROGUI_ListModel::flags( const QModelIndex& theIndex ) const
217 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
218 if( theIndex.isValid() )
219 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
221 return Qt::ItemIsDropEnabled | aDefaultFlags;
226 QMimeData* HYDROGUI_ListModel::mimeData( const QModelIndexList& theIndexes ) const
228 QMimeData* aMimeData = new QMimeData();
229 QByteArray anEncodedData;
230 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
232 QList<int> anIdsList = getIds( theIndexes, true );
233 foreach( int anId, anIdsList )
236 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
242 QStringList HYDROGUI_ListModel::mimeTypes() const
245 aTypes << OBJ_LIST_MIME_TYPE;
251 bool HYDROGUI_ListModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
252 int theRow, int theColumn, const QModelIndex& theParent )
254 if( theAction == Qt::IgnoreAction)
257 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
263 // TODO: to disable drop between items use: int aDropItemId = theParent.row();
264 int aDropItemId = theParent.isValid() ? theParent.row() : theRow;
266 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
267 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
268 QList<int> anIdsList;
269 while( !aStream.atEnd() )
276 move( anIdsList, DragAndDrop, false, aDropItemId ); //TODO set visibility?
282 Qt::DropActions HYDROGUI_ListModel::supportedDropActions() const
284 return Qt::MoveAction | Qt::CopyAction;
289 Qt::DropActions HYDROGUI_ListModel::supportedDragActions() const
291 // Set the supported drag actions for the items in the model
292 return Qt::MoveAction | Qt::CopyAction;
296 Get list of ids by the list model indexes.
297 @param theIsToSort defines if the list of ids should be sorted in ascending order
298 @return the list of ids
300 QList<int> HYDROGUI_ListModel::getIds( const QModelIndexList& theIndexes,
301 bool theIsToSort ) const
304 foreach( const QModelIndex& anIndex, theIndexes ) {
305 anIds << anIndex.row();
317 @param theItem the item id to move
318 @param theType the move operation type
319 @param theIsVisibleOnly indicates if do move relating to the visible objects only
320 @param theDropItem the drop item id ( used for drag and drop obly )
321 @return true in case of success
323 bool HYDROGUI_ListModel::move( const int theItem, const OpType theType,
324 bool theIsVisibleOnly, const int theDropItem )
327 if ( theItem < 0 || theItem >= myObjects.count() ) {
331 int aDestinationIndex = -1;
332 bool isInsertBefore = false;
336 isInsertBefore = true;
338 aDestinationIndex = theItem - 1;
339 if ( theIsVisibleOnly ) {
340 while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
347 if ( theItem < myObjects.count() - 1 ) {
348 aDestinationIndex = theItem + 1;
349 if ( theIsVisibleOnly ) {
350 while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
357 isInsertBefore = true;
359 aDestinationIndex = 0;
363 if ( theItem < myObjects.count() - 1 ) {
364 aDestinationIndex = myObjects.count() - 1;
368 if ( theItem > theDropItem ) {
369 isInsertBefore = true;
370 aDestinationIndex = theDropItem;
372 aDestinationIndex = theDropItem - 1;
377 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
378 int aDestinationRow = isInsertBefore ? aDestinationIndex : aDestinationIndex + 1;
379 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
380 myPrevObjects = myObjects;
381 myObjects.move( theItem, aDestinationIndex );
392 @param theItems the list of item ids to move
393 @param theType the move operation type
394 @param theIsVisibleOnly indicates if do move relating to the visible objects only
395 @param theDropItem the drop item id ( used for drag and drop obly )
396 @return true in case of success
398 bool HYDROGUI_ListModel::move( const QList<int>& theItems, const OpType theType,
399 bool theIsVisibleOnly, const int theDropItem )
403 QListIterator<int> anIt( theItems );
411 while ( anIt.hasPrevious() ) {
412 int anId = anIt.previous();
413 if ( theType == Top ) {
417 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
426 while ( anIt.hasNext() ) {
427 int anId = anIt.next();
428 if ( theType == Bottom ) {
432 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
440 aRes = isDragAndDropAllowed( theItems, theDropItem );
443 int aDropItem = theDropItem;
444 while ( anIt.hasNext() ) {
445 int anId = anIt.next();
446 aDropItem = theDropItem + aDropShift;
447 if ( anId > aDropItem ) {
452 if ( ( aDropItem - anId ) != 1 ) {
456 move( anId, theType, theIsVisibleOnly, aDropItem );
468 Check if drag and drop operation allowed.
469 @param theItems the list of dragged item ids
470 @param theDropItem the drop item id
471 @return true if drag and drop allowed
473 bool HYDROGUI_ListModel::isDragAndDropAllowed( const QList<int>& theItems,
474 const int theDropItem ) const
476 bool isAllowed = false;
478 if ( theDropItem >= 0 &&
479 // TODO: to disable drop between items use: theDropItem < myObjects.count()
480 theDropItem <= myObjects.count() &&
481 !theItems.empty() && theItems.count() < myObjects.count() &&
482 !theItems.contains( theDropItem )) {
490 Enable/disable decoration (eye icon).
491 @param theIsToEnable if true - the decoration will be enabled
493 void HYDROGUI_ListModel::setDecorationEnabled( const bool theIsToEnable )
495 myIsDecorationEnabled = theIsToEnable;
498 void HYDROGUI_ListModel::undoLastMove()
501 myObjects = myPrevObjects;