From 9325eb5a7b2fad95968776a4868e4aaf65768e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Sun, 2 Nov 2014 23:00:11 +0100 Subject: [PATCH] added drag support to classic and grid view. by now grid doesn't support multiple drags and pressents some secondary effects (cells are moved inside the grid, and target is not working properly) --- YACReaderLibrary/db/comic_model.cpp | 2 +- YACReaderLibrary/qml/GridComicsView.qml | 11 ++++- custom_widgets/yacreader_table_view.cpp | 59 ++++++++++++++++--------- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/YACReaderLibrary/db/comic_model.cpp b/YACReaderLibrary/db/comic_model.cpp index 2e47a166..4137970f 100644 --- a/YACReaderLibrary/db/comic_model.cpp +++ b/YACReaderLibrary/db/comic_model.cpp @@ -148,7 +148,7 @@ Qt::ItemFlags ComicModel::flags(const QModelIndex &index) const return 0; if(index.column() == ComicModel::Rating) return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; - return Qt::ItemIsEnabled | Qt::ItemIsSelectable; + return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled; } //! [4] diff --git a/YACReaderLibrary/qml/GridComicsView.qml b/YACReaderLibrary/qml/GridComicsView.qml index 6acabb26..262cfcbc 100644 --- a/YACReaderLibrary/qml/GridComicsView.qml +++ b/YACReaderLibrary/qml/GridComicsView.qml @@ -28,6 +28,8 @@ Rectangle { height: grid.cellHeight color: backgroundColor MouseArea { + id: mouseArea + drag.target: realCell anchors.fill: parent onClicked: { comicsSelectionHelper.clear(); @@ -38,7 +40,14 @@ Rectangle { Rectangle { id: realCell - width: 156; height: 287 + Drag.active: mouseArea.drag.active + Drag.hotSpot.x: 32 + Drag.hotSpot.y: 32 + Drag.dragType: Drag.Automatic + Drag.mimeData: { "text/plain": "comic" } + Drag.proposedAction: Qt.CopyAction + + width: 156; height: 287 color: ((dummyValue || !dummyValue) && comicsSelectionHelper.isSelectedIndex(index)) || grid.currentIndex === index?selectedColor:cellColor; anchors.horizontalCenter: parent.horizontalCenter diff --git a/custom_widgets/yacreader_table_view.cpp b/custom_widgets/yacreader_table_view.cpp index 3b57356a..6ee76259 100644 --- a/custom_widgets/yacreader_table_view.cpp +++ b/custom_widgets/yacreader_table_view.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "comic_item.h" @@ -47,9 +49,9 @@ YACReaderTableView::YACReaderTableView(QWidget *parent) : //comicView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents); verticalHeader()->setDefaultSectionSize(24); #if QT_VERSION >= 0x050000 - verticalHeader()->setSectionsClickable(false); //TODO comportamiento anómalo + verticalHeader()->setSectionsClickable(false); //TODO comportamiento anómalo #else - verticalHeader()->setClickable(false); //TODO comportamiento anómalo + verticalHeader()->setClickable(false); //TODO comportamiento anómalo #endif setCornerButtonEnabled(false); @@ -65,6 +67,9 @@ YACReaderTableView::YACReaderTableView(QWidget *parent) : showDeletingProgressAnimation = new QPropertyAnimation(deletingProgress,"pos"); showDeletingProgressAnimation->setDuration(150);*/ + + //drag + setDragEnabled(true); } void YACReaderTableView::mouseMoveEvent(QMouseEvent *event) @@ -100,25 +105,37 @@ void YACReaderTableView::mouseMoveEvent(QMouseEvent *event) } void YACReaderTableView::mousePressEvent(QMouseEvent * event) { - QTableView::mousePressEvent(event); - QModelIndex mi = indexAt(event->pos()); - if(mi.isValid()) - { - QList selectedIndexes = this->selectedIndexes(); - if(selectedIndexes.contains(mi)) - { - if(mi.column() == 11) - { - if(!editing) - { - editing = true; - currentIndexEditing = mi; - edit(mi); - myeditor = indexWidget(mi); - } - } - } - } + QTableView::mousePressEvent(event); + QModelIndex mi = indexAt(event->pos()); + if(mi.isValid()) + { + QList selectedIndexes = this->selectedIndexes(); + if(selectedIndexes.contains(mi)) + { + if(mi.column() == 11) + { + if(!editing) + { + editing = true; + currentIndexEditing = mi; + edit(mi); + myeditor = indexWidget(mi); + } + return; + } + } + } + + QMimeData *mimeData = new QMimeData; + + mimeData->setText("comic"); //TODO set the right mime data + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setPixmap(QPixmap(":/images/openInYACReader.png")); //TODO add better image + + Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); + } void YACReaderTableView::leaveEvent(QEvent * event) {