146 lines
3.6 KiB
C++
146 lines
3.6 KiB
C++
#include "enhancedtoolbar.h"
|
|
#include "draggabletoolbutton.h"
|
|
#include <QHBoxLayout>
|
|
#include <QMimeData>
|
|
#include <QDrag>
|
|
#include <QPainter>
|
|
#include <QApplication>
|
|
#include <QDebug>
|
|
|
|
EnhancedToolBar::EnhancedToolBar(QWidget *parent)
|
|
: QToolBar(parent)
|
|
, m_dragEnabled(true)
|
|
{
|
|
setIconSize(QSize(32, 32));
|
|
setToolButtonStyle(Qt::ToolButtonIconOnly);
|
|
setMovable(true);
|
|
setFloatable(true);
|
|
}
|
|
|
|
void EnhancedToolBar::addTool(const QString &toolType, const QString &toolName,
|
|
const QIcon &icon)
|
|
{
|
|
if (m_buttons.contains(toolType)) {
|
|
return;
|
|
}
|
|
|
|
// 创建自定义按钮
|
|
DraggableToolButton *button = new DraggableToolButton(this);
|
|
button->setIcon(icon);
|
|
button->setIconSize(iconSize());
|
|
button->setText(toolName);
|
|
button->setToolTip(toolName);
|
|
button->setCheckable(true);
|
|
button->setToolData(toolType);
|
|
button->setDragEnabled(m_dragEnabled);
|
|
|
|
// 连接信号
|
|
connect(button, &DraggableToolButton::clicked,
|
|
this, &EnhancedToolBar::onToolButtonClicked);
|
|
connect(button, &DraggableToolButton::dragStarted,
|
|
this, &EnhancedToolBar::onToolButtonDragStarted);
|
|
|
|
// 添加到工具栏
|
|
addWidget(button);
|
|
m_buttons[toolType] = button;
|
|
|
|
// 如果没有当前工具,设置第一个为当前
|
|
if (m_currentTool.isEmpty()) {
|
|
setCurrentTool(toolType);
|
|
}
|
|
}
|
|
|
|
void EnhancedToolBar::onToolButtonClicked()
|
|
{
|
|
DraggableToolButton *button = qobject_cast<DraggableToolButton*>(sender());
|
|
if (button) {
|
|
QString toolType = button->toolData();
|
|
setCurrentTool(toolType);
|
|
}
|
|
}
|
|
|
|
void EnhancedToolBar::onToolButtonDragStarted(const QString &toolData, const QPoint &globalPos)
|
|
{
|
|
if (m_dragEnabled) {
|
|
startDrag(toolData, globalPos);
|
|
}
|
|
}
|
|
|
|
void EnhancedToolBar::startDrag(const QString &toolType, const QPoint &globalPos)
|
|
{
|
|
DraggableToolButton *button = m_buttons.value(toolType);
|
|
if (!button) {
|
|
return;
|
|
}
|
|
|
|
QString toolName = button->text();
|
|
QIcon icon = button->icon();
|
|
|
|
qDebug() << "开始拖拽:" << toolType << toolName;
|
|
|
|
// 创建MIME数据
|
|
QMimeData *mimeData = new QMimeData();
|
|
mimeData->setText(toolName);
|
|
mimeData->setData("application/x-tooltype", toolType.toUtf8());
|
|
|
|
// 创建拖拽对象
|
|
QDrag *drag = new QDrag(this);
|
|
drag->setMimeData(mimeData);
|
|
|
|
// 创建预览图像
|
|
if (!icon.isNull()) {
|
|
QPixmap pixmap = icon.pixmap(32, 32);
|
|
drag->setPixmap(pixmap);
|
|
drag->setHotSpot(QPoint(16, 16));
|
|
}
|
|
|
|
// 发出信号
|
|
emit toolDragged(toolType, globalPos);
|
|
|
|
// 执行拖拽
|
|
Qt::DropAction result = drag->exec(Qt::CopyAction);
|
|
|
|
qDebug() << "拖拽完成,结果:" << result;
|
|
}
|
|
|
|
QString EnhancedToolBar::currentTool() const
|
|
{
|
|
return m_currentTool;
|
|
}
|
|
|
|
void EnhancedToolBar::setCurrentTool(const QString &toolType)
|
|
{
|
|
if (m_currentTool == toolType) {
|
|
return;
|
|
}
|
|
|
|
// 取消之前的选择
|
|
if (!m_currentTool.isEmpty() && m_buttons.contains(m_currentTool)) {
|
|
m_buttons[m_currentTool]->setChecked(false);
|
|
}
|
|
|
|
// 设置新的选择
|
|
if (m_buttons.contains(toolType)) {
|
|
m_buttons[toolType]->setChecked(true);
|
|
m_currentTool = toolType;
|
|
emit toolSelected(toolType);
|
|
} else {
|
|
m_currentTool = "";
|
|
}
|
|
}
|
|
|
|
void EnhancedToolBar::setDragEnabled(bool enabled)
|
|
{
|
|
m_dragEnabled = enabled;
|
|
|
|
// 更新所有按钮
|
|
for (DraggableToolButton *button : m_buttons.values()) {
|
|
button->setDragEnabled(enabled);
|
|
}
|
|
}
|
|
|
|
bool EnhancedToolBar::dragEnabled() const
|
|
{
|
|
return m_dragEnabled;
|
|
}
|