Files
frelay/Gui/Control/cpTableWidget.cpp

81 lines
2.0 KiB
C++
Raw Normal View History

2025-06-18 10:42:53 +08:00
#include "cpTableWidget.h"
#include <QApplication>
#include <QDrag>
#include <QMimeData>
#include <QPainter>
2025-06-21 14:05:39 +08:00
#include <Util.h>
2025-06-18 10:42:53 +08:00
CpTableWidget::CpTableWidget(QWidget* parent) : QTableWidget(parent)
{
}
CpTableWidget::~CpTableWidget()
{
2025-06-20 09:45:39 +08:00
}
void CpTableWidget::dropEvent(QDropEvent* event)
{
2025-06-20 16:00:18 +08:00
if (!event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
event->ignore();
return;
}
QByteArray encoded = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&encoded, QIODevice::ReadOnly);
2025-06-21 14:05:39 +08:00
QPoint pos = event->pos();
int startRow = rowAt(pos.y());
int startCol = columnAt(pos.x());
if (startCol != 1 && startCol != 2) {
event->ignore();
return;
}
QStringList draggedData;
2025-06-20 16:00:18 +08:00
while (!stream.atEnd()) {
int row, col;
QMap<int, QVariant> roleData;
stream >> row >> col >> roleData;
if (col != 1) {
continue;
}
2025-06-21 14:05:39 +08:00
draggedData.append(roleData[Qt::DisplayRole].toString());
}
if (draggedData.isEmpty()) {
event->ignore();
return;
}
int currentRow = startRow;
if (currentRow == -1) {
currentRow = rowCount();
}
for (const QString& text : draggedData) {
if (currentRow >= rowCount()) {
insertRow(rowCount());
}
QString cur = startCol == 1 ? Util::Join(GlobalData::Ins()->GetLocalRoot(), text)
: Util::Join(GlobalData::Ins()->GetRemoteRoot(), text);
QTableWidgetItem* item = this->item(currentRow, startCol);
if (!item) {
item = new QTableWidgetItem(cur);
setItem(currentRow, startCol, item);
} else {
item->setText(cur);
}
currentRow++;
2025-06-20 16:00:18 +08:00
}
2025-06-21 14:05:39 +08:00
event->acceptProposedAction();
2025-06-18 10:42:53 +08:00
}
void CpTableWidget::dragEnterEvent(QDragEnterEvent* event)
{
2025-06-20 16:00:18 +08:00
if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
event->acceptProposedAction();
} else {
event->ignore();
}
2025-06-18 10:42:53 +08:00
}