diff options
Diffstat (limited to 'examples/widgets/widgets/tablet')
-rw-r--r-- | examples/widgets/widgets/tablet/mainwindow.cpp | 10 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/mainwindow.h | 1 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletcanvas.cpp | 52 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletcanvas.h | 2 |
4 files changed, 45 insertions, 20 deletions
diff --git a/examples/widgets/widgets/tablet/mainwindow.cpp b/examples/widgets/widgets/tablet/mainwindow.cpp index 0d63ac316b..feae16dd83 100644 --- a/examples/widgets/widgets/tablet/mainwindow.cpp +++ b/examples/widgets/widgets/tablet/mainwindow.cpp @@ -60,6 +60,7 @@ MainWindow::MainWindow(TabletCanvas *canvas) createMenus(); setWindowTitle(tr("Tablet Example")); setCentralWidget(m_canvas); + QCoreApplication::setAttribute(Qt::AA_CompressHighFrequencyEvents); } //! [0] @@ -97,6 +98,11 @@ void MainWindow::setSaturationValuator(QAction *action) } //! [4] +void MainWindow::setEventCompression(bool compress) +{ + QCoreApplication::setAttribute(Qt::AA_CompressTabletEvents, compress); +} + //! [5] void MainWindow::save() { @@ -220,6 +226,10 @@ void MainWindow::createMenus() connect(colorSaturationGroup, &QActionGroup::triggered, this, &MainWindow::setSaturationValuator); + QAction *compressAction = tabletMenu->addAction(tr("Co&mpress events")); + compressAction->setCheckable(true); + connect(compressAction, &QAction::toggled, this, &MainWindow::setEventCompression); + QMenu *helpMenu = menuBar()->addMenu("&Help"); helpMenu->addAction(tr("A&bout"), this, &MainWindow::about); helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); diff --git a/examples/widgets/widgets/tablet/mainwindow.h b/examples/widgets/widgets/tablet/mainwindow.h index 68af1f1687..4b99324f04 100644 --- a/examples/widgets/widgets/tablet/mainwindow.h +++ b/examples/widgets/widgets/tablet/mainwindow.h @@ -71,6 +71,7 @@ private slots: void setAlphaValuator(QAction *action); void setLineWidthValuator(QAction *action); void setSaturationValuator(QAction *action); + void setEventCompression(bool compress); void save(); void load(); void about(); diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp index 90b3222970..4b11568dfe 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.cpp +++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp @@ -65,21 +65,9 @@ TabletCanvas::TabletCanvas() , m_deviceDown(false) { resize(500, 500); - initPixmap(); setAutoFillBackground(true); setAttribute(Qt::WA_TabletTracking); } - -void TabletCanvas::initPixmap() -{ - QPixmap newPixmap = QPixmap(width(), height()); - newPixmap.fill(Qt::white); - QPainter painter(&newPixmap); - if (!m_pixmap.isNull()) - painter.drawPixmap(0, 0, m_pixmap); - painter.end(); - m_pixmap = newPixmap; -} //! [0] //! [1] @@ -105,23 +93,26 @@ bool TabletCanvas::loadImage(const QString &file) //! [3] void TabletCanvas::tabletEvent(QTabletEvent *event) { - switch (event->type()) { case QEvent::TabletPress: if (!m_deviceDown) { m_deviceDown = true; lastPoint.pos = event->posF(); + lastPoint.pressure = event->pressure(); lastPoint.rotation = event->rotation(); } break; case QEvent::TabletMove: +#ifndef Q_OS_IOS if (event->device() == QTabletEvent::RotationStylus) updateCursor(event); +#endif if (m_deviceDown) { updateBrush(event); QPainter painter(&m_pixmap); paintPixmap(painter, event); lastPoint.pos = event->posF(); + lastPoint.pressure = event->pressure(); lastPoint.rotation = event->rotation(); } break; @@ -138,8 +129,23 @@ void TabletCanvas::tabletEvent(QTabletEvent *event) //! [3] //! [4] +void TabletCanvas::initPixmap() +{ + qreal dpr = devicePixelRatioF(); + QPixmap newPixmap = QPixmap(width() * dpr, height() * dpr); + newPixmap.setDevicePixelRatio(dpr); + newPixmap.fill(Qt::white); + QPainter painter(&newPixmap); + if (!m_pixmap.isNull()) + painter.drawPixmap(0, 0, m_pixmap); + painter.end(); + m_pixmap = newPixmap; +} + void TabletCanvas::paintEvent(QPaintEvent *) { + if (m_pixmap.isNull()) + initPixmap(); QPainter painter(this); painter.drawPixmap(0, 0, m_pixmap); } @@ -171,13 +177,14 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) painter.setPen(Qt::NoPen); painter.setBrush(m_brush); QPolygonF poly; - qreal halfWidth = m_pen.widthF(); - QPointF brushAdjust(qSin(qDegreesToRadians(lastPoint.rotation)) * halfWidth, - qCos(qDegreesToRadians(lastPoint.rotation)) * halfWidth); + qreal halfWidth = pressureToWidth(lastPoint.pressure); + QPointF brushAdjust(qSin(qDegreesToRadians(-lastPoint.rotation)) * halfWidth, + qCos(qDegreesToRadians(-lastPoint.rotation)) * halfWidth); poly << lastPoint.pos + brushAdjust; poly << lastPoint.pos - brushAdjust; - brushAdjust = QPointF(qSin(qDegreesToRadians(event->rotation())) * halfWidth, - qCos(qDegreesToRadians(event->rotation())) * halfWidth); + halfWidth = m_pen.widthF(); + brushAdjust = QPointF(qSin(qDegreesToRadians(-event->rotation())) * halfWidth, + qCos(qDegreesToRadians(-event->rotation())) * halfWidth); poly << event->posF() - brushAdjust; poly << event->posF() + brushAdjust; painter.drawConvexPolygon(poly); @@ -215,6 +222,11 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) } //! [5] +qreal TabletCanvas::pressureToWidth(qreal pressure) +{ + return pressure * 10 + 1; +} + //! [7] void TabletCanvas::updateBrush(const QTabletEvent *event) { @@ -260,7 +272,7 @@ void TabletCanvas::updateBrush(const QTabletEvent *event) //! [9] //! [10] switch (m_lineWidthValuator) { case PressureValuator: - m_pen.setWidthF(event->pressure() * 10 + 1); + m_pen.setWidthF(pressureToWidth(event->pressure())); break; case TiltValuator: m_pen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12); @@ -305,7 +317,7 @@ void TabletCanvas::updateCursor(const QTabletEvent *event) QPainter painter(&img); QTransform transform = painter.transform(); transform.translate(16, 16); - transform.rotate(-event->rotation()); + transform.rotate(event->rotation()); painter.setTransform(transform); painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); painter.drawImage(-24, -24, origImg); diff --git a/examples/widgets/widgets/tablet/tabletcanvas.h b/examples/widgets/widgets/tablet/tabletcanvas.h index 1784e05916..a1b31c65bf 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.h +++ b/examples/widgets/widgets/tablet/tabletcanvas.h @@ -103,6 +103,7 @@ private: void initPixmap(); void paintPixmap(QPainter &painter, QTabletEvent *event); Qt::BrushStyle brushPattern(qreal value); + qreal pressureToWidth(qreal pressure); void updateBrush(const QTabletEvent *event); void updateCursor(const QTabletEvent *event); @@ -117,6 +118,7 @@ private: struct Point { QPointF pos; + qreal pressure; qreal rotation; } lastPoint; }; |