| 1 | /*************************************************************************** |
| 2 | * This file is part of Robocut. * |
| 3 | * Copyright (C) 2010 Tim Hutt <[email protected]> * |
| 4 | * Copyright (C) 2010 Markus Schulz <[email protected]> * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 3 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
| 19 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include "PathPaintPage.h" |
| 23 | |
| 24 | #include "PathPaintEngine.h" |
| 25 | |
| 26 | #include <QDebug> |
| 27 | |
| 28 | |
| 29 | |
| 30 | uint qHash(const QPolygonF& key) |
| 31 | { |
| 32 | QByteArray repr; |
| 33 | for (int i = 0; i < key.size(); ++i) |
| 34 | { |
| 35 | double x = key[i].x(); |
| 36 | double y = key[i].y(); |
| 37 | |
| 38 | repr.append(reinterpret_cast<char*>(&x), sizeof(x)); |
| 39 | repr.append(reinterpret_cast<char*>(&y), sizeof(y)); |
| 40 | } |
| 41 | return qHash(repr); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | PathPaintDevice::PathPaintDevice(double widthInMm, double heightInMm, double pixelsPerMm) |
| 47 | { |
| 48 | engine = NULL; |
| 49 | width = widthInMm; |
| 50 | height = heightInMm; |
| 51 | pathsClipped = false; |
| 52 | ppm = pixelsPerMm; |
| 53 | if (ppm == 0.0) |
| 54 | ppm = 1.0; |
| 55 | } |
| 56 | |
| 57 | PathPaintDevice::~PathPaintDevice() |
| 58 | { |
| 59 | if (engine) |
| 60 | delete engine; |
| 61 | } |
| 62 | |
| 63 | void PathPaintDevice::addPath(const QPolygonF& path) |
| 64 | { |
| 65 | if (pagePathSet.contains(path)) |
| 66 | return; |
| 67 | pagePathSet.insert(path); |
| 68 | pagePaths.append(path); |
| 69 | |
| 70 | // Clip the path. |
| 71 | for (int j = 0; j < pagePaths.back().size(); ++j) |
| 72 | { |
| 73 | // pagePaths are in mm, so convert from pixels to mm. |
| 74 | pagePaths.back()[j] /= ppm; |
| 75 | |
| 76 | if (pagePaths.back()[j].x() < 0.0) |
| 77 | { |
| 78 | pathsClipped = true; |
| 79 | pagePaths.back()[j].setX(0.0); |
| 80 | } |
| 81 | if (pagePaths.back()[j].y() < 0.0) |
| 82 | { |
| 83 | pathsClipped = true; |
| 84 | pagePaths.back()[j].setY(0.0); |
| 85 | } |
| 86 | if (pagePaths.back()[j].x() > width) |
| 87 | { |
| 88 | pathsClipped = true; |
| 89 | pagePaths.back()[j].setX(width); |
| 90 | } |
| 91 | if (pagePaths.back()[j].y() > height) |
| 92 | { |
| 93 | pathsClipped = true; |
| 94 | pagePaths.back()[j].setY(height); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | QPaintEngine* PathPaintDevice::paintEngine() const |
| 101 | { |
| 102 | if (!engine) |
| 103 | engine = new PathPaintEngine(const_cast<PathPaintDevice*>(this)); |
| 104 | return engine; |
| 105 | } |
| 106 | |
| 107 | QList<QPolygonF> PathPaintDevice::paths() |
| 108 | { |
| 109 | return pagePaths; |
| 110 | } |
| 111 | |
| 112 | int PathPaintDevice::metric(PaintDeviceMetric metric) const |
| 113 | { |
| 114 | switch (metric) |
| 115 | { |
| 116 | // Width in pixels. |
| 117 | case PdmWidth: |
| 118 | return width * ppm; |
| 119 | case PdmHeight: |
| 120 | return height * ppm; |
| 121 | case PdmWidthMM: |
| 122 | return width; |
| 123 | case PdmHeightMM: |
| 124 | return height; |
| 125 | case PdmNumColors: |
| 126 | return 2; |
| 127 | case PdmDepth: |
| 128 | return 1; |
| 129 | case PdmDpiX: |
| 130 | return 25.4 * ppm; // Convert to inches. |
| 131 | case PdmDpiY: |
| 132 | return 25.4 * ppm; |
| 133 | case PdmPhysicalDpiX: |
| 134 | return 25.4 * ppm; |
| 135 | case PdmPhysicalDpiY: |
| 136 | return 25.4 * ppm; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | bool PathPaintDevice::clipped() const |
| 142 | { |
| 143 | return pathsClipped; |
| 144 | } |