drag: cusTabwidget basic drag.
This commit is contained in:
@@ -33,6 +33,7 @@ void FileManager::SetModeStr(const QString& modeStr, int type, ClientCore* clien
|
|||||||
remotePtr->registerPathCall([this](const QString& path) { ShowPath(path); });
|
remotePtr->registerPathCall([this](const QString& path) { ShowPath(path); });
|
||||||
remotePtr->registerFileCall([this](const DirFileInfoVec& info) { ShowFile(info); });
|
remotePtr->registerFileCall([this](const DirFileInfoVec& info) { ShowFile(info); });
|
||||||
remotePtr->setClientCore(clientCore);
|
remotePtr->setClientCore(clientCore);
|
||||||
|
fileTrans_ = new FileTrans(clientCore);
|
||||||
fileHelper_ = remotePtr;
|
fileHelper_ = remotePtr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,7 +62,7 @@ void FileManager::InitControl()
|
|||||||
ui->tableWidget->viewport()->setAcceptDrops(true);
|
ui->tableWidget->viewport()->setAcceptDrops(true);
|
||||||
ui->tableWidget->setDropIndicatorShown(true);
|
ui->tableWidget->setDropIndicatorShown(true);
|
||||||
ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||||
ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);
|
ui->tableWidget->setDragDropMode(QAbstractItemView::NoDragDrop);
|
||||||
|
|
||||||
connect(ui->btnHome, &QPushButton::clicked, this, &FileManager::evtHome);
|
connect(ui->btnHome, &QPushButton::clicked, this, &FileManager::evtHome);
|
||||||
connect(ui->btnVisit, &QPushButton::clicked, this, &FileManager::evtFile);
|
connect(ui->btnVisit, &QPushButton::clicked, this, &FileManager::evtFile);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <Util.h>
|
#include <Util.h>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
|
#include <FileTrans.h>
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class FileManager;
|
class FileManager;
|
||||||
@@ -39,6 +40,7 @@ private:
|
|||||||
QString curRoot_;
|
QString curRoot_;
|
||||||
QMenu* localMenu_;
|
QMenu* localMenu_;
|
||||||
QMenu* remoteMenu_;
|
QMenu* remoteMenu_;
|
||||||
|
FileTrans* fileTrans_;
|
||||||
std::shared_ptr<DirFileHelper> fileHelper_;
|
std::shared_ptr<DirFileHelper> fileHelper_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#include "cusTableWidget.h"
|
#include "cusTableWidget.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
#include <QDrag>
|
#include <QDrag>
|
||||||
#include <QMimeData>
|
#include <QMimeData>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
CustomTableWidget::CustomTableWidget(QWidget* parent)
|
CustomTableWidget::CustomTableWidget(QWidget* parent) : QTableWidget(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,22 +19,10 @@ void CustomTableWidget::dropEvent(QDropEvent* event)
|
|||||||
event->ignore();
|
event->ignore();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
QTableWidget::dropEvent(event);
|
||||||
QModelIndex index = indexAt(event->pos());
|
|
||||||
if (!index.isValid()) {
|
|
||||||
event->ignore();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString data = QString::fromUtf8(event->mimeData()->data("application/x-custom-data"));
|
|
||||||
QStringList items = data.split(",");
|
|
||||||
|
|
||||||
//emit customDropRequested(items.first(), index.row(), index.column(), event->source());
|
|
||||||
|
|
||||||
event->acceptProposedAction();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CustomTableWidget::dragMoveEvent(QDragMoveEvent* event)
|
void CustomTableWidget::dragEnterEvent(QDragEnterEvent* event)
|
||||||
{
|
{
|
||||||
const QTableWidget* source = qobject_cast<const QTableWidget*>(event->source());
|
const QTableWidget* source = qobject_cast<const QTableWidget*>(event->source());
|
||||||
if (source == this) {
|
if (source == this) {
|
||||||
@@ -40,28 +30,44 @@ void CustomTableWidget::dragMoveEvent(QDragMoveEvent* event)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event->mimeData()->hasFormat("application/x-custom-data")) {
|
if (event->mimeData()->hasFormat("application/x-custom-data")) {
|
||||||
event->setDropAction(Qt::MoveAction);
|
|
||||||
event->acceptProposedAction();
|
event->acceptProposedAction();
|
||||||
} else {
|
} else {
|
||||||
event->ignore();
|
event->ignore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CustomTableWidget::startDrag(Qt::DropActions supportedActions)
|
void CustomTableWidget::mouseMoveEvent(QMouseEvent* event)
|
||||||
{
|
{
|
||||||
|
if (!(event->buttons() & Qt::LeftButton)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((event->pos() - startPos_).manhattanLength() < QApplication::startDragDistance()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QDrag* drag = new QDrag(this);
|
QDrag* drag = new QDrag(this);
|
||||||
QMimeData* mimeData = new QMimeData;
|
QMimeData* mimeData = new QMimeData;
|
||||||
|
|
||||||
QStringList selectedTexts;
|
QStringList selectedTexts;
|
||||||
foreach (QTableWidgetItem* item, selectedItems()) {
|
foreach (QTableWidgetItem* item, selectedItems()) {
|
||||||
|
if (item->column() == 1) {
|
||||||
selectedTexts << item->text();
|
selectedTexts << item->text();
|
||||||
}
|
}
|
||||||
mimeData->setData("application/x-custom-data", selectedTexts.join(",").toUtf8());
|
}
|
||||||
|
mimeData->setData("application/x-custom-data", selectedTexts.join("|").toUtf8());
|
||||||
drag->setMimeData(mimeData);
|
drag->setMimeData(mimeData);
|
||||||
QPixmap pixmap(100, 50);
|
QPixmap pixmap(100, 50);
|
||||||
pixmap.fill(Qt::lightGray);
|
pixmap.fill(Qt::lightGray);
|
||||||
QPainter painter(&pixmap);
|
QPainter painter(&pixmap);
|
||||||
painter.drawText(pixmap.rect(), Qt::AlignCenter, QString("%1 ITEM").arg(selectedTexts.count()));
|
painter.drawText(pixmap.rect(), Qt::AlignCenter, QString("%1 ITEM").arg(selectedTexts.count()));
|
||||||
drag->setPixmap(pixmap);
|
drag->setPixmap(pixmap);
|
||||||
drag->exec(supportedActions);
|
drag->exec(Qt::CopyAction | Qt::MoveAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CustomTableWidget::mousePressEvent(QMouseEvent* event)
|
||||||
|
{
|
||||||
|
QTableWidget::mousePressEvent(event);
|
||||||
|
if (event->button() == Qt::LeftButton) {
|
||||||
|
startPos_ = event->pos();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,12 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void dropEvent(QDropEvent* event) override;
|
void dropEvent(QDropEvent* event) override;
|
||||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
void dragEnterEvent(QDragEnterEvent* event);
|
||||||
void startDrag(Qt::DropActions supportedActions) override;
|
void mouseMoveEvent(QMouseEvent* event) override;
|
||||||
|
void mousePressEvent(QMouseEvent* event) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
QPoint startPos_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user