Guest User

Untitled

a guest
Jun 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <QtGui>
  2.  
  3. class MyGraphicsScene : public QGraphicsScene
  4. {
  5. public:
  6. MyGraphicsScene(QWidget *parent = 0) : QGraphicsScene(parent), pressed(false) {}
  7.  
  8. void inputPress(QPointF pos)
  9. {
  10. pressed = true;
  11. path = new QPainterPath(pos);
  12. }
  13.  
  14. void inputMove(QPointF pos, qreal pressure = 1.0)
  15. {
  16. if (pressed) {
  17. path->lineTo(pos);
  18. QGraphicsPathItem *pathItem = new QGraphicsPathItem(*path);
  19. pathItem->setPen(QPen(Qt::SolidPattern, 3 * pressure, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
  20. addItem(pathItem);
  21.  
  22. delete path;
  23. path = new QPainterPath(pos);
  24. }
  25. }
  26.  
  27. void inputRelease(QPointF pos)
  28. {
  29. Q_UNUSED(pos);
  30. delete path;
  31. pressed = false;
  32. }
  33.  
  34. private:
  35. QPainterPath *path;
  36. bool pressed;
  37. };
  38.  
  39.  
  40. class MyGraphicsView : public QGraphicsView
  41. {
  42. public:
  43. MyGraphicsView(MyGraphicsScene *scene, QWidget * parent = 0) : QGraphicsView(scene, parent) {}
  44.  
  45. MyGraphicsScene* scene()
  46. {
  47. return static_cast<MyGraphicsScene*>(QGraphicsView::scene());
  48. }
  49.  
  50. void keyPressEvent(QKeyEvent *event)
  51. {
  52. if (event->key() == Qt::Key_K) {
  53. scene()->clear();
  54. event->accept();
  55. } else {
  56. event->ignore();
  57. }
  58. }
  59.  
  60. void tabletEvent(QTabletEvent *event)
  61. {
  62. bool accepted = true;
  63. switch (event->type())
  64. {
  65. case QEvent::TabletPress:
  66. scene()->inputPress(event->hiResGlobalPos() + mapFromGlobal(QPoint(0, 0)));
  67. break;
  68. case QEvent::TabletMove:
  69. scene()->inputMove(event->hiResGlobalPos() + mapFromGlobal(QPoint(0, 0)), event->pressure());
  70. break;
  71. case QEvent::TabletRelease:
  72. scene()->inputRelease(event->hiResGlobalPos() + mapFromGlobal(QPoint(0, 0)));
  73. break;
  74. default:
  75. accepted = false;
  76. break;
  77. }
  78. event->setAccepted(accepted);
  79. }
  80.  
  81. void mousePressEvent(QMouseEvent *event)
  82. {
  83. scene()->inputPress(mapToScene(event->pos()));
  84. }
  85.  
  86. void mouseMoveEvent(QMouseEvent *event)
  87. {
  88. scene()->inputMove(mapToScene(event->pos()));
  89. }
  90.  
  91. void mouseReleaseEvent(QMouseEvent *event)
  92. {
  93. scene()->inputRelease(mapToScene(event->pos()));
  94. }
  95. };
  96.  
  97.  
  98. class OverlayWidget : public QWidget
  99. {
  100. public:
  101. OverlayWidget(QWidget *parent = 0) : QWidget(parent)
  102. {
  103. setAttribute(Qt::WA_NoMousePropagation);
  104. }
  105.  
  106. void paintEvent(QPaintEvent *)
  107. {
  108. QPainter painter(this);
  109. painter.setPen(Qt::NoPen);
  110. painter.setBrush(QColor(255, 0, 0, 127));
  111. painter.drawRect(0, 0, width(), height());
  112. }
  113. };
  114.  
  115.  
  116. int main(int argc, char *argv[])
  117. {
  118. QApplication app(argc, argv);
  119.  
  120. const int width = 800;
  121. const int height = 600;
  122.  
  123. MyGraphicsScene scene;
  124. MyGraphicsView view(&scene);
  125. QRect screenRect = app.desktop()->availableGeometry();
  126. view.setGeometry((screenRect.width() - width) / 2, (screenRect.height() - height) / 2, width, height);
  127. view.setSceneRect(0, 0, width, height);
  128. view.setRenderHints(QPainter::Antialiasing);
  129. view.show();
  130.  
  131. QWidget *overlay = new OverlayWidget(&view);
  132. overlay->setGeometry(view.width() / 2, 0, view.width() / 2, view.height());
  133. overlay->show();
  134.  
  135. return app.exec();
  136. }
Add Comment
Please, Sign In to add comment