77 lines
1.6 KiB
C++
77 lines
1.6 KiB
C++
// draggabletoolbutton.cpp
|
|
#include "draggabletoolbutton.h"
|
|
#include <QMouseEvent>
|
|
#include <QApplication>
|
|
#include <QDebug>
|
|
|
|
DraggableToolButton::DraggableToolButton(QWidget *parent)
|
|
: QToolButton(parent)
|
|
, m_dragEnabled(true)
|
|
, m_isDragging(false)
|
|
{
|
|
setMouseTracking(true);
|
|
}
|
|
|
|
void DraggableToolButton::setDragEnabled(bool enabled)
|
|
{
|
|
m_dragEnabled = enabled;
|
|
}
|
|
|
|
bool DraggableToolButton::dragEnabled() const
|
|
{
|
|
return m_dragEnabled;
|
|
}
|
|
|
|
void DraggableToolButton::setToolData(const QString &data)
|
|
{
|
|
m_toolData = data;
|
|
}
|
|
|
|
QString DraggableToolButton::toolData() const
|
|
{
|
|
return m_toolData;
|
|
}
|
|
|
|
void DraggableToolButton::mousePressEvent(QMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton && m_dragEnabled) {
|
|
m_pressPos = event->pos();
|
|
m_isDragging = false;
|
|
}
|
|
|
|
// 不调用基类,完全自己处理
|
|
event->accept();
|
|
}
|
|
|
|
void DraggableToolButton::mouseMoveEvent(QMouseEvent *event)
|
|
{
|
|
if (!m_dragEnabled || !(event->buttons() & Qt::LeftButton)) {
|
|
event->ignore();
|
|
return;
|
|
}
|
|
|
|
// 检查是否达到拖拽阈值
|
|
int moveDist = (event->pos() - m_pressPos).manhattanLength();
|
|
if (moveDist >= QApplication::startDragDistance()) {
|
|
if (!m_isDragging) {
|
|
m_isDragging = true;
|
|
emit dragStarted(m_toolData, event->globalPosition().toPoint());
|
|
}
|
|
}
|
|
|
|
event->accept();
|
|
}
|
|
|
|
void DraggableToolButton::mouseReleaseEvent(QMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton) {
|
|
if (!m_isDragging) {
|
|
// 没有拖拽,是点击
|
|
emit clicked();
|
|
}
|
|
m_isDragging = false;
|
|
}
|
|
|
|
event->accept();
|
|
}
|