diff options
Diffstat (limited to 'examples/widgets/widgets/tablet')
-rw-r--r-- | examples/widgets/widgets/tablet/mainwindow.cpp | 229 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/mainwindow.h | 58 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletapplication.cpp | 2 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletapplication.h | 4 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletcanvas.cpp | 113 | ||||
-rw-r--r-- | examples/widgets/widgets/tablet/tabletcanvas.h | 46 |
6 files changed, 174 insertions, 278 deletions
diff --git a/examples/widgets/widgets/tablet/mainwindow.cpp b/examples/widgets/widgets/tablet/mainwindow.cpp index 5e84f5b6a2..950a5a2e29 100644 --- a/examples/widgets/widgets/tablet/mainwindow.cpp +++ b/examples/widgets/widgets/tablet/mainwindow.cpp @@ -45,237 +45,172 @@ //! [0] MainWindow::MainWindow(TabletCanvas *canvas) + : m_canvas(canvas), m_colorDialog(Q_NULLPTR) { - myCanvas = canvas; - createActions(); createMenus(); - - myCanvas->setColor(Qt::red); - myCanvas->setLineWidthType(TabletCanvas::LineWidthPressure); - myCanvas->setAlphaChannelType(TabletCanvas::AlphaTangentialPressure); - myCanvas->setColorSaturationType(TabletCanvas::NoSaturation); - setWindowTitle(tr("Tablet Example")); - setCentralWidget(myCanvas); + setCentralWidget(m_canvas); } //! [0] //! [1] -void MainWindow::brushColorAct() +void MainWindow::setBrushColor() { - QColor color = QColorDialog::getColor(myCanvas->color()); - - if (color.isValid()) - myCanvas->setColor(color); + if (!m_colorDialog) { + m_colorDialog = new QColorDialog(this); + m_colorDialog->setModal(false); + m_colorDialog->setCurrentColor(m_canvas->color()); + connect(m_colorDialog, &QColorDialog::colorSelected, m_canvas, &TabletCanvas::setColor); + } + m_colorDialog->setVisible(true); } //! [1] //! [2] -void MainWindow::alphaActionTriggered(QAction *action) +void MainWindow::setAlphaValuator(QAction *action) { - if (action == alphaChannelPressureAction) { - myCanvas->setAlphaChannelType(TabletCanvas::AlphaPressure); - } else if (action == alphaChannelTangentialPressureAction) { - myCanvas->setAlphaChannelType(TabletCanvas::AlphaTangentialPressure); - } else if (action == alphaChannelTiltAction) { - myCanvas->setAlphaChannelType(TabletCanvas::AlphaTilt); - } else { - myCanvas->setAlphaChannelType(TabletCanvas::NoAlpha); - } + m_canvas->setAlphaChannelValuator(action->data().value<TabletCanvas::Valuator>()); } //! [2] //! [3] -void MainWindow::lineWidthActionTriggered(QAction *action) +void MainWindow::setLineWidthValuator(QAction *action) { - if (action == lineWidthPressureAction) { - myCanvas->setLineWidthType(TabletCanvas::LineWidthPressure); - } else if (action == lineWidthTiltAction) { - myCanvas->setLineWidthType(TabletCanvas::LineWidthTilt); - } else { - myCanvas->setLineWidthType(TabletCanvas::NoLineWidth); - } + m_canvas->setLineWidthType(action->data().value<TabletCanvas::Valuator>()); } //! [3] //! [4] -void MainWindow::saturationActionTriggered(QAction *action) +void MainWindow::setSaturationValuator(QAction *action) { - if (action == colorSaturationVTiltAction) { - myCanvas->setColorSaturationType(TabletCanvas::SaturationVTilt); - } else if (action == colorSaturationHTiltAction) { - myCanvas->setColorSaturationType(TabletCanvas::SaturationHTilt); - } else if (action == colorSaturationPressureAction) { - myCanvas->setColorSaturationType(TabletCanvas::SaturationPressure); - } else { - myCanvas->setColorSaturationType(TabletCanvas::NoSaturation); - } + m_canvas->setColorSaturationValuator(action->data().value<TabletCanvas::Valuator>()); } //! [4] //! [5] -void MainWindow::saveAct() +void MainWindow::save() { QString path = QDir::currentPath() + "/untitled.png"; QString fileName = QFileDialog::getSaveFileName(this, tr("Save Picture"), path); - if (!myCanvas->saveImage(fileName)) + if (!m_canvas->saveImage(fileName)) QMessageBox::information(this, "Error Saving Picture", "Could not save the image"); } //! [5] //! [6] -void MainWindow::loadAct() +void MainWindow::load() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Picture"), QDir::currentPath()); - if (!myCanvas->loadImage(fileName)) + if (!m_canvas->loadImage(fileName)) QMessageBox::information(this, "Error Opening Picture", "Could not open picture"); } //! [6] //! [7] -void MainWindow::aboutAct() +void MainWindow::about() { QMessageBox::about(this, tr("About Tablet Example"), - tr("This example shows use of a Wacom tablet in Qt")); + tr("This example shows how to use a graphics drawing tablet in Qt.")); } //! [7] //! [8] -void MainWindow::createActions() +void MainWindow::createMenus() { + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(tr("&Open..."), this, &MainWindow::load, QKeySequence::Open); + fileMenu->addAction(tr("&Save As..."), this, &MainWindow::save, QKeySequence::SaveAs); + fileMenu->addAction(tr("E&xit"), this, &MainWindow::close, QKeySequence::Quit); + + QMenu *brushMenu = menuBar()->addMenu(tr("&Brush")); + brushMenu->addAction(tr("&Brush Color..."), this, &MainWindow::setBrushColor, tr("Ctrl+B")); //! [8] - brushColorAction = new QAction(tr("&Brush Color..."), this); - brushColorAction->setShortcut(tr("Ctrl+C")); - connect(brushColorAction, SIGNAL(triggered()), - this, SLOT(brushColorAct())); + + QMenu *tabletMenu = menuBar()->addMenu(tr("&Tablet")); + QMenu *lineWidthMenu = tabletMenu->addMenu(tr("&Line Width")); + + QAction *lineWidthPressureAction = lineWidthMenu->addAction(tr("&Pressure")); + lineWidthPressureAction->setData(TabletCanvas::PressureValuator); + lineWidthPressureAction->setCheckable(true); + lineWidthPressureAction->setChecked(true); + + QAction *lineWidthTiltAction = lineWidthMenu->addAction(tr("&Tilt")); + lineWidthTiltAction->setData(TabletCanvas::TiltValuator); + lineWidthTiltAction->setCheckable(true); + + QAction *lineWidthFixedAction = lineWidthMenu->addAction(tr("&Fixed")); + lineWidthFixedAction->setData(TabletCanvas::NoValuator); + lineWidthFixedAction->setCheckable(true); + + QActionGroup *lineWidthGroup = new QActionGroup(this); + lineWidthGroup->addAction(lineWidthPressureAction); + lineWidthGroup->addAction(lineWidthTiltAction); + lineWidthGroup->addAction(lineWidthFixedAction); + connect(lineWidthGroup, &QActionGroup::triggered, this, + &MainWindow::setLineWidthValuator); //! [9] - alphaChannelPressureAction = new QAction(tr("&Pressure"), this); + QMenu *alphaChannelMenu = tabletMenu->addMenu(tr("&Alpha Channel")); + QAction *alphaChannelPressureAction = alphaChannelMenu->addAction(tr("&Pressure")); + alphaChannelPressureAction->setData(TabletCanvas::PressureValuator); alphaChannelPressureAction->setCheckable(true); - alphaChannelTangentialPressureAction = new QAction(tr("T&angential Pressure"), this); + QAction *alphaChannelTangentialPressureAction = alphaChannelMenu->addAction(tr("T&angential Pressure")); + alphaChannelTangentialPressureAction->setData(TabletCanvas::TangentialPressureValuator); alphaChannelTangentialPressureAction->setCheckable(true); alphaChannelTangentialPressureAction->setChecked(true); - alphaChannelTiltAction = new QAction(tr("&Tilt"), this); + QAction *alphaChannelTiltAction = alphaChannelMenu->addAction(tr("&Tilt")); + alphaChannelTiltAction->setData(TabletCanvas::TiltValuator); alphaChannelTiltAction->setCheckable(true); - noAlphaChannelAction = new QAction(tr("No Alpha Channel"), this); + QAction *noAlphaChannelAction = alphaChannelMenu->addAction(tr("No Alpha Channel")); + noAlphaChannelAction->setData(TabletCanvas::NoValuator); noAlphaChannelAction->setCheckable(true); - alphaChannelGroup = new QActionGroup(this); + QActionGroup *alphaChannelGroup = new QActionGroup(this); alphaChannelGroup->addAction(alphaChannelPressureAction); alphaChannelGroup->addAction(alphaChannelTangentialPressureAction); alphaChannelGroup->addAction(alphaChannelTiltAction); alphaChannelGroup->addAction(noAlphaChannelAction); - connect(alphaChannelGroup, SIGNAL(triggered(QAction*)), - this, SLOT(alphaActionTriggered(QAction*))); - + connect(alphaChannelGroup, &QActionGroup::triggered, + this, &MainWindow::setAlphaValuator); //! [9] - colorSaturationVTiltAction = new QAction(tr("&Vertical Tilt"), this); + + QMenu *colorSaturationMenu = tabletMenu->addMenu(tr("&Color Saturation")); + + QAction *colorSaturationVTiltAction = colorSaturationMenu->addAction(tr("&Vertical Tilt")); + colorSaturationVTiltAction->setData(TabletCanvas::VTiltValuator); colorSaturationVTiltAction->setCheckable(true); - colorSaturationHTiltAction = new QAction(tr("&Horizontal Tilt"), this); + QAction *colorSaturationHTiltAction = colorSaturationMenu->addAction(tr("&Horizontal Tilt")); + colorSaturationHTiltAction->setData(TabletCanvas::HTiltValuator); colorSaturationHTiltAction->setCheckable(true); - colorSaturationPressureAction = new QAction(tr("&Pressure"), this); + QAction *colorSaturationPressureAction = colorSaturationMenu->addAction(tr("&Pressure")); + colorSaturationPressureAction->setData(TabletCanvas::PressureValuator); colorSaturationPressureAction->setCheckable(true); - noColorSaturationAction = new QAction(tr("&No Color Saturation"), this); + QAction *noColorSaturationAction = colorSaturationMenu->addAction(tr("&No Color Saturation")); + noColorSaturationAction->setData(TabletCanvas::NoValuator); noColorSaturationAction->setCheckable(true); noColorSaturationAction->setChecked(true); - colorSaturationGroup = new QActionGroup(this); + QActionGroup *colorSaturationGroup = new QActionGroup(this); colorSaturationGroup->addAction(colorSaturationVTiltAction); colorSaturationGroup->addAction(colorSaturationHTiltAction); colorSaturationGroup->addAction(colorSaturationPressureAction); colorSaturationGroup->addAction(noColorSaturationAction); - connect(colorSaturationGroup, SIGNAL(triggered(QAction*)), - this, SLOT(saturationActionTriggered(QAction*))); - - lineWidthPressureAction = new QAction(tr("&Pressure"), this); - lineWidthPressureAction->setCheckable(true); - lineWidthPressureAction->setChecked(true); - - lineWidthTiltAction = new QAction(tr("&Tilt"), this); - lineWidthTiltAction->setCheckable(true); - - lineWidthFixedAction = new QAction(tr("&Fixed"), this); - lineWidthFixedAction->setCheckable(true); - - lineWidthGroup = new QActionGroup(this); - lineWidthGroup->addAction(lineWidthPressureAction); - lineWidthGroup->addAction(lineWidthTiltAction); - lineWidthGroup->addAction(lineWidthFixedAction); - connect(lineWidthGroup, SIGNAL(triggered(QAction*)), - this, SLOT(lineWidthActionTriggered(QAction*))); - - exitAction = new QAction(tr("E&xit"), this); - exitAction->setShortcuts(QKeySequence::Quit); - connect(exitAction, SIGNAL(triggered()), - this, SLOT(close())); - - loadAction = new QAction(tr("&Open..."), this); - loadAction->setShortcuts(QKeySequence::Open); - connect(loadAction, SIGNAL(triggered()), - this, SLOT(loadAct())); - - saveAction = new QAction(tr("&Save As..."), this); - saveAction->setShortcuts(QKeySequence::SaveAs); - connect(saveAction, SIGNAL(triggered()), - this, SLOT(saveAct())); - - aboutAction = new QAction(tr("A&bout"), this); - aboutAction->setShortcut(tr("Ctrl+B")); - connect(aboutAction, SIGNAL(triggered()), - this, SLOT(aboutAct())); - - aboutQtAction = new QAction(tr("About &Qt"), this); - aboutQtAction->setShortcut(tr("Ctrl+Q")); - connect(aboutQtAction, SIGNAL(triggered()), - qApp, SLOT(aboutQt())); -//! [10] -} -//! [10] - -//! [11] -void MainWindow::createMenus() -{ - fileMenu = menuBar()->addMenu(tr("&File")); - fileMenu->addAction(loadAction); - fileMenu->addAction(saveAction); - fileMenu->addSeparator(); - fileMenu->addAction(exitAction); - - brushMenu = menuBar()->addMenu(tr("&Brush")); - brushMenu->addAction(brushColorAction); - - tabletMenu = menuBar()->addMenu(tr("&Tablet")); - - lineWidthMenu = tabletMenu->addMenu(tr("&Line Width")); - lineWidthMenu->addAction(lineWidthPressureAction); - lineWidthMenu->addAction(lineWidthTiltAction); - lineWidthMenu->addAction(lineWidthFixedAction); - - alphaChannelMenu = tabletMenu->addMenu(tr("&Alpha Channel")); - alphaChannelMenu->addAction(alphaChannelPressureAction); - alphaChannelMenu->addAction(alphaChannelTangentialPressureAction); - alphaChannelMenu->addAction(alphaChannelTiltAction); - alphaChannelMenu->addAction(noAlphaChannelAction); - - colorSaturationMenu = tabletMenu->addMenu(tr("&Color Saturation")); - colorSaturationMenu->addAction(colorSaturationVTiltAction); - colorSaturationMenu->addAction(colorSaturationHTiltAction); - colorSaturationMenu->addAction(noColorSaturationAction); + connect(colorSaturationGroup, &QActionGroup::triggered, + this, &MainWindow::setSaturationValuator); - helpMenu = menuBar()->addMenu("&Help"); - helpMenu->addAction(aboutAction); - helpMenu->addAction(aboutQtAction); + QMenu *helpMenu = menuBar()->addMenu("&Help"); + helpMenu->addAction(tr("A&bout"), this, &MainWindow::about); + helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); } -//! [11] diff --git a/examples/widgets/widgets/tablet/mainwindow.h b/examples/widgets/widgets/tablet/mainwindow.h index c6ac2e6026..23d587baac 100644 --- a/examples/widgets/widgets/tablet/mainwindow.h +++ b/examples/widgets/widgets/tablet/mainwindow.h @@ -44,10 +44,7 @@ #include <QMainWindow> QT_BEGIN_NAMESPACE -class QAction; -class QActionGroup; -class QMenu; -class QStatusBar; +class QColorDialog; QT_END_NAMESPACE class TabletCanvas; @@ -60,54 +57,19 @@ public: MainWindow(TabletCanvas *canvas); private slots: - void brushColorAct(); - void alphaActionTriggered(QAction *action); - void lineWidthActionTriggered(QAction *action); - void saturationActionTriggered(QAction *action); - void saveAct(); - void loadAct(); - void aboutAct(); + void setBrushColor(); + void setAlphaValuator(QAction *action); + void setLineWidthValuator(QAction *action); + void setSaturationValuator(QAction *action); + void save(); + void load(); + void about(); private: - void createActions(); void createMenus(); - TabletCanvas *myCanvas; - - QAction *brushColorAction; - QActionGroup *brushActionGroup; - - QActionGroup *alphaChannelGroup; - QAction *alphaChannelPressureAction; - QAction *alphaChannelTangentialPressureAction; - QAction *alphaChannelTiltAction; - QAction *noAlphaChannelAction; - - QActionGroup *colorSaturationGroup; - QAction *colorSaturationVTiltAction; - QAction *colorSaturationHTiltAction; - QAction *colorSaturationPressureAction; - QAction *noColorSaturationAction; - - QActionGroup *lineWidthGroup; - QAction *lineWidthPressureAction; - QAction *lineWidthTiltAction; - QAction *lineWidthFixedAction; - - QAction *exitAction; - QAction *saveAction; - QAction *loadAction; - - QAction *aboutAction; - QAction *aboutQtAction; - - QMenu *fileMenu; - QMenu *brushMenu; - QMenu *tabletMenu; - QMenu *helpMenu; - QMenu *colorSaturationMenu; - QMenu *lineWidthMenu; - QMenu *alphaChannelMenu; + TabletCanvas *m_canvas; + QColorDialog *m_colorDialog; }; //! [0] diff --git a/examples/widgets/widgets/tablet/tabletapplication.cpp b/examples/widgets/widgets/tablet/tabletapplication.cpp index 3e1356ee9c..7c0912c8f9 100644 --- a/examples/widgets/widgets/tablet/tabletapplication.cpp +++ b/examples/widgets/widgets/tablet/tabletapplication.cpp @@ -47,7 +47,7 @@ bool TabletApplication::event(QEvent *event) { if (event->type() == QEvent::TabletEnterProximity || event->type() == QEvent::TabletLeaveProximity) { - myCanvas->setTabletDevice(static_cast<QTabletEvent *>(event)); + m_canvas->setTabletDevice(static_cast<QTabletEvent *>(event)); return true; } return QApplication::event(event); diff --git a/examples/widgets/widgets/tablet/tabletapplication.h b/examples/widgets/widgets/tablet/tabletapplication.h index d755ff9891..be238b9a0d 100644 --- a/examples/widgets/widgets/tablet/tabletapplication.h +++ b/examples/widgets/widgets/tablet/tabletapplication.h @@ -56,10 +56,10 @@ public: bool event(QEvent *event) Q_DECL_OVERRIDE; void setCanvas(TabletCanvas *canvas) - { myCanvas = canvas; } + { m_canvas = canvas; } private: - TabletCanvas *myCanvas; + TabletCanvas *m_canvas; }; //! [0] diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp index 8ff3d41e0e..0d2fc2a958 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.cpp +++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp @@ -45,17 +45,18 @@ //! [0] TabletCanvas::TabletCanvas() + : QWidget(Q_NULLPTR) + , m_alphaChannelValuator(TangentialPressureValuator) + , m_colorSaturationValuator(NoValuator) + , m_lineWidthValuator(PressureValuator) + , m_color(Qt::red) + , m_brush(m_color) + , m_pen(m_brush, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin) + , m_deviceDown(false) { resize(500, 500); - myColor = Qt::red; - myBrush = QBrush(myColor); - myPen = QPen(myBrush, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); initPixmap(); setAutoFillBackground(true); - deviceDown = false; - alphaChannelType = AlphaTangentialPressure; - colorSaturationType = NoSaturation; - lineWidthType = LineWidthPressure; } void TabletCanvas::initPixmap() @@ -63,24 +64,24 @@ void TabletCanvas::initPixmap() QPixmap newPixmap = QPixmap(width(), height()); newPixmap.fill(Qt::white); QPainter painter(&newPixmap); - if (!pixmap.isNull()) - painter.drawPixmap(0, 0, pixmap); + if (!m_pixmap.isNull()) + painter.drawPixmap(0, 0, m_pixmap); painter.end(); - pixmap = newPixmap; + m_pixmap = newPixmap; } //! [0] //! [1] bool TabletCanvas::saveImage(const QString &file) { - return pixmap.save(file); + return m_pixmap.save(file); } //! [1] //! [2] bool TabletCanvas::loadImage(const QString &file) { - bool success = pixmap.load(file); + bool success = m_pixmap.load(file); if (success) { update(); @@ -96,8 +97,8 @@ void TabletCanvas::tabletEvent(QTabletEvent *event) switch (event->type()) { case QEvent::TabletPress: - if (!deviceDown) { - deviceDown = true; + if (!m_deviceDown) { + m_deviceDown = true; lastPoint.pos = event->posF(); lastPoint.rotation = event->rotation(); } @@ -105,17 +106,17 @@ void TabletCanvas::tabletEvent(QTabletEvent *event) case QEvent::TabletMove: if (event->device() == QTabletEvent::RotationStylus) updateCursor(event); - if (deviceDown) { + if (m_deviceDown) { updateBrush(event); - QPainter painter(&pixmap); + QPainter painter(&m_pixmap); paintPixmap(painter, event); lastPoint.pos = event->posF(); lastPoint.rotation = event->rotation(); } break; case QEvent::TabletRelease: - if (deviceDown && event->buttons() == Qt::NoButton) - deviceDown = false; + if (m_deviceDown && event->buttons() == Qt::NoButton) + m_deviceDown = false; break; default: break; @@ -128,7 +129,7 @@ void TabletCanvas::tabletEvent(QTabletEvent *event) void TabletCanvas::paintEvent(QPaintEvent *) { QPainter painter(this); - painter.drawPixmap(0, 0, pixmap); + painter.drawPixmap(0, 0, m_pixmap); } //! [4] @@ -142,10 +143,10 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) case QTabletEvent::Airbrush: { painter.setPen(Qt::NoPen); - QRadialGradient grad(lastPoint.pos, myPen.widthF() * 10.0); - QColor color = myBrush.color(); + QRadialGradient grad(lastPoint.pos, m_pen.widthF() * 10.0); + QColor color = m_brush.color(); color.setAlphaF(color.alphaF() * 0.25); - grad.setColorAt(0, myBrush.color()); + grad.setColorAt(0, m_brush.color()); grad.setColorAt(0.5, Qt::transparent); painter.setBrush(grad); qreal radius = grad.radius(); @@ -154,11 +155,11 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) break; case QTabletEvent::RotationStylus: { - myBrush.setStyle(Qt::SolidPattern); + m_brush.setStyle(Qt::SolidPattern); painter.setPen(Qt::NoPen); - painter.setBrush(myBrush); + painter.setBrush(m_brush); QPolygonF poly; - qreal halfWidth = myPen.widthF(); + qreal halfWidth = m_pen.widthF(); QPointF brushAdjust(qSin(qDegreesToRadians(lastPoint.rotation)) * halfWidth, qCos(qDegreesToRadians(lastPoint.rotation)) * halfWidth); poly << lastPoint.pos + brushAdjust; @@ -195,7 +196,7 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) } // FALL-THROUGH case QTabletEvent::Stylus: - painter.setPen(myPen); + painter.setPen(m_pen); painter.drawLine(lastPoint.pos, event->posF()); break; } @@ -206,68 +207,69 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) void TabletCanvas::updateBrush(const QTabletEvent *event) { int hue, saturation, value, alpha; - myColor.getHsv(&hue, &saturation, &value, &alpha); + m_color.getHsv(&hue, &saturation, &value, &alpha); int vValue = int(((event->yTilt() + 60.0) / 120.0) * 255); int hValue = int(((event->xTilt() + 60.0) / 120.0) * 255); //! [7] //! [8] - switch (alphaChannelType) { - case AlphaPressure: - myColor.setAlphaF(event->pressure()); + switch (m_alphaChannelValuator) { + case PressureValuator: + m_color.setAlphaF(event->pressure()); break; - case AlphaTangentialPressure: + case TangentialPressureValuator: if (event->device() == QTabletEvent::Airbrush) - myColor.setAlphaF(qMax(0.01, (event->tangentialPressure() + 1.0) / 2.0)); + m_color.setAlphaF(qMax(0.01, (event->tangentialPressure() + 1.0) / 2.0)); else - myColor.setAlpha(255); + m_color.setAlpha(255); break; - case AlphaTilt: - myColor.setAlpha(maximum(abs(vValue - 127), abs(hValue - 127))); + case TiltValuator: + m_color.setAlpha(maximum(abs(vValue - 127), abs(hValue - 127))); break; default: - myColor.setAlpha(255); + m_color.setAlpha(255); } //! [8] //! [9] - switch (colorSaturationType) { - case SaturationVTilt: - myColor.setHsv(hue, vValue, value, alpha); + switch (m_colorSaturationValuator) { + case VTiltValuator: + m_color.setHsv(hue, vValue, value, alpha); break; - case SaturationHTilt: - myColor.setHsv(hue, hValue, value, alpha); + case HTiltValuator: + m_color.setHsv(hue, hValue, value, alpha); break; - case SaturationPressure: - myColor.setHsv(hue, int(event->pressure() * 255.0), value, alpha); + case PressureValuator: + m_color.setHsv(hue, int(event->pressure() * 255.0), value, alpha); break; default: ; } //! [9] //! [10] - switch (lineWidthType) { - case LineWidthPressure: - myPen.setWidthF(event->pressure() * 10 + 1); + switch (m_lineWidthValuator) { + case PressureValuator: + m_pen.setWidthF(event->pressure() * 10 + 1); break; - case LineWidthTilt: - myPen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12); + case TiltValuator: + m_pen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12); break; default: - myPen.setWidthF(1); + m_pen.setWidthF(1); } //! [10] //! [11] if (event->pointerType() == QTabletEvent::Eraser) { - myBrush.setColor(Qt::white); - myPen.setColor(Qt::white); - myPen.setWidthF(event->pressure() * 10 + 1); + m_brush.setColor(Qt::white); + m_pen.setColor(Qt::white); + m_pen.setWidthF(event->pressure() * 10 + 1); } else { - myBrush.setColor(myColor); - myPen.setColor(myColor); + m_brush.setColor(m_color); + m_pen.setColor(m_color); } } //! [11] +//! [12] void TabletCanvas::updateCursor(const QTabletEvent *event) { QCursor cursor; @@ -285,7 +287,7 @@ void TabletCanvas::updateCursor(const QTabletEvent *event) case QTabletEvent::RotationStylus: { QImage origImg(QLatin1String(":/images/cursor-felt-marker.png")); QImage img(32, 32, QImage::Format_ARGB32); - QColor solid = myColor; + QColor solid = m_color; solid.setAlpha(255); img.fill(solid); QPainter painter(&img); @@ -307,6 +309,7 @@ void TabletCanvas::updateCursor(const QTabletEvent *event) } setCursor(cursor); } +//! [12] void TabletCanvas::resizeEvent(QResizeEvent *) { diff --git a/examples/widgets/widgets/tablet/tabletcanvas.h b/examples/widgets/widgets/tablet/tabletcanvas.h index a7335dbaf0..6044884d32 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.h +++ b/examples/widgets/widgets/tablet/tabletcanvas.h @@ -61,27 +61,26 @@ class TabletCanvas : public QWidget Q_OBJECT public: - enum AlphaChannelType { AlphaPressure, AlphaTangentialPressure, AlphaTilt, NoAlpha }; - enum ColorSaturationType { SaturationVTilt, SaturationHTilt, - SaturationPressure, NoSaturation }; - enum LineWidthType { LineWidthPressure, LineWidthTilt, NoLineWidth }; + enum Valuator { PressureValuator, TangentialPressureValuator, + TiltValuator, VTiltValuator, HTiltValuator, NoValuator }; + Q_ENUM(Valuator) TabletCanvas(); bool saveImage(const QString &file); bool loadImage(const QString &file); - void setAlphaChannelType(AlphaChannelType type) - { alphaChannelType = type; } - void setColorSaturationType(ColorSaturationType type) - { colorSaturationType = type; } - void setLineWidthType(LineWidthType type) - { lineWidthType = type; } - void setColor(const QColor &color) - { myColor = color; } + void setAlphaChannelValuator(Valuator type) + { m_alphaChannelValuator = type; } + void setColorSaturationValuator(Valuator type) + { m_colorSaturationValuator = type; } + void setLineWidthType(Valuator type) + { m_lineWidthValuator = type; } + void setColor(const QColor &c) + { if (c.isValid()) m_color = c; } QColor color() const - { return myColor; } + { return m_color; } void setTabletDevice(QTabletEvent *event) - { myTabletDevice = event->device(); updateCursor(event); } + { updateCursor(event); } int maximum(int a, int b) { return a > b ? a : b; } @@ -97,17 +96,14 @@ private: void updateBrush(const QTabletEvent *event); void updateCursor(const QTabletEvent *event); - AlphaChannelType alphaChannelType; - ColorSaturationType colorSaturationType; - LineWidthType lineWidthType; - QTabletEvent::PointerType pointerType; - QTabletEvent::TabletDevice myTabletDevice; - QColor myColor; - - QPixmap pixmap; - QBrush myBrush; - QPen myPen; - bool deviceDown; + Valuator m_alphaChannelValuator; + Valuator m_colorSaturationValuator; + Valuator m_lineWidthValuator; + QColor m_color; + QPixmap m_pixmap; + QBrush m_brush; + QPen m_pen; + bool m_deviceDown; struct Point { QPointF pos; |