examples/widgets/tablet/tabletcanvas.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include <math.h>
       
    44 
       
    45 #include "tabletcanvas.h"
       
    46 
       
    47 //! [0]
       
    48 TabletCanvas::TabletCanvas()
       
    49 {
       
    50     resize(500, 500);
       
    51     myBrush = QBrush();
       
    52     myPen = QPen();
       
    53     initImage();
       
    54     setAutoFillBackground(true);
       
    55     deviceDown = false;
       
    56     myColor = Qt::red;
       
    57     myTabletDevice = QTabletEvent::Stylus;
       
    58     alphaChannelType = NoAlpha;
       
    59     colorSaturationType = NoSaturation;
       
    60     lineWidthType = LineWidthPressure;
       
    61 }
       
    62 
       
    63 void TabletCanvas::initImage()
       
    64 {
       
    65     QImage newImage = QImage(width(), height(), QImage::Format_ARGB32);
       
    66     QPainter painter(&newImage);
       
    67     painter.fillRect(0, 0, newImage.width(), newImage.height(), Qt::white);
       
    68     if (!image.isNull())
       
    69         painter.drawImage(0, 0, image);
       
    70     painter.end();
       
    71     image = newImage;
       
    72 }
       
    73 //! [0]
       
    74 
       
    75 //! [1]
       
    76 bool TabletCanvas::saveImage(const QString &file)
       
    77 {
       
    78     return image.save(file);
       
    79 }
       
    80 //! [1]
       
    81 
       
    82 //! [2]
       
    83 bool TabletCanvas::loadImage(const QString &file)
       
    84 {
       
    85     bool success = image.load(file);
       
    86 
       
    87     if (success) {
       
    88         update();
       
    89         return true;
       
    90     }
       
    91     return false;
       
    92 }
       
    93 //! [2]
       
    94 
       
    95 //! [3]
       
    96 void TabletCanvas::tabletEvent(QTabletEvent *event)
       
    97 {
       
    98 
       
    99     switch (event->type()) {
       
   100         case QEvent::TabletPress:
       
   101             if (!deviceDown) {
       
   102                 deviceDown = true;
       
   103                 polyLine[0] = polyLine[1] = polyLine[2] = event->pos();
       
   104             }
       
   105             break;
       
   106         case QEvent::TabletRelease:
       
   107             if (deviceDown)
       
   108                 deviceDown = false;
       
   109             break;
       
   110         case QEvent::TabletMove:
       
   111             polyLine[2] = polyLine[1];
       
   112             polyLine[1] = polyLine[0];
       
   113             polyLine[0] = event->pos();
       
   114 
       
   115             if (deviceDown) {
       
   116                 updateBrush(event);
       
   117                 QPainter painter(&image);
       
   118                 paintImage(painter, event);
       
   119             }
       
   120             break;
       
   121         default:
       
   122             break;
       
   123     }
       
   124     update();
       
   125 }
       
   126 //! [3]
       
   127 
       
   128 //! [4]
       
   129 void TabletCanvas::paintEvent(QPaintEvent *)
       
   130 {
       
   131     QPainter painter(this);
       
   132     painter.drawImage(QPoint(0, 0), image);
       
   133 }
       
   134 //! [4]
       
   135 
       
   136 //! [5]
       
   137 void TabletCanvas::paintImage(QPainter &painter, QTabletEvent *event)
       
   138 {
       
   139     QPoint brushAdjust(10, 10);
       
   140 
       
   141     switch (myTabletDevice) {
       
   142         case QTabletEvent::Airbrush:
       
   143             myBrush.setColor(myColor);
       
   144             myBrush.setStyle(brushPattern(event->pressure()));
       
   145             painter.setPen(Qt::NoPen);
       
   146             painter.setBrush(myBrush);
       
   147 
       
   148             for (int i = 0; i < 3; ++i) {
       
   149                 painter.drawEllipse(QRect(polyLine[i] - brushAdjust,
       
   150                                     polyLine[i] + brushAdjust));
       
   151             }
       
   152             break;
       
   153         case QTabletEvent::Puck:
       
   154         case QTabletEvent::FourDMouse:
       
   155         case QTabletEvent::RotationStylus:
       
   156             {
       
   157                 const QString error(tr("This input device is not supported by the example."));
       
   158 #ifndef QT_NO_STATUSTIP
       
   159                 QStatusTipEvent status(error);
       
   160                 QApplication::sendEvent(this, &status);
       
   161 #else
       
   162                 qWarning() << error;
       
   163 #endif
       
   164             }
       
   165             break;
       
   166         default:
       
   167             {
       
   168                 const QString error(tr("Unknown tablet device - treating as stylus"));
       
   169 #ifndef QT_NO_STATUSTIP
       
   170                 QStatusTipEvent status(error);
       
   171                 QApplication::sendEvent(this, &status);
       
   172 #else
       
   173                 qWarning() << error;
       
   174 #endif
       
   175             }
       
   176             // FALL-THROUGH
       
   177         case QTabletEvent::Stylus:
       
   178             painter.setBrush(myBrush);
       
   179             painter.setPen(myPen);
       
   180             painter.drawLine(polyLine[1], event->pos());
       
   181             break;
       
   182     }
       
   183 }
       
   184 //! [5]
       
   185 
       
   186 //! [6]
       
   187 Qt::BrushStyle TabletCanvas::brushPattern(qreal value)
       
   188 {
       
   189     int pattern = int((value) * 100.0) % 7;
       
   190 
       
   191     switch (pattern) {
       
   192         case 0:
       
   193             return Qt::SolidPattern;
       
   194         case 1:
       
   195             return Qt::Dense1Pattern;
       
   196         case 2:
       
   197             return Qt::Dense2Pattern;
       
   198         case 3:
       
   199             return Qt::Dense3Pattern;
       
   200         case 4:
       
   201             return Qt::Dense4Pattern;
       
   202         case 5:
       
   203             return Qt::Dense5Pattern;
       
   204         case 6:
       
   205             return Qt::Dense6Pattern;
       
   206         default:
       
   207             return Qt::Dense7Pattern;
       
   208     }
       
   209 }
       
   210 //! [6]
       
   211 
       
   212 //! [7]
       
   213 void TabletCanvas::updateBrush(QTabletEvent *event)
       
   214 {
       
   215     int hue, saturation, value, alpha;
       
   216     myColor.getHsv(&hue, &saturation, &value, &alpha);
       
   217 
       
   218     int vValue = int(((event->yTilt() + 60.0) / 120.0) * 255);
       
   219     int hValue = int(((event->xTilt() + 60.0) / 120.0) * 255);
       
   220 //! [7] //! [8]
       
   221 
       
   222     switch (alphaChannelType) {
       
   223         case AlphaPressure:
       
   224             myColor.setAlpha(int(event->pressure() * 255.0));
       
   225             break;
       
   226         case AlphaTilt:
       
   227             myColor.setAlpha(maximum(abs(vValue - 127), abs(hValue - 127)));
       
   228             break;
       
   229         default:
       
   230             myColor.setAlpha(255);
       
   231     }
       
   232 
       
   233 //! [8] //! [9]
       
   234     switch (colorSaturationType) {
       
   235         case SaturationVTilt:
       
   236             myColor.setHsv(hue, vValue, value, alpha);
       
   237             break;
       
   238         case SaturationHTilt:
       
   239             myColor.setHsv(hue, hValue, value, alpha);
       
   240             break;
       
   241         case SaturationPressure:
       
   242             myColor.setHsv(hue, int(event->pressure() * 255.0), value, alpha);
       
   243             break;
       
   244         default:
       
   245             ;
       
   246     }
       
   247 
       
   248 //! [9] //! [10]
       
   249     switch (lineWidthType) {
       
   250         case LineWidthPressure:
       
   251             myPen.setWidthF(event->pressure() * 10 + 1);
       
   252             break;
       
   253         case LineWidthTilt:
       
   254             myPen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12);
       
   255             break;
       
   256         default:
       
   257             myPen.setWidthF(1);
       
   258     }
       
   259 
       
   260 //! [10] //! [11]
       
   261     if (event->pointerType() == QTabletEvent::Eraser) {
       
   262         myBrush.setColor(Qt::white);
       
   263         myPen.setColor(Qt::white);
       
   264         myPen.setWidthF(event->pressure() * 10 + 1);
       
   265     } else {
       
   266         myBrush.setColor(myColor);
       
   267         myPen.setColor(myColor);
       
   268     }
       
   269 }
       
   270 //! [11]
       
   271 
       
   272 void TabletCanvas::resizeEvent(QResizeEvent *)
       
   273 {
       
   274     initImage();
       
   275     polyLine[0] = polyLine[1] = polyLine[2] = QPoint();
       
   276 }