Changeset View
Changeset View
Standalone View
Standalone View
libs/flake/resources/KoGamutMask.cpp
1 | #include "KoGamutMask.h" | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 Anna Medonosova <anna.medonosova@gmail.com> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Lesser General Public License as published by | ||||
6 | * the Free Software Foundation; version 2.1 of the License. | ||||
7 | * | ||||
8 | * This library is distributed in the hope that it will be useful, | ||||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
11 | * GNU Lesser General Public License for more details. | ||||
12 | * | ||||
13 | * You should have received a copy of the GNU Lesser General Public License | ||||
14 | * along with this program; if not, write to the Free Software | ||||
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
16 | */ | ||||
17 | | ||||
18 | #include "KoGamutMask.h" | ||||
2 | 19 | | |||
3 | #include <cstring> | 20 | #include <cstring> | ||
4 | 21 | | |||
5 | #include <QVector> | 22 | #include <QVector> | ||
6 | #include <QString> | 23 | #include <QString> | ||
7 | #include <QFile> | 24 | #include <QFile> | ||
8 | #include <QList> | 25 | #include <QList> | ||
9 | #include <QDomDocument> | 26 | #include <QDomDocument> | ||
Show All 39 Lines | |||||
49 | void KoGamutMaskShape::paint(QPainter &painter, const KoViewConverter& viewConverter) | 66 | void KoGamutMaskShape::paint(QPainter &painter, const KoViewConverter& viewConverter) | ||
50 | { | 67 | { | ||
51 | painter.save(); | 68 | painter.save(); | ||
52 | painter.setTransform(m_maskShape->absoluteTransformation(&viewConverter) * painter.transform()); | 69 | painter.setTransform(m_maskShape->absoluteTransformation(&viewConverter) * painter.transform()); | ||
53 | m_maskShape->paint(painter, viewConverter, m_shapePaintingContext); | 70 | m_maskShape->paint(painter, viewConverter, m_shapePaintingContext); | ||
54 | painter.restore(); | 71 | painter.restore(); | ||
55 | } | 72 | } | ||
56 | 73 | | |||
74 | void KoGamutMaskShape::paintStroke(QPainter &painter, const KoViewConverter &viewConverter) | ||||
75 | { | ||||
76 | painter.save(); | ||||
77 | painter.setTransform(m_maskShape->absoluteTransformation(&viewConverter) * painter.transform()); | ||||
78 | m_maskShape->paintStroke(painter, viewConverter, m_shapePaintingContext); | ||||
79 | painter.restore(); | ||||
80 | | ||||
81 | } | ||||
57 | 82 | | |||
58 | struct Q_DECL_HIDDEN KoGamutMask::Private { | 83 | struct Q_DECL_HIDDEN KoGamutMask::Private { | ||
59 | QString name; | 84 | QString name; | ||
60 | QString title; | 85 | QString title; | ||
61 | QString description; | 86 | QString description; | ||
62 | QByteArray data; | 87 | QByteArray data; | ||
63 | QVector<KoGamutMaskShape*> maskShapes; | 88 | QVector<KoGamutMaskShape*> maskShapes; | ||
64 | QVector<KoGamutMaskShape*> previewShapes; | 89 | QVector<KoGamutMaskShape*> previewShapes; | ||
65 | QSizeF maskSize; // at 100DPI | 90 | QSizeF maskSize; | ||
66 | }; | 91 | }; | ||
67 | 92 | | |||
68 | KoGamutMask::KoGamutMask(const QString& filename) | 93 | KoGamutMask::KoGamutMask(const QString& filename) | ||
69 | : KoResource(filename) | 94 | : KoResource(filename) | ||
70 | , d(new Private()) | 95 | , d(new Private()) | ||
71 | { | 96 | { | ||
72 | 97 | d->maskSize = QSizeF(144.0,144.0); | |||
73 | } | 98 | } | ||
74 | 99 | | |||
75 | KoGamutMask::KoGamutMask() | 100 | KoGamutMask::KoGamutMask() | ||
76 | : KoResource(QString()) | 101 | : KoResource(QString()) | ||
77 | , d(new Private()) | 102 | , d(new Private()) | ||
78 | { | 103 | { | ||
104 | d->maskSize = QSizeF(144.0,144.0); | ||||
79 | } | 105 | } | ||
80 | 106 | | |||
81 | KoGamutMask::KoGamutMask(KoGamutMask* rhs) | 107 | KoGamutMask::KoGamutMask(KoGamutMask* rhs) | ||
82 | : QObject(0) | 108 | : QObject(0) | ||
83 | , KoResource(QString()) | 109 | , KoResource(QString()) | ||
84 | , d(new Private()) | 110 | , d(new Private()) | ||
85 | { | 111 | { | ||
86 | setFilename(rhs->filename()); | 112 | setFilename(rhs->filename()); | ||
87 | setTitle(rhs->title()); | 113 | setTitle(rhs->title()); | ||
88 | setDescription(rhs->description()); | 114 | setDescription(rhs->description()); | ||
89 | d->maskSize = rhs->d->maskSize; | 115 | d->maskSize = rhs->d->maskSize; | ||
90 | 116 | | |||
91 | QList<KoShape*> newShapes; | 117 | QList<KoShape*> newShapes; | ||
92 | for(KoGamutMaskShape* sh: d->maskShapes) { | 118 | for(KoShape* sh: rhs->koShapes()) { | ||
93 | KoShape* shape = sh->koShape(); | 119 | newShapes.append(sh->cloneShape()); | ||
94 | newShapes.append(shape); | | |||
95 | } | 120 | } | ||
96 | 121 | | |||
97 | setMaskShapes(newShapes); | 122 | setMaskShapes(newShapes); | ||
98 | 123 | | |||
99 | setValid(true); | 124 | setValid(true); | ||
100 | } | 125 | } | ||
101 | 126 | | |||
102 | 127 | | |||
Show All 26 Lines | 153 | } else { | |||
129 | shapeVector = &d->maskShapes; | 154 | shapeVector = &d->maskShapes; | ||
130 | } | 155 | } | ||
131 | 156 | | |||
132 | for(KoGamutMaskShape* shape: *shapeVector) { | 157 | for(KoGamutMaskShape* shape: *shapeVector) { | ||
133 | shape->paint(painter, viewConverter); | 158 | shape->paint(painter, viewConverter); | ||
134 | } | 159 | } | ||
135 | } | 160 | } | ||
136 | 161 | | |||
162 | void KoGamutMask::paintStroke(QPainter &painter, KoViewConverter &viewConverter, bool preview) | ||||
163 | { | ||||
164 | QVector<KoGamutMaskShape*>* shapeVector; | ||||
165 | | ||||
166 | if (preview && !d->previewShapes.isEmpty()) { | ||||
167 | shapeVector = &d->previewShapes; | ||||
168 | } else { | ||||
169 | shapeVector = &d->maskShapes; | ||||
170 | } | ||||
171 | | ||||
172 | for(KoGamutMaskShape* shape: *shapeVector) { | ||||
173 | shape->paintStroke(painter, viewConverter); | ||||
174 | } | ||||
175 | } | ||||
176 | | ||||
137 | bool KoGamutMask::load() | 177 | bool KoGamutMask::load() | ||
138 | { | 178 | { | ||
139 | QFile file(filename()); | 179 | QFile file(filename()); | ||
140 | if (file.size() == 0) return false; | 180 | if (file.size() == 0) return false; | ||
141 | if (!file.open(QIODevice::ReadOnly)) { | 181 | if (!file.open(QIODevice::ReadOnly)) { | ||
142 | warnFlake << "Can't open file " << filename(); | 182 | warnFlake << "Can't open file " << filename(); | ||
143 | return false; | 183 | return false; | ||
144 | } | 184 | } | ||
145 | bool res = loadFromDevice(&file); | 185 | bool res = loadFromDevice(&file); | ||
146 | setValid(res); | | |||
147 | file.close(); | 186 | file.close(); | ||
148 | return res; | 187 | return res; | ||
149 | } | 188 | } | ||
150 | 189 | | |||
151 | bool KoGamutMask::loadFromDevice(QIODevice *dev) | 190 | bool KoGamutMask::loadFromDevice(QIODevice *dev) | ||
152 | { | 191 | { | ||
153 | if (!dev->isOpen()) dev->open(QIODevice::ReadOnly); | 192 | if (!dev->isOpen()) dev->open(QIODevice::ReadOnly); | ||
154 | 193 | | |||
155 | d->data = dev->readAll(); | 194 | d->data = dev->readAll(); | ||
156 | 195 | | |||
157 | Q_ASSERT(d->data.size() != 0); | 196 | // TODO: test | ||
197 | KIS_ASSERT_RECOVER_RETURN_VALUE(d->data.size() != 0, false); | ||||
dkazakov: Looks like a forgotten comment :) | |||||
158 | 198 | | |||
159 | if (filename().isNull()) { | 199 | if (filename().isNull()) { | ||
160 | warnFlake << "Cannot load gamut mask" << name() << "there is no filename set"; | 200 | warnFlake << "Cannot load gamut mask" << name() << "there is no filename set"; | ||
161 | return false; | 201 | return false; | ||
162 | } | 202 | } | ||
163 | 203 | | |||
164 | if (d->data.isNull()) { | 204 | if (d->data.isNull()) { | ||
165 | QFile file(filename()); | 205 | QFile file(filename()); | ||
Show All 19 Lines | |||||
185 | QByteArray data; | 225 | QByteArray data; | ||
186 | data.resize(store->size()); | 226 | data.resize(store->size()); | ||
187 | QByteArray ba = store->read(store->size()); | 227 | QByteArray ba = store->read(store->size()); | ||
188 | store->close(); | 228 | store->close(); | ||
189 | 229 | | |||
190 | KoXmlDocument xmlDocument; | 230 | KoXmlDocument xmlDocument; | ||
191 | QString errorMsg; | 231 | QString errorMsg; | ||
192 | int errorLine = 0; | 232 | int errorLine = 0; | ||
193 | int errorColumn; | 233 | int errorColumn = 0; | ||
194 | 234 | | |||
195 | bool ok = xmlDocument.setContent(ba, false, &errorMsg, &errorLine, &errorColumn); | 235 | bool ok = xmlDocument.setContent(ba, false, &errorMsg, &errorLine, &errorColumn); | ||
196 | if (!ok) { | 236 | if (!ok) { | ||
197 | 237 | | |||
198 | errorFlake << "Parsing error in " << filename() << "! Aborting!" << endl | 238 | errorFlake << "Parsing error in " << filename() << "! Aborting!" << endl | ||
199 | << " In line: " << errorLine << ", column: " << errorColumn << endl | 239 | << " In line: " << errorLine << ", column: " << errorColumn << endl | ||
200 | << " Error message: " << errorMsg << endl; | 240 | << " Error message: " << errorMsg << endl; | ||
201 | errorFlake << "Parsing error in the main document at line" << errorLine | 241 | errorFlake << "Parsing error in the main document at line" << errorLine | ||
Show All 26 Lines | 263 | if (store->open("preview.png")) { | |||
228 | preview.load(&previewDev, "PNG"); | 268 | preview.load(&previewDev, "PNG"); | ||
229 | setImage(preview); | 269 | setImage(preview); | ||
230 | 270 | | |||
231 | (void)store->close(); | 271 | (void)store->close(); | ||
232 | } | 272 | } | ||
233 | 273 | | |||
234 | buf.close(); | 274 | buf.close(); | ||
235 | 275 | | |||
276 | setValid(true); | ||||
277 | | ||||
236 | return true; | 278 | return true; | ||
237 | } | 279 | } | ||
238 | 280 | | |||
239 | void KoGamutMask::setMaskShapes(QList<KoShape*> shapes) | 281 | void KoGamutMask::setMaskShapes(QList<KoShape*> shapes) | ||
240 | { | 282 | { | ||
241 | setMaskShapesToVector(shapes, d->maskShapes); | 283 | setMaskShapesToVector(shapes, d->maskShapes); | ||
242 | } | 284 | } | ||
243 | 285 | | |||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | 363 | { | |||
322 | d->description = description; | 364 | d->description = description; | ||
323 | } | 365 | } | ||
324 | 366 | | |||
325 | QSizeF KoGamutMask::maskSize() | 367 | QSizeF KoGamutMask::maskSize() | ||
326 | { | 368 | { | ||
327 | return d->maskSize; | 369 | return d->maskSize; | ||
328 | } | 370 | } | ||
329 | 371 | | |||
330 | // TODO: rethink preview | | |||
331 | void KoGamutMask::setPreviewMaskShapes(QList<KoShape*> shapes) | 372 | void KoGamutMask::setPreviewMaskShapes(QList<KoShape*> shapes) | ||
332 | { | 373 | { | ||
333 | setMaskShapesToVector(shapes, d->previewShapes); | 374 | setMaskShapesToVector(shapes, d->previewShapes); | ||
334 | } | 375 | } | ||
335 | 376 | | |||
336 | void KoGamutMask::setMaskShapesToVector(QList<KoShape *> shapes, QVector<KoGamutMaskShape *> &targetVector) | 377 | void KoGamutMask::setMaskShapesToVector(QList<KoShape *> shapes, QVector<KoGamutMaskShape *> &targetVector) | ||
337 | { | 378 | { | ||
338 | targetVector.clear(); | 379 | targetVector.clear(); | ||
Show All 12 Lines |
Looks like a forgotten comment :)