1 /*
2  * Copyright (C) 2015 Dan Leinir Turthra Jensen <admin@leinir.dk>
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 "AcbfBody.h"
23 #include "AcbfPage.h"
24 
25 #include <QXmlStreamReader>
26 
27 #include <acbf_debug.h>
28 
29 using namespace AdvancedComicBookFormat;
30 
31 class Body::Private
32 {
33 public:
Private()34     Private() {}
35     QString bgcolor;
36     QList<Page*> pages;
37 };
38 
Body(Document * parent)39 Body::Body(Document* parent)
40     : QObject(parent)
41     , d(new Private)
42 {
43     static const int typeId = qRegisterMetaType<Body*>("Body*");
44     Q_UNUSED(typeId);
45 }
46 
47 Body::~Body() = default;
48 
document() const49 Document * Body::document() const
50 {
51     return qobject_cast<Document*>(parent());
52 }
53 
toXml(QXmlStreamWriter * writer)54 void Body::toXml(QXmlStreamWriter *writer)
55 {
56     writer->writeStartElement(QStringLiteral("body"));
57 
58     for(Page* page : d->pages) {
59         page->toXml(writer);
60     }
61 
62     writer->writeEndElement();
63 }
64 
fromXml(QXmlStreamReader * xmlReader)65 bool Body::fromXml(QXmlStreamReader *xmlReader)
66 {
67     setBgcolor(xmlReader->attributes().value(QStringLiteral("bgcolor")).toString());
68     while(xmlReader->readNextStartElement())
69     {
70         if(xmlReader->name() == QStringLiteral("page"))
71         {
72             Page* newPage = new Page(document());
73             if(!newPage->fromXml(xmlReader)) {
74                 return false;
75             }
76             d->pages.append(newPage);
77         }
78         else
79         {
80             qCWarning(ACBF_LOG) << Q_FUNC_INFO << "currently unsupported subsection:" << xmlReader->name();
81             xmlReader->skipCurrentElement();
82         }
83     }
84     if (xmlReader->hasError()) {
85         qCWarning(ACBF_LOG) << Q_FUNC_INFO << "Failed to read ACBF XML document at token" << xmlReader->name() << "(" << xmlReader->lineNumber() << ":" << xmlReader->columnNumber() << ") The reported error was:" << xmlReader->errorString();
86     }
87     qCDebug(ACBF_LOG) << Q_FUNC_INFO << "Created body with" << d->pages.count() << "pages";
88     return !xmlReader->hasError();
89 }
90 
bgcolor() const91 QString Body::bgcolor() const
92 {
93     return d->bgcolor;
94 }
95 
setBgcolor(const QString & newColor)96 void Body::setBgcolor(const QString& newColor)
97 {
98     d->bgcolor = newColor;
99     emit bgcolorChanged();
100 }
101 
pages() const102 QList<Page *> Body::pages() const
103 {
104     return d->pages;
105 }
106 
page(int index) const107 Page * Body::page(int index) const
108 {
109     return d->pages.at(index);
110 }
111 
pageIndex(Page * page) const112 int Body::pageIndex(Page* page) const
113 {
114     return d->pages.indexOf(page);
115 }
116 
addPage(Page * page,int index)117 void Body::addPage(Page* page, int index)
118 {
119     if(index > -1 && d->pages.count() < index) {
120         d->pages.insert(index, page);
121     }
122     else {
123         d->pages.append(page);
124     }
125     emit pageCountChanged();
126 }
127 
removePage(Page * page)128 void Body::removePage(Page* page)
129 {
130     d->pages.removeAll(page);
131     emit pageCountChanged();
132 }
133 
swapPages(Page * swapThis,Page * withThis)134 bool Body::swapPages(Page* swapThis, Page* withThis)
135 {
136     int index1 = d->pages.indexOf(swapThis);
137     int index2 = d->pages.indexOf(withThis);
138     if(index1 > -1 && index2 > -1) {
139         d->pages.swapItemsAt(index1, index2);
140         emit pageCountChanged();
141         return true;
142     }
143     return false;
144 }
145 
pageCount()146 int Body::pageCount()
147 {
148     return d->pages.size();
149 }
150