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 );
55 // Set the supported drag actions for the items in the model
56 setSupportedDragActions( Qt::MoveAction | Qt::CopyAction );
62 HYDROGUI_ListModel::~HYDROGUI_ListModel()
68 QVariant HYDROGUI_ListModel::data( const QModelIndex &theIndex, int theRole ) const
72 int aRow = theIndex.row();
73 int aColumn = theIndex.column();
79 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
80 return myObjects.at( aRow ).first->GetName();
86 case Qt::DecorationRole:
88 if( myIsDecorationEnabled &&
89 aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
90 bool isVisible = isObjectVisible( aRow );
100 case HYDROGUI_VisibleRole:
102 bool isVisible = isObjectVisible( aRow );
103 return QVariant( isVisible ).toString();
106 case HYDROGUI_EntryRole:
108 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
109 aVariant = HYDROGUI_DataObject::dataObjectEntry( myObjects.at( aRow ).first );
120 int HYDROGUI_ListModel::rowCount( const QModelIndex &theParent ) const
122 return myObjects.count();
127 @param theObjects the list of pairs (object; object visibility)
129 void HYDROGUI_ListModel::setObjects( const Object2VisibleList& theObjects )
131 myObjects = theObjects;
138 @return the list of objects ordered according to the model
140 HYDROGUI_ListModel::ObjectList HYDROGUI_ListModel::getObjects() const
142 ObjectList anObjects;
144 foreach( const Object2Visible& anItem, myObjects ) {
145 anObjects << anItem.first;
152 Add the object to the end of the list.
153 @param theObjects the pair (object; visibility)
155 void HYDROGUI_ListModel::addObject( const Object2Visible& theObject )
157 myObjects << theObject;
163 Remove the object from the list.
164 @param theObjectName the name of the object to remove
166 void HYDROGUI_ListModel::removeObjectByName( const QString& theObjectName )
168 Object2Visible anItem;
169 foreach( anItem, myObjects ) {
170 if ( anItem.first->GetName() == theObjectName ) {
175 myObjects.removeAll(anItem);
182 Check if the object is visible.
183 @param theIndex the object index
184 @return true if the object is visible, false - otherwise
186 bool HYDROGUI_ListModel::isObjectVisible( int theIndex ) const
188 bool isVisible = false;
190 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
191 isVisible = myObjects.at( theIndex ).second;
199 QVariant HYDROGUI_ListModel::headerData( int theSection,
200 Qt::Orientation theOrientation,
203 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
208 return tr( "VISIBLE" );
210 return tr( "OBJECT_NAME" );
218 Qt::ItemFlags HYDROGUI_ListModel::flags( const QModelIndex& theIndex ) const
220 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
221 if( theIndex.isValid() )
222 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
224 return Qt::ItemIsDropEnabled | aDefaultFlags;
229 QMimeData* HYDROGUI_ListModel::mimeData( const QModelIndexList& theIndexes ) const
231 QMimeData* aMimeData = new QMimeData();
232 QByteArray anEncodedData;
233 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
235 QList<int> anIdsList = getIds( theIndexes, true );
236 foreach( int anId, anIdsList )
239 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
245 QStringList HYDROGUI_ListModel::mimeTypes() const
248 aTypes << OBJ_LIST_MIME_TYPE;
254 bool HYDROGUI_ListModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
255 int theRow, int theColumn, const QModelIndex& theParent )
257 if( theAction == Qt::IgnoreAction)
260 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
266 // TODO: to disable drop between items use: int aDropItemId = theParent.row();
267 int aDropItemId = theParent.isValid() ? theParent.row() : theRow;
269 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
270 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
271 QList<int> anIdsList;
272 while( !aStream.atEnd() )
279 move( anIdsList, DragAndDrop, false, aDropItemId ); //TODO set visibility?
285 Qt::DropActions HYDROGUI_ListModel::supportedDropActions() const
287 return Qt::MoveAction | Qt::CopyAction;
291 Get list of ids by the list model indexes.
292 @param theIsToSort defines if the list of ids should be sorted in ascending order
293 @return the list of ids
295 QList<int> HYDROGUI_ListModel::getIds( const QModelIndexList& theIndexes,
296 bool theIsToSort ) const
299 foreach( const QModelIndex& anIndex, theIndexes ) {
300 anIds << anIndex.row();
312 @param theItem the item id to move
313 @param theType the move operation type
314 @param theIsVisibleOnly indicates if do move relating to the visible objects only
315 @param theDropItem the drop item id ( used for drag and drop obly )
316 @return true in case of success
318 bool HYDROGUI_ListModel::move( const int theItem, const OpType theType,
319 bool theIsVisibleOnly, const int theDropItem )
322 if ( theItem < 0 || theItem >= myObjects.count() ) {
326 int aDestinationIndex = -1;
327 bool isInsertBefore = false;
331 isInsertBefore = true;
333 aDestinationIndex = theItem - 1;
334 if ( theIsVisibleOnly ) {
335 while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
342 if ( theItem < myObjects.count() - 1 ) {
343 aDestinationIndex = theItem + 1;
344 if ( theIsVisibleOnly ) {
345 while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
352 isInsertBefore = true;
354 aDestinationIndex = 0;
358 if ( theItem < myObjects.count() - 1 ) {
359 aDestinationIndex = myObjects.count() - 1;
363 if ( theItem > theDropItem ) {
364 isInsertBefore = true;
365 aDestinationIndex = theDropItem;
367 aDestinationIndex = theDropItem - 1;
372 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
373 int aDestinationRow = isInsertBefore ? aDestinationIndex : aDestinationIndex + 1;
374 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
375 myPrevObjects = myObjects;
376 myObjects.move( theItem, aDestinationIndex );
387 @param theItems the list of item ids to move
388 @param theType the move operation type
389 @param theIsVisibleOnly indicates if do move relating to the visible objects only
390 @param theDropItem the drop item id ( used for drag and drop obly )
391 @return true in case of success
393 bool HYDROGUI_ListModel::move( const QList<int>& theItems, const OpType theType,
394 bool theIsVisibleOnly, const int theDropItem )
398 QListIterator<int> anIt( theItems );
406 while ( anIt.hasPrevious() ) {
407 int anId = anIt.previous();
408 if ( theType == Top ) {
412 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
421 while ( anIt.hasNext() ) {
422 int anId = anIt.next();
423 if ( theType == Bottom ) {
427 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
435 aRes = isDragAndDropAllowed( theItems, theDropItem );
438 int aDropItem = theDropItem;
439 while ( anIt.hasNext() ) {
440 int anId = anIt.next();
441 aDropItem = theDropItem + aDropShift;
442 if ( anId > aDropItem ) {
447 if ( ( aDropItem - anId ) != 1 ) {
451 move( anId, theType, theIsVisibleOnly, aDropItem );
463 Check if drag and drop operation allowed.
464 @param theItems the list of dragged item ids
465 @param theDropItem the drop item id
466 @return true if drag and drop allowed
468 bool HYDROGUI_ListModel::isDragAndDropAllowed( const QList<int>& theItems,
469 const int theDropItem ) const
471 bool isAllowed = false;
473 if ( theDropItem >= 0 &&
474 // TODO: to disable drop between items use: theDropItem < myObjects.count()
475 theDropItem <= myObjects.count() &&
476 !theItems.empty() && theItems.count() < myObjects.count() &&
477 !theItems.contains( theDropItem )) {
485 Enable/disable decoration (eye icon).
486 @param theIsToEnable if true - the decoration will be enabled
488 void HYDROGUI_ListModel::setDecorationEnabled( const bool theIsToEnable )
490 myIsDecorationEnabled = theIsToEnable;
493 void HYDROGUI_ListModel::undoLastMove()
495 myObjects = myPrevObjects;