162 lines
3.8 KiB
C++
162 lines
3.8 KiB
C++
// EnhancedToolBar.cpp
|
|
#include "enhancedToolBar.h"
|
|
#include <QAction>
|
|
#include <QDebug>
|
|
|
|
EnhancedToolBar::EnhancedToolBar(const QString &title, QWidget *parent)
|
|
: QToolBar(title, parent)
|
|
, m_currentTool("")
|
|
{
|
|
init();
|
|
}
|
|
|
|
EnhancedToolBar::EnhancedToolBar(QWidget *parent)
|
|
: QToolBar(parent)
|
|
, m_currentTool("")
|
|
{
|
|
init();
|
|
}
|
|
|
|
void EnhancedToolBar::init()
|
|
{
|
|
// 设置工具栏属性
|
|
setIconSize(QSize(32, 32));
|
|
setToolButtonStyle(Qt::ToolButtonIconOnly);
|
|
setMovable(true);
|
|
setFloatable(true);
|
|
setAllowedAreas(Qt::AllToolBarAreas);
|
|
|
|
// 创建互斥的动作组
|
|
m_actionGroup = new QActionGroup(this);
|
|
m_actionGroup->setExclusive(true);
|
|
|
|
// 连接信号
|
|
connect(this, &QToolBar::actionTriggered,
|
|
this, &EnhancedToolBar::onActionTriggered);
|
|
}
|
|
|
|
QAction* EnhancedToolBar::createAction(const QString &toolType,
|
|
const QString &toolName,
|
|
const QIcon &icon)
|
|
{
|
|
QAction *action = new QAction(icon, toolName, this);
|
|
action->setCheckable(true);
|
|
action->setData(toolType);
|
|
action->setToolTip(toolName);
|
|
return action;
|
|
}
|
|
|
|
void EnhancedToolBar::addTool(const QString &toolType, const QString &toolName,
|
|
const QIcon &icon)
|
|
{
|
|
if (m_actions.contains(toolType)) {
|
|
return; // 已存在
|
|
}
|
|
|
|
QAction *action = createAction(toolType, toolName, icon);
|
|
|
|
m_actionGroup->addAction(action);
|
|
addAction(action);
|
|
m_actions[toolType] = action;
|
|
m_actionToType[action] = toolType;
|
|
|
|
// 如果没有当前工具,自动选择第一个工具
|
|
if (m_currentTool.isEmpty()) {
|
|
setCurrentTool(toolType);
|
|
}
|
|
|
|
emit toolAdded(toolType);
|
|
}
|
|
|
|
void EnhancedToolBar::removeTool(const QString &toolType)
|
|
{
|
|
if (m_actions.contains(toolType)) {
|
|
QAction *action = m_actions[toolType];
|
|
|
|
removeAction(action);
|
|
m_actionGroup->removeAction(action);
|
|
m_actions.remove(toolType);
|
|
m_actionToType.remove(action);
|
|
|
|
action->deleteLater();
|
|
|
|
if (m_currentTool == toolType) {
|
|
m_currentTool = "";
|
|
}
|
|
|
|
emit toolRemoved(toolType);
|
|
}
|
|
}
|
|
|
|
void EnhancedToolBar::setCurrentTool(const QString &toolType)
|
|
{
|
|
if (toolType.isEmpty()) {
|
|
// 清除当前选择
|
|
if (m_actionGroup->checkedAction()) {
|
|
m_actionGroup->checkedAction()->setChecked(false);
|
|
}
|
|
m_currentTool = "";
|
|
emit toolSelected("");
|
|
}
|
|
else if (m_actions.contains(toolType)) {
|
|
m_actions[toolType]->setChecked(true);
|
|
m_currentTool = toolType;
|
|
emit toolSelected(toolType);
|
|
}
|
|
}
|
|
|
|
QString EnhancedToolBar::currentTool() const
|
|
{
|
|
return m_currentTool;
|
|
}
|
|
|
|
QString EnhancedToolBar::toolName(const QString &toolType) const
|
|
{
|
|
if (m_actions.contains(toolType)) {
|
|
return m_actions[toolType]->text();
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
QIcon EnhancedToolBar::toolIcon(const QString &toolType) const
|
|
{
|
|
if (m_actions.contains(toolType)) {
|
|
return m_actions[toolType]->icon();
|
|
}
|
|
return QIcon();
|
|
}
|
|
|
|
QStringList EnhancedToolBar::availableTools() const
|
|
{
|
|
return m_actions.keys();
|
|
}
|
|
|
|
void EnhancedToolBar::clearTools()
|
|
{
|
|
for (auto it = m_actions.begin(); it != m_actions.end(); ++it) {
|
|
removeAction(it.value());
|
|
m_actionGroup->removeAction(it.value());
|
|
it.value()->deleteLater();
|
|
}
|
|
|
|
m_actions.clear();
|
|
m_actionToType.clear();
|
|
m_currentTool = "";
|
|
}
|
|
|
|
void EnhancedToolBar::addTools(const QList<QPair<QString, QString>> &tools)
|
|
{
|
|
for (const auto &tool : tools) {
|
|
addTool(tool.first, tool.second);
|
|
}
|
|
}
|
|
|
|
void EnhancedToolBar::onActionTriggered(QAction *action)
|
|
{
|
|
if (m_actionToType.contains(action)) {
|
|
QString toolType = m_actionToType[action];
|
|
m_currentTool = toolType;
|
|
emit toolSelected(toolType);
|
|
}
|
|
}
|