examples/widgets/tablet/tabletcanvas.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    48 TabletCanvas::TabletCanvas()
    48 TabletCanvas::TabletCanvas()
    49 {
    49 {
    50     resize(500, 500);
    50     resize(500, 500);
    51     myBrush = QBrush();
    51     myBrush = QBrush();
    52     myPen = QPen();
    52     myPen = QPen();
    53     initImage();
    53     initPixmap();
    54     setAutoFillBackground(true);
    54     setAutoFillBackground(true);
    55     deviceDown = false;
    55     deviceDown = false;
    56     myColor = Qt::red;
    56     myColor = Qt::red;
    57     myTabletDevice = QTabletEvent::Stylus;
    57     myTabletDevice = QTabletEvent::Stylus;
    58     alphaChannelType = NoAlpha;
    58     alphaChannelType = NoAlpha;
    59     colorSaturationType = NoSaturation;
    59     colorSaturationType = NoSaturation;
    60     lineWidthType = LineWidthPressure;
    60     lineWidthType = LineWidthPressure;
    61 }
    61 }
    62 
    62 
    63 void TabletCanvas::initImage()
    63 void TabletCanvas::initPixmap()
    64 {
    64 {
    65     QImage newImage = QImage(width(), height(), QImage::Format_ARGB32);
    65     QPixmap newPixmap = QPixmap(width(), height());
    66     QPainter painter(&newImage);
    66     newPixmap.fill(Qt::white);
    67     painter.fillRect(0, 0, newImage.width(), newImage.height(), Qt::white);
    67     QPainter painter(&newPixmap);
    68     if (!image.isNull())
    68     if (!pixmap.isNull())
    69         painter.drawImage(0, 0, image);
    69         painter.drawPixmap(0, 0, pixmap);
    70     painter.end();
    70     painter.end();
    71     image = newImage;
    71     pixmap = newPixmap;
    72 }
    72 }
    73 //! [0]
    73 //! [0]
    74 
    74 
    75 //! [1]
    75 //! [1]
    76 bool TabletCanvas::saveImage(const QString &file)
    76 bool TabletCanvas::saveImage(const QString &file)
    77 {
    77 {
    78     return image.save(file);
    78     return pixmap.save(file);
    79 }
    79 }
    80 //! [1]
    80 //! [1]
    81 
    81 
    82 //! [2]
    82 //! [2]
    83 bool TabletCanvas::loadImage(const QString &file)
    83 bool TabletCanvas::loadImage(const QString &file)
    84 {
    84 {
    85     bool success = image.load(file);
    85     bool success = pixmap.load(file);
    86 
    86 
    87     if (success) {
    87     if (success) {
    88         update();
    88         update();
    89         return true;
    89         return true;
    90     }
    90     }
   112             polyLine[1] = polyLine[0];
   112             polyLine[1] = polyLine[0];
   113             polyLine[0] = event->pos();
   113             polyLine[0] = event->pos();
   114 
   114 
   115             if (deviceDown) {
   115             if (deviceDown) {
   116                 updateBrush(event);
   116                 updateBrush(event);
   117                 QPainter painter(&image);
   117                 QPainter painter(&pixmap);
   118                 paintImage(painter, event);
   118                 paintPixmap(painter, event);
   119             }
   119             }
   120             break;
   120             break;
   121         default:
   121         default:
   122             break;
   122             break;
   123     }
   123     }
   127 
   127 
   128 //! [4]
   128 //! [4]
   129 void TabletCanvas::paintEvent(QPaintEvent *)
   129 void TabletCanvas::paintEvent(QPaintEvent *)
   130 {
   130 {
   131     QPainter painter(this);
   131     QPainter painter(this);
   132     painter.drawImage(QPoint(0, 0), image);
   132     painter.drawPixmap(0, 0, pixmap);
   133 }
   133 }
   134 //! [4]
   134 //! [4]
   135 
   135 
   136 //! [5]
   136 //! [5]
   137 void TabletCanvas::paintImage(QPainter &painter, QTabletEvent *event)
   137 void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event)
   138 {
   138 {
   139     QPoint brushAdjust(10, 10);
   139     QPoint brushAdjust(10, 10);
   140 
   140 
   141     switch (myTabletDevice) {
   141     switch (myTabletDevice) {
   142         case QTabletEvent::Airbrush:
   142         case QTabletEvent::Airbrush:
   269 }
   269 }
   270 //! [11]
   270 //! [11]
   271 
   271 
   272 void TabletCanvas::resizeEvent(QResizeEvent *)
   272 void TabletCanvas::resizeEvent(QResizeEvent *)
   273 {
   273 {
   274     initImage();
   274     initPixmap();
   275     polyLine[0] = polyLine[1] = polyLine[2] = QPoint();
   275     polyLine[0] = polyLine[1] = polyLine[2] = QPoint();
   276 }
   276 }