2 * This file is part of jSpeed.
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.
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.
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/>.
19 #include <QtCore/QString>
20 #include <QtCore/QDebug>
21 #include <QtGui/QGraphicsScene>
22 #include <QtGui/QGraphicsPixmapItem>
23 #include "imageelement.h"
25 #include "graphicsscene.h"
29 const GraphicsElement::AttributeDetails ATTRIBUTES[ImageElement::ATTRIBUTE_COUNT] =
34 {"visiblewhen", false},
41 ImageElement::ImageElement(Reader* reader): GraphicsElement(reader),
42 width_(0), height_(0), imageSet_(false)
44 element_ = new QGraphicsPixmapItem();
47 bool ImageElement::setAttribute(QString const& name,
53 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
55 Attribute attr = static_cast<Attribute>(attrId);
60 element_->setX(intVal);
63 element_->setY(intVal);
66 element_->setZValue(intVal);
69 setVisibleWhen(strToVisibleWhen(value));
72 return loadImage(value);
81 qDebug() << "Unknown attribute: " << attr;
93 void ImageElement::addToScene(GraphicsScene* scene)
102 if(width_ > 0 && height_ > 0)
104 pix = pixmap_.scaled(width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
108 pix = pixmap_.scaledToWidth(width_, Qt::SmoothTransformation);
112 pix = pixmap_.scaledToHeight(height_, Qt::SmoothTransformation);
119 element_->setPixmap(pix);
120 scene->addItem(element_);
123 void ImageElement::update()
127 bool ImageElement::loadImage(QString const& name)
131 if(!readFile(name, data))
136 if(!pixmap_.loadFromData(data))
138 setError("Invalid image file: " + name);
147 QGraphicsItem* ImageElement::getElement() const