Tablet example: update the cursor according to the tool

Change-Id: Ibbe530856bb833e465dd9fa1da5425c018fecc21
Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
This commit is contained in:
Shawn Rutledge 2015-07-27 17:58:03 +02:00 committed by Shawn Rutledge
parent 6a991f9cce
commit e026fdc4a7
9 changed files with 62 additions and 11 deletions

View File

@ -0,0 +1,8 @@
<RCC>
<qresource>
<file>images/cursor-airbrush.png</file>
<file>images/cursor-eraser.png</file>
<file>images/cursor-felt-marker.png</file>
<file>images/cursor-pencil.png</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 823 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1,12 +1,13 @@
QT += widgets QT += widgets
HEADERS = mainwindow.h \ HEADERS = mainwindow.h \
tabletcanvas.h \ tabletcanvas.h \
tabletapplication.h tabletapplication.h
SOURCES = mainwindow.cpp \ SOURCES = mainwindow.cpp \
main.cpp \ main.cpp \
tabletcanvas.cpp \ tabletcanvas.cpp \
tabletapplication.cpp tabletapplication.cpp
RESOURCES += images.qrc
# install # install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/tablet target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/tablet

View File

@ -47,8 +47,7 @@ bool TabletApplication::event(QEvent *event)
{ {
if (event->type() == QEvent::TabletEnterProximity || if (event->type() == QEvent::TabletEnterProximity ||
event->type() == QEvent::TabletLeaveProximity) { event->type() == QEvent::TabletLeaveProximity) {
myCanvas->setTabletDevice( myCanvas->setTabletDevice(static_cast<QTabletEvent *>(event));
static_cast<QTabletEvent *>(event)->device());
return true; return true;
} }
return QApplication::event(event); return QApplication::event(event);

View File

@ -103,6 +103,8 @@ void TabletCanvas::tabletEvent(QTabletEvent *event)
} }
break; break;
case QEvent::TabletMove: case QEvent::TabletMove:
if (event->device() == QTabletEvent::RotationStylus)
updateCursor(event);
if (deviceDown) { if (deviceDown) {
updateBrush(event); updateBrush(event);
QPainter painter(&pixmap); QPainter painter(&pixmap);
@ -201,7 +203,7 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event)
//! [5] //! [5]
//! [7] //! [7]
void TabletCanvas::updateBrush(QTabletEvent *event) void TabletCanvas::updateBrush(const QTabletEvent *event)
{ {
int hue, saturation, value, alpha; int hue, saturation, value, alpha;
myColor.getHsv(&hue, &saturation, &value, &alpha); myColor.getHsv(&hue, &saturation, &value, &alpha);
@ -266,6 +268,46 @@ void TabletCanvas::updateBrush(QTabletEvent *event)
} }
//! [11] //! [11]
void TabletCanvas::updateCursor(const QTabletEvent *event)
{
QCursor cursor;
if (event->type() != QEvent::TabletLeaveProximity) {
if (event->pointerType() == QTabletEvent::Eraser) {
cursor = QCursor(QPixmap(":/images/cursor-eraser.png"), 3, 28);
} else {
switch (event->device()) {
case QTabletEvent::Stylus:
cursor = QCursor(QPixmap(":/images/cursor-pencil.png"), 0, 0);
break;
case QTabletEvent::Airbrush:
cursor = QCursor(QPixmap(":/images/cursor-airbrush.png"), 3, 4);
break;
case QTabletEvent::RotationStylus: {
QImage origImg(QLatin1String(":/images/cursor-felt-marker.png"));
QImage img(32, 32, QImage::Format_ARGB32);
QColor solid = myColor;
solid.setAlpha(255);
img.fill(solid);
QPainter painter(&img);
QTransform transform = painter.transform();
transform.translate(16, 16);
transform.rotate(-event->rotation());
painter.setTransform(transform);
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
painter.drawImage(-24, -24, origImg);
painter.setCompositionMode(QPainter::CompositionMode_HardLight);
painter.drawImage(-24, -24, origImg);
painter.end();
cursor = QCursor(QPixmap::fromImage(img), 16, 16);
} break;
default:
break;
}
}
}
setCursor(cursor);
}
void TabletCanvas::resizeEvent(QResizeEvent *) void TabletCanvas::resizeEvent(QResizeEvent *)
{ {
initPixmap(); initPixmap();

View File

@ -80,8 +80,8 @@ public:
{ myColor = color; } { myColor = color; }
QColor color() const QColor color() const
{ return myColor; } { return myColor; }
void setTabletDevice(QTabletEvent::TabletDevice device) void setTabletDevice(QTabletEvent *event)
{ myTabletDevice = device; } { myTabletDevice = event->device(); updateCursor(event); }
int maximum(int a, int b) int maximum(int a, int b)
{ return a > b ? a : b; } { return a > b ? a : b; }
@ -94,7 +94,8 @@ private:
void initPixmap(); void initPixmap();
void paintPixmap(QPainter &painter, QTabletEvent *event); void paintPixmap(QPainter &painter, QTabletEvent *event);
Qt::BrushStyle brushPattern(qreal value); Qt::BrushStyle brushPattern(qreal value);
void updateBrush(QTabletEvent *event); void updateBrush(const QTabletEvent *event);
void updateCursor(const QTabletEvent *event);
AlphaChannelType alphaChannelType; AlphaChannelType alphaChannelType;
ColorSaturationType colorSaturationType; ColorSaturationType colorSaturationType;