Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtGui>
- class MyGraphicsScene : public QGraphicsScene
- {
- public:
- MyGraphicsScene(QWidget *parent = 0) : QGraphicsScene(parent), pressed(false) {}
- void inputPress(QPointF pos)
- {
- pressed = true;
- path = new QPainterPath(pos);
- }
- void inputMove(QPointF pos, qreal pressure = 1.0)
- {
- if (pressed) {
- path->lineTo(pos);
- QGraphicsPathItem *pathItem = new QGraphicsPathItem(*path);
- pathItem->setPen(QPen(Qt::SolidPattern, 3 * pressure, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
- addItem(pathItem);
- delete path;
- path = new QPainterPath(pos);
- }
- }
- void inputRelease(QPointF pos)
- {
- Q_UNUSED(pos);
- delete path;
- pressed = false;
- }
- private:
- QPainterPath *path;
- bool pressed;
- };
- class MyGraphicsView : public QGraphicsView
- {
- public:
- MyGraphicsView(MyGraphicsScene *scene, QWidget * parent = 0) : QGraphicsView(scene, parent) {}
- MyGraphicsScene* scene()
- {
- return static_cast<MyGraphicsScene*>(QGraphicsView::scene());
- }
- void keyPressEvent(QKeyEvent *event)
- {
- if (event->key() == Qt::Key_K) {
- scene()->clear();
- event->accept();
- } else {
- event->ignore();
- }
- }
- void tabletEvent(QTabletEvent *event)
- {
- bool accepted = true;
- switch (event->type())
- {
- case QEvent::TabletPress:
- scene()->inputPress(event->hiResGlobalPos() + mapFromGlobal(QPoint(0, 0)));
- break;
- case QEvent::TabletMove:
- scene()->inputMove(event->hiResGlobalPos() + mapFromGlobal(QPoint(0, 0)), event->pressure());
- break;
- case QEvent::TabletRelease:
- scene()->inputRelease(event->hiResGlobalPos() + mapFromGlobal(QPoint(0, 0)));
- break;
- default:
- accepted = false;
- break;
- }
- event->setAccepted(accepted);
- }
- void mousePressEvent(QMouseEvent *event)
- {
- scene()->inputPress(mapToScene(event->pos()));
- }
- void mouseMoveEvent(QMouseEvent *event)
- {
- scene()->inputMove(mapToScene(event->pos()));
- }
- void mouseReleaseEvent(QMouseEvent *event)
- {
- scene()->inputRelease(mapToScene(event->pos()));
- }
- };
- class OverlayWidget : public QWidget
- {
- public:
- OverlayWidget(QWidget *parent = 0) : QWidget(parent)
- {
- setAttribute(Qt::WA_NoMousePropagation);
- }
- void paintEvent(QPaintEvent *)
- {
- QPainter painter(this);
- painter.setPen(Qt::NoPen);
- painter.setBrush(QColor(255, 0, 0, 127));
- painter.drawRect(0, 0, width(), height());
- }
- };
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- const int width = 800;
- const int height = 600;
- MyGraphicsScene scene;
- MyGraphicsView view(&scene);
- QRect screenRect = app.desktop()->availableGeometry();
- view.setGeometry((screenRect.width() - width) / 2, (screenRect.height() - height) / 2, width, height);
- view.setSceneRect(0, 0, width, height);
- view.setRenderHints(QPainter::Antialiasing);
- view.show();
- QWidget *overlay = new OverlayWidget(&view);
- overlay->setGeometry(view.width() / 2, 0, view.width() / 2, view.height());
- overlay->show();
- return app.exec();
- }
Add Comment
Please, Sign In to add comment