myList = new QListView( this );
myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
-
+ myList->setDragEnabled( true );
+ myList->setAcceptDrops( true );
+ myList->viewport()->setAcceptDrops( true );
+ myList->setDropIndicatorShown( true );
+ myList->setDragDropMode( QAbstractItemView::InternalMove );
+
HYDROGUI_ZLevelsModel* aModel = new HYDROGUI_ZLevelsModel();
QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
aFilteredModel->setSourceModel( aModel );
aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
}
QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
- aModel->move( aSelectedIds, theType );
+ aModel->move( aSelectedIds, ( HYDROGUI_ZLevelsModel::OpType )theType );
}
}
}
//
#include "HYDROGUI_ZLevelsModel.h"
+#include <QMimeData>
+
+const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
HYDROGUI_ZLevelsModel::HYDROGUI_ZLevelsModel( QObject* theParent )
: QAbstractListModel( theParent )
{
myEmpty = QPixmap( 16, 16 );
myEmpty.fill( Qt::white );
- myEye = QPixmap( "eye.png" );
+ myEye = QPixmap( "eye.png" );//TODO: loading from resources
+ setSupportedDragActions( Qt::MoveAction | Qt::CopyAction );
}
HYDROGUI_ZLevelsModel::~HYDROGUI_ZLevelsModel()
return QVariant();
}
break;
+
case Qt::DecorationRole:
{
if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
return QVariant();
}
break;
+
case HYDROGUI_VisibleRole:
{
bool isVisible = IsObjectVisible( aRow );
return QVariant( isVisible ).toString();
}
+ break;
}
return aVariant;
return QVariant();
}
+Qt::ItemFlags HYDROGUI_ZLevelsModel::flags( const QModelIndex& theIndex ) const
+{
+ Qt::ItemFlags aDefaultFlags = QAbstractListModel::flags( theIndex );
+ if( theIndex.isValid() )
+ return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | aDefaultFlags;
+ else
+ return Qt::ItemIsDropEnabled | aDefaultFlags;
+}
+
+QMimeData* HYDROGUI_ZLevelsModel::mimeData( const QModelIndexList& theIndices ) const
+{
+ QMimeData* aMimeData = new QMimeData();
+ QByteArray anEncodedData;
+ QDataStream aStream( &anEncodedData, QIODevice::WriteOnly );
+
+ QList<int> anIdsList = getIds( theIndices );
+ foreach( int anId, anIdsList )
+ aStream << anId;
+
+ aMimeData->setData( OBJ_LIST_MIME_TYPE, anEncodedData );
+ return aMimeData;
+}
+
+QStringList HYDROGUI_ZLevelsModel::mimeTypes() const
+{
+ QStringList aTypes;
+ aTypes << OBJ_LIST_MIME_TYPE;
+ return aTypes;
+}
+
+bool HYDROGUI_ZLevelsModel::dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
+ int theRow, int theColumn, const QModelIndex& theParent )
+{
+ if( theAction == Qt::IgnoreAction)
+ return true;
+
+ if( !theData->hasFormat( OBJ_LIST_MIME_TYPE ))
+ return false;
+
+ if( theColumn > 0 )
+ return false;
+
+ int aDropItemId = theParent.row();
+
+ QByteArray anEncodedData = theData->data( OBJ_LIST_MIME_TYPE );
+ QDataStream aStream( &anEncodedData, QIODevice::ReadOnly );
+ QList<int> anIdsList;
+ while( !aStream.atEnd() )
+ {
+ int anId;
+ aStream >> anId;
+ anIdsList << anId;
+ }
+ move( anIdsList, DragAndDrop, aDropItemId );
+ return true;
+}
+
+Qt::DropActions HYDROGUI_ZLevelsModel::supportedDropActions() const
+{
+ return Qt::MoveAction | Qt::CopyAction;
+}
+
QList<int> HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const
{
QList<int> anIds;
return anIds;
}
-bool HYDROGUI_ZLevelsModel::move( const int theItem, const int theType,
+bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
const int theDropItem )
{
bool aRes = false;
return aRes;
}
-bool HYDROGUI_ZLevelsModel::move( const QList<int>& theItems, const int theType,
- const int theDropItem )
+bool HYDROGUI_ZLevelsModel::move( const QList<int>& theItems, const OpType theType,
+ const int theDropItem )
{
bool aRes = true;
reset(); //TODO dataChanged?
return aRes;
-}
\ No newline at end of file
+}
+
virtual QVariant headerData( int theSection,
Qt::Orientation theOrientation,
int theRole = Qt::DisplayRole ) const;
+ virtual Qt::ItemFlags flags( const QModelIndex& theIndex ) const;
+ virtual QMimeData* mimeData( const QModelIndexList& theIndices ) const;
+ virtual QStringList mimeTypes() const;
+ virtual bool dropMimeData( const QMimeData* theData, Qt::DropAction theAction,
+ int theRow, int theColumn, const QModelIndex& theParent );
+ virtual Qt::DropActions supportedDropActions() const;
QList<int> getIds( const QModelIndexList& theIndexes, bool theIsToSort = true ) const;
void setObjects( const QList<QString>& theObjects );
- bool move( const int theItem, const int theType,
+ bool move( const int theItem, const OpType theType,
const int theDropItem = -1 );
- bool move( const QList<int>& theItems, const int theType,
+ bool move( const QList<int>& theItems, const OpType theType,
const int theDropItem = -1 );
protected: