Bootstrap

使用GraphView实现简单的绘图工具

ShapeItem代码:

ShapeItem::ShapeItem(ShapeType type)
{
	m_type = type;
	m_lt = QPointF(0, 0);
	m_rb = QPointF(0, 0);
	m_deleteEnable = false;
	m_bll = BllData::getInstance();
	connect(m_bll, &BllData::deleteShapeEnableSignal, this, &ShapeItem::deleteShapeEnableSlot);
	this->setFlags(QGraphicsItem::ItemIsSelectable |
		QGraphicsItem::ItemIsMovable |
		QGraphicsItem::ItemIsFocusable);
}

ShapeItem::~ShapeItem()
{}
QRectF ShapeItem::boundingRect() const {
	int w = abs(m_lt.x() - m_rb.x());
	int h = abs(m_lt.y() - m_rb.y());
	QRectF rect(m_lt.x(), m_lt.y(), w + 2, h + 2);
	return rect;
}
void ShapeItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
	Q_UNUSED(option);
	Q_UNUSED(widget);
	painter->setPen(QPen(Qt::darkBlue, 1, Qt::SolidLine));
	int width = abs(m_lt.x() - m_rb.x());
	int height = abs(m_lt.y() - m_rb.y());
	QPointF center((m_lt.x() + m_rb.x()) / 2, (m_lt.y() + m_rb.y()) / 2);
	if (m_type == RectShape) {
		QRectF rect(m_lt.x(), m_lt.y(), width, height);
		painter->drawRect(rect);
	}
	else if (m_type == SquareShape) {
		QRectF rect(m_lt.x(), m_lt.y(), width, width);
		painter->drawRect(rect);
	}
	else if (m_type == CircleShape) {
		QRectF rect(m_lt.x(), m_lt.y(), width, width);
		painter->drawEllipse(rect);
	}
	else if (m_type == EllipseShape) {
		QRectF rect(m_lt.x(), m_lt.y(), width, height);
		painter->drawEllipse(rect);
	}
}
void ShapeItem::setTopLeft(QPointF lt) {
	m_lt = lt;
}
QPointF ShapeItem::getTopLeft() {
	return m_lt;
}
void ShapeItem::setBottomRight(QPointF rb) {
	m_rb = rb;
}
QPointF ShapeItem::getBottomRight() {
	return m_rb;
}
void ShapeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) {
	qDebug() << "===================mousePressEvent   pos: " << event->scenePos();
	if (m_deleteEnable) {
		m_deleteEnable = false;
		m_bll->returnDeletePos(m_lt);
	}
}
void ShapeItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
	qDebug() << "================ShapeItem::mousePressEvent---------pos--" << event->pos();
}
void ShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
	qDebug() << "=================ShapeItem::mouseMoveEvent-------------";
	qreal dx = event->scenePos().x() - event->lastScenePos().x();
	qreal dy = event->scenePos().y() - event->lastScenePos().y();
	this->moveBy(dx, dy);
}
void ShapeItem::deleteShapeEnableSlot() {
	m_deleteEnable = true;
}
void ShapeItem::wheelEvent(QGraphicsSceneWheelEvent* event) {
	static int cnt = 0;
	cnt++;
	if (cnt > 1000) {
		cnt = 0;
	}
	if (cnt % 5 == 0) {
		int d = event->delta();
		QPointF center((m_lt.x() + m_rb.x()) / 2, (m_lt.y() + m_rb.y()) / 2);
		int w = abs(m_lt.x() - m_rb.x());
		int h = abs(m_lt.y() - m_rb.y());
		if (d > 0) {
			w = w + 5;
			h = h + 5;
			m_lt.setX(center.x() - w / 2);
			m_lt.setY(center.y() - h / 2);
			m_rb.setX(center.x() + w / 2);
			m_rb.setY(center.y() + h / 2);
		}
		else {
			w = w - 5;
			h = h - 5;
			w = w > 5 ? w : 5;
			h = h > 5 ? h : 5;
			m_lt.setX(center.x() - w / 2);
			m_lt.setY(center.y() - h / 2);
			m_rb.setX(center.x() + w / 2);
			m_rb.setY(center.y() + h / 2);
		}
		m_bll->wheelUpdateData();
	}
}

主UI代码:

GraphViewDemo2::GraphViewDemo2(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
	m_type = NoShape;
	m_bll = BllData::getInstance();
    m_scene = new GraphicsScene;
	/*connect(m_bll, &BllData::updateRectPosSignal, this, &GraphViewDemo2::updateRectPosSlot);
	connect(m_bll, &BllData::releaseRectPosSignal, this, &GraphViewDemo2::releaseRectPosSlot);*/
	connect(m_bll, &BllData::returnDeletePosSignal, this, &GraphViewDemo2::returnDeletePosSlot);
	connect(m_bll, &BllData::wheelUpdateDataSignal, this, &GraphViewDemo2::wheelUpdateDataSlot);
	connect(m_bll, &BllData::pressRectPosSignal, this, &GraphViewDemo2::pressRectPosSlot);
	//ui.graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	//ui.graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	 设置场景范围
	ui.graphicsView->resize(1440, 1120);
	//ui.graphicsView->setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
	 反锯齿
	//ui.graphicsView->setRenderHints(QPainter::Antialiasing);
	m_scene->setBackgroundBrush(Qt::gray);
	ui.graphicsView->setScene(m_scene);
}

