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()
66 void HYDROGUI_ListModel::setBackgroundColor(int theInd, QColor theColor)
68 myColoredRow[theInd] = theColor;
71 void HYDROGUI_ListModel::clearAllBackgroundColors()
76 QColor HYDROGUI_ListModel::getBackgroundColor(int theInd) const
78 if (myColoredRow.count( theInd ))
79 return myColoredRow[theInd];
86 QVariant HYDROGUI_ListModel::data( const QModelIndex &theIndex, int theRole ) const
90 int aRow = theIndex.row();
91 int aColumn = theIndex.column();
97 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
98 return myObjects.at( aRow ).first->GetName();
103 case Qt::BackgroundRole:
105 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() && myColoredRow.contains(aRow))
107 QBrush aBackgr(myColoredRow[aRow]);
114 case Qt::DecorationRole:
116 if( myIsDecorationEnabled &&
117 aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
118 bool isVisible = isObjectVisible( aRow );
128 case HYDROGUI_VisibleRole:
130 bool isVisible = isObjectVisible( aRow );
131 return QVariant( isVisible ).toString();
134 case HYDROGUI_EntryRole:
136 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() ) {
137 aVariant = HYDROGUI_DataObject::dataObjectEntry( myObjects.at( aRow ).first );
148 int HYDROGUI_ListModel::rowCount( const QModelIndex &theParent ) const
150 return myObjects.count();
155 @param theObjects the list of pairs (object; object visibility)
157 void HYDROGUI_ListModel::setObjects( const Object2VisibleList& theObjects )
159 myObjects = theObjects;
166 @return the list of objects ordered according to the model
168 HYDROGUI_ListModel::ObjectList HYDROGUI_ListModel::getObjects() const
170 ObjectList anObjects;
172 foreach( const Object2Visible& anItem, myObjects ) {
173 anObjects << anItem.first;
180 Add the object to the end of the list.
181 @param theObjects the pair (object; visibility)
183 void HYDROGUI_ListModel::addObject( const Object2Visible& theObject )
185 myObjects << theObject;
191 Remove the object from the list.
192 @param theObjectName the name of the object to remove
194 void HYDROGUI_ListModel::removeObjectByName( const QString& theObjectName )
196 Object2Visible anItem;
197 foreach( anItem, myObjects ) {
198 if ( anItem.first->GetName() == theObjectName ) {
203 myObjects.removeAll(anItem);
210 Check if the object is visible.
211 @param theIndex the object index
212 @return true if the object is visible, false - otherwise
214 bool HYDROGUI_ListModel::isObjectVisible( int theIndex ) const
216 bool isVisible = false;
218 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
219 isVisible = myObjects.at( theIndex ).second;
227 QVariant HYDROGUI_ListModel::headerData( int theSection,
228 Qt::Orientation theOrientation,
231 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
236 return tr( "VISIBLE" );
238 return tr( "OBJECT_NAME" );
246 Qt::ItemFlags HYDROGUI_ListModel::flags( const QModelIndex& theIndex ) const
248 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
249 if( theIndex.isValid() )
250 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
252 return Qt::ItemIsDropEnabled | aDefaultFlags;
257 QMimeData* HYDROGUI_ListModel::mimeData( const QModelIndexList& theIndexes ) const
259 QMimeData* aMimeData = new QMimeData();
260 QByteArray anEncodedData;
261 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
263 QList<int> anIdsList = getIds( theIndexes, true );
264 foreach( int anId, anIdsList )
267 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
273 QStringList HYDROGUI_ListModel::mimeTypes() const
276 aTypes << OBJ_LIST_MIME_TYPE;
282 bool HYDROGUI_ListModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
283 int theRow, int theColumn, const QModelIndex& theParent )
285 if( theAction == Qt::IgnoreAction)
288 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
294 // TODO: to disable drop between items use: int aDropItemId = theParent.row();
295 int aDropItemId = theParent.isValid() ? theParent.row() : theRow;
297 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
298 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
299 QList<int> anIdsList;
300 while( !aStream.atEnd() )
307 move( anIdsList, DragAndDrop, false, aDropItemId ); //TODO set visibility?
313 Qt::DropActions HYDROGUI_ListModel::supportedDropActions() const
315 return Qt::MoveAction | Qt::CopyAction;
319 Get list of ids by the list model indexes.
320 @param theIsToSort defines if the list of ids should be sorted in ascending order
321 @return the list of ids
323 QList<int> HYDROGUI_ListModel::getIds( const QModelIndexList& theIndexes,
324 bool theIsToSort ) const
327 foreach( const QModelIndex& anIndex, theIndexes ) {
328 anIds << anIndex.row();
340 @param theItem the item id to move
341 @param theType the move operation type
342 @param theIsVisibleOnly indicates if do move relating to the visible objects only
343 @param theDropItem the drop item id ( used for drag and drop obly )
344 @return true in case of success
346 bool HYDROGUI_ListModel::move( const int theItem, const OpType theType,
347 bool theIsVisibleOnly, const int theDropItem )
350 if ( theItem < 0 || theItem >= myObjects.count() ) {
354 int aDestinationIndex = -1;
355 bool isInsertBefore = false;
359 isInsertBefore = true;
361 aDestinationIndex = theItem - 1;
362 if ( theIsVisibleOnly ) {
363 while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
370 if ( theItem < myObjects.count() - 1 ) {
371 aDestinationIndex = theItem + 1;
372 if ( theIsVisibleOnly ) {
373 while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
380 isInsertBefore = true;
382 aDestinationIndex = 0;
386 if ( theItem < myObjects.count() - 1 ) {
387 aDestinationIndex = myObjects.count() - 1;
391 if ( theItem > theDropItem ) {
392 isInsertBefore = true;
393 aDestinationIndex = theDropItem;
395 aDestinationIndex = theDropItem - 1;
400 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
401 int aDestinationRow = isInsertBefore ? aDestinationIndex : aDestinationIndex + 1;
402 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
403 myPrevObjects = myObjects;
404 myObjects.move( theItem, aDestinationIndex );
415 @param theItems the list of item ids to move
416 @param theType the move operation type
417 @param theIsVisibleOnly indicates if do move relating to the visible objects only
418 @param theDropItem the drop item id ( used for drag and drop obly )
419 @return true in case of success
421 bool HYDROGUI_ListModel::move( const QList<int>& theItems, const OpType theType,
422 bool theIsVisibleOnly, const int theDropItem )
426 QListIterator<int> anIt( theItems );
434 while ( anIt.hasPrevious() ) {
435 int anId = anIt.previous();
436 if ( theType == Top ) {
440 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
449 while ( anIt.hasNext() ) {
450 int anId = anIt.next();
451 if ( theType == Bottom ) {
455 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
463 aRes = isDragAndDropAllowed( theItems, theDropItem );
466 int aDropItem = theDropItem;
467 while ( anIt.hasNext() ) {
468 int anId = anIt.next();
469 aDropItem = theDropItem + aDropShift;
470 if ( anId > aDropItem ) {
475 if ( ( aDropItem - anId ) != 1 ) {
479 move( anId, theType, theIsVisibleOnly, aDropItem );
491 Check if drag and drop operation allowed.
492 @param theItems the list of dragged item ids
493 @param theDropItem the drop item id
494 @return true if drag and drop allowed
496 bool HYDROGUI_ListModel::isDragAndDropAllowed( const QList<int>& theItems,
497 const int theDropItem ) const
499 bool isAllowed = false;
501 if ( theDropItem >= 0 &&
502 // TODO: to disable drop between items use: theDropItem < myObjects.count()
503 theDropItem <= myObjects.count() &&
504 !theItems.empty() && theItems.count() < myObjects.count() &&
505 !theItems.contains( theDropItem )) {
513 Enable/disable decoration (eye icon).
514 @param theIsToEnable if true - the decoration will be enabled
516 void HYDROGUI_ListModel::setDecorationEnabled( const bool theIsToEnable )
518 myIsDecorationEnabled = theIsToEnable;
521 void HYDROGUI_ListModel::undoLastMove()
523 myObjects = myPrevObjects;