All Classes Namespaces Functions Variables Enumerations Properties Pages
bezierarea.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
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 */
17 
18 
19 #include "bezierarea.h"
20 #include "pencilerror.h"
21 
22 #include <QXmlStreamWriter>
23 #include <QDomElement>
24 
25 BezierArea::BezierArea()
26 {
27 }
28 
29 BezierArea::BezierArea(QList<VertexRef> vertexList, int color)
30 {
31  mVertex = vertexList;
32  mColorNumber = color;
33  mSelected = false;
34 }
35 
36 VertexRef BezierArea::getVertexRef(int i)
37 {
38  while (i >= mVertex.size() )
39  {
40  i = i - mVertex.size();
41  }
42  while (i < 0 )
43  {
44  i = i + mVertex.size();
45  }
46  return mVertex[i];
47 }
48 
49 void BezierArea::setSelected(bool YesOrNo)
50 {
51  mSelected = YesOrNo;
52 }
53 
54 Status BezierArea::createDomElement( QXmlStreamWriter& xmlStream )
55 {
56  xmlStream.writeStartElement( "area" );
57  xmlStream.writeAttribute( "colourNumber", QString::number( mColorNumber ) );
58  xmlStream.writeAttribute("filled", QString::number( mIsFilled ) );
59 
60  int errorLocation = -1;
61  for ( int i = 0; i < mVertex.size(); i++ )
62  {
63  xmlStream.writeEmptyElement( "vertex" );
64  xmlStream.writeAttribute( "curve", QString::number( mVertex.at( i ).curveNumber ) );
65  xmlStream.writeAttribute( "vertex", QString::number( mVertex.at( i ).vertexNumber ) );
66 
67  if ( errorLocation < 0 && xmlStream.hasError() )
68  {
69  errorLocation = i;
70  }
71  }
72 
73  xmlStream.writeEndElement(); // Close area element
74 
75  if ( xmlStream.hasError() && errorLocation >= 0 )
76  {
77  DebugDetails debugInfo;
78  debugInfo << "BezierArea::createDomElement";
79  debugInfo << QString("colorNumber = %1").arg(mColorNumber);
80  debugInfo << QString("- mVertex[%1] has failed to write").arg(errorLocation);
81  debugInfo << QString("&nbsp;&nbsp;curve = %1").arg(mVertex.at(errorLocation).curveNumber);
82  debugInfo << QString("&nbsp;&nbsp;vertex = %1 ").arg(mVertex.at(errorLocation).vertexNumber);
83 
84  return Status( Status::FAIL, debugInfo );
85  }
86 
87  return Status::OK;
88 }
89 
90 void BezierArea::loadDomElement(const QDomElement& element)
91 {
92  mColorNumber = element.attribute("colourNumber").toInt();
93 
94  QDomNode vertexTag = element.firstChild();
95  while (!vertexTag.isNull())
96  {
97  QDomElement vertexElement = vertexTag.toElement();
98  if (!vertexElement.isNull())
99  {
100  if (vertexElement.tagName() == "vertex")
101  {
102  mVertex.append( VertexRef(vertexElement.attribute("curve").toInt() , vertexElement.attribute("vertex").toInt() ) );
103  }
104  }
105  vertexTag = vertexTag.nextSibling();
106  }
107 }
QString attribute(const QString &name, const QString &defValue) const const
bool hasError() const const
const T & at(int i) const const
int size() const const
QDomNode nextSibling() const const
QDomElement toElement() const const
QString number(int n, int base)
void append(const T &value)
int toInt(bool *ok, int base) const const
bool isNull() const const
void writeAttribute(const QString &qualifiedName, const QString &value)
QDomNode firstChild() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString tagName() const const
void writeEmptyElement(const QString &qualifiedName)
void writeEndElement()
void writeStartElement(const QString &qualifiedName)