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;
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 QList<QPair<QString, bool>>& theObjects )
91 myObjects = theObjects;
96 bool HYDROGUI_ZLevelsModel::IsObjectVisible( int theIndex ) const
98 bool isVisible = false;
100 if ( theIndex >= 0 && theIndex < myObjects.count() ) {
101 isVisible = myObjects.at( theIndex ).second;
107 QVariant HYDROGUI_ZLevelsModel::headerData( int theSection,
108 Qt::Orientation theOrientation,
111 if( theOrientation==Qt::Horizontal && theRole==Qt::DisplayRole )
116 return tr( "VISIBLE" );
118 return tr( "OBJECT_NAME" );
124 Qt::ItemFlags HYDROGUI_ZLevelsModel::flags( const QModelIndex& theIndex ) const
126 Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
127 if( theIndex.isValid() )
128 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
130 return Qt::ItemIsDropEnabled | aDefaultFlags;
133 QMimeData* HYDROGUI_ZLevelsModel::mimeData( const QModelIndexList& theIndices ) const
135 QMimeData* aMimeData = new QMimeData();
136 QByteArray anEncodedData;
137 QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
139 QList<int> anIdsList = getIds( theIndices );
140 foreach( int anId, anIdsList )
143 aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
147 QStringList HYDROGUI_ZLevelsModel::mimeTypes() const
150 aTypes << OBJ_LIST_MIME_TYPE;
154 bool HYDROGUI_ZLevelsModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
155 int theRow, int theColumn, const QModelIndex& theParent )
157 if( theAction == Qt::IgnoreAction)
160 if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
166 int aDropItemId = theParent.row();
168 QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
169 QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
170 QList<int> anIdsList;
171 while( !aStream.atEnd() )
177 move( anIdsList, DragAndDrop, aDropItemId );
181 Qt::DropActions HYDROGUI_ZLevelsModel::supportedDropActions() const
183 return Qt::MoveAction | Qt::CopyAction;
186 QList<int> HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const
189 foreach( const QModelIndex& anIndex, theIndexes ) {
190 anIds << anIndex.row();
200 bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
201 bool theIsVisibleOnly, const int theDropItem )
204 if ( theItem < 0 || theItem >= myObjects.count() ) {
208 int aDestinationIndex = -1;
213 aDestinationIndex = theItem - 1;
214 if ( theIsVisibleOnly ) {
215 while ( aDestinationIndex >= 0 && !IsObjectVisible( aDestinationIndex ) ) {
222 if ( theItem < myObjects.count() - 1 ) {
223 aDestinationIndex = theItem + 1;
224 if ( theIsVisibleOnly ) {
225 while ( aDestinationIndex < myObjects.count() && !IsObjectVisible( aDestinationIndex ) ) {
233 aDestinationIndex = 0;
237 if ( theItem < myObjects.count() - 1 ) {
238 aDestinationIndex = myObjects.count() - 1;
243 if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
244 int aDestinationRow = (theType == Up || theType == Top) ? aDestinationIndex : aDestinationIndex + 1;
245 if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
246 myObjects.move( theItem, aDestinationIndex );
255 bool HYDROGUI_ZLevelsModel::move( const QList<int>& theItems, const OpType theType,
256 bool theIsVisibleOnly, const int theDropItem )
260 bool isReverse = theType == Top || theType == Down;
261 QListIterator<int> anIt( theItems );
265 while ( anIt.hasPrevious() ) {
266 int anId = anIt.previous();
267 if ( theType == Top ) {
271 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
277 while ( anIt.hasNext() ) {
278 int anId = anIt.next();
279 if ( theType == Bottom ) {
283 if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
290 // reset(); //TODO dataChanged?