GraphViewDemo2::~GraphViewDemo2()
{
}
void GraphViewDemo2::pressRectPosSlot(QPointF pos) {
	int w = 100;
	int h = 80;
	ShapeItem* item;
	if (m_type == RectShape) {
		int ltx = pos.x() - w / 2;
		int lty = pos.y() - h / 2;
		int rbx = pos.x() + w / 2;
		int rby = pos.y() + h / 2;
		item = new ShapeItem(RectShape);
		item->setTopLeft(QPointF(ltx, lty));
		item->setBottomRight(QPointF(rbx, rby));
		m_scene->addItem(item);
	}
	else if (m_type == SquareShape) {
		h = 100;
		int ltx = pos.x() - w / 2;
		int lty = pos.y() - h / 2;
		int rbx = pos.x() + w / 2;
		int rby = pos.y() + h / 2;
		item = new ShapeItem(SquareShape);
		item->setTopLeft(QPointF(ltx, lty));
		item->setBottomRight(QPointF(rbx, rby));
		m_scene->addItem(item);
	}
	else if (m_type == CircleShape) {
		h = 100;
		int ltx = pos.x() - w / 2;
		int lty = pos.y() - h / 2;
		int rbx = pos.x() + w / 2;
		int rby = pos.y() + h / 2;
		item = new ShapeItem(CircleShape);
		item->setTopLeft(QPointF(ltx, lty));
		item->setBottomRight(QPointF(rbx, rby));
		m_scene->addItem(item);
	}
	else if (m_type == EllipseShape) {
		h = 80;
		int ltx = pos.x() - w / 2;
		int lty = pos.y() - h / 2;
		int rbx = pos.x() + w / 2;
		int rby = pos.y() + h / 2;
		item = new ShapeItem(EllipseShape);
		item->setTopLeft(QPointF(ltx, lty));
		item->setBottomRight(QPointF(rbx, rby));
		m_scene->addItem(item);
	}
	m_type = NoShape;
}
void GraphViewDemo2::returnDeletePosSlot(QPointF pos) {
	qDebug() << "returnPressPosSlot pos:  " << pos;
	for (int i = 0; i < m_scene->items().size(); i++) {
		ShapeItem* item = (ShapeItem*)m_scene->items()[i];
		QPointF lt = item->getTopLeft();
		if (pos == lt) {
			qDebug() << "index: " << i;
			m_scene->removeItem(item);
			break;
		}
	}
}
void GraphViewDemo2::on_deleteBtn_clicked() {
	m_bll->deleteShapeEnable();
}
void GraphViewDemo2::on_clearBtn_clicked() {
	m_scene->clear();
	//ui.graphicsView->setScene(m_scene);
}
void GraphViewDemo2::wheelUpdateDataSlot() {
	m_scene->update();
}
void GraphViewDemo2::on_rectBtn_clicked() {
	m_bll->setShapeEnable();
	m_type = RectShape;
}
void GraphViewDemo2::on_squareBtn_clicked() {
	m_bll->setShapeEnable();
	m_type = SquareShape;
}
void GraphViewDemo2::on_circleBtn_clicked() {
	m_bll->setShapeEnable();
	m_type = CircleShape;
}
void GraphViewDemo2::on_ellipseBtn_clicked() {
	m_bll->setShapeEnable();
	m_type = EllipseShape;
}

Scene代码:

GraphicsScene::GraphicsScene(QObject *parent)
	: QGraphicsScene(parent)
{
	m_startP.setX(0);
	m_startP.setY(0);
	m_endP.setX(0);
	m_endP.setY(0);
	m_enable = false;
	m_bll = BllData::getInstance();
	connect(m_bll, &BllData::setShapeEnableSignal, this, &GraphicsScene::setShapeEnableSlot);
}

GraphicsScene::~GraphicsScene()
{}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* event) {
		if (m_enable) {
		m_enable = false;
		m_startP = event->scenePos();
		m_endP = m_startP;
		m_bll->pressRectPos(m_startP);
	}
	else {
		//qDebug() << "GraphicsScene::mousePressEvent---------------";
		//event->ignore();
		QGraphicsScene::mousePressEvent(event);
	}
	}
void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
		QGraphicsScene::mouseMoveEvent(event);
	//qDebug() << "m_startp: " << m_startP << " m_endp: " << m_endP;
}
void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
		QGraphicsScene::mouseReleaseEvent(event);
}
void GraphicsScene::setShapeEnableSlot() {
	qDebug() << "setRectEnableSlot---------------";
	m_enable = true;
}

在这里插入图片描述

;