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_ZLevelsModel.h"
25 #include <SUIT_Session.h>
26 #include <SUIT_ResourceMgr.h>
30 const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
32 HYDROGUI_ZLevelsModel::HYDROGUI_ZLevelsModel( QObject* theParent )
33 : QAbstractListModel( theParent )
35 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
37 myEmpty = QPixmap( 16, 16 );
38 myEmpty.fill( Qt::white );
39 myEye = aResMgr->loadPixmap( "HYDRO", tr( "EYE_ICO" ) );
40 setSupportedDragActions( Qt::MoveAction | Qt::CopyAction );
43 HYDROGUI_ZLevelsModel::~HYDROGUI_ZLevelsModel()
47 QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole ) const
51 int aRow = theIndex.row();
52 int aColumn = theIndex.column();
58 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
59 return myObjects.at( aRow ).first->GetName();
65 case Qt::DecorationRole:
67 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
69 bool isVisible = isObjectVisible( aRow );
79 case HYDROGUI_VisibleRole:
81 bool isVisible = isObjectVisible( aRow );
82 return QVariant( isVisible ).toString();
90 int HYDROGUI_ZLevelsModel::rowCount( const QModelIndex &theParent ) const
92 return myObjects.count();
95 void HYDROGUI_ZLevelsModel::setObjects( const Object2VisibleList& theObjects )
97 myObjects = theObjects;
102 HYDROGUI_ZLevelsModel::ObjectList HYDROGUI_ZLevelsModel::getObjects() const
104 ObjectList anObjects;
106 foreach( const Object2Visible& anItem, myObjects ) {
107 anObjects << anItem.first;
113 bool HYDROGUI_ZLevelsModel::isObjectVisible( int theIndex ) const
115 bool isVisible = false;
117 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
118 isVisible = myObjects.at( theIndex ).second;
124 QVariant HYDROGUI_ZLevelsModel::headerData( int theSection,
125 Qt::Orientation theOrientation,
128 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
133 return tr( "VISIBLE" );
135 return tr( "OBJECT_NAME" );
141 Qt::ItemFlags HYDROGUI_ZLevelsModel::flags( const QModelIndex& theIndex ) const
143 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
144 if( theIndex.isValid() )
145 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
147 return Qt::ItemIsDropEnabled | aDefaultFlags;
150 QMimeData* HYDROGUI_ZLevelsModel::mimeData( const QModelIndexList& theIndices ) const
152 QMimeData* aMimeData = new QMimeData();
153 QByteArray anEncodedData;
154 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
156 QList<int> anIdsList = getIds( theIndices );
157 foreach( int anId, anIdsList )
160 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
164 QStringList HYDROGUI_ZLevelsModel::mimeTypes() const
167 aTypes << OBJ_LIST_MIME_TYPE;
171 bool HYDROGUI_ZLevelsModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
172 int theRow, int theColumn, const QModelIndex& theParent )
174 if( theAction == Qt::IgnoreAction)
177 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
183 int aDropItemId = theParent.row();
185 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
186 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
187 QList<int> anIdsList;
188 while( !aStream.atEnd() )
194 qSort( anIdsList ); // TODO should be moved?
195 move( anIdsList, DragAndDrop, false, aDropItemId ); //TODO set visibility?
199 Qt::DropActions HYDROGUI_ZLevelsModel::supportedDropActions() const
201 return Qt::MoveAction | Qt::CopyAction;
204 QList<int> HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const
207 foreach( const QModelIndex& anIndex, theIndexes ) {
208 anIds << anIndex.row();
218 bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
219 bool theIsVisibleOnly, const int theDropItem )
222 if ( theItem < 0 || theItem >= myObjects.count() ) {
226 int aDestinationIndex = -1;
227 bool isInsertBefore = false;
231 isInsertBefore = true;
233 aDestinationIndex = theItem - 1;
234 if ( theIsVisibleOnly ) {
235 while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
242 if ( theItem < myObjects.count() - 1 ) {
243 aDestinationIndex = theItem + 1;
244 if ( theIsVisibleOnly ) {
245 while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
252 isInsertBefore = true;
254 aDestinationIndex = 0;
258 if ( theItem < myObjects.count() - 1 ) {
259 aDestinationIndex = myObjects.count() - 1;
263 if ( theItem > theDropItem ) {
264 isInsertBefore = true;
265 aDestinationIndex = theDropItem;
267 aDestinationIndex = theDropItem - 1;
272 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
273 int aDestinationRow = isInsertBefore ? aDestinationIndex : aDestinationIndex + 1;
274 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
275 myObjects.move( theItem, aDestinationIndex );
284 bool HYDROGUI_ZLevelsModel::move( const QList<int>& theItems, const OpType theType,
285 bool theIsVisibleOnly, const int theDropItem )
289 QListIterator<int> anIt( theItems );
297 while ( anIt.hasPrevious() ) {
298 int anId = anIt.previous();
299 if ( theType == Top ) {
303 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
312 while ( anIt.hasNext() ) {
313 int anId = anIt.next();
314 if ( theType == Bottom ) {
318 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
326 aRes = isDragAndDropAllowed( theItems, theDropItem );
329 int aDropItem = theDropItem;
330 while ( anIt.hasNext() ) {
331 int anId = anIt.next();
332 aDropItem = theDropItem + aDropShift;
333 if ( anId > aDropItem ) {
338 if ( ( aDropItem - anId ) != 1 ) {
342 move( anId, theType, theIsVisibleOnly, aDropItem );
353 bool HYDROGUI_ZLevelsModel::isDragAndDropAllowed( const QList<int>& theItems,
354 const int theDropItem ) const
356 bool isAllowed = false;
358 if ( theDropItem >= 0 && theDropItem < myObjects.count() &&
359 !theItems.empty() && theItems.count() < myObjects.count() &&
360 !theItems.contains( theDropItem )) {