|
|
#include <QPainter>
|
|
|
|
|
|
#include "ZxBaseUtil.h"
|
|
|
#include "nmObjRegion.h"
|
|
|
#include "nmObjRegionTool.h"
|
|
|
#include "nmObjPointWell.h"
|
|
|
|
|
|
#include "nmDataAnalyzeManager.h"
|
|
|
|
|
|
#include "ZxPlot.h"
|
|
|
|
|
|
nmObjRegionTool::nmObjRegionTool() : nmObjToolBase()
|
|
|
, m_pCurPlot(nullptr)
|
|
|
{
|
|
|
setText(tr("nObjRegionTool"));
|
|
|
m_oNot = NMOT_Region;
|
|
|
|
|
|
// 在数据中心获取所有可连接的点(屏幕坐标)
|
|
|
m_vecCurAllPts = nmDataAnalyzeManager::getCurrentInstance()->getPosAllConnectablePoints();
|
|
|
}
|
|
|
|
|
|
bool nmObjRegionTool::onLeftDown(const QPointF& pt)
|
|
|
{
|
|
|
if(NULL == m_pObj) { // 绘图对象尚未创建(鼠标选点ing)
|
|
|
m_bTracking = true;
|
|
|
QPointF p = pt;
|
|
|
|
|
|
changeCursor(Qt::CrossCursor);
|
|
|
|
|
|
// 对于持续性选点的工具,需要设置
|
|
|
getView()->setCursorKeeping(true);
|
|
|
|
|
|
// 判断鼠标是否点击了之前的点中
|
|
|
for(int i = 0; i < m_vecCurAllPts.count(); i++) {
|
|
|
double nDistance = 2.2f;
|
|
|
|
|
|
if((qAbs(m_vecCurAllPts[i].x() - p.x()) < nDistance) && \
|
|
|
(qAbs(m_vecCurAllPts[i].y() - p.y()) < nDistance)) {
|
|
|
// 更新当前点的坐标
|
|
|
p = m_vecCurAllPts[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
m_ptStart = p;
|
|
|
m_vecPoints.push_back(p);
|
|
|
|
|
|
return true;
|
|
|
} else { // 已绑定绘图对象
|
|
|
return nmObjToolBase::onLeftDown(pt);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool nmObjRegionTool::onMouseMove(const QPointF& pt)
|
|
|
{
|
|
|
if(NULL == m_pObj) {
|
|
|
m_ptMove = pt;
|
|
|
|
|
|
if(m_bTracking) {
|
|
|
updateLastMoveArea(true);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
} else {
|
|
|
return nmObjToolBase::onMouseMove(pt);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool nmObjRegionTool::onLeftUp(const QPointF& pt)
|
|
|
{
|
|
|
QPointF p = pt;
|
|
|
if(NULL == m_pObj) {
|
|
|
if(!m_vecPoints.isEmpty()) {
|
|
|
updateLastMoveArea(true);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
} else {
|
|
|
if(m_pObj->isSelected()){
|
|
|
double dDistance = 2.2f;
|
|
|
for(int i = 0; i < m_vecCurAllPts.count(); i++) {
|
|
|
if((qAbs(m_vecCurAllPts[i].x() - pt.x()) < dDistance) && \
|
|
|
(qAbs(m_vecCurAllPts[i].y() - pt.y()) < dDistance) && \
|
|
|
m_oHitOption == OHO_Point)
|
|
|
{
|
|
|
p = m_vecCurAllPts[i];
|
|
|
}
|
|
|
}
|
|
|
return nmObjToolBase::onLeftUp(p);
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool nmObjRegionTool::onLeftDoubleClick(const QPointF& pt)
|
|
|
{
|
|
|
if(NULL == m_pObj) {
|
|
|
// 区域至少需要 3 个点才能闭合成有效面,点数不足时吞掉双击。
|
|
|
if (m_vecPoints.count() < 3)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
changeCursor(Qt::ArrowCursor);
|
|
|
emit sigPtsFinished(m_vecPoints);
|
|
|
|
|
|
m_vecPoints.clear();
|
|
|
|
|
|
if(m_bTracking)
|
|
|
{
|
|
|
m_bTracking = false;
|
|
|
}
|
|
|
|
|
|
// 双击已完成本工具的处理,这里直接消费事件。
|
|
|
return true;
|
|
|
} else {
|
|
|
return nmObjToolBase::onLeftDoubleClick(pt);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmObjRegionTool::onPaint(QPainter* painter, const ZxPaintParam& param)
|
|
|
{
|
|
|
nmObjRegion* pObj = dynamic_cast<nmObjRegion*>(m_pObj);
|
|
|
|
|
|
if(pObj == nullptr) {
|
|
|
|
|
|
if(!m_bTracking && m_vecPoints.size() > 0) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
Qt::CursorShape o = Qt::CrossCursor;
|
|
|
changeCursor(o);
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
// 将需要选中的点绘制正方形
|
|
|
double dDistance = 2.2f;
|
|
|
for(int i = 0; i < m_vecCurAllPts.count(); i++) {
|
|
|
QPointF pt = m_vecCurAllPts[i];
|
|
|
QRectF rect(pt.x() - dDistance * 0.5f, pt.y() - dDistance * 0.5f, dDistance, dDistance);
|
|
|
painter->setPen(QPen(QBrush(Qt::black), \
|
|
|
0.5f, Qt::SolidLine));
|
|
|
painter->drawRect(rect);
|
|
|
}
|
|
|
|
|
|
// 判断鼠标是否进入了某个点的范围,填充捕捉到的矩形背景
|
|
|
for(int i = 0; i < m_vecCurAllPts.count(); i++) {
|
|
|
if((qAbs(m_vecCurAllPts[i].x() - m_ptMove.x()) < dDistance) && \
|
|
|
(qAbs(m_vecCurAllPts[i].y() - m_ptMove.y()) < dDistance)) {
|
|
|
// 修改当前点的的状态
|
|
|
QPointF pt = m_vecCurAllPts[i];
|
|
|
QRectF rect(pt.x() - dDistance * 0.5f, pt.y() - dDistance * 0.5f, dDistance, dDistance);
|
|
|
QPen pen(QBrush(Qt::black), 0.5f, Qt::DotLine);
|
|
|
painter->setPen(pen);
|
|
|
painter->drawRect(rect);
|
|
|
QBrush br(Qt::green);
|
|
|
painter->fillRect(rect, br);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
painter->restore();
|
|
|
|
|
|
paintTrackingVirtualObj(painter, param);
|
|
|
} else {// 已绑定绘图对象
|
|
|
if(m_pObj->isSelected()){
|
|
|
painter->save();
|
|
|
|
|
|
m_vecCurAllPts.clear();
|
|
|
m_vecCurAllPts = nmDataAnalyzeManager::getCurrentInstance()->removePosByObject(pObj->getRegionData());
|
|
|
|
|
|
double dDistance = 2.2f;
|
|
|
for(int i = 0; i < m_vecCurAllPts.count(); i++) {
|
|
|
|
|
|
QPointF pt = m_vecCurAllPts[i];
|
|
|
QRectF rect(pt.x() - dDistance * 0.5f, pt.y() - dDistance * 0.5f, dDistance, dDistance);
|
|
|
painter->setPen(QPen(QBrush(Qt::black), \
|
|
|
0.5f, Qt::SolidLine));
|
|
|
painter->drawRect(rect);
|
|
|
}
|
|
|
// 判断鼠标是否进入了某个点的范围,填充捕捉到的矩形背景
|
|
|
for(int i = 0; i < m_vecCurAllPts.count(); i++) {
|
|
|
if((qAbs(m_vecCurAllPts[i].x() - m_ptMove.x()) < dDistance) && \
|
|
|
(qAbs(m_vecCurAllPts[i].y() - m_ptMove.y()) < dDistance) && \
|
|
|
m_oHitOption == OHO_Point) {
|
|
|
// 修改当前点的的状态
|
|
|
QPointF pt = m_vecCurAllPts[i];
|
|
|
QRectF rect(pt.x() - dDistance * 0.5f, pt.y() - dDistance * 0.5f, dDistance, dDistance);
|
|
|
QPen pen(QBrush(Qt::black), 0.5f, Qt::DotLine);
|
|
|
painter->setPen(pen);
|
|
|
painter->drawRect(rect);
|
|
|
QBrush br(Qt::green);
|
|
|
painter->fillRect(rect, br);
|
|
|
}
|
|
|
}
|
|
|
painter->restore();
|
|
|
}
|
|
|
paintTrackingRealObj(painter, param);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void nmObjRegionTool::paintTrackingVirtualObj(QPainter* painter, const ZxPaintParam& param)
|
|
|
{
|
|
|
painter->save();
|
|
|
|
|
|
{
|
|
|
QPen pen(Qt::SolidLine);
|
|
|
painter->setPen(pen);
|
|
|
|
|
|
for(int i = 0; i < m_vecPoints.count() - 1; i++) {
|
|
|
painter->drawLine(m_vecPoints[i], m_vecPoints[i + 1]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(m_bTracking || !m_vecPoints.isEmpty()) {
|
|
|
QPen pen(Qt::DashLine);
|
|
|
painter->setPen(pen);
|
|
|
painter->drawLine(m_ptStart, m_ptMove);
|
|
|
}
|
|
|
|
|
|
painter->restore();
|
|
|
}
|
|
|
|
|
|
void nmObjRegionTool::paintTrackingRealObj(QPainter* painter, const ZxPaintParam& param)
|
|
|
{
|
|
|
if(!m_bTracking) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
nmObjRegion* pObj = dynamic_cast<nmObjRegion*>(m_pObj);
|
|
|
Q_ASSERT(NULL != pObj);
|
|
|
|
|
|
if (nullptr == pObj)
|
|
|
{
|
|
|
return; // 如果pObj为nullptr,直接返回
|
|
|
}
|
|
|
|
|
|
int nIndex = pObj->m_nHitIndex;
|
|
|
Qt::CursorShape o = getCursorBy((int)m_oHitOption, nIndex);
|
|
|
|
|
|
QVector<QPointF> vec = pObj->getAllPos();
|
|
|
QPointF ptOld = m_ptStart;
|
|
|
QPointF ptNew = getCurrentPos();
|
|
|
|
|
|
if(m_oHitOption == OHO_Point) {
|
|
|
if(nIndex >= 0 && nIndex < vec.count()) {
|
|
|
vec[nIndex] = getCurrentPos();
|
|
|
}
|
|
|
} else if(m_oHitOption == OHO_Bound) {
|
|
|
for(int i = 0; i < vec.count(); i++) {
|
|
|
vec[i] = pObj->offsetPoint(vec[i], ptOld, ptNew);
|
|
|
}
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
painter->save();
|
|
|
QPen pen(Qt::DashLine);
|
|
|
painter->setPen(pen);
|
|
|
|
|
|
QPainterPath oPath;
|
|
|
oPath.moveTo(vec[0]);
|
|
|
|
|
|
for(int i = 1; i < vec.count(); i++) {
|
|
|
oPath.lineTo(vec[i]);
|
|
|
}
|
|
|
|
|
|
oPath.closeSubpath();
|
|
|
|
|
|
QColor clr = pObj->getBackgrdColor();
|
|
|
clr.setAlpha(128);
|
|
|
painter->fillPath(oPath, QBrush(clr));
|
|
|
|
|
|
changeCursor(o);
|
|
|
|
|
|
painter->restore();
|
|
|
}
|
|
|
|
|
|
Qt::CursorShape nmObjRegionTool::getCursorBy(const int& nOption,
|
|
|
const int& nSubIndex)
|
|
|
{
|
|
|
if(nOption < 0) {
|
|
|
return Qt::ArrowCursor;
|
|
|
}
|
|
|
|
|
|
// 选择状态下起作用
|
|
|
if (!m_pObj->isSelected()) {
|
|
|
return Qt::ArrowCursor;
|
|
|
}
|
|
|
|
|
|
ObjHitOption oHitOption = (ObjHitOption)nOption;
|
|
|
|
|
|
if(oHitOption == OHO_Point) {
|
|
|
return (Qt::CrossCursor);
|
|
|
} else if(oHitOption == OHO_Bound) {
|
|
|
return (Qt::CrossCursor);
|
|
|
}
|
|
|
|
|
|
return Qt::ArrowCursor;
|
|
|
}
|
|
|
|