| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtWidgets> |
| 52 | #if defined(QT_PRINTSUPPORT_LIB) |
| 53 | #include <QtPrintSupport/qtprintsupportglobal.h> |
| 54 | #if QT_CONFIG(printdialog) |
| 55 | #include <QPrinter> |
| 56 | #include <QPrintDialog> |
| 57 | #endif |
| 58 | #endif |
| 59 | |
| 60 | #include "scribblearea.h" |
| 61 | |
| 62 | static const qreal MinimumDiameter = 3.0; |
| 63 | static const qreal MaximumDiameter = 50.0; |
| 64 | |
| 65 | //! [0] |
| 66 | ScribbleArea::ScribbleArea(QWidget *parent) |
| 67 | : QWidget(parent) |
| 68 | { |
| 69 | setAttribute(Qt::WA_AcceptTouchEvents); |
| 70 | setAttribute(Qt::WA_StaticContents); |
| 71 | modified = false; |
| 72 | |
| 73 | myPenColors |
| 74 | << QColor("green" ) |
| 75 | << QColor("purple" ) |
| 76 | << QColor("red" ) |
| 77 | << QColor("blue" ) |
| 78 | << QColor("yellow" ) |
| 79 | |
| 80 | << QColor("pink" ) |
| 81 | << QColor("orange" ) |
| 82 | << QColor("brown" ) |
| 83 | << QColor("grey" ) |
| 84 | << QColor("black" ); |
| 85 | } |
| 86 | //! [0] |
| 87 | |
| 88 | //! [1] |
| 89 | bool ScribbleArea::openImage(const QString &fileName) |
| 90 | //! [1] //! [2] |
| 91 | { |
| 92 | QImage loadedImage; |
| 93 | if (!loadedImage.load(fileName)) |
| 94 | return false; |
| 95 | |
| 96 | QSize newSize = loadedImage.size().expandedTo(size()); |
| 97 | resizeImage(&loadedImage, newSize); |
| 98 | image = loadedImage; |
| 99 | modified = false; |
| 100 | update(); |
| 101 | return true; |
| 102 | } |
| 103 | //! [2] |
| 104 | |
| 105 | //! [3] |
| 106 | bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat) |
| 107 | //! [3] //! [4] |
| 108 | { |
| 109 | QImage visibleImage = image; |
| 110 | resizeImage(&visibleImage, size()); |
| 111 | |
| 112 | if (visibleImage.save(fileName, fileFormat)) { |
| 113 | modified = false; |
| 114 | return true; |
| 115 | } else { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | //! [4] |
| 120 | |
| 121 | //! [9] |
| 122 | void ScribbleArea::clearImage() |
| 123 | //! [9] //! [10] |
| 124 | { |
| 125 | image.fill(qRgb(255, 255, 255)); |
| 126 | modified = true; |
| 127 | update(); |
| 128 | } |
| 129 | //! [10] |
| 130 | |
| 131 | //! [12] //! [13] |
| 132 | void ScribbleArea::paintEvent(QPaintEvent *event) |
| 133 | //! [13] //! [14] |
| 134 | { |
| 135 | QPainter painter(this); |
| 136 | const QRect rect = event->rect(); |
| 137 | painter.drawImage(rect.topLeft(), image, rect); |
| 138 | } |
| 139 | //! [14] |
| 140 | |
| 141 | //! [15] |
| 142 | void ScribbleArea::resizeEvent(QResizeEvent *event) |
| 143 | //! [15] //! [16] |
| 144 | { |
| 145 | if (width() > image.width() || height() > image.height()) { |
| 146 | int newWidth = qMax(width() + 128, image.width()); |
| 147 | int newHeight = qMax(height() + 128, image.height()); |
| 148 | resizeImage(&image, QSize(newWidth, newHeight)); |
| 149 | update(); |
| 150 | } |
| 151 | QWidget::resizeEvent(event); |
| 152 | } |
| 153 | //! [16] |
| 154 | |
| 155 | //! [19] |
| 156 | void ScribbleArea::resizeImage(QImage *image, const QSize &newSize) |
| 157 | //! [19] //! [20] |
| 158 | { |
| 159 | if (image->size() == newSize) |
| 160 | return; |
| 161 | |
| 162 | QImage newImage(newSize, QImage::Format_RGB32); |
| 163 | newImage.fill(qRgb(255, 255, 255)); |
| 164 | QPainter painter(&newImage); |
| 165 | painter.drawImage(QPoint(0, 0), *image); |
| 166 | *image = newImage; |
| 167 | } |
| 168 | //! [20] |
| 169 | |
| 170 | //! [21] |
| 171 | void ScribbleArea::print() |
| 172 | { |
| 173 | #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) |
| 174 | QPrinter printer(QPrinter::HighResolution); |
| 175 | |
| 176 | QPrintDialog printDialog(&printer, this); |
| 177 | //! [21] //! [22] |
| 178 | if (printDialog.exec() == QDialog::Accepted) { |
| 179 | QPainter painter(&printer); |
| 180 | QRect rect = painter.viewport(); |
| 181 | QSize size = image.size(); |
| 182 | size.scale(rect.size(), Qt::KeepAspectRatio); |
| 183 | painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
| 184 | painter.setWindow(image.rect()); |
| 185 | painter.drawImage(0, 0, image); |
| 186 | } |
| 187 | #endif // QT_CONFIG(printdialog) |
| 188 | } |
| 189 | //! [22] |
| 190 | |
| 191 | bool ScribbleArea::event(QEvent *event) |
| 192 | { |
| 193 | switch (event->type()) { |
| 194 | case QEvent::TouchBegin: |
| 195 | case QEvent::TouchUpdate: |
| 196 | case QEvent::TouchEnd: |
| 197 | { |
| 198 | const QTouchEvent *touch = static_cast<QTouchEvent *>(event); |
| 199 | const QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
| 200 | for (const QTouchEvent::TouchPoint &touchPoint : touchPoints) { |
| 201 | switch (touchPoint.state()) { |
| 202 | case Qt::TouchPointStationary: |
| 203 | case Qt::TouchPointReleased: |
| 204 | // don't do anything if this touch point hasn't moved or has been released |
| 205 | continue; |
| 206 | default: |
| 207 | { |
| 208 | QSizeF diams = touchPoint.ellipseDiameters(); |
| 209 | if (diams.isEmpty()) { |
| 210 | qreal diameter = MaximumDiameter; |
| 211 | if (touch->pointingDevice()->capabilities().testFlag(QPointingDevice::Capability::Pressure)) |
| 212 | diameter = MinimumDiameter + (MaximumDiameter - MinimumDiameter) * touchPoint.pressure(); |
| 213 | diams = QSizeF(diameter, diameter); |
| 214 | } |
| 215 | |
| 216 | QPainter painter(&image); |
| 217 | painter.setPen(Qt::NoPen); |
| 218 | painter.setBrush(myPenColors.at(touchPoint.id() % myPenColors.count())); |
| 219 | painter.drawEllipse(touchPoint.position(), diams.width() / 2, diams.height() / 2); |
| 220 | painter.end(); |
| 221 | |
| 222 | modified = true; |
| 223 | const int rad = 2; |
| 224 | QRectF rect(QPointF(), diams); |
| 225 | rect.moveCenter(touchPoint.position()); |
| 226 | update(rect.toRect().adjusted(-rad,-rad, +rad, +rad)); |
| 227 | } |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | default: |
| 234 | return QWidget::event(event); |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |