Changeset View
Changeset View
Standalone View
Standalone View
src/acbf/AcbfFrame.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | | ||||
22 | #include "AcbfFrame.h" | ||||
23 | #include <QDebug> | ||||
24 | #include <QXmlStreamReader> | ||||
25 | | ||||
26 | using namespace AdvancedComicBookFormat; | ||||
27 | | ||||
28 | class Frame::Private | ||||
29 | { | ||||
30 | public: | ||||
31 | Private() | ||||
32 | {} | ||||
33 | QString bgcolor; | ||||
34 | QList<QPoint> points; | ||||
35 | }; | ||||
36 | | ||||
37 | Frame::Frame(Page* parent) | ||||
38 | : QObject(parent) | ||||
39 | , d(new Private) | ||||
40 | { | ||||
41 | } | ||||
42 | | ||||
43 | Frame::~Frame() = default; | ||||
44 | | ||||
45 | void Frame::toXml(QXmlStreamWriter* writer) { | ||||
46 | writer->writeStartElement(QStringLiteral("frame")); | ||||
47 | | ||||
48 | QStringList points; | ||||
49 | Q_FOREACH(const QPoint& point, d->points) { | ||||
50 | points << QStringLiteral("%1,%2").arg(QString::number(point.x())).arg(QString::number(point.y())); | ||||
51 | } | ||||
52 | writer->writeAttribute(QStringLiteral("points"), points.join(' ')); | ||||
53 | if(!d->bgcolor.isEmpty()) { | ||||
54 | writer->writeAttribute(QStringLiteral("bgcolor"), d->bgcolor); | ||||
55 | } | ||||
56 | writer->writeEndElement(); | ||||
57 | } | ||||
58 | | ||||
59 | bool Frame::fromXml(QXmlStreamReader *xmlReader) | ||||
60 | { | ||||
61 | setBgcolor(xmlReader->attributes().value(QStringLiteral("bgcolor")).toString()); | ||||
62 | | ||||
63 | QStringList points = xmlReader->attributes().value(QStringLiteral("points")).toString().split(' '); | ||||
64 | | ||||
65 | Q_FOREACH(const QString& point, points) { | ||||
66 | QStringList elements = point.split(','); | ||||
67 | if(elements.length() == 2) | ||||
68 | { | ||||
69 | addPoint(QPoint(elements.at(0).toInt(), elements.at(1).toInt())); | ||||
70 | } | ||||
71 | else | ||||
72 | { | ||||
73 | qWarning() << "Failed to construct one of the points for a frame. Attempted to handle the point" << point << "in the data" << points; | ||||
74 | return false; | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | if (xmlReader->hasError()) { | ||||
79 | qWarning() << Q_FUNC_INFO << "Failed to read ACBF XML document at token" << xmlReader->name() << "(" << xmlReader->lineNumber() << ":" << xmlReader->columnNumber() << ") The reported error was:" << xmlReader->errorString(); | ||||
80 | } | ||||
81 | qDebug() << Q_FUNC_INFO << "Created a frame with " << points.count() << "points"; | ||||
82 | | ||||
83 | return !xmlReader->hasError(); | ||||
84 | } | ||||
85 | | ||||
86 | QList<QPoint> Frame::points() const | ||||
87 | { | ||||
88 | return d->points; | ||||
89 | } | ||||
90 | | ||||
91 | QPoint Frame::point(int index) const | ||||
92 | { | ||||
93 | return d->points.at(index); | ||||
94 | } | ||||
95 | | ||||
96 | int Frame::pointIndex(const QPoint& point) const | ||||
97 | { | ||||
98 | return d->points.indexOf(point); | ||||
99 | } | ||||
100 | | ||||
101 | void Frame::addPoint(const QPoint& point, int index) | ||||
102 | { | ||||
103 | if(index > -1 && d->points.count() < index) { | ||||
104 | d->points.insert(index, point); | ||||
105 | } | ||||
106 | else { | ||||
107 | d->points.append(point); | ||||
108 | } | ||||
109 | } | ||||
110 | | ||||
111 | void Frame::removePoint(const QPoint& point) | ||||
112 | { | ||||
113 | d->points.removeAll(point); | ||||
114 | } | ||||
115 | | ||||
116 | bool Frame::swapPoints(const QPoint& swapThis, const QPoint& withThis) | ||||
117 | { | ||||
118 | int index1 = d->points.indexOf(swapThis); | ||||
119 | int index2 = d->points.indexOf(withThis); | ||||
120 | if(index1 > -1 && index2 > -1) { | ||||
121 | d->points.swap(index1, index2); | ||||
122 | return true; | ||||
123 | } | ||||
124 | return false; | ||||
125 | } | ||||
126 | | ||||
127 | QString Frame::bgcolor() const | ||||
128 | { | ||||
129 | return d->bgcolor; | ||||
130 | } | ||||
131 | | ||||
132 | void Frame::setBgcolor(const QString& newColor) | ||||
133 | { | ||||
134 | d->bgcolor = newColor; | ||||
135 | } |