[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "scribblearea.h"
|
---|
| 44 |
|
---|
| 45 | //! [0]
|
---|
| 46 | ScribbleArea::ScribbleArea(QWidget *parent)
|
---|
| 47 | : QWidget(parent)
|
---|
| 48 | {
|
---|
| 49 | setAttribute(Qt::WA_StaticContents);
|
---|
| 50 | modified = false;
|
---|
| 51 | scribbling = false;
|
---|
| 52 | myPenWidth = 1;
|
---|
| 53 | myPenColor = Qt::blue;
|
---|
| 54 | }
|
---|
| 55 | //! [0]
|
---|
| 56 |
|
---|
| 57 | //! [1]
|
---|
| 58 | bool ScribbleArea::openImage(const QString &fileName)
|
---|
| 59 | //! [1] //! [2]
|
---|
| 60 | {
|
---|
| 61 | QImage loadedImage;
|
---|
| 62 | if (!loadedImage.load(fileName))
|
---|
| 63 | return false;
|
---|
| 64 |
|
---|
| 65 | QSize newSize = loadedImage.size().expandedTo(size());
|
---|
| 66 | resizeImage(&loadedImage, newSize);
|
---|
| 67 | image = loadedImage;
|
---|
| 68 | modified = false;
|
---|
| 69 | update();
|
---|
| 70 | return true;
|
---|
| 71 | }
|
---|
| 72 | //! [2]
|
---|
| 73 |
|
---|
| 74 | //! [3]
|
---|
| 75 | bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat)
|
---|
| 76 | //! [3] //! [4]
|
---|
| 77 | {
|
---|
| 78 | QImage visibleImage = image;
|
---|
| 79 | resizeImage(&visibleImage, size());
|
---|
| 80 |
|
---|
| 81 | if (visibleImage.save(fileName, fileFormat)) {
|
---|
| 82 | modified = false;
|
---|
| 83 | return true;
|
---|
| 84 | } else {
|
---|
| 85 | return false;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | //! [4]
|
---|
| 89 |
|
---|
| 90 | //! [5]
|
---|
| 91 | void ScribbleArea::setPenColor(const QColor &newColor)
|
---|
| 92 | //! [5] //! [6]
|
---|
| 93 | {
|
---|
| 94 | myPenColor = newColor;
|
---|
| 95 | }
|
---|
| 96 | //! [6]
|
---|
| 97 |
|
---|
| 98 | //! [7]
|
---|
| 99 | void ScribbleArea::setPenWidth(int newWidth)
|
---|
| 100 | //! [7] //! [8]
|
---|
| 101 | {
|
---|
| 102 | myPenWidth = newWidth;
|
---|
| 103 | }
|
---|
| 104 | //! [8]
|
---|
| 105 |
|
---|
| 106 | //! [9]
|
---|
| 107 | void ScribbleArea::clearImage()
|
---|
| 108 | //! [9] //! [10]
|
---|
| 109 | {
|
---|
| 110 | image.fill(qRgb(255, 255, 255));
|
---|
| 111 | modified = true;
|
---|
| 112 | update();
|
---|
| 113 | }
|
---|
| 114 | //! [10]
|
---|
| 115 |
|
---|
| 116 | //! [11]
|
---|
| 117 | void ScribbleArea::mousePressEvent(QMouseEvent *event)
|
---|
| 118 | //! [11] //! [12]
|
---|
| 119 | {
|
---|
| 120 | if (event->button() == Qt::LeftButton) {
|
---|
| 121 | lastPoint = event->pos();
|
---|
| 122 | scribbling = true;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void ScribbleArea::mouseMoveEvent(QMouseEvent *event)
|
---|
| 127 | {
|
---|
| 128 | if ((event->buttons() & Qt::LeftButton) && scribbling)
|
---|
| 129 | drawLineTo(event->pos());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void ScribbleArea::mouseReleaseEvent(QMouseEvent *event)
|
---|
| 133 | {
|
---|
| 134 | if (event->button() == Qt::LeftButton && scribbling) {
|
---|
| 135 | drawLineTo(event->pos());
|
---|
| 136 | scribbling = false;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | //! [12] //! [13]
|
---|
[561] | 141 | void ScribbleArea::paintEvent(QPaintEvent *event)
|
---|
[2] | 142 | //! [13] //! [14]
|
---|
| 143 | {
|
---|
| 144 | QPainter painter(this);
|
---|
[561] | 145 | QRect dirtyRect = event->rect();
|
---|
| 146 | painter.drawImage(dirtyRect, image, dirtyRect);
|
---|
[2] | 147 | }
|
---|
| 148 | //! [14]
|
---|
| 149 |
|
---|
| 150 | //! [15]
|
---|
| 151 | void ScribbleArea::resizeEvent(QResizeEvent *event)
|
---|
| 152 | //! [15] //! [16]
|
---|
| 153 | {
|
---|
| 154 | if (width() > image.width() || height() > image.height()) {
|
---|
| 155 | int newWidth = qMax(width() + 128, image.width());
|
---|
| 156 | int newHeight = qMax(height() + 128, image.height());
|
---|
| 157 | resizeImage(&image, QSize(newWidth, newHeight));
|
---|
| 158 | update();
|
---|
| 159 | }
|
---|
| 160 | QWidget::resizeEvent(event);
|
---|
| 161 | }
|
---|
| 162 | //! [16]
|
---|
| 163 |
|
---|
| 164 | //! [17]
|
---|
| 165 | void ScribbleArea::drawLineTo(const QPoint &endPoint)
|
---|
| 166 | //! [17] //! [18]
|
---|
| 167 | {
|
---|
| 168 | QPainter painter(&image);
|
---|
| 169 | painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
|
---|
| 170 | Qt::RoundJoin));
|
---|
| 171 | painter.drawLine(lastPoint, endPoint);
|
---|
| 172 | modified = true;
|
---|
| 173 |
|
---|
| 174 | int rad = (myPenWidth / 2) + 2;
|
---|
| 175 | update(QRect(lastPoint, endPoint).normalized()
|
---|
| 176 | .adjusted(-rad, -rad, +rad, +rad));
|
---|
| 177 | lastPoint = endPoint;
|
---|
| 178 | }
|
---|
| 179 | //! [18]
|
---|
| 180 |
|
---|
| 181 | //! [19]
|
---|
| 182 | void ScribbleArea::resizeImage(QImage *image, const QSize &newSize)
|
---|
| 183 | //! [19] //! [20]
|
---|
| 184 | {
|
---|
| 185 | if (image->size() == newSize)
|
---|
| 186 | return;
|
---|
| 187 |
|
---|
| 188 | QImage newImage(newSize, QImage::Format_RGB32);
|
---|
| 189 | newImage.fill(qRgb(255, 255, 255));
|
---|
| 190 | QPainter painter(&newImage);
|
---|
| 191 | painter.drawImage(QPoint(0, 0), *image);
|
---|
| 192 | *image = newImage;
|
---|
| 193 | }
|
---|
| 194 | //! [20]
|
---|
| 195 |
|
---|
| 196 | //! [21]
|
---|
| 197 | void ScribbleArea::print()
|
---|
| 198 | {
|
---|
| 199 | #ifndef QT_NO_PRINTER
|
---|
| 200 | QPrinter printer(QPrinter::HighResolution);
|
---|
| 201 |
|
---|
| 202 | QPrintDialog *printDialog = new QPrintDialog(&printer, this);
|
---|
| 203 | //! [21] //! [22]
|
---|
| 204 | if (printDialog->exec() == QDialog::Accepted) {
|
---|
| 205 | QPainter painter(&printer);
|
---|
| 206 | QRect rect = painter.viewport();
|
---|
| 207 | QSize size = image.size();
|
---|
| 208 | size.scale(rect.size(), Qt::KeepAspectRatio);
|
---|
| 209 | painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
|
---|
| 210 | painter.setWindow(image.rect());
|
---|
| 211 | painter.drawImage(0, 0, image);
|
---|
| 212 | }
|
---|
| 213 | #endif // QT_NO_PRINTER
|
---|
| 214 | }
|
---|
| 215 | //! [22]
|
---|