#include "DrawROI.h"
DrawROI::DrawROI(QGraphicsView* GraphicsView, QObject*parent)
: QObject(parent)
{
m_graphicsView = GraphicsView;
m_isDrawMultipROIS = false;
isROIBeginChange = false;
m_currentROIIndex = 0;
initViewer();
}
DrawROI::~DrawROI()
{
}
void DrawROI::drawROI()
{
if (m_pixmap.isNull())
return;
for (int i = 0; i < m_rois.size(); i++)
{
QPen pen;
pen.setColor(Qt::red);
pen.setWidth(EDGE_WIDTH);
QFont font;
font.setPixelSize(12); //设置字体大小
QPainter painter(m_graphicsView->viewport());
painter.begin(m_graphicsView); //绘画开始
painter.setBrush(QBrush(QColor(0, 0, 200, 20))); //画刷填充框内颜色
painter.setPen(pen);
painter.setFont(font);
//roi图元坐标转视图坐标,因为画roi是按视图坐标画的
QPoint roiViewPoint = m_graphicsView->mapFromScene(m_rois[i].x(), m_rois[i].y());
QRect roi(roiViewPoint.x(), roiViewPoint.y(), m_rois[i].width(), m_rois[i].height());
//QPixmap rawImg = m_pixmap.scaled(m_graphicsView->size(), Qt::KeepAspectRatio);
// QPixmap rawImg = m_pixmap;
QString strPoint = QString("X:%0, Y:%1").arg(m_rois[i].x()).arg(m_rois[i].y()); //roi位置信息
QString strSize = QString("W:%0, H:%1").arg(m_rois[i].width()).arg(m_rois[i].height()); //roi大小信息
std::map<int, QRect>::iterator it = m_rois.begin();
for (int j = 0; j < i; j++)
{
it++;
}
QString roiIndex = QString("#roi:%0").arg(it->first); //roi序号
painter.drawText(roi.topLeft().x(), roi.topLeft().y() - 5, strSize);
painter.drawText(roi.topLeft().x(), roi.topLeft().y() - 18, strPoint);
painter.drawText(roi.topLeft().x(), roi.topLeft().y() - 31, roiIndex);
// painter.drawRect(m_roiRect);
painter.drawRect(roi);
if (m_rois[i].width() != 0 && m_rois[i].height() != 0)
{
//绘制中间十字架
QPen dashPen(Qt::white);
dashPen.setWidth(MIDDLELINE_WIDTH);
dashPen.setStyle(Qt::DashDotLine);
painter.setPen(dashPen);
//从上往下画线,获取roi的y坐标始终为线宽的上边
painter.drawLine(roi.topLeft().x() + m_rois[i].width() / 2, roi.topLeft().y() + EDGE_WIDTH, roi.bottomRight().x() - m_rois[i].width() / 2, roi.bottomRight().y());
//从左往右画线, 获取roi的x坐标始终是线宽的左边
painter.drawLine(roi.topLeft().x() + EDGE_WIDTH, roi.topLeft().y() + m_rois[i].height() / 2, roi.bottomRight().x(), roi.bottomRight().y() - m_rois[i].height() / 2);
//绘制边缘九个点
//painter.setBrush(Qt::green);
//pen.setWidth(0);
//painter.setPen(pen);
//painter.drawRect(m_roiRect.topLeft().x(), m_roiRect.topLeft().y(), POINT_WIDTH, POINT_HEIGHT); //左上角
//painter.drawRect(m_roiRect.topLeft().x(), m_roiRect.topLeft().y() + m_roiRect.height() / 2 - POINT_HEIGHT / 2, POINT_WIDTH, POINT_HEIGHT); //左边中心点
//painter.drawRect(m_roiRect.bottomLeft().x(), m_roiRect.bottomLeft().y() - POINT_HEIGHT, POINT_WIDTH, POINT_HEIGHT); //左下角
//painter.drawRect(m_roiRect.topLeft().x() + m_roiRect.width() / 2 - POINT_WIDTH / 2, m_roiRect.topLeft().y(), POINT_WIDTH, POINT_HEIGHT); //顶部中心
//painter.drawRect(m_roiRect.topLeft().x() + m_roiRect.width() / 2 - POINT_WIDTH / 2, m_roiRect.topLeft().y() + m_roiRect.height() / 2 - POINT_HEIGHT / 2, POINT_WIDTH, POINT_HEIGHT); //中心点
//painter.drawRect(m_roiRect.bottomLeft().x() + m_roiRect.width() / 2 - POINT_WIDTH / 2, m_roiRect.bottomLeft().y() - POINT_HEIGHT, POINT_WIDTH, POINT_HEIGHT); //底部中心点
//painter.drawRect(m_roiRect.topRight().x() - POINT_WIDTH, m_roiRect.topRight().y(), POINT_WIDTH, POINT_HEIGHT); //右上角
//painter.drawRect(m_roiRect.topRight().x() - POINT_WIDTH, m_roiRect.topRight().y() + m_roiRect.height() / 2 - POINT_HEIGHT / 2, POINT_WIDTH, POINT_HEIGHT); //右边中心点
//painter.drawRect(m_roiRect.bottomRight().x() - POINT_WIDTH, m_roiRect.bottomRight().y() - POINT_HEIGHT, POINT_WIDTH, POINT_HEIGHT); //右下角点
}
//绘画结束
painter.end();
m_graphicsView->viewport()->update();
}
}
void DrawROI::changeROIBegin(const QPoint& point)
{
//场景坐标
QPointF fScenePoint = m_graphicsView->mapToScene(point);
//图形项坐标
QGraphicsItem* pItem = m_graphicsView->scene()->itemAt(fScenePoint, QTransform());
if (NULL == pItem)
{
isBeginGraw = false;
return;
}
isBeginGraw = true;
QPointF fItemPoint = pItem->mapFromScene(fScenePoint); //获取图形项坐标
QPoint itemPoint(fItemPoint.x(), fItemPoint.y());
EmDirection dir = region(itemPoint); //获取鼠标当前的坐标在ROI什么位置
m_currentDir = dir;
isROIBeginChange = true;
if (dir == DIR_MIDDLE)
{
//鼠标在矩形中心位置
m_graphicsView->setCursor(Qt::ClosedHandCursor);
m_moveStartPoint.setX(itemPoint.x());
m_moveStartPoint.setY(itemPoint.y());
m_bMovedPressed = true;
}
else if (dir == DIR_NONE)
{
//鼠标在矩形外部
m_graphicsView->setCursor(Qt::ArrowCursor);
m_bPainterPressed = true;
m_paintStartPoint.setX(itemPoint.x());
m_paintStartPoint.setY(itemPoint.y());
m_currentROIIndex = m_rois.size(); //更新roi序号
}
else
{
//鼠标在矩形边缘
m_moveStartPoint.setX(itemPoint.x());
m_moveStartPoint.setY(itemPoint.y());
m_bScalePressed = true;
m_emCurDir = dir;
}
}
void DrawROI::changeROIInProgress(const QPoint& point)
{
if (!isBeginGraw) return;
QPointF fScenePoint = m_graphicsView->mapToScene(point);
//图形项坐标
QGraphicsItem* pItem = m_graphicsView->scene()->itemAt(fScenePoint, QTransform());
QPoint itemPoint;
if (pItem != NULL)
{
QPointF fItemPoint = pItem->mapFromScene(fScenePoint); //获取图形项坐标
QPoint item(fItemPoint.x(), fItemPoint.y());
itemPoint = item;
}
else
{
//绘制roi超过图像边界进行处理
if (fScenePoint.x() < 0 && fScenePoint.y() < 0)
{
QPoint item(0, 0);
itemPoint = item;
}
else if (fScenePoint.x() < 0)
{
QPoint item(0, fScenePoint.y());
itemPoint = item;
}
else if (fScenePoint.y() < 0)
{
QPoint item(fScenePoint.x(), 0);
itemPoint = item;
}
else if (fScenePoint.x() >= m_pixmap.width() && fScenePoint.y() >= m_pixmap.height())
{
QPoint item(m_pixmap.width() - 1, m_pixmap.height() - 1);
itemPoint = item;
}
else if (fScenePoint.x() >= m_pixmap.width())
{
QPoint item(m_pixmap.width() - 1, fScenePoint.y());
itemPoint = item;
}
else if (fScenePoint.y() >= m_pixmap.height())
{
QPoint item(fScenePoint.x(), m_pixmap.height() - 1);
itemPoint = item;
}
}
if (m_bPainterPressed)
{
//正在绘制状态
paintRect(itemPoint);
}
else if (m_bMovedPressed)
{
//判断拖动roi是否超过图像边界
if ((m_rois[m_currentROIIndex].x() <= 0 && m_rois[m_currentROIIndex].y() <= 0))
{
m_rois[m_currentROIIndex].setX(0);
m_rois[m_currentROIIndex].setY(0);
}
else if ((m_rois[m_currentROIIndex].x() <= 0))
{
m_rois[m_currentROIIndex].setX(0);
}
else if ((m_rois[m_currentROIIndex].y() <= 0))
{
m_rois[m_currentROIIndex].setY(0);
到此这篇qpainter 绘制图片(qpainterpath cubicto)的文章就介绍到这了,更多相关 内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/rfx/60613.html