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_ListModel.h"
25 #include "HYDROGUI_DataObject.h"
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
32 const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
37 @param theParent the parent object
39 HYDROGUI_ListModel::HYDROGUI_ListModel( QObject* theParent )
40 : QAbstractListModel( theParent )
42 // Get resource manager
43 SUIT_ResourceMgr* aResMgr = 0;
44 SUIT_Session* aSession = SUIT_Session::session();
46 aResMgr = SUIT_Session::session()->resourceMgr();
49 // Define eye icon and empty icon
50 myEmpty = QPixmap( 16, 16 );
51 myEmpty.fill( Qt::transparent );
53 myEye = aResMgr->loadPixmap( "HYDRO", tr( "EYE_ICO" ) );
55 myEye = QPixmap( 16, 16 );
56 myEye.fill( Qt::black );
59 // Set the supported drag actions for the items in the model
60 setSupportedDragActions( Qt::MoveAction | Qt::CopyAction );
66 HYDROGUI_ListModel::~HYDROGUI_ListModel()
72 QVariant HYDROGUI_ListModel::data( const QModelIndex &theIndex, int theRole ) const
76 int aRow = theIndex.row();
77 int aColumn = theIndex.column();
83 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
84 return myObjects.at( aRow ).first->GetName();
90 case Qt::DecorationRole:
92 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
94 bool isVisible = isObjectVisible( aRow );
104 case HYDROGUI_VisibleRole:
106 bool isVisible = isObjectVisible( aRow );
107 return QVariant( isVisible ).toString();
110 case HYDROGUI_EntryRole:
112 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
113 aVariant = HYDROGUI_DataObject::dataObjectEntry( myObjects.at( aRow ).first );
124 int HYDROGUI_ListModel::rowCount( const QModelIndex &theParent ) const
126 return myObjects.count();
131 @param theObjects the list of pairs (object; object visibility)
133 void HYDROGUI_ListModel::setObjects( const Object2VisibleList& theObjects )
135 myObjects = theObjects;
142 @return the list of objects ordered according to the model
144 HYDROGUI_ListModel::ObjectList HYDROGUI_ListModel::getObjects() const
146 ObjectList anObjects;
148 foreach( const Object2Visible& anItem, myObjects ) {
149 anObjects << anItem.first;
156 Check if the object is visible.
157 @param theIndex the object index
158 @return true if the object is visible, false - otherwise
160 bool HYDROGUI_ListModel::isObjectVisible( int theIndex ) const
162 bool isVisible = false;
164 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
165 isVisible = myObjects.at( theIndex ).second;
173 QVariant HYDROGUI_ListModel::headerData( int theSection,
174 Qt::Orientation theOrientation,
177 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
182 return tr( "VISIBLE" );
184 return tr( "OBJECT_NAME" );
192 Qt::ItemFlags HYDROGUI_ListModel::flags( const QModelIndex& theIndex ) const
194 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
195 if( theIndex.isValid() )
196 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
198 return Qt::ItemIsDropEnabled | aDefaultFlags;
203 QMimeData* HYDROGUI_ListModel::mimeData( const QModelIndexList& theIndexes ) const
205 QMimeData* aMimeData = new QMimeData();
206 QByteArray anEncodedData;
207 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
209 QList<int> anIdsList = getIds( theIndexes, true );
210 foreach( int anId, anIdsList )
213 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
219 QStringList HYDROGUI_ListModel::mimeTypes() const
222 aTypes << OBJ_LIST_MIME_TYPE;
228 bool HYDROGUI_ListModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
229 int theRow, int theColumn, const QModelIndex& theParent )
231 if( theAction == Qt::IgnoreAction)
234 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
240 // TODO: to disable drop between items use: int aDropItemId = theParent.row();
241 int aDropItemId = theParent.isValid() ? theParent.row() : theRow;
243 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
244 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
245 QList<int> anIdsList;
246 while( !aStream.atEnd() )
253 move( anIdsList, DragAndDrop, false, aDropItemId ); //TODO set visibility?
259 Qt::DropActions HYDROGUI_ListModel::supportedDropActions() const
261 return Qt::MoveAction | Qt::CopyAction;
265 Get list of ids by the list model indexes.
266 @param theIsToSort defines if the list of ids should be sorted in ascending order
267 @return the list of ids
269 QList<int> HYDROGUI_ListModel::getIds( const QModelIndexList& theIndexes,
270 bool theIsToSort ) const
273 foreach( const QModelIndex& anIndex, theIndexes ) {
274 anIds << anIndex.row();
286 @param theItem the item id to move
287 @param theType the move operation type
288 @param theIsVisibleOnly indicates if do move relating to the visible objects only
289 @param theDropItem the drop item id ( used for drag and drop obly )
290 @return true in case of success
292 bool HYDROGUI_ListModel::move( const int theItem, const OpType theType,
293 bool theIsVisibleOnly, const int theDropItem )
296 if ( theItem < 0 || theItem >= myObjects.count() ) {
300 int aDestinationIndex = -1;
301 bool isInsertBefore = false;
305 isInsertBefore = true;
307 aDestinationIndex = theItem - 1;
308 if ( theIsVisibleOnly ) {
309 while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
316 if ( theItem < myObjects.count() - 1 ) {
317 aDestinationIndex = theItem + 1;
318 if ( theIsVisibleOnly ) {
319 while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
326 isInsertBefore = true;
328 aDestinationIndex = 0;
332 if ( theItem < myObjects.count() - 1 ) {
333 aDestinationIndex = myObjects.count() - 1;
337 if ( theItem > theDropItem ) {
338 isInsertBefore = true;
339 aDestinationIndex = theDropItem;
341 aDestinationIndex = theDropItem - 1;
346 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
347 int aDestinationRow = isInsertBefore ? aDestinationIndex : aDestinationIndex + 1;
348 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
349 myObjects.move( theItem, aDestinationIndex );
360 @param theItems the list of item ids to move
361 @param theType the move operation type
362 @param theIsVisibleOnly indicates if do move relating to the visible objects only
363 @param theDropItem the drop item id ( used for drag and drop obly )
364 @return true in case of success
366 bool HYDROGUI_ListModel::move( const QList<int>& theItems, const OpType theType,
367 bool theIsVisibleOnly, const int theDropItem )
371 QListIterator<int> anIt( theItems );
379 while ( anIt.hasPrevious() ) {
380 int anId = anIt.previous();
381 if ( theType == Top ) {
385 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
394 while ( anIt.hasNext() ) {
395 int anId = anIt.next();
396 if ( theType == Bottom ) {
400 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
408 aRes = isDragAndDropAllowed( theItems, theDropItem );
411 int aDropItem = theDropItem;
412 while ( anIt.hasNext() ) {
413 int anId = anIt.next();
414 aDropItem = theDropItem + aDropShift;
415 if ( anId > aDropItem ) {
420 if ( ( aDropItem - anId ) != 1 ) {
424 move( anId, theType, theIsVisibleOnly, aDropItem );
436 Check if drag and drop operation allowed.
437 @param theItems the list of dragged item ids
438 @param theDropItem the drop item id
439 @return true if drag and drop allowed
441 bool HYDROGUI_ListModel::isDragAndDropAllowed( const QList<int>& theItems,
442 const int theDropItem ) const
444 bool isAllowed = false;
446 if ( theDropItem >= 0 &&
447 // TODO: to disable drop between items use: theDropItem < myObjects.count()
448 theDropItem <= myObjects.count() &&
449 !theItems.empty() && theItems.count() < myObjects.count() &&
450 !theItems.contains( theDropItem )) {