Remove code duplication with startDrag()
This commit is contained in:
committed by
craig.p.drummond
parent
1f5e9498ce
commit
931ca0d33a
@@ -35,8 +35,8 @@
|
||||
#include <QPaintEvent>
|
||||
|
||||
ListView::ListView(QWidget *parent)
|
||||
: QListView(parent)
|
||||
, menu(0)
|
||||
: QListView(parent)
|
||||
, menu(0)
|
||||
{
|
||||
setDragEnabled(true);
|
||||
setContextMenuPolicy(Qt::NoContextMenu);
|
||||
@@ -73,37 +73,6 @@ bool ListView::haveUnSelectedItems() const
|
||||
return selectionModel() && selectionModel()->selectedIndexes().count()!=model()->rowCount();
|
||||
}
|
||||
|
||||
void ListView::startDrag(Qt::DropActions supportedActions)
|
||||
{
|
||||
QModelIndexList indexes = selectedIndexes();
|
||||
if (indexes.count() > 0) {
|
||||
QMimeData *data = model()->mimeData(indexes);
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(data);
|
||||
QPixmap pix;
|
||||
|
||||
if (1==indexes.count()) {
|
||||
QVariant var=model()->data(indexes.first(), ItemView::Role_Image);
|
||||
QImage img=var.value<QImage>();
|
||||
if (img.isNull()) {
|
||||
pix=var.value<QPixmap>();
|
||||
} else {
|
||||
pix=QPixmap::fromImage(img);
|
||||
}
|
||||
}
|
||||
int pixSize=Utils::isHighDpi() ? 64 : 32;
|
||||
if (pix.isNull()) {
|
||||
drag->setPixmap(Icons::self()->audioFileIcon.pixmap(pixSize, pixSize));
|
||||
} else {
|
||||
drag->setPixmap(pix.width()<pixSize ? pix : pix.scaled(QSize(pixSize, pixSize), Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||||
}
|
||||
drag->start(supportedActions);
|
||||
}
|
||||
}
|
||||
|
||||
void ListView::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (Qt::NoModifier==event->modifiers() && Qt::LeftButton==event->button()) {
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <QListView>
|
||||
#include <QPixmap>
|
||||
#include "treeview.h"
|
||||
|
||||
class QIcon;
|
||||
|
||||
@@ -41,7 +42,7 @@ public:
|
||||
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
|
||||
bool haveSelectedItems() const;
|
||||
bool haveUnSelectedItems() const;
|
||||
void startDrag(Qt::DropActions supportedActions);
|
||||
void startDrag(Qt::DropActions supportedActions) { TreeView::drag(supportedActions, this, selectedIndexes()); }
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
QModelIndexList selectedIndexes() const { return selectedIndexes(false); }
|
||||
QModelIndexList selectedIndexes(bool sorted) const;
|
||||
|
||||
@@ -89,9 +89,9 @@ bool TreeView::getForceSingleClick()
|
||||
}
|
||||
|
||||
TreeView::TreeView(QWidget *parent, bool menuAlwaysAllowed)
|
||||
: QTreeView(parent)
|
||||
, forceSingleColumn(false)
|
||||
, alwaysAllowMenu(menuAlwaysAllowed)
|
||||
: QTreeView(parent)
|
||||
, forceSingleColumn(false)
|
||||
, alwaysAllowMenu(menuAlwaysAllowed)
|
||||
{
|
||||
setDragEnabled(true);
|
||||
setContextMenuPolicy(Qt::NoContextMenu);
|
||||
@@ -153,20 +153,19 @@ bool TreeView::haveUnSelectedItems() const
|
||||
return selectionModel() && selectionModel()->selectedIndexes().count()!=model()->rowCount();
|
||||
}
|
||||
|
||||
void TreeView::startDrag(Qt::DropActions supportedActions)
|
||||
void TreeView::drag(Qt::DropActions supportedActions, QAbstractItemView *view, const QModelIndexList &items)
|
||||
{
|
||||
QModelIndexList indexes = selectedIndexes();
|
||||
if (indexes.count() > 0) {
|
||||
QMimeData *data = model()->mimeData(indexes);
|
||||
if (items.count() > 0) {
|
||||
QMimeData *data = view->model()->mimeData(items);
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
QDrag *drag = new QDrag(this);
|
||||
QDrag *drag = new QDrag(view);
|
||||
drag->setMimeData(data);
|
||||
QPixmap pix;
|
||||
|
||||
if (1==indexes.count()) {
|
||||
QVariant var=model()->data(indexes.first(), ItemView::Role_Image);
|
||||
if (1==items.count()) {
|
||||
QVariant var=view->model()->data(items.first(), ItemView::Role_Image);
|
||||
QImage img=var.value<QImage>();
|
||||
if (img.isNull()) {
|
||||
pix=var.value<QPixmap>();
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
static void setForceSingleClick(bool v);
|
||||
static bool getForceSingleClick();
|
||||
static QModelIndexList sortIndexes(const QModelIndexList &list);
|
||||
static void drag(Qt::DropActions supportedActions, QAbstractItemView *view, const QModelIndexList &items);
|
||||
|
||||
TreeView(QWidget *parent=0, bool menuAlwaysAllowed=false);
|
||||
virtual ~TreeView();
|
||||
@@ -49,7 +50,7 @@ public:
|
||||
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
|
||||
bool haveSelectedItems() const;
|
||||
bool haveUnSelectedItems() const;
|
||||
void startDrag(Qt::DropActions supportedActions);
|
||||
void startDrag(Qt::DropActions supportedActions) { drag(supportedActions, this, selectedIndexes()); }
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
QModelIndexList selectedIndexes() const { return selectedIndexes(true); }
|
||||
QModelIndexList selectedIndexes(bool sorted) const;
|
||||
|
||||
Reference in New Issue
Block a user