You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmPlot/nmPlotView.cpp

259 lines
7.7 KiB
C++

//#include <QMenu>
//#include <QFileDialog>
#include "ZxBaseUtil.h"
#include "zxLogInstance.h"
#include "ZxPaintParam.h"
#include "ZxSubAxisX.h"
#include "ZxSubAxisY.h"
#include "nmPlotScene.h"
#include "nmPlotView.h"
#include "nmObjToolBase.h"
#include <QMouseEvent>
#include <QKeyEvent>
ZX_DEFINE_DYNAMIC(mPlotView, nmPlotView)
nmPlotView::nmPlotView()
{
// 未按下鼠标时也接收视口移动事件,实时更新模型位置。
viewport()->setMouseTracking(true);
}
void nmPlotView::onPrepareContextMenu(QMenu* pMenu,
const QPointF& pos)
{
{
nmPlotScene* pScene = dynamic_cast<nmPlotScene*>(getScene());
if (nullptr != pScene && nullptr != pScene->m_pPlot)
{
ZxPlot* pPlot = pScene->m_pPlot;
pPlot->onPrepareContextMenu(pMenu, pos);
{
QAction* pAction = pMenu->addAction(zxLoadIcon("Undo"), tr("Undo"));
connect(pAction, SIGNAL(triggered()), this, SLOT(slotUndo()));
pAction->setEnabled(pPlot->canUndo());
}
{
QAction* pAction = pMenu->addAction(zxLoadIcon("Redo"), tr("Redo"));
connect(pAction, SIGNAL(triggered()), this, SLOT(slotRedo()));
pAction->setEnabled(pPlot->canRedo());
}
pMenu->addSeparator();
}
pMenu->addSeparator();
}
tCurvePlotView::onPrepareContextMenu(pMenu, pos);
}
// 拖拽支持
void nmPlotView::dragEnterEvent(QDragEnterEvent *event)
{
m_bDraging = false;
emit sigCheckDragAccepted(event, m_bDraging);
if (m_bDraging)
{
event->accept();
}
}
void nmPlotView::dropEvent(QDropEvent *event)
{
if (m_bDraging)
{
m_bDraging = false;
emit sigDealwithDrop(event);
event->accept();
}
}
void nmPlotView::mouseMoveEvent(QMouseEvent* e)
{
ZxRenderView::mouseMoveEvent(e);
nmPlotScene* pScene = dynamic_cast<nmPlotScene*>(ZxRenderView::getScene());
if (nullptr == pScene || nullptr == pScene->m_pPlot)
{
return;
}
QPointF pos = mapToScene(QPoint(e->x(), e->y()));
ZxTempLayer* pTempLayer = pScene->getTempLayer();
if (nullptr != pTempLayer)
{
// 范围
QRectF rt = pScene->m_pPlot->getInnerRectF();
pTempLayer->setCrossLineRect(rt);
// 位置
pTempLayer->setCrossLinePos(pos);
// 文字
{
ZxSubAxisBase* pX = dynamic_cast<ZxSubAxisBase*>(pScene->m_pPlot->getMainAxisX());
ZxSubAxisBase* pY = dynamic_cast<ZxSubAxisBase*>(pScene->m_pPlot->getMainAxisY());
ZxSubAxisBase* pY2 = pScene->m_pPlot->getSysAxisY(AP_Right);
if (nullptr != pY2) //加入双Y轴的考虑
{
if (!pY2->isVisible() || pY2->isMinimizeAxis() || pY2 == pY)
pY2 = nullptr;
}
double dX = pX->getValueForPos(pos.x());
double dY = pY->getValueForPos(pos.y());
QString sValueX = ZxBaseUtil::getValidStr(dX, 3);
QString sValueY = ZxBaseUtil::getValidStr(dY, 3);
QString sValueY2 = "";
if (nullptr != pY2)//加入双Y轴的考虑
{
double dY2 = pY2->getValueForPos(pos.y());
sValueY2 = ZxBaseUtil::getValidStr(dY2, 3);
}
QString sTipsEx = "";
emit sigGetTipsExOfPos(sTipsEx, dX, dY); //此处不考虑双Y轴,TODO
// tips
QString s = QString("%1:%2 %3").arg(pX->getName()).arg(sValueX).arg(pX->getUnit());
s += ";";
if (!sTipsEx.isEmpty())
{
QPen pen = QPen(QColor(128, 128, 128));
pTempLayer->setCrossLinePen(pen);
pen.setWidthF(0.1);
s += sTipsEx;
}
else
{
pTempLayer->setCrossLinePen(QPen(QColor(192, 192, 192)));
s += QString("%1:%2 %3").arg(pY->getName()).arg(sValueY).arg(pY->getUnit());
if (!sValueY2.isEmpty() && nullptr != pY2)
s += QString(";%1:%2 %3").arg(pY2->getName()).arg(sValueY2).arg(pY2->getUnit());
}
pTempLayer->setTips(s);
// 状态栏
if (rt.contains(pos))
{
// 复用坐标轴换算结果,通知模型位置窗口刷新坐标。
emit sigPointerPos(dX, dY);
s = tr(" Current Pos:%1-%2").arg(sValueX).arg(sValueY);
if (!sValueY2.isEmpty())
s += QString("-%1").arg(sValueY2);
if (!sTipsEx.isEmpty())
{
s += " ";
s += sTipsEx;
}
if (!m_sCreationHint.isEmpty()) {
// 原因放在坐标前,避免状态栏较窄时看不到继续绘制的提示。
s = m_sCreationHint + " " + s;
}
}
else
{
s = "";
}
emit sigFresh2Status(s);
}
}
}
void nmPlotView::setCreationHint(const QString& hint)
{
if (m_sCreationHint == hint) {
return;
}
m_sCreationHint = hint;
// 双击检查失败后立即显示,不依赖下一次鼠标移动;沿用原有状态栏信号。
QString status = hint;
emit sigFresh2Status(status);
}
void nmPlotView::setCancellableTool(nmObjToolBase* tool)
{
m_cancellableTool = tool;
}
void nmPlotView::keyPressEvent(QKeyEvent* e)
{
if (e->key() == Qt::Key_Escape && m_cancellableTool &&
m_cancellableTool->getView() == this &&
m_cancellableTool->getRootTool() == getActiveTool() &&
m_cancellableTool->cancelInteraction()) {
// 创建取消可能同步移除工具;调用后只清理视图的弱引用,不再访问该工具。
m_cancellableTool = nullptr;
m_pendingCursorTool = nullptr;
e->accept();
return;
}
ZxRenderView::keyPressEvent(e);
}
void nmPlotView::requestHoverCursorRefresh(nmObjToolBase* tool)
{
m_pendingCursorTool = tool;
}
void nmPlotView::mouseReleaseEvent(QMouseEvent* e)
{
// 请求只在当前松手事件内有效;弱引用防止提交或同步槽销毁工具后仍访问它。
QPointer<ZxTool> rootTool = getActiveTool();
m_pendingCursorTool = nullptr;
ZxRenderView::mouseReleaseEvent(e);
QPointer<nmObjToolBase> objTool = m_pendingCursorTool;
m_pendingCursorTool = nullptr;
if (e->button() == Qt::LeftButton && e->buttons() == Qt::NoButton &&
rootTool && getActiveTool() == rootTool.data() && objTool &&
objTool->getView() == this && viewport()->rect().contains(e->pos())) {
// 只查询本次工具的悬停样式,不模拟移动,不重复提交坐标或触发吸附。
objTool->refreshHoverCursor(mapToScene(e->pos()));
}
}
void nmPlotView::focusOutEvent(QFocusEvent *e)
{
ZxRenderView::focusOutEvent(e);
nmPlotScene* pScene = dynamic_cast<nmPlotScene*>(ZxRenderView::getScene());
if (nullptr != pScene)
{
ZxTempLayer* pTempLayer = pScene->getTempLayer();
if (nullptr != pTempLayer)
{
pTempLayer->clearLineAndTips();
return;
}
}
}
void nmPlotView::leaveEvent(QEvent *e)
{
ZxRenderView::leaveEvent(e);
nmPlotScene* pScene = dynamic_cast<nmPlotScene*>(ZxRenderView::getScene());
if (nullptr != pScene)
{
ZxTempLayer* pTempLayer = pScene->getTempLayer();
if (nullptr != pTempLayer)
{
pTempLayer->clearLineAndTips();
// return;
}
}
QString s = "";
emit sigFresh2Status(s);
}