9abb612964818491036e839a27ba906d21c668fc
[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      {"src", false},
35      {"width", true},
36      {"height", true}
37     };
38 }
39
40 ImageElement::ImageElement(Reader* reader): GraphicsElement(reader),
41 width_(0), height_(0), imageSet_(false)
42 {
43     element_ = new QGraphicsPixmapItem();
44 }
45
46 bool ImageElement::setAttribute(QString const& name,
47                                 QString const& value)
48 {
49     int intVal = 0;
50     int attrId = -1;
51
52     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
53     {
54         Attribute attr = static_cast<Attribute>(attrId);
55
56         switch(attr)
57         {
58         case XPOS:
59             element_->setX(intVal);
60             break;
61         case YPOS:
62             element_->setY(intVal);
63             break;
64         case ZPOS:
65             element_->setZValue(intVal);
66             break;
67         case SRC:
68             return loadImage(value);
69             break;
70         case WIDTH:
71             width_ = intVal;
72             break;
73         case HEIGHT:
74             height_ = intVal;
75             break;
76         default:
77             qDebug() << "Unknown attribute: " << attr;
78             return false;
79         }
80
81         return true;
82     }
83     else
84     {
85         return false;
86     }
87 }
88
89 void ImageElement::addToScene(GraphicsScene* scene)
90 {
91     if(!imageSet_)
92     {
93         return;
94     }
95
96     QPixmap pix;
97
98     if(width_ > 0 && height_ > 0)
99     {
100         pix = pixmap_.scaled(width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
101     }
102     else if(width_ > 0)
103     {
104         pix = pixmap_.scaledToWidth(width_, Qt::SmoothTransformation);
105     }
106     else if(height_ > 0)
107     {
108         pix = pixmap_.scaledToHeight(height_, Qt::SmoothTransformation);
109     }
110     else
111     {
112         pix = pixmap_;
113     }
114
115     element_->setPixmap(pix);
116     scene->addItem(element_);
117 }
118
119 void ImageElement::update()
120 {
121 }
122
123 bool ImageElement::loadImage(QString const& name)
124 {
125     QByteArray data;
126
127     if(!readFile(name, data))
128     {
129         return false;
130     }
131
132     if(!pixmap_.loadFromData(data))
133     {
134         setError("Invalid image file: " + name);
135         return false;
136     }
137
138     imageSet_ = true;
139
140     return true;
141 }
142
143 QGraphicsItem* ImageElement::getElement() const
144 {
145     return element_;
146 }