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.
143 lines
4.0 KiB
C++
143 lines
4.0 KiB
C++
#include <QLineF>
|
|
#include <QPainter>
|
|
#include <QPainterPath>
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
#include "ZxRenderView.h"
|
|
#include "nmObjRound.h"
|
|
#include "nmObjRoundTool.h"
|
|
|
|
nmObjRoundTool::nmObjRoundTool() : nmObjToolBase()
|
|
{
|
|
setText(tr("nObjRoundTool"));
|
|
m_oNot = NMOT_Round;
|
|
enableInteractionCursor();
|
|
enableEscapeCancellation();
|
|
}
|
|
|
|
bool nmObjRoundTool::onLeftDown(const QPointF& pt)
|
|
{
|
|
if (m_bCreationCancelled) return true;
|
|
if (m_pObj != nullptr) return nmObjToolBase::onLeftDown(pt);
|
|
|
|
beginCreationCursor();
|
|
setCreationHint(QString());
|
|
m_ptStart = pt;
|
|
m_ptMove = pt;
|
|
m_bTracking = true;
|
|
return true;
|
|
}
|
|
|
|
bool nmObjRoundTool::onMouseMove(const QPointF& pt)
|
|
{
|
|
if (m_bCreationCancelled) return true;
|
|
if (m_pObj != nullptr) return nmObjToolBase::onMouseMove(pt);
|
|
|
|
beginCreationCursor();
|
|
m_ptMove = pt;
|
|
if (m_bTracking) updateLastMoveArea(true);
|
|
return true;
|
|
}
|
|
|
|
bool nmObjRoundTool::onLeftUp(const QPointF& pt)
|
|
{
|
|
if (m_bCreationCancelled) return true;
|
|
if (m_pObj != nullptr) return nmObjToolBase::onLeftUp(pt);
|
|
ZxRenderView* view = getView();
|
|
if (!m_bTracking || view == nullptr) return true;
|
|
|
|
m_bTracking = false;
|
|
QVector<QPointF> points;
|
|
const bool validGeometry = nmObjRound::calculateCreationPoints(m_ptStart, pt, points);
|
|
const qreal minimumDiameter = 3.0;
|
|
const qreal diameter = validGeometry
|
|
? QLineF(view->mapFromScene(m_ptStart), view->mapFromScene(pt)).length() : 0.0;
|
|
if (!qIsFinite(diameter) || diameter < minimumDiameter) {
|
|
setCreationHint(tr("The circle diameter is too small."));
|
|
view->setSnapshotMode(false);
|
|
updateScene();
|
|
return true; // 保留创建模式,允许重画或按 Esc 退出。
|
|
}
|
|
|
|
// 完成信号可能同步移除工具,发出信号前先释放光标和创建状态。
|
|
resetInteractionCursor();
|
|
emit sigPtsFinished(points);
|
|
return true;
|
|
}
|
|
|
|
bool nmObjRoundTool::onLeftDoubleClick(const QPointF& pt)
|
|
{
|
|
if (m_bCreationCancelled) return true;
|
|
if (m_pObj == nullptr) {
|
|
m_bTracking = false;
|
|
if (getView() != nullptr) getView()->setSnapshotMode(false);
|
|
updateScene();
|
|
return true;
|
|
}
|
|
|
|
int option = -1;
|
|
int index = -1;
|
|
if (m_pObj->_runHitTest(pt, option, index) && option == OHO_Inner) {
|
|
m_bTracking = false;
|
|
m_oHitOption = OHO_None;
|
|
m_pObj->m_oHitOption = OHO_None;
|
|
m_pObj->m_nHitIndex = -1;
|
|
m_pObj->setSelected(false);
|
|
if (getView() != nullptr) {
|
|
getView()->setSnapshotMode(false);
|
|
getView()->restoreCursor();
|
|
}
|
|
updateScene();
|
|
return true;
|
|
}
|
|
return nmObjToolBase::onLeftDoubleClick(pt);
|
|
}
|
|
|
|
void nmObjRoundTool::onPaint(QPainter* painter, const ZxPaintParam& param)
|
|
{
|
|
Q_UNUSED(param);
|
|
if (m_bCreationCancelled || !m_bTracking) return;
|
|
|
|
QVector<QPointF> points;
|
|
const nmObjRound* circle = dynamic_cast<const nmObjRound*>(m_pObj);
|
|
if (circle != nullptr) {
|
|
if (!circle->calculateMovePoints(m_ptStart, getCurrentPos(), m_oHitOption,
|
|
circle->m_nHitIndex, points)) return;
|
|
} else if (!nmObjRound::calculateCreationPoints(m_ptStart, m_ptMove, points)) {
|
|
return;
|
|
}
|
|
|
|
QPainterPath path;
|
|
path.addEllipse(points[0], points[1].x(), points[1].y());
|
|
painter->save();
|
|
if (circle != nullptr) {
|
|
QColor color = circle->getBackgrdColor();
|
|
color.setAlpha(48);
|
|
painter->fillPath(path, QBrush(color));
|
|
}
|
|
painter->strokePath(path, QPen(Qt::DashLine));
|
|
painter->restore();
|
|
}
|
|
|
|
Qt::CursorShape nmObjRoundTool::interactionCursor(int nOption, int nSubIndex, bool dragging) const
|
|
{
|
|
if (nOption == OHO_Inner) {
|
|
return nmObjToolBase::interactionCursor(OHO_Bound, nSubIndex, dragging);
|
|
}
|
|
if (nOption != OHO_Point ||
|
|
nmObjToolBase::interactionCursor(nOption, nSubIndex, dragging) == Qt::ArrowCursor) {
|
|
return Qt::ArrowCursor;
|
|
}
|
|
switch (nSubIndex) {
|
|
case nmObjRound::TopControlPoint:
|
|
case nmObjRound::BottomControlPoint:
|
|
return Qt::SizeVerCursor;
|
|
case nmObjRound::RightControlPoint:
|
|
case nmObjRound::LeftControlPoint:
|
|
return Qt::SizeHorCursor;
|
|
default:
|
|
return Qt::ArrowCursor;
|
|
}
|
|
}
|
|
|