458 lines
12 KiB
C++
458 lines
12 KiB
C++
/**
|
|
* @file ModuleToolBarGraphicsItem.cpp
|
|
* @brief 模块工具栏
|
|
* @date 2011-09-22
|
|
*/
|
|
#include <QGraphicsSceneHoverEvent>
|
|
#include <QPainter>
|
|
#include <QGradient>
|
|
#include <QtDebug>
|
|
#include <QObject>
|
|
#include <QCoreApplication>
|
|
#include <QDir>
|
|
#include <QGraphicsScene>
|
|
#include "ModuleToolBarGraphicsItem.h"
|
|
#include "ModuleGraphicsItem.h"
|
|
#include "GeneralWorkflowScene.h"
|
|
#include "WorkflowConst.h"
|
|
|
|
namespace pai
|
|
{
|
|
namespace graphics2d
|
|
{
|
|
/**
|
|
* @class CModuleToolBarStyle
|
|
* @brief 模块工具栏风格
|
|
*/
|
|
class CModuleToolBarStyle
|
|
{
|
|
public:
|
|
/**
|
|
* @brief 构造函数
|
|
*/
|
|
CModuleToolBarStyle();
|
|
|
|
/**
|
|
* @brief 获取关闭按钮颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetCloseColor() const;
|
|
|
|
/**
|
|
* @breif 获取关闭按钮高亮开始颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetCloseHighBeginColor() const;
|
|
|
|
/**
|
|
* @breif 获取关闭按钮高亮结束颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetCloseHighEndColor() const;
|
|
|
|
/**
|
|
* @brief 获取除了关闭按钮部分的颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetPanelColor() const;
|
|
|
|
/**
|
|
* @brief 获取除了关闭按钮部分的开始颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetPanelHighBeginColor() const;
|
|
|
|
/**
|
|
* @brief 获取除了关闭按钮部分的结束颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetPanelHighEndColor() const;
|
|
|
|
/**
|
|
* @brief 获取margin区域的颜色
|
|
* @return 颜色
|
|
*/
|
|
QRgb GetMarginColor() const;
|
|
|
|
/**
|
|
* @brief 获取宽度
|
|
* @return 宽度
|
|
*/
|
|
qreal GetWidth() const;
|
|
|
|
/**
|
|
* @brief 获取高度
|
|
* @return 高度
|
|
*/
|
|
qreal GetHeight() const;
|
|
|
|
private:
|
|
qreal m_width; ///< 宽度
|
|
qreal m_height; ///< 高度
|
|
QRgb m_PanelColor; ///< panel区域颜色
|
|
QRgb m_MarginColor; ///< margin区域颜色
|
|
QRgb m_CloseColor; ///< 关闭按钮颜色
|
|
QRgb m_PanelHighBeginColor; ///< panel高亮开始颜色
|
|
QRgb m_PanelHighEndColor; ///< panel高亮结束颜色
|
|
QRgb m_CloseHighBeginColor; ///< 关闭按钮高亮开始颜色
|
|
QRgb m_CloseHighEndColor; ///< 光比按钮高亮结束颜色
|
|
};
|
|
|
|
CModuleToolBarStyle::CModuleToolBarStyle() :
|
|
m_width(32.0),
|
|
m_height(60.0),
|
|
m_PanelColor(QColor("#E5F6FE").rgb()),
|
|
m_MarginColor(QColor("#C8DEE6").rgb()),
|
|
m_CloseColor(QColor("#E4FAF1").rgb()),
|
|
m_PanelHighBeginColor(QColor("#FCFEFF").rgb()),
|
|
m_PanelHighEndColor(QColor("#F1FBFF").rgb()),
|
|
m_CloseHighBeginColor(QColor("#FCFEFD").rgb()),
|
|
m_CloseHighEndColor(QColor("#F5FDFA").rgb())
|
|
{
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetCloseColor() const
|
|
{
|
|
return m_CloseColor;
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetCloseHighBeginColor() const
|
|
{
|
|
return m_CloseHighBeginColor;
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetCloseHighEndColor() const
|
|
{
|
|
return m_CloseHighEndColor;
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetPanelColor() const
|
|
{
|
|
return m_PanelColor;
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetPanelHighBeginColor() const
|
|
{
|
|
return m_PanelHighBeginColor;
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetPanelHighEndColor() const
|
|
{
|
|
return m_PanelHighEndColor;
|
|
}
|
|
|
|
QRgb CModuleToolBarStyle::GetMarginColor() const
|
|
{
|
|
return m_MarginColor;
|
|
}
|
|
|
|
qreal CModuleToolBarStyle::GetWidth() const
|
|
{
|
|
return m_width;
|
|
}
|
|
|
|
qreal CModuleToolBarStyle::GetHeight() const
|
|
{
|
|
return m_height;
|
|
}
|
|
|
|
// 暂时用全局变量来存储一个唯一的模块工具栏的样式
|
|
pai::graphics2d::CModuleToolBarStyle g_ModuleToolBarStyle;
|
|
QString GetImagePath()
|
|
{
|
|
static QString imgpath;
|
|
if(imgpath!="") return imgpath;
|
|
QString strImagePath;
|
|
{
|
|
QString strPathTmp = QCoreApplication::applicationDirPath() + QDir::separator();
|
|
strImagePath = QDir::toNativeSeparators( strPathTmp );
|
|
QDir dir;
|
|
strImagePath=strPathTmp + "image" + QDir::separator();
|
|
}
|
|
imgpath=strImagePath;
|
|
return imgpath;
|
|
}
|
|
|
|
ModuleToolBarGraphicsItem::ModuleToolBarGraphicsItem() :
|
|
QGraphicsItem(),
|
|
m_HoverButtonIndex(-1),
|
|
m_pModuleItem(NULL),
|
|
m_hovering(false),
|
|
m_ScenceReadOnly(false)
|
|
{
|
|
setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
setAcceptHoverEvents(true);
|
|
m_OpacityEffect.setOpacity(0.3);
|
|
this->setGraphicsEffect(&m_OpacityEffect);
|
|
QString pat=GetImagePath();
|
|
m_pixmaps.append(QPixmap(pat+"Close.png"));
|
|
m_pixmaps.append(QPixmap(pat+"add_monitor.png").scaled(18, 18, Qt::KeepAspectRatio));
|
|
m_pixmaps.append(QPixmap(pat+"delete_monitor.png").scaled(18, 18, Qt::KeepAspectRatio));
|
|
m_pixmaps.append(QPixmap(pat+"Edit.png"));
|
|
|
|
qreal dHeight = g_ModuleToolBarStyle.GetHeight();
|
|
|
|
qreal dRectY = -dHeight / 2 + 1;
|
|
m_ButtonRects.append(QRectF(-10.0, dRectY, 20.0, 12.0));// close button
|
|
dRectY += 16;
|
|
m_ButtonRects.append(QRectF(-10.0, dRectY, 20.0, 20.0));
|
|
dRectY += 20;
|
|
m_ButtonRects.append(QRectF(-10.0, dRectY, 20.0, 20.0));
|
|
}
|
|
|
|
ModuleToolBarGraphicsItem::~ModuleToolBarGraphicsItem()
|
|
{
|
|
}
|
|
|
|
int ModuleToolBarGraphicsItem::type() const
|
|
{
|
|
return Type;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::SetScenceReadOnly(bool readonly)
|
|
{
|
|
m_ScenceReadOnly = readonly;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::SetToolBarVisible(bool visible)
|
|
{
|
|
setVisible(!m_ScenceReadOnly && visible);
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::AttachModuleItem(ModuleGraphicsItem *pModuleItem)
|
|
{
|
|
m_pModuleItem = pModuleItem;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::DetachModuleItem()
|
|
{
|
|
m_pModuleItem = NULL;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *pEvent)
|
|
{
|
|
m_OpacityEffect.setEnabled(false);
|
|
QGraphicsItem::hoverEnterEvent(pEvent);
|
|
this->m_hovering = true;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::hoverMoveEvent(QGraphicsSceneHoverEvent *pEvent)
|
|
{
|
|
for(int i = 0; i < m_ButtonRects.size(); ++i)
|
|
{
|
|
if(m_ButtonRects.at(i).contains(pEvent->pos()))
|
|
{
|
|
m_HoverButtonIndex = i;
|
|
|
|
if(i == 0)
|
|
{
|
|
setToolTip(QObject::tr("Delete Module"));
|
|
}
|
|
else if(i == 1)
|
|
{
|
|
if(m_pModuleItem && m_pModuleItem->GetMonitoring())
|
|
{
|
|
setToolTip(QObject::tr(g_szDeleteMonitor));
|
|
}
|
|
else
|
|
{
|
|
setToolTip(QObject::tr(g_szAddMonitor));
|
|
}
|
|
}
|
|
else if(i == 2)
|
|
{
|
|
setToolTip(QObject::tr("Edit Module"));
|
|
}
|
|
else
|
|
{
|
|
setToolTip("");
|
|
}
|
|
|
|
QGraphicsItem::hoverMoveEvent(pEvent);
|
|
update();
|
|
|
|
return;
|
|
}
|
|
}
|
|
setToolTip("");
|
|
QGraphicsItem::hoverMoveEvent(pEvent);
|
|
m_HoverButtonIndex = -1;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *pEvent)
|
|
{
|
|
if(!((pEvent->pos().y() <= boundingRect().height()) &&
|
|
(pEvent->pos().x() <= -boundingRect().width() / 2) &&
|
|
(pEvent->pos().x() >= -boundingRect().width() / 2 - 2)))
|
|
{
|
|
m_OpacityEffect.setEnabled(true);
|
|
|
|
//设置模块图元为鼠标离开状态
|
|
if(m_pModuleItem)
|
|
{
|
|
m_pModuleItem->m_OnHovering = false;
|
|
m_pModuleItem->update();
|
|
}
|
|
this->DetachModuleItem();
|
|
setVisible(false);
|
|
|
|
this->m_hovering = false;
|
|
}
|
|
|
|
if(qAbs(pEvent->pos().y()) > (boundingRect().height() / 2))
|
|
{
|
|
m_OpacityEffect.setEnabled(true);
|
|
|
|
//设置模块图元为鼠标离开状态
|
|
if(m_pModuleItem)
|
|
{
|
|
m_pModuleItem->m_OnHovering = false;
|
|
m_pModuleItem->update();
|
|
}
|
|
this->DetachModuleItem();
|
|
setVisible(false);
|
|
|
|
this->m_hovering = false;
|
|
}
|
|
QGraphicsItem::hoverLeaveEvent(pEvent);
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *pEvent)
|
|
{
|
|
QGraphicsItem::mouseReleaseEvent(pEvent);
|
|
if(m_HoverButtonIndex == 0)
|
|
{
|
|
// click close button
|
|
if(m_pModuleItem)
|
|
{
|
|
GeneralWorkflowScene *pScene = dynamic_cast< GeneralWorkflowScene* > (m_pModuleItem->scene());
|
|
ModuleGraphicsItem::m_EventAgent.ItemNeedBeDelete(pScene, m_pModuleItem);
|
|
this->DetachModuleItem();
|
|
this->setVisible(false);
|
|
return;
|
|
}
|
|
}
|
|
else if(m_HoverButtonIndex == 1)
|
|
{
|
|
//click monitor button
|
|
if(m_pModuleItem)
|
|
{
|
|
//发送监控事件
|
|
ModuleGraphicsItem::m_EventAgent.SetMonitoring(m_pModuleItem, !m_pModuleItem->GetMonitoring());
|
|
return;
|
|
}
|
|
}
|
|
else if(m_HoverButtonIndex == 2)
|
|
{
|
|
//click edit module button
|
|
if(m_pModuleItem != NULL)
|
|
{
|
|
ModuleGraphicsItem::m_EventAgent.ItemSetName(m_pModuleItem);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
QRectF ModuleToolBarGraphicsItem::boundingRect() const
|
|
{
|
|
return QRectF(-g_ModuleToolBarStyle.GetWidth() / 2 - 2,
|
|
-g_ModuleToolBarStyle.GetHeight() / 2,
|
|
g_ModuleToolBarStyle.GetWidth() + 2,
|
|
g_ModuleToolBarStyle.GetHeight());
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::SetHovering(bool hovering)
|
|
{
|
|
m_hovering = hovering;
|
|
}
|
|
|
|
bool ModuleToolBarGraphicsItem::GetHovering() const
|
|
{
|
|
return m_hovering;
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::SetOpacityEffect(bool enabled)
|
|
{
|
|
this->m_OpacityEffect.setEnabled(enabled);
|
|
}
|
|
|
|
void ModuleToolBarGraphicsItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem */*pOption*/, QWidget */*pWidget*/)
|
|
{
|
|
const qreal dCloseHeight = 14.0;
|
|
const qreal EPS = 1e-10;
|
|
qreal dHeight = g_ModuleToolBarStyle.GetHeight();
|
|
qreal dStop = 0;
|
|
|
|
QPainterPath path;
|
|
|
|
QRectF realRect(boundingRect());
|
|
realRect.adjust(2, 0, 0, 0);
|
|
path.addRoundedRect(realRect, 2.0, 2.0);
|
|
|
|
QLinearGradient linearGrad(QPointF(0, -dHeight / 2), QPointF(0, dHeight / 2));
|
|
|
|
//关闭按钮部分高光部分的渐变
|
|
linearGrad.setColorAt(0, g_ModuleToolBarStyle.GetCloseHighBeginColor());
|
|
dStop = ((dCloseHeight - 1) / 2) / dHeight;
|
|
linearGrad.setColorAt(dStop, g_ModuleToolBarStyle.GetCloseHighEndColor());
|
|
//关闭按钮部分正常光部分的渐变
|
|
linearGrad.setColorAt(dStop + EPS, g_ModuleToolBarStyle.GetCloseColor());
|
|
dStop = (dCloseHeight) / dHeight;
|
|
linearGrad.setColorAt(dStop, g_ModuleToolBarStyle.GetCloseColor());
|
|
//其他按钮部分高光部分的渐变
|
|
linearGrad.setColorAt(dStop + EPS, g_ModuleToolBarStyle.GetPanelHighBeginColor());
|
|
dStop += (10.0 / dHeight);
|
|
linearGrad.setColorAt(dStop, g_ModuleToolBarStyle.GetPanelHighEndColor());
|
|
//其他按钮部分正常光部分的渐变
|
|
linearGrad.setColorAt(dStop + EPS, g_ModuleToolBarStyle.GetPanelColor());
|
|
linearGrad.setColorAt(1, g_ModuleToolBarStyle.GetPanelColor());
|
|
|
|
//开始绘制
|
|
//开始绘制
|
|
QBrush oldBrush = pPainter->brush();
|
|
QPen oldPen = pPainter->pen();
|
|
QBrush brush(linearGrad);
|
|
QPen pen(QColor(g_ModuleToolBarStyle.GetMarginColor()));
|
|
pen.setWidthF(0.0);
|
|
pPainter->setPen(pen);
|
|
pPainter->setBrush(brush);
|
|
//绘制边框和背景
|
|
pPainter->drawPath(path);
|
|
|
|
pPainter->setBrush(oldBrush);
|
|
|
|
//绘制按钮边框
|
|
QPainterPath closeButtonPath;
|
|
QRectF closeButtonArea(realRect);
|
|
closeButtonArea.adjust(0, 0, 0, -(dHeight - dCloseHeight));
|
|
closeButtonPath.addRoundedRect(closeButtonArea, 2.0, 2.0);
|
|
pPainter->drawPath(closeButtonPath);
|
|
|
|
if(m_HoverButtonIndex > 1)
|
|
{
|
|
pPainter->drawRect(m_ButtonRects.at(m_HoverButtonIndex));
|
|
}
|
|
|
|
pPainter->setPen(oldPen);
|
|
|
|
//绘制关闭按钮图标
|
|
QRectF buttonRect = m_ButtonRects.at(0).adjusted(1, 0, 0, 0);
|
|
pPainter->drawPixmap(buttonRect.x(), buttonRect.y(), m_pixmaps.at(0), 0, 0, 0, 0);
|
|
//绘制监控按钮图标
|
|
buttonRect = m_ButtonRects.at(1).adjusted(2, 2, 0, 0);
|
|
if(m_pModuleItem && m_pModuleItem->GetMonitoring())
|
|
{
|
|
pPainter->drawPixmap(buttonRect.x(), buttonRect.y(), m_pixmaps.at(2), 0, 0, 0, 0);
|
|
}
|
|
else
|
|
{
|
|
pPainter->drawPixmap(buttonRect.x(), buttonRect.y(), m_pixmaps.at(1), 0, 0, 0, 0);
|
|
}
|
|
//绘制编辑按钮图标
|
|
buttonRect = m_ButtonRects.at(2).adjusted(2, 2, 0, 0);
|
|
pPainter->drawPixmap(buttonRect.x(), buttonRect.y(), m_pixmaps.at(3), 0, 0, 0, 0);
|
|
}
|
|
|
|
}
|
|
}
|