class BoardLayer : public Qk2dViewLayer{public: BoardLayer(); const QImage& image() const { return m_image; } QTransf._qt roi">
当前位置:   article > 正文

Qt图层结构——实现ROI选取、选框放缩移动、背景图像移动放缩_qt roi

qt roi

文件结构:

头文件boardlayer.h

  1. #ifndef BOARDLAYER_H
  2. #define BOARDLAYER_H
  3. #include "qk2dview.h"
  4. #include <QImage>
  5. class BoardLayer : public Qk2dViewLayer
  6. {
  7. public:
  8. BoardLayer();
  9. const QImage& image() const { return m_image; }
  10. QTransform imageToWorld() const { return m_imgToWorld; }
  11. QTransform worldToImage() const { return m_worldToImg; }
  12. private:
  13. void draw_ex(QPainter &painter) override;
  14. void calcTransform();
  15. private:
  16. QImage m_image;
  17. QPointF m_origin;
  18. double m_pixelSize;
  19. QRectF m_worldRect; // image rect in world coordinate
  20. QTransform m_imgToWorld; // convert image coordinate to world coordinate
  21. QTransform m_worldToImg; // convert world coordinate to image coordinate
  22. };
  23. #endif // BOARDLAYER_H

 component.h

  1. #ifndef COMPONENT_H
  2. #define COMPONENT_H
  3. #include <QPointF>
  4. #include <QSizeF>
  5. #include <vector>
  6. struct Component
  7. {
  8. Component(const QPointF &center, const QSizeF &size, float angle = 0)
  9. : m_pos(center)
  10. , m_size(size)
  11. , m_angle(angle)
  12. , m_unitVecX()
  13. , m_unitVecY()
  14. , m_vertex()
  15. , m_selected(false)
  16. {
  17. calcVertex();
  18. }
  19. void calcVertex();
  20. bool contains(const QPointF &point);
  21. QPointF m_pos;
  22. QSizeF m_size;
  23. float m_angle;
  24. QPointF m_unitVecX;
  25. QPointF m_unitVecY;
  26. QPointF m_vertex[8];
  27. bool m_selected;
  28. };
  29. extern std::vector<Component> g_m_components;
  30. #endif // COMPONENT_H

componentlayer.h

  1. #ifndef COMPONENTLAYER_H
  2. #define COMPONENTLAYER_H
  3. #include "qk2dview.h"
  4. #include "component.h"
  5. class ComponentLayer : public Qk2dViewLayer
  6. {
  7. public:
  8. ComponentLayer();
  9. void mousePressEvent(QMouseEvent *event) override;
  10. void mouseReleaseEvent(QMouseEvent *event) override;
  11. void mouseMoveEvent(QMouseEvent *event) override;
  12. void mouseDoubleClickEvent(QMouseEvent *event) override;
  13. private:
  14. void draw_ex(QPainter &painter) override;
  15. private:
  16. Component* m_component;
  17. int m_hitVertex;
  18. };
  19. #endif // COMPONENTLAYER_H

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include "qk2dview.h"
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class MainWindow; }
  7. QT_END_NAMESPACE
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. MainWindow(QWidget *parent = nullptr);
  13. ~MainWindow();
  14. private:
  15. Ui::MainWindow *ui;
  16. Qk2dView m_view;
  17. };
  18. #endif // MAINWINDOW_H

qk2dview.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include "qk2dview.h"
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class MainWindow; }
  7. QT_END_NAMESPACE
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. MainWindow(QWidget *parent = nullptr);
  13. ~MainWindow();
  14. private:
  15. Ui::MainWindow *ui;
  16. Qk2dView m_view;
  17. };
  18. #endif // MAINWINDOW_H

