...
[jspeed] / src / imageelement.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QString>
20 #include <QtCore/QDebug>
21 #include <QtGui/QGraphicsScene>
22 #include <QtGui/QGraphicsPixmapItem>
23 #include "imageelement.h"
24 #include "reader.h"
25 #include "graphicsscene.h"
26
27 namespace
28 {
29     const GraphicsElement::AttributeDetails ATTRIBUTES[ImageElement::ATTRIBUTE_COUNT] =
30     {
31      {"xpos", true},
32      {"ypos", true},
33      {"zpos", true},
34      {"visiblewhen", false},
35      {"src", false},
36      {"width", true},
37      {"height", true}
38     };
39 }
40
41 ImageElement::ImageElement(Reader* reader): GraphicsElement(reader),
42 width_(0), height_(0), imageSet_(false)
43 {
44     element_ = new QGraphicsPixmapItem();
45 }
46
47 bool ImageElement::setAttribute(QString const& name,
48                                 QString const& value)
49 {
50     int intVal = 0;
51     int attrId = -1;
52
53     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
54     {
55         Attribute attr = static_cast<Attribute>(attrId);
56
57         switch(attr)
58         {
59         case XPOS:
60             element_->setX(intVal);
61             break;
62         case YPOS:
63             element_->setY(intVal);
64             break;
65         case ZPOS:
66             element_->setZValue(intVal);
67             break;
68         case VISIBLEWHEN:
69             setVisibleWhen(strToVisibleWhen(value));
70             break;
71         case SRC:
72             return loadImage(value);
73             break;
74         case WIDTH:
75             width_ = intVal;
76             break;
77         case HEIGHT:
78             height_ = intVal;
79             break;
80         default:
81             qDebug() << "Unknown attribute: " << attr;
82             return false;
83         }
84
85         return true;
86     }
87     else
88     {
89         return false;
90     }
91 }
92
93 void ImageElement::addToScene(GraphicsScene* scene)
94 {
95     if(!imageSet_)
96     {
97         return;
98     }
99
100     QPixmap pix;
101
102     if(width_ > 0 && height_ > 0)
103     {
104         pix = pixmap_.scaled(width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
105     }
106     else if(width_ > 0)
107     {
108         pix = pixmap_.scaledToWidth(width_, Qt::SmoothTransformation);
109     }
110     else if(height_ > 0)
111     {
112         pix = pixmap_.scaledToHeight(height_, Qt::SmoothTransformation);
113     }
114     else
115     {
116         pix = pixmap_;
117     }
118
119     element_->setPixmap(pix);
120     scene->addItem(element_);
121 }
122
123 void ImageElement::update()
124 {
125 }
126
127 bool ImageElement::loadImage(QString const& name)
128 {
129     QByteArray data;
130
131     if(!readFile(name, data))
132     {
133         return false;
134     }
135
136     if(!pixmap_.loadFromData(data))
137     {
138         setError("Invalid image file: " + name);
139         return false;
140     }
141
142     imageSet_ = true;
143
144     return true;
145 }
146
147 QGraphicsItem* ImageElement::getElement() const
148 {
149     return element_;
150 }