From ad92f89b49d4cea0ba952b9606b8018fdd059f59 Mon Sep 17 00:00:00 2001 From: asl Date: Wed, 19 Mar 2014 13:21:37 +0000 Subject: [PATCH] draft implementation of the drag-n-drop --- src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx | 9 +++- src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx | 80 ++++++++++++++++++++++++++-- src/ZLEVEL/HYDROGUI_ZLevelsModel.h | 10 +++- src/ZLEVEL/main.cpp | 2 +- 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx b/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx index 7448ad36..0808a2a5 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx +++ b/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx @@ -41,7 +41,12 @@ HYDROGUI_ZLevelsDlg::HYDROGUI_ZLevelsDlg( QWidget* theParent ) 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 ); @@ -120,7 +125,7 @@ void HYDROGUI_ZLevelsDlg::onMove( int theType ) aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex ); } QList aSelectedIds = aModel->getIds( aSelectedSourceIndexes ); - aModel->move( aSelectedIds, theType ); + aModel->move( aSelectedIds, ( HYDROGUI_ZLevelsModel::OpType )theType ); } } } diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx b/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx index fe49aac2..70d575b3 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx +++ b/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx @@ -21,13 +21,17 @@ // #include "HYDROGUI_ZLevelsModel.h" +#include + +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() @@ -51,6 +55,7 @@ QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole ) return QVariant(); } break; + case Qt::DecorationRole: { if( aColumn==0 && aRow >=0 && aRow < myObjects.count() ) @@ -64,11 +69,13 @@ QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole ) return QVariant(); } break; + case HYDROGUI_VisibleRole: { bool isVisible = IsObjectVisible( aRow ); return QVariant( isVisible ).toString(); } + break; } return aVariant; @@ -112,6 +119,68 @@ QVariant HYDROGUI_ZLevelsModel::headerData( int theSection, 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 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 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 HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const { QList anIds; @@ -126,7 +195,7 @@ QList HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, boo 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; @@ -149,8 +218,8 @@ bool HYDROGUI_ZLevelsModel::move( const int theItem, const int theType, return aRes; } -bool HYDROGUI_ZLevelsModel::move( const QList& theItems, const int theType, - const int theDropItem ) +bool HYDROGUI_ZLevelsModel::move( const QList& theItems, const OpType theType, + const int theDropItem ) { bool aRes = true; @@ -176,4 +245,5 @@ bool HYDROGUI_ZLevelsModel::move( const QList& theItems, const int theType, reset(); //TODO dataChanged? return aRes; -} \ No newline at end of file +} + diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsModel.h b/src/ZLEVEL/HYDROGUI_ZLevelsModel.h index 5dbd2a9f..028b6584 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsModel.h +++ b/src/ZLEVEL/HYDROGUI_ZLevelsModel.h @@ -50,14 +50,20 @@ public: 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 getIds( const QModelIndexList& theIndexes, bool theIsToSort = true ) const; void setObjects( const QList& 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& theItems, const int theType, + bool move( const QList& theItems, const OpType theType, const int theDropItem = -1 ); protected: diff --git a/src/ZLEVEL/main.cpp b/src/ZLEVEL/main.cpp index 8c51cb3d..48b9828d 100644 --- a/src/ZLEVEL/main.cpp +++ b/src/ZLEVEL/main.cpp @@ -9,7 +9,7 @@ int main( int argc, char** argv ){ QList< QString > anObjects; anObjects << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H"; aDlg->setObjects( anObjects ); - aDlg->exec(); + aDlg->show(); return app.exec(); } \ No newline at end of file -- 2.39.2