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:
parent
6a991f9cce
commit
e026fdc4a7
8
examples/widgets/widgets/tablet/images.qrc
Normal file
8
examples/widgets/widgets/tablet/images.qrc
Normal 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>
|
BIN
examples/widgets/widgets/tablet/images/cursor-airbrush.png
Normal file
BIN
examples/widgets/widgets/tablet/images/cursor-airbrush.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 823 B |
BIN
examples/widgets/widgets/tablet/images/cursor-eraser.png
Normal file
BIN
examples/widgets/widgets/tablet/images/cursor-eraser.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
BIN
examples/widgets/widgets/tablet/images/cursor-felt-marker.png
Normal file
BIN
examples/widgets/widgets/tablet/images/cursor-felt-marker.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 513 B |
BIN
examples/widgets/widgets/tablet/images/cursor-pencil.png
Normal file
BIN
examples/widgets/widgets/tablet/images/cursor-pencil.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
@ -1,12 +1,13 @@
|
||||
QT += widgets
|
||||
|
||||
HEADERS = mainwindow.h \
|
||||
tabletcanvas.h \
|
||||
tabletapplication.h
|
||||
tabletcanvas.h \
|
||||
tabletapplication.h
|
||||
SOURCES = mainwindow.cpp \
|
||||
main.cpp \
|
||||
tabletcanvas.cpp \
|
||||
tabletapplication.cpp
|
||||
main.cpp \
|
||||
tabletcanvas.cpp \
|
||||
tabletapplication.cpp
|
||||
RESOURCES += images.qrc
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/tablet
|
||||
|
@ -47,8 +47,7 @@ bool TabletApplication::event(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::TabletEnterProximity ||
|
||||
event->type() == QEvent::TabletLeaveProximity) {
|
||||
myCanvas->setTabletDevice(
|
||||
static_cast<QTabletEvent *>(event)->device());
|
||||
myCanvas->setTabletDevice(static_cast<QTabletEvent *>(event));
|
||||
return true;
|
||||
}
|
||||
return QApplication::event(event);
|
||||
|
@ -103,6 +103,8 @@ void TabletCanvas::tabletEvent(QTabletEvent *event)
|
||||
}
|
||||
break;
|
||||
case QEvent::TabletMove:
|
||||
if (event->device() == QTabletEvent::RotationStylus)
|
||||
updateCursor(event);
|
||||
if (deviceDown) {
|
||||
updateBrush(event);
|
||||
QPainter painter(&pixmap);
|
||||
@ -201,7 +203,7 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event)
|
||||
//! [5]
|
||||
|
||||
//! [7]
|
||||
void TabletCanvas::updateBrush(QTabletEvent *event)
|
||||
void TabletCanvas::updateBrush(const QTabletEvent *event)
|
||||
{
|
||||
int hue, saturation, value, alpha;
|
||||
myColor.getHsv(&hue, &saturation, &value, &alpha);
|
||||
@ -266,6 +268,46 @@ void TabletCanvas::updateBrush(QTabletEvent *event)
|
||||
}
|
||||
//! [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 *)
|
||||
{
|
||||
initPixmap();
|
||||
|
@ -80,8 +80,8 @@ public:
|
||||
{ myColor = color; }
|
||||
QColor color() const
|
||||
{ return myColor; }
|
||||
void setTabletDevice(QTabletEvent::TabletDevice device)
|
||||
{ myTabletDevice = device; }
|
||||
void setTabletDevice(QTabletEvent *event)
|
||||
{ myTabletDevice = event->device(); updateCursor(event); }
|
||||
int maximum(int a, int b)
|
||||
{ return a > b ? a : b; }
|
||||
|
||||
@ -94,7 +94,8 @@ private:
|
||||
void initPixmap();
|
||||
void paintPixmap(QPainter &painter, QTabletEvent *event);
|
||||
Qt::BrushStyle brushPattern(qreal value);
|
||||
void updateBrush(QTabletEvent *event);
|
||||
void updateBrush(const QTabletEvent *event);
|
||||
void updateCursor(const QTabletEvent *event);
|
||||
|
||||
AlphaChannelType alphaChannelType;
|
||||
ColorSaturationType colorSaturationType;
|
||||
|
Loading…
Reference in New Issue
Block a user