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"
26 const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
28 HYDROGUI_ZLevelsModel::HYDROGUI_ZLevelsModel( QObject* theParent )
29 : QAbstractListModel( theParent )
31 myEmpty = QPixmap( 16, 16 );
32 myEmpty.fill( Qt::white );
33 myEye = QPixmap( "eye.png" );//TODO: loading from resources
34 setSupportedDragActions( Qt::MoveAction | Qt::CopyAction );
37 HYDROGUI_ZLevelsModel::~HYDROGUI_ZLevelsModel()
41 QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole ) const
45 int aRow = theIndex.row();
46 int aColumn = theIndex.column();
52 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
53 return myObjects.at( aRow ).first->GetName();
59 case Qt::DecorationRole:
61 if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
63 bool isVisible = isObjectVisible( aRow );
73 case HYDROGUI_VisibleRole:
75 bool isVisible = isObjectVisible( aRow );
76 return QVariant( isVisible ).toString();
84 int HYDROGUI_ZLevelsModel::rowCount( const QModelIndex &theParent ) const
86 return myObjects.count();
89 void HYDROGUI_ZLevelsModel::setObjects( const Object2VisibleList& theObjects )
91 myObjects = theObjects;
96 HYDROGUI_ZLevelsModel::ObjectList HYDROGUI_ZLevelsModel::getObjects() const
100 foreach( const Object2Visible& anItem, myObjects ) {
101 anObjects << anItem.first;
107 bool HYDROGUI_ZLevelsModel::isObjectVisible( int theIndex ) const
109 bool isVisible = false;
111 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
112 isVisible = myObjects.at( theIndex ).second;
118 QVariant HYDROGUI_ZLevelsModel::headerData( int theSection,
119 Qt::Orientation theOrientation,
122 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
127 return tr( "VISIBLE" );
129 return tr( "OBJECT_NAME" );
135 Qt::ItemFlags HYDROGUI_ZLevelsModel::flags( const QModelIndex& theIndex ) const
137 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
138 if( theIndex.isValid() )
139 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
141 return Qt::ItemIsDropEnabled | aDefaultFlags;
144 QMimeData* HYDROGUI_ZLevelsModel::mimeData( const QModelIndexList& theIndices ) const
146 QMimeData* aMimeData = new QMimeData();
147 QByteArray anEncodedData;
148 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
150 QList<int> anIdsList = getIds( theIndices );
151 foreach( int anId, anIdsList )
154 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
158 QStringList HYDROGUI_ZLevelsModel::mimeTypes() const
161 aTypes << OBJ_LIST_MIME_TYPE;
165 bool HYDROGUI_ZLevelsModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
166 int theRow, int theColumn, const QModelIndex& theParent )
168 if( theAction == Qt::IgnoreAction)
171 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
177 int aDropItemId = theParent.row();
179 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
180 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
181 QList<int> anIdsList;
182 while( !aStream.atEnd() )
188 move( anIdsList, DragAndDrop, aDropItemId );
192 Qt::DropActions HYDROGUI_ZLevelsModel::supportedDropActions() const
194 return Qt::MoveAction | Qt::CopyAction;
197 QList<int> HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const
200 foreach( const QModelIndex& anIndex, theIndexes ) {
201 anIds << anIndex.row();
211 bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
212 bool theIsVisibleOnly, const int theDropItem )
215 if ( theItem < 0 || theItem >= myObjects.count() ) {
219 int aDestinationIndex = -1;
224 aDestinationIndex = theItem - 1;
225 if ( theIsVisibleOnly ) {
226 while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
233 if ( theItem < myObjects.count() - 1 ) {
234 aDestinationIndex = theItem + 1;
235 if ( theIsVisibleOnly ) {
236 while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
244 aDestinationIndex = 0;
248 if ( theItem < myObjects.count() - 1 ) {
249 aDestinationIndex = myObjects.count() - 1;
254 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
255 int aDestinationRow = (theType == Up || theType == Top) ? aDestinationIndex : aDestinationIndex + 1;
256 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
257 myObjects.move( theItem, aDestinationIndex );
266 bool HYDROGUI_ZLevelsModel::move( const QList<int>& theItems, const OpType theType,
267 bool theIsVisibleOnly, const int theDropItem )
271 bool isReverse = theType == Top || theType == Down;
272 QListIterator<int> anIt( theItems );
276 while ( anIt.hasPrevious() ) {
277 int anId = anIt.previous();
278 if ( theType == Top ) {
282 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
288 while ( anIt.hasNext() ) {
289 int anId = anIt.next();
290 if ( theType == Bottom ) {
294 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
301 // reset(); //TODO dataChanged?