#include "WindowAppBlockComment.h" #include "DialogEditComment.h" #include "Pou.h" #include "WindowAppPouScene.h" #include "VPCommand.h" WindowAppBlockComment::WindowAppBlockComment( TOOL* pNewTool, POU* Pou, bool bShowOnly, QGraphicsObject* parent ) : WindowAppBlockBase(pNewTool, Pou, bShowOnly, parent) , isResizing(false) { // 设置默认的注释文字 if (this->m_toolInfo->strComment.isEmpty()) { this->m_toolInfo->strComment = COMM_DEFAULT_TEXT; } // 初始化UI风格 this->initStyle(); // 设置注释控件的默认宽度 blockRect.setRect( 0, 0, COMM_BASIC_WIDTH, COMM_BASIC_HEIGHT ); // 更新矩形区域的尺寸 this->updateRect(); reDirection = ResizeNone; // 2022-10-3,初始化右键菜单 createContextMenu(); } WindowAppBlockComment::~WindowAppBlockComment() { } /// /// 初始化UI风格 /// void WindowAppBlockComment::initStyle() { // 允许移动 setFlag(QGraphicsItem::ItemIsMovable); // 允许选中 setFlag(QGraphicsItem::ItemIsSelectable); // 允许获得焦点 setFlag(QGraphicsItem::ItemIsFocusable); // 允许发送尺寸变更通知 setFlag(QGraphicsItem::ItemSendsGeometryChanges); // 响应鼠标悬停事件(重要,否则QGraphicsItem默认是不响应鼠标悬停事件的) this->setAcceptHoverEvents(true); } /// /// 绘制注释功能块 /// /// /// /// void WindowAppBlockComment::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setRenderHints(QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing); painter->save(); // 绘制边框 this->drawFrame(painter); // 绘制文字 this->drawContent(painter); painter->restore(); } //////////////////////////////////////////////////////////////// // 初始化功能块的右键菜单 void WindowAppBlockComment::createContextMenu() { WindowAppBlockBase::createContextMenu(); connect(deleteAction, &QAction::triggered, this, &WindowAppBlockComment::onMenuDelete); contextMenu = new QMenu(); contextMenu->addAction(deleteAction); } /// /// 绘制边框 /// /// void WindowAppBlockComment::drawFrame(QPainter* painter) { // 设置画笔 if (!this->isSelected()) { painter->setPen(QPen(COLOR_FRAME_COMM, PEN_WIDTH_FRAME)); } else { painter->setPen(QPen(COLOR_FRAME_COMM, PEN_WIDTH_FRAME_BOLD)); } // 绘制边框 painter->drawRoundedRect(blockRect, 0, 0); //// 设置画笔 //painter->setPen(QPen(COLOR_COMM_FRAME, PEN_WIDTH_FRAME)); // 设置透明 //painter->setBrush(Qt::transparent); } /// /// 刷新全部(包括文字和尺寸) /// void WindowAppBlockComment::updateAll() { // 更新矩形区域的尺寸 this->updateRect(); // 刷新界面 this->update(); } /// /// 更新矩形区域的尺寸 /// void WindowAppBlockComment::updateRect() { // 根据文字内容计算实际的文字矩形区域(包括换行) QRectF textRect = QFontMetrics(FONT_COMM).boundingRect(QRect(), Qt::TextWordWrap, m_toolInfo->strComment); // 根据文字矩形区域调整外边框 double xFix = textRect.width() - blockRect.width(); double yFix = textRect.height() - blockRect.height(); blockRect.setRight(blockRect.right() + xFix + COMM_SIDE_X_SPACING * 2.0); blockRect.setBottom(blockRect.bottom() + yFix - COMM_SIDE_Y_SPACING * 2.0); // 如果调整完后的区域小于最小区域,则调成到最小区域 if (blockRect.width() < COMM_BASIC_WIDTH) { blockRect.setRight(blockRect.left() + COMM_BASIC_WIDTH); } if (blockRect.height() < COMM_BASIC_HEIGHT) { blockRect.setBottom(blockRect.top() + COMM_BASIC_HEIGHT); } } /// /// 响应双击事件,弹出编辑内容对话框 /// /// void WindowAppBlockComment::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) { // 只有鼠标左键才响应 if (event->button() == Qt::LeftButton) { DialogEditComment dialogComm(m_toolInfo->strComment); // 接收用户输入的注释内容 if (dialogComm.exec() == QDialog::Accepted) { this->m_toolInfo->strComment = dialogComm.m_strComment; // 如果用户输入了空的注释内容,直接删除本注释控件 if (this->m_toolInfo->strComment.isEmpty()) { qDebug() << "WindowAppBlockComment::mouseDoubleClickEvent - m_strComment is empty, delete myslef."; // 向scene发送通知 emit this->emptyContent(this); } // 刷新整个界面 this->updateAll(); } } } /// /// 绘制文字(文字左上角对齐) /// /// void WindowAppBlockComment::drawContent(QPainter* painter) { // 设置画笔 painter->setPen(QPen(COLOR_FRAME_COMM, PEN_WIDTH_FRAME)); // 绘制文字 painter->setFont(FONT_COMM); // 文字区域比当前四边略微缩小一点 QRectF textRect = blockRect.adjusted( COMM_SIDE_X_SPACING, COMM_SIDE_Y_SPACING, - COMM_SIDE_Y_SPACING, - COMM_SIDE_Y_SPACING ); //QTextOption toption(Qt::AlignLeft | Qt::AlignTop); //toption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); painter->drawText(textRect, Qt::TextWordWrap, m_toolInfo->strComment); } /// /// 计算区域边界 /// /// QRectF WindowAppBlockComment::boundingRect() const { return blockRect; } /// /// 菜单 - Delete /// void WindowAppBlockComment::onMenuDelete() { //// 由Scene统一删除 //m_pPou->parentScene()->delToolItem(this); // 生成删除指令,调用Undo体系进行删除 PouToolDelCommand* toolDelCommand = new PouToolDelCommand( (WindowAppPouScene*)this->scene(), this->m_toolInfo, this->scenePos() ); ((WindowAppPouScene*)scene())->m_CommandManager.executeCommand(toolDelCommand); } //====================================================================== // // 注释控件缩放相关代码 // //====================================================================== /// /// 鼠标悬浮事件(用于显示拉伸用的鼠标) /// /// void WindowAppBlockComment::hoverMoveEvent(QGraphicsSceneHoverEvent* event) { // qDebug() << "WindowAppBlockComment::hoverMoveEvent"; // 如果控件处于选中状态,则根据鼠标位置显示拉伸鼠标 if (this->isSelected()) { QPointF mousePoint = event->pos(); QPointF mouseHandler = QPointF(10, 10); QPointF topLeft = blockRect.topLeft(); QPointF topRight = blockRect.topRight(); QPointF bottomLeft = blockRect.bottomLeft(); QPointF bottomRight = blockRect.bottomRight(); // resize top if (mousePoint.x() >= (topLeft.x() + mouseHandler.x()) && mousePoint.x() <= (topRight.x() - mouseHandler.x()) && mousePoint.y() >= (topLeft.y() - mouseHandler.y()) && mousePoint.y() <= (topLeft.y() + mouseHandler.y())) { setCursor(Qt::SizeVerCursor); } // resize bottom else if (mousePoint.x() >= (bottomLeft.x() + mouseHandler.x()) && mousePoint.x() <= (bottomRight.x() - mouseHandler.x()) && mousePoint.y() >= (bottomLeft.y() - mouseHandler.y()) && mousePoint.y() <= (bottomLeft.y() + mouseHandler.y())) { setCursor(Qt::SizeVerCursor); } // resize left else if (mousePoint.x() >= (topLeft.x() - mouseHandler.x()) && mousePoint.x() <= (topLeft.x() + mouseHandler.x()) && mousePoint.y() >= (topLeft.y() + mouseHandler.y()) && mousePoint.y() <= (bottomLeft.y() - mouseHandler.y())) { setCursor(Qt::SizeHorCursor); } // resize right else if (mousePoint.x() >= (topRight.x() - mouseHandler.x()) && mousePoint.x() <= (topRight.x() + mouseHandler.x()) && mousePoint.y() >= (topRight.y() + mouseHandler.y()) && mousePoint.y() <= (bottomRight.y() - mouseHandler.y())) { setCursor(Qt::SizeHorCursor); } // resize top left else if (mousePoint.x() <= (topLeft.x() + mouseHandler.x()) && mousePoint.x() >= (topLeft.x() - mouseHandler.x()) && mousePoint.y() <= (topLeft.y() + mouseHandler.y()) && mousePoint.y() >= (topLeft.y() - mouseHandler.y())) { setCursor(Qt::SizeFDiagCursor); } // resize bottom right else if (mousePoint.x() <= (bottomRight.x() + mouseHandler.x()) && mousePoint.x() >= (bottomRight.x() - mouseHandler.x()) && mousePoint.y() <= (bottomRight.y() + mouseHandler.y()) && mousePoint.y() >= (bottomRight.y() - mouseHandler.y())) { setCursor(Qt::SizeFDiagCursor); } // resize bottom left else if (mousePoint.x() <= (bottomLeft.x() + mouseHandler.x()) && mousePoint.x() >= (bottomLeft.x() - mouseHandler.x()) && mousePoint.y() >= (bottomLeft.y() - mouseHandler.y()) && mousePoint.y() <= (bottomLeft.y() + mouseHandler.y())) { setCursor(Qt::SizeBDiagCursor); } // resize top right else if (mousePoint.x() <= (topRight.x() + mouseHandler.x()) && mousePoint.x() >= (topRight.x() - mouseHandler.x()) && mousePoint.y() >= (topRight.y() - mouseHandler.y()) && mousePoint.y() <= (topRight.y() + mouseHandler.y())) { setCursor(Qt::SizeBDiagCursor); } else { setCursor(Qt::ArrowCursor); } } QGraphicsObject::hoverMoveEvent(event); } /// /// 鼠标移动事件(用于拉伸控件) /// /// void WindowAppBlockComment::mouseMoveEvent(QGraphicsSceneMouseEvent* event) { if (!isSelected()) { return; } QPointF mousePoint = event->pos(); // 如果处于拉伸状态,则调整控件的矩形区域 if (this->isResizing) { if (reDirection & ResizeLeft) { blockRect.setLeft(mousePoint.x()); } if (reDirection & ResizeRight) { blockRect.setRight(mousePoint.x()); } if (reDirection & ResizeTop) { blockRect.setTop(mousePoint.y()); } if (reDirection & ResizeBottom) { blockRect.setBottom(mousePoint.y()); } scene()->update(); return; } QGraphicsItem::mouseMoveEvent(event); } /// /// 鼠标按下事件(用于触发控件拉伸) /// /// void WindowAppBlockComment::mousePressEvent(QGraphicsSceneMouseEvent* event) { QPointF mousePoint = event->pos(); QPointF mouseHandler = QPointF(5, 5); QPointF topLeft = blockRect.topLeft(); QPointF topRight = blockRect.topRight(); QPointF bottomLeft = blockRect.bottomLeft(); QPointF bottomRight = blockRect.bottomRight(); reDirection = ResizeNone; this->isResizing = false; // resize top if (mousePoint.x() >= (topLeft.x() + 0) && mousePoint.x() <= (topRight.x() - 0) && mousePoint.y() >= (topLeft.y() - mouseHandler.y()) && mousePoint.y() <= (topLeft.y() + mouseHandler.y())) { reDirection |= ResizeTop; this->isResizing = true; } // resize bottom if (mousePoint.x() >= (bottomLeft.x() + 0) && mousePoint.x() <= (bottomRight.x() - 0) && mousePoint.y() >= (bottomLeft.y() - mouseHandler.y()) && mousePoint.y() <= (bottomLeft.y() + mouseHandler.y())) { reDirection |= ResizeBottom; this->isResizing = true; } // resize left if (mousePoint.x() >= (topLeft.x() - mouseHandler.x()) && mousePoint.x() <= (topLeft.x() + mouseHandler.x()) && mousePoint.y() >= (topLeft.y() + 0) && mousePoint.y() <= (bottomLeft.y() - 0)) { reDirection |= ResizeLeft; this->isResizing = true; } // resize right if (mousePoint.x() >= (topRight.x() - mouseHandler.x()) && mousePoint.x() <= (topRight.x() + mouseHandler.x()) && mousePoint.y() >= (topRight.y() + 0) && mousePoint.y() <= (bottomRight.y() - 0)) { reDirection |= ResizeRight; this->isResizing = true; } // 根据移动方向设置不同的鼠标指针 switch (reDirection) { case ResizeRight: case ResizeLeft: setCursor(Qt::SizeHorCursor); break; case ResizeBottom: case ResizeTop: setCursor(Qt::SizeVerCursor); break; case ResizeRight | ResizeBottom: case ResizeLeft | ResizeTop: setCursor(Qt::SizeFDiagCursor); break; case ResizeLeft | ResizeBottom: case ResizeRight | ResizeTop: setCursor(Qt::SizeBDiagCursor); break; default: setCursor(Qt::ArrowCursor); break; } // 2022-10-4,调用一下基类的mousePressEvent,执行对应的功能(多选,位置保存等等) WindowAppBlockBase::mousePressEvent(event); QGraphicsObject::mousePressEvent(event); } /// /// 鼠标释放事件(用于鼠标还原以及接收拉伸状态) /// /// void WindowAppBlockComment::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { // 还原鼠标 setCursor(Qt::ArrowCursor); // 结束拉伸状态 this->isResizing = false; reDirection = ResizeNone; // 2022-10-4,调用一下基类的mouseReleaseEvent,执行对应的功能 WindowAppBlockBase::mouseReleaseEvent(event); QGraphicsObject::mouseReleaseEvent(event); } //====================================================================== // // 注释控件缩放相关代码 - 结束 // //====================================================================== //==================================================== // // Del // //==================================================== ///// ///// 键盘按下时(回车键结束编辑) ///// ///// //void WindowAppBlockComment::keyPressEvent(QKeyEvent* event) //{ // // 点击回车失去焦点,编辑完成,后续给FocusOutEvent处理 // if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) // { // clearFocus(); // // return; // } // // QGraphicsTextItem::keyPressEvent(event); //} //void WindowAppBlockComment::keyReleaseEvent(QKeyEvent* event) //{ // // 编译完毕后,更新一下外边界 // this->updateRect(); // this->update(); // // QGraphicsTextItem::keyReleaseEvent(event); //} //QVariant WindowAppBlockComment::itemChange(GraphicsItemChange change, // const QVariant& value) //{ // if (change == QGraphicsItem::ItemSelectedHasChanged) // { // emit selectedChange(this); // } // // return value; //} ///// ///// 在文本内容变更时触发,自动更新边距 ///// //void WindowAppBlockComment::onContentsChanged() //{ // // 有内容变更时,更新一下外边界 // this->updateRect(); // this->update(); //} // // ///// ///// 响应双击事件,激活文本编辑模式 ///// ///// //void WindowAppBlockComment::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) //{ // if (event->button() == Qt::LeftButton && textInteractionFlags() == Qt::NoTextInteraction) // { // setTextInteractionFlags(Qt::TextEditorInteraction); // } // // QGraphicsTextItem::mouseDoubleClickEvent(event); //} // // ///// ///// 重写Shape函数,使其没有文字的空白部分也可以响应消息 ///// ///// //QPainterPath WindowAppBlockComment::shape() const //{ // QPainterPath path; // path.addRect(blockRect); // return path; //} //void WindowAppBlockComment::hoverEnterEvent(QGraphicsSceneHoverEvent* event) //{ // qDebug() << "WindowAppBlockComment::hoverEnterEvent"; // // update(); // QGraphicsObject::hoverEnterEvent(event); //} // // //void WindowAppBlockComment::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) //{ // qDebug() << "WindowAppBlockComment::hoverLeaveEvent"; // // setCursor(QCursor(Qt::ArrowCursor)); // scene()->update(sceneBoundingRect()); // update(); // QGraphicsObject::hoverLeaveEvent(event); //} //void WindowAppBlockComment::hoverMoveEvent(QGraphicsSceneHoverEvent* event) //{ // if (this->isSelected()) // { // QPointF mousePoint = event->pos(); // QPointF mouseHandler = QPointF(5, 5); // QPointF topLeft = blockRect.topLeft(); // QPointF topRight = blockRect.topRight(); // QPointF bottomLeft = blockRect.bottomLeft(); // QPointF bottomRight = blockRect.bottomRight(); // // // resize top // if (mousePoint.x() >= (topLeft.x() + mouseHandler.x()) && // mousePoint.x() <= (topRight.x() - mouseHandler.x()) && // mousePoint.y() >= (topLeft.y() - mouseHandler.y()) && // mousePoint.y() <= (topLeft.y() + mouseHandler.y())) { // setCursor(Qt::SizeVerCursor); // } // // resize bottom // else if (mousePoint.x() >= (bottomLeft.x() + mouseHandler.x()) && // mousePoint.x() <= (bottomRight.x() - mouseHandler.x()) && // mousePoint.y() >= (bottomLeft.y() - mouseHandler.y()) && // mousePoint.y() <= (bottomLeft.y() + mouseHandler.y())) { // setCursor(Qt::SizeVerCursor); // } // // resize left // else if (mousePoint.x() >= (topLeft.x() - mouseHandler.x()) && // mousePoint.x() <= (topLeft.x() + mouseHandler.x()) && // mousePoint.y() >= (topLeft.y() + mouseHandler.y()) && // mousePoint.y() <= (bottomLeft.y() - mouseHandler.y())) { // setCursor(Qt::SizeHorCursor); // } // // resize right // else if (mousePoint.x() >= (topRight.x() - mouseHandler.x()) && // mousePoint.x() <= (topRight.x() + mouseHandler.x()) && // mousePoint.y() >= (topRight.y() + mouseHandler.y()) && // mousePoint.y() <= (bottomRight.y() - mouseHandler.y())) { // setCursor(Qt::SizeHorCursor); // } // // resize top left // else if (mousePoint.x() <= (topLeft.x() + mouseHandler.x()) && // mousePoint.x() >= (topLeft.x() - mouseHandler.x()) && // mousePoint.y() <= (topLeft.y() + mouseHandler.y()) && // mousePoint.y() >= (topLeft.y() - mouseHandler.y())) { // setCursor(Qt::SizeFDiagCursor); // } // // resize bottom right // else if (mousePoint.x() <= (bottomRight.x() + mouseHandler.x()) && // mousePoint.x() >= (bottomRight.x() - mouseHandler.x()) && // mousePoint.y() <= (bottomRight.y() + mouseHandler.y()) && // mousePoint.y() >= (bottomRight.y() - mouseHandler.y())) { // setCursor(Qt::SizeFDiagCursor); // } // // resize bottom left // else if (mousePoint.x() <= (bottomLeft.x() + mouseHandler.x()) && // mousePoint.x() >= (bottomLeft.x() - mouseHandler.x()) && // mousePoint.y() >= (bottomLeft.y() - mouseHandler.y()) && // mousePoint.y() <= (bottomLeft.y() + mouseHandler.y())) { // setCursor(Qt::SizeBDiagCursor); // } // // resize top right // else if (mousePoint.x() <= (topRight.x() + mouseHandler.x()) && // mousePoint.x() >= (topRight.x() - mouseHandler.x()) && // mousePoint.y() >= (topRight.y() - mouseHandler.y()) && // mousePoint.y() <= (topRight.y() + mouseHandler.y())) { // setCursor(Qt::SizeBDiagCursor); // } // else { // setCursor(Qt::ArrowCursor); // } // } // QGraphicsObject::hoverMoveEvent(event); //} ///** // * @brief Element::paintSelected // * @details 绘制选中状态 // * @param painter // * @param iLineWidth 线宽 // */ //void WindowAppBlockComment::paintSelected(QPainter* painter, int iLineWidth) //{ // if (isSelected()) // { // painter->save(); // // painter->setBrush(Qt::NoBrush); // // //QPen pen(Qt::green, iLineWidth, Qt::DashLine); // //painter->setPen(pen); // QRectF rect = blockRect.adjusted(-1, -1, 1, 1); // // painter->drawRect(rect); // // QPen pen2(Qt::green, iLineWidth, Qt::SolidLine); // painter->setPen(pen2); // QPointF pt = QPointF(3, 3); // // painter->drawRect(QRectF(rect.topLeft() - pt, rect.topLeft() + pt)); // QPointF ptMid = QPointF(rect.left() + rect.width() / 2, rect.top()); // painter->drawRect(QRectF(ptMid - pt, ptMid + pt)); // painter->drawRect(QRectF(rect.topRight() - pt, rect.topRight() + pt)); // // ptMid = QPointF(rect.left(), rect.top() + rect.height() / 2); // painter->drawRect(QRectF(ptMid - pt, ptMid + pt)); // ptMid = QPointF(rect.right(), rect.top() + rect.height() / 2); // painter->drawRect(QRectF(ptMid - pt, ptMid + pt)); // // painter->drawRect(QRectF(rect.bottomLeft() - pt, rect.bottomLeft() + pt)); // ptMid = QPointF(rect.left() + rect.width() / 2, rect.bottom()); // painter->drawRect(QRectF(ptMid - pt, ptMid + pt)); // painter->drawRect(QRectF(rect.bottomRight() - pt, rect.bottomRight() + pt)); // // painter->restore(); // } //}