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