Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

74 Zeilen
1.9 KiB

  1. #include "hmieditor.h"
  2. #include <QPen>
  3. #include <QBrush>
  4. void Shape::draw(QPainter& painter) const {
  5. painter.setPen(QPen(Qt::blue, 2));
  6. painter.setBrush(QBrush(Qt::lightGray, Qt::DiagCrossPattern));
  7. if (type == Circle) {
  8. painter.drawEllipse(rect);
  9. } else if (type == Rectangle) {
  10. painter.drawRect(rect);
  11. }
  12. }
  13. HMIEditor::HMIEditor(QWidget *parent)
  14. : QWidget(parent), currentShapeType(Shape::Rectangle), isDrawing(false)
  15. {
  16. setMinimumSize(400, 300);
  17. setStyleSheet("background-color: white;");
  18. }
  19. void HMIEditor::onDrawCircle() {
  20. currentShapeType = Shape::Circle;
  21. }
  22. void HMIEditor::onDrawRect() {
  23. currentShapeType = Shape::Rectangle;
  24. }
  25. void HMIEditor::paintEvent(QPaintEvent *event) {
  26. Q_UNUSED(event);
  27. QPainter painter(this);
  28. // 绘制所有已完成的图形
  29. for (const auto& shape : shapes) {
  30. shape.draw(painter);
  31. }
  32. // 绘制正在绘制的图形
  33. if (isDrawing) {
  34. painter.setPen(QPen(Qt::red, 2, Qt::DashLine));
  35. if (currentShapeType == Shape::Circle) {
  36. painter.drawEllipse(currentDrawingRect);
  37. } else {
  38. painter.drawRect(currentDrawingRect);
  39. }
  40. }
  41. }
  42. void HMIEditor::mousePressEvent(QMouseEvent *event) {
  43. if (event->button() == Qt::LeftButton) {
  44. isDrawing = true;
  45. currentDrawingRect.setTopLeft(event->pos());
  46. currentDrawingRect.setBottomRight(event->pos());
  47. update();
  48. }
  49. }
  50. void HMIEditor::mouseReleaseEvent(QMouseEvent *event) {
  51. if (event->button() == Qt::LeftButton && isDrawing) {
  52. isDrawing = false;
  53. currentDrawingRect.setBottomRight(event->pos());
  54. shapes.emplace_back(currentShapeType, currentDrawingRect);
  55. update();
  56. }
  57. }
  58. void HMIEditor::mouseMoveEvent(QMouseEvent *event) {
  59. if (isDrawing) {
  60. currentDrawingRect.setBottomRight(event->pos());
  61. update();
  62. }
  63. }