recipelayer.h

  1. #ifndef RECIPELAYER_H
  2. #define RECIPELAYER_H
  3. #include "qk2dview.h"
  4. class RecipeLayer : public Qk2dViewLayer
  5. {
  6. public:
  7. RecipeLayer();
  8. void mousePressEvent(QMouseEvent *event) override;
  9. void mouseReleaseEvent(QMouseEvent *event) override;
  10. void mouseMoveEvent(QMouseEvent *event) override;
  11. private:
  12. void draw_ex(QPainter &painter) override;
  13. private:
  14. bool m_selecting;
  15. QPointF m_selectStartPos;
  16. QPointF m_selectEndPos;
  17. };
  18. #endif // RECIPELAYER_H

resultlayer.h

  1. #ifndef RESULTLAYER_H
  2. #define RESULTLAYER_H
  3. #include "qk2dview.h"
  4. class ResultLayer : public Qk2dViewLayer
  5. {
  6. public:
  7. ResultLayer();
  8. void setText(const QString& text, bool status);
  9. private:
  10. void draw_ex(QPainter &painter) override;
  11. private:
  12. QString m_text;
  13. bool m_status;
  14. };
  15. #endif // RESULTLAYER_H


下面是源文件:

boardlayer.cpp

  1. #include "boardlayer.h"
  2. #include <QPointF>
  3. #include <QPainter>
  4. #include <QDebug>
  5. BoardLayer::BoardLayer()
  6. : Qk2dViewLayer()
  7. , m_image("D:/testImage/0015.bmp")
  8. , m_origin(650, 50) // origin at (650, 50)设置图片起始位置
  9. , m_pixelSize(0.03) // 30 um / pixel收放比例
  10. , m_worldRect()
  11. , m_imgToWorld()
  12. , m_worldToImg()
  13. {
  14. calcTransform();
  15. }
  16. void BoardLayer::draw_ex(QPainter &painter)
  17. {
  18. QRectF rect = m_view->worldToWin().mapRect(m_worldRect);
  19. painter.drawImage(rect, m_image);
  20. }
  21. void BoardLayer::calcTransform()
  22. {
  23. double trans_x = m_origin.x() - m_image.width()*m_pixelSize;
  24. double trans_y = m_origin.y() + m_image.height()*m_pixelSize;
  25. m_imgToWorld = QTransform::fromScale(m_pixelSize, -m_pixelSize);//缩放
  26. m_imgToWorld *= QTransform::fromTranslate(trans_x, trans_y);//平移
  27. m_worldToImg = m_imgToWorld.inverted();//反转
  28. m_worldRect = m_imgToWorld.mapRect(m_image.rect()).normalized();
  29. }

component.cpp

  1. #include "component.h"
  2. const static double PI = 3.1415926;
  3. std::vector<Component> g_m_components = {
  4. Component(QPointF(393.5, 198), QSizeF(33, 33), 45),
  5. Component(QPointF(562.5, 198), QSizeF(33, 33)),
  6. Component(QPointF(398.5, 140), QSizeF(23, 23), 120),
  7. Component(QPointF(567, 140), QSizeF(23, 23))
  8. };
  9. void Component::calcVertex()
  10. {
  11. double radAngle = m_angle * PI / 180;
  12. m_unitVecX = QPointF(cos(radAngle), sin(radAngle));
  13. m_unitVecY = QPointF(-sin(radAngle), cos(radAngle));
  14. QPointF vecx = m_unitVecX*m_size.width()/2;
  15. QPointF vecY = m_unitVecY*m_size.height()/2;
  16. m_vertex[0] = m_pos - vecx - vecY;
  17. m_vertex[1] = m_pos - vecY;
  18. m_vertex[2] = m_pos + vecx - vecY;
  19. m_vertex[3] = m_pos + vecx;
  20. m_vertex[4] = m_pos + vecx + vecY;
  21. m_vertex[5] = m_pos + vecY;
  22. m_vertex[6] = m_pos - vecx + vecY;
  23. m_vertex[7] = m_pos - vecx;
  24. }
  25. bool Component::contains(const QPointF &point)
  26. {
  27. bool result(false);
  28. int i = 0, j = 3;
  29. for(; i < 4; j = i++)
  30. {
  31. if(((m_vertex[2*i].y()>point.y()) != (m_vertex[2*j].y()>point.y())) &&
  32. (point.x() < (m_vertex[2*j].x()-m_vertex[2*i].x()) * (point.y()-m_vertex[2*i].y())
  33. / (m_vertex[2*j].y()-m_vertex[2*i].y()) + m_vertex[2*i].x()))
  34. {
  35. result = !result;
  36. }
  37. }
  38. return result;
  39. }

componentlayer.cpp

  1. #include "componentlayer.h"
  2. ComponentLayer::ComponentLayer()
  3. : m_component()
  4. , m_hitVertex(-1)
  5. {
  6. }
  7. void ComponentLayer::mousePressEvent(QMouseEvent *event)
  8. {
  9. if(m_component)
  10. {
  11. event->accept();
  12. QPointF pos = m_view->winToWorld().map(event->localPos());
  13. // control points hit test
  14. m_hitVertex = -1;
  15. QPointF dist = pos - m_component->m_pos;
  16. if(fabs(dist.x()) <= 1 && fabs(dist.y()) <= 1)
  17. {
  18. m_hitVertex = 8;
  19. return;
  20. }
  21. for(int i = 0; i < 8; ++i)
  22. {
  23. QPointF dist = pos - m_component->m_vertex[i];
  24. if(fabs(dist.x()) <= 1 && fabs(dist.y()) <= 1)
  25. {
  26. m_hitVertex = i;
  27. return; // hit one of the control posints
  28. }
  29. }
  30. }
  31. }
  32. void ComponentLayer::mouseReleaseEvent(QMouseEvent *event)
  33. {
  34. if(m_component) // clear ROI editing hit vertex
  35. {
  36. event->accept();
  37. if(m_hitVertex != -1)
  38. {
  39. m_hitVertex = -1;
  40. }
  41. }
  42. }
  43. void ComponentLayer::mouseMoveEvent(QMouseEvent *event)
  44. {
  45. if(m_component) // editing component ROI
  46. {
  47. event->accept();
  48. if(m_hitVertex != -1)
  49. {
  50. QPointF pos = m_view->winToWorld().map(event->localPos());
  51. QPointF vec = pos - m_component->m_pos;
  52. switch(m_hitVertex)
  53. {
  54. case 0:
  55. case 2:
  56. case 4:
  57. case 6:
  58. {
  59. double width = fabs(QPointF::dotProduct(vec, m_component->m_unitVecX)) * 2;
  60. double height = fabs(QPointF::dotProduct(vec, m_component->m_unitVecY)) * 2;
  61. m_component->m_size.setWidth(width);
  62. m_component->m_size.setHeight(height);
  63. }break;
  64. case 1:
  65. case 5:
  66. {
  67. double height = fabs(QPointF::dotProduct(vec, m_component->m_unitVecY)) * 2;
  68. m_component->m_size.setHeight(height);
  69. }break;
  70. case 3:
  71. case 7:
  72. {
  73. double width = fabs(QPointF::dotProduct(vec, m_component->m_unitVecX)) * 2;
  74. m_component->m_size.setWidth(width);
  75. }break;
  76. case 8:
  77. {
  78. m_component->m_pos = pos;
  79. }break;
  80. }
  81. m_component->calcVertex();
  82. m_view->update();
  83. }
  84. }
  85. }
  86. void ComponentLayer::mouseDoubleClickEvent(QMouseEvent *event)
  87. {
  88. if(event->button() == Qt::LeftButton)
  89. {
  90. QPointF pos = m_view->winToWorld().map(event->localPos());
  91. m_component = nullptr;
  92. for(auto & compnent : g_m_components)
  93. {
  94. if(compnent.contains(pos))
  95. {
  96. m_component = &compnent;
  97. event->accept();
  98. break;
  99. }
  100. }
  101. m_view->update();
  102. }
  103. }
  104. void ComponentLayer::draw_ex(QPainter &painter)
  105. {
  106. if(m_component)
  107. {
  108. QPen pen;
  109. pen.setWidth(2);
  110. pen.setCosmetic(true);
  111. pen.setColor(Qt::yellow);
  112. painter.setPen(pen);
  113. painter.setTransform(m_view->worldToWin());
  114. // draw roi rect
  115. painter.drawLine(m_component->m_vertex[0], m_component->m_vertex[2]);
  116. painter.drawLine(m_component->m_vertex[2], m_component->m_vertex[4]);
  117. painter.drawLine(m_component->m_vertex[4], m_component->m_vertex[6]);
  118. painter.drawLine(m_component->m_vertex[6], m_component->m_vertex[0]);
  119. // draw polarity
  120. painter.drawLine(m_component->m_vertex[1] + 0.3*(m_component->m_vertex[5] - m_component->m_vertex[1]),
  121. m_component->m_vertex[0] + 0.3*(m_component->m_vertex[2]-m_component->m_vertex[0]));
  122. painter.drawLine(m_component->m_vertex[1] + 0.3*(m_component->m_vertex[5] - m_component->m_vertex[1]),
  123. m_component->m_vertex[0] + 0.7*(m_component->m_vertex[2]-m_component->m_vertex[0]));
  124. painter.setBrush(Qt::yellow);
  125. for(int i = 0; i < 8; ++i)
  126. {
  127. painter.drawEllipse(m_component->m_vertex[i], 0.5, 0.5);
  128. }
  129. painter.drawLine(m_component->m_pos-QPointF(0.5, 0), m_component->m_pos+QPointF(0.5, 0));
  130. painter.drawLine(m_component->m_pos-QPointF(0, 0.5), m_component->m_pos+QPointF(0, 0.5));
  131. }
  132. }

main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MainWindow w;
  7. w.show();
  8. return a.exec();
  9. }

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "boardlayer.h"
  4. #include "recipelayer.h"
  5. #include "componentlayer.h"
  6. #include "resultlayer.h"
  7. MainWindow::MainWindow(QWidget *parent)
  8. : QMainWindow(parent)
  9. , ui(new Ui::MainWindow)
  10. , m_view(this)
  11. {
  12. ui->setupUi(this);
  13. setCentralWidget(&m_view);
  14. std::shared_ptr<BoardLayer> boardLayer = std::make_shared<BoardLayer>();
  15. m_view.setDrawer(Qk2dView::BoardLayer, boardLayer);
  16. // std::shared_ptr<RecipeLayer> recipeLayer = std::make_shared<RecipeLayer>();
  17. // m_view.setDrawer(Qk2dView::RecipeLayer, recipeLayer);
  18. // std::shared_ptr<ComponentLayer> compLayer = std::make_shared<ComponentLayer>();
  19. // m_view.setDrawer(Qk2dView::ComponentLayer, compLayer);
  20. // std::shared_ptr<ResultLayer> resultLayer = std::make_shared<ResultLayer>();
  21. // m_view.setDrawer(Qk2dView::ResultLayer, resultLayer);
  22. // resultLayer->setText("OK", true);
  23. // resultLayer->setVisiable(false);
  24. }
  25. MainWindow::~MainWindow()
  26. {
  27. delete ui;
  28. }

qk2dview.cpp

  1. #include "qk2dview.h"
  2. #include <QWheelEvent>
  3. #include <QPainter>
  4. #include <QTransform>
  5. #include <QDebug>
  6. Qk2dViewLayer::Qk2dViewLayer()
  7. : m_view(nullptr)
  8. , m_visiable(true)
  9. {
  10. }
  11. void Qk2dViewLayer::setView(Qk2dView *view)
  12. {
  13. m_view = view;
  14. }
  15. void Qk2dViewLayer::setVisiable(bool visiable)
  16. {
  17. m_visiable = visiable;
  18. }
  19. bool Qk2dViewLayer::visiable() const
  20. {
  21. return m_visiable;
  22. }
  23. void Qk2dViewLayer::draw(QPainter &painter)
  24. {
  25. painter.save();
  26. draw_ex(painter);
  27. painter.restore();
  28. }
  29. Qk2dView::Qk2dView(QWidget *parent)
  30. : QWidget(parent)
  31. , m_layers()
  32. , m_roiRect()
  33. , m_winToWorld()
  34. , m_worldToWin()
  35. , m_isDragging(false)
  36. , m_dragStartPos()
  37. {
  38. m_layers.fill(nullptr);
  39. setMouseTracking(true);
  40. setRoiRect(QRectF(270, 30, 410, 290));
  41. }
  42. void Qk2dView::setRoiRect(const QRectF &roi)
  43. {
  44. m_roiRect = roi.normalized();
  45. calcTransform();
  46. update();
  47. }
  48. void Qk2dView::setDrawer(Layer layer, std::shared_ptr<Qk2dViewLayer> drawer)
  49. {
  50. if(m_layers[layer])
  51. {
  52. m_layers[layer]->setView(nullptr);
  53. }
  54. if(drawer)
  55. {
  56. drawer->setView(this);
  57. }
  58. m_layers[layer] = drawer;
  59. update();
  60. }
  61. std::shared_ptr<Qk2dViewLayer> Qk2dView::getDrawer(Layer layer)
  62. {
  63. return m_layers[layer];
  64. }
  65. void Qk2dView::setDrawerVisiable(Layer layer, bool visiable)
  66. {
  67. if(m_layers[layer])
  68. {
  69. m_layers[layer]->setVisiable(visiable);
  70. update();
  71. }
  72. }
  73. void Qk2dView::mousePressEvent(QMouseEvent *event)
  74. {
  75. if(event->type() == QEvent::MouseButtonDblClick)
  76. {
  77. event->setAccepted(false);
  78. return;
  79. }
  80. else
  81. {
  82. QWidget::mousePressEvent(event);
  83. if(event->button() == Qt::MidButton)
  84. {
  85. m_isDragging = true;
  86. m_dragStartPos = event->localPos();
  87. }
  88. for(auto it = m_layers.rbegin(); it != m_layers.rend(); ++it)
  89. {
  90. if((*it) && (*it)->visiable())
  91. {
  92. (*it)->mousePressEvent(event);
  93. if(event->isAccepted())
  94. {
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. void Qk2dView::mouseReleaseEvent(QMouseEvent *event)
  102. {
  103. QWidget::mouseReleaseEvent(event);
  104. if(m_isDragging)
  105. {
  106. m_isDragging = false;
  107. }
  108. for(auto it = m_layers.rbegin(); it != m_layers.rend(); ++it)
  109. {
  110. if((*it) && (*it)->visiable())
  111. {
  112. (*it)->mouseReleaseEvent(event);
  113. if(event->isAccepted())
  114. {
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. void Qk2dView::mouseMoveEvent(QMouseEvent *event)
  121. {
  122. QWidget::mouseMoveEvent(event);
  123. if(m_isDragging)
  124. {
  125. QPointF pos = event->localPos();
  126. QPointF trans = pos - m_dragStartPos;
  127. m_dragStartPos = pos;
  128. setRoiRect(m_roiRect.translated(-trans.x() * m_winToWorld.m11(), -trans.y() * m_winToWorld.m22()));
  129. }
  130. for(auto it = m_layers.rbegin(); it != m_layers.rend(); ++it)
  131. {
  132. if((*it) && (*it)->visiable())
  133. {
  134. (*it)->mouseMoveEvent(event);
  135. if(event->isAccepted())
  136. {
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. void Qk2dView::mouseDoubleClickEvent(QMouseEvent *event)
  143. {
  144. QWidget::mouseDoubleClickEvent(event);
  145. for(auto it = m_layers.rbegin(); it != m_layers.rend(); ++it)
  146. {
  147. if((*it) && (*it)->visiable())
  148. {
  149. (*it)->mouseDoubleClickEvent(event);
  150. if(event->isAccepted())
  151. {
  152. break;
  153. }
  154. }
  155. }
  156. }
  157. void Qk2dView::paintEvent(QPaintEvent *event)
  158. {
  159. QWidget::paintEvent(event);
  160. QPainter painter(this);
  161. painter.setRenderHint(QPainter::Antialiasing, true);
  162. painter.fillRect(rect(), QColor(20, 20, 20));
  163. for(auto it = m_layers.begin(); it != m_layers.end(); ++it)
  164. {
  165. if(*it && (*it)->visiable())
  166. {
  167. (*it)->draw(painter);
  168. }
  169. }
  170. }
  171. void Qk2dView::wheelEvent(QWheelEvent *event)
  172. {
  173. QWidget::wheelEvent(event);
  174. int numDegrees = event->angleDelta().y() / 8;
  175. double zoomFactor = 1 - numDegrees / 15.0 * 0.1;
  176. QRectF viewRect = m_winToWorld.mapRect(QRectF(rect()));
  177. QPointF pos = m_winToWorld.map(event->position());
  178. double top = pos.y() + (viewRect.top() - pos.y()) * zoomFactor;
  179. double bottom = pos.y() + (viewRect.bottom() - pos.y()) * zoomFactor;
  180. double left = pos.x() + (viewRect.left() - pos.x()) * zoomFactor;
  181. double right = pos.x() + (viewRect.right() - pos.x()) * zoomFactor;
  182. QRectF rect(left, top, right-left, bottom-top);
  183. setRoiRect(rect);
  184. }
  185. void Qk2dView::resizeEvent(QResizeEvent *event)
  186. {
  187. QWidget::resizeEvent(event);
  188. calcTransform();
  189. }
  190. void Qk2dView::calcTransform()
  191. {
  192. double w2hRatio = m_roiRect.width() / m_roiRect.height();
  193. double w(width()), h(height());
  194. double tempWidth = height() * w2hRatio;
  195. QRectF windowRect; // in window coordinate system
  196. if(tempWidth <= width())
  197. {
  198. w = tempWidth;
  199. windowRect = QRectF((width()-w)/2.0, 0, w, h);
  200. }
  201. else
  202. {
  203. h = width() / w2hRatio;
  204. windowRect = QRectF(0, (height()-h) / 2.0, w, h);
  205. }
  206. double scale_x, scale_y, trans_x, trans_y;
  207. scale_x = windowRect.width()/m_roiRect.width();
  208. scale_y = -windowRect.height()/m_roiRect.height();
  209. trans_x = windowRect.left() - scale_x*m_roiRect.left();
  210. trans_y = windowRect.bottom() - scale_y*m_roiRect.top();
  211. m_worldToWin = QTransform::fromScale(scale_x, scale_y);
  212. m_worldToWin *= QTransform::fromTranslate(trans_x, trans_y);
  213. m_winToWorld = m_worldToWin.inverted();
  214. }

recipelayer.cpp

  1. #include "recipelayer.h"
  2. #include "component.h"
  3. #include <QDebug>
  4. RecipeLayer::RecipeLayer()
  5. : Qk2dViewLayer()
  6. , m_selecting(false)
  7. , m_selectStartPos()
  8. , m_selectEndPos()
  9. {
  10. }
  11. void RecipeLayer::mousePressEvent(QMouseEvent *event)
  12. {
  13. if(event->button() == Qt::LeftButton)//判断是否按下左键
  14. {
  15. QPointF pos = m_view->winToWorld().map(event->localPos());
  16. m_selecting = true;
  17. m_selectStartPos = pos;
  18. m_selectEndPos = pos;
  19. }
  20. }
  21. void RecipeLayer::mouseReleaseEvent(QMouseEvent *event)
  22. {
  23. if(event->button() == Qt::LeftButton)
  24. {
  25. if(m_selecting) // selecting
  26. {
  27. m_selecting = false;
  28. QRectF rect = QRectF(m_selectStartPos, m_selectEndPos).normalized();
  29. for(auto & compnent : g_m_components)
  30. {
  31. if(rect.contains(compnent.m_pos))
  32. {
  33. compnent.m_selected = true;
  34. }
  35. else if(!(event->modifiers() & Qt::ControlModifier))
  36. {
  37. compnent.m_selected = false;
  38. }
  39. }
  40. }
  41. }
  42. m_view->update();
  43. }
  44. void RecipeLayer::mouseMoveEvent(QMouseEvent *event)
  45. {
  46. QPointF pos = m_view->winToWorld().map(event->localPos());
  47. if(m_selecting)
  48. {
  49. m_selectEndPos = pos;
  50. }
  51. m_view->update();
  52. }
  53. void RecipeLayer::draw_ex(QPainter &painter)
  54. {
  55. QPen pen;
  56. pen.setWidth(2);
  57. pen.setCosmetic(true);
  58. painter.setTransform(m_view->worldToWin());
  59. for(const auto & compnent : g_m_components)
  60. {
  61. if(compnent.m_selected)
  62. {
  63. pen.setColor(Qt::green);
  64. }
  65. else
  66. {
  67. pen.setColor(Qt::blue);
  68. }
  69. painter.setPen(pen);
  70. // draw roi rect
  71. painter.drawLine(compnent.m_vertex[0], compnent.m_vertex[2]);
  72. painter.drawLine(compnent.m_vertex[2], compnent.m_vertex[4]);
  73. painter.drawLine(compnent.m_vertex[4], compnent.m_vertex[6]);
  74. painter.drawLine(compnent.m_vertex[6], compnent.m_vertex[0]);
  75. // draw polarity
  76. painter.drawLine(compnent.m_vertex[1] + 0.3*(compnent.m_vertex[5] - compnent.m_vertex[1]),
  77. compnent.m_vertex[0] + 0.3*(compnent.m_vertex[2]-compnent.m_vertex[0]));
  78. painter.drawLine(compnent.m_vertex[1] + 0.3*(compnent.m_vertex[5] - compnent.m_vertex[1]),
  79. compnent.m_vertex[0] + 0.7*(compnent.m_vertex[2]-compnent.m_vertex[0]));
  80. }
  81. if(m_selecting)
  82. {
  83. pen.setColor(Qt::yellow);
  84. painter.setPen(pen);
  85. painter.setBrush(QColor(255, 255, 255, 100));
  86. painter.drawRect(QRectF(m_selectStartPos, m_selectEndPos));
  87. }
  88. }

resultlayer.cpp

  1. #include "resultlayer.h"
  2. ResultLayer::ResultLayer()
  3. : Qk2dViewLayer()
  4. , m_text("Good")
  5. , m_status(true)
  6. {
  7. }
  8. void ResultLayer::setText(const QString& text, bool status)
  9. {
  10. m_text = text;
  11. m_status = status;
  12. if(m_view)
  13. {
  14. m_view->update();
  15. }
  16. }
  17. void ResultLayer::draw_ex(QPainter &painter)
  18. {
  19. QPen pen;
  20. pen.setWidth(2);
  21. pen.setCosmetic(true);
  22. QBrush brush;
  23. QColor color;
  24. if(m_status)
  25. {
  26. color = QColor(0, 255, 0, 150);
  27. }
  28. else
  29. {
  30. color = QColor(255, 0, 0, 150);
  31. }
  32. pen.setColor(color);
  33. brush.setColor(color);
  34. painter.setPen(pen);
  35. painter.setBrush(brush);
  36. QRectF winRect = m_view->rect();
  37. QFont font = painter.font();
  38. font.setBold(true);
  39. font.setPixelSize(std::min(winRect.width(), winRect.height())/1.5);
  40. painter.setFont(font);
  41. painter.drawText(winRect, Qt::AlignHCenter | Qt::AlignVCenter, m_text);
  42. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/297469
推荐阅读
相关标签
  

闽ICP备14008679号