summaryrefslogtreecommitdiffstats
path: root/examples/widgets/widgets
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2017-05-03 13:55:48 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2017-05-08 17:49:12 +0000
commit3ea4f54db7351352146161a54e45f00ddaba8715 (patch)
tree17a70ad5e329da6b70e2a5d6b517fcb91525dc4b /examples/widgets/widgets
parent7124096f2bd722c05ceb3e863cd92ddebb886e13 (diff)
tablet example: make smoother strokes with the rotation stylus
Each stroke is a polygon (quadrilateral) connecting the previous tablet event point to the current one. Previously we used the same width for both ends of the stroke, so rapid changes in pressure caused steps in the stroke width. Now it is tapered from one end to the other. Change-Id: I909a2e85334a1a6e20fd28ee4babf6825da36612 Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'examples/widgets/widgets')
-rw-r--r--examples/widgets/widgets/tablet/tabletcanvas.cpp12
-rw-r--r--examples/widgets/widgets/tablet/tabletcanvas.h2
2 files changed, 12 insertions, 2 deletions
diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp
index 03e05e42f6..54e8544a3d 100644
--- a/examples/widgets/widgets/tablet/tabletcanvas.cpp
+++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp
@@ -111,6 +111,7 @@ void TabletCanvas::tabletEvent(QTabletEvent *event)
if (!m_deviceDown) {
m_deviceDown = true;
lastPoint.pos = event->posF();
+ lastPoint.pressure = event->pressure();
lastPoint.rotation = event->rotation();
}
break;
@@ -122,6 +123,7 @@ void TabletCanvas::tabletEvent(QTabletEvent *event)
QPainter painter(&m_pixmap);
paintPixmap(painter, event);
lastPoint.pos = event->posF();
+ lastPoint.pressure = event->pressure();
lastPoint.rotation = event->rotation();
}
break;
@@ -171,11 +173,12 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event)
painter.setPen(Qt::NoPen);
painter.setBrush(m_brush);
QPolygonF poly;
- qreal halfWidth = m_pen.widthF();
+ 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;
+ halfWidth = m_pen.widthF();
brushAdjust = QPointF(qSin(qDegreesToRadians(event->rotation())) * halfWidth,
qCos(qDegreesToRadians(event->rotation())) * halfWidth);
poly << event->posF() - brushAdjust;
@@ -215,6 +218,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 +268,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);
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;
};