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 <QtGui/QGraphicsPixmapItem>
20 #include <QtCore/QDebug>
21 #include <QtCore/QTimeLine>
25 #include "graphicsscene.h"
30 const GraphicsElement::AttributeDetails ATTRIBUTES[Pointer::ATTRIBUTE_COUNT] =
35 {"visiblewhen", false},
41 {"xrotationpoint", true},
42 {"yrotationpoint", true}
45 int const ANIMATION_TIME = 500;
46 int const ANIMATION_FRAMES = 50;
47 int const ANIMATION_UPDATEINTERVAL = 20;
50 Pointer::Pointer(Reader* reader, bool animate): GraphicsElement(reader),
51 xRotationPoint_(0), yRotationPoint_(0), x_(0), y_(0), zeroAngle_(0),
52 fullAngle_(180), zeroSpeed_(0), fullSpeed_(220), targetAngle_(0),
53 startAngle_(0), angle_(-1), imageSet_(false), animate_(animate), timer_(0)
55 element_ = new QGraphicsPixmapItem();
59 timer_ = new QTimeLine(ANIMATION_TIME);
60 timer_->setFrameRange(0, ANIMATION_FRAMES);
61 timer_->setUpdateInterval(ANIMATION_UPDATEINTERVAL);
62 connect(timer_, SIGNAL(frameChanged(int)), this, SLOT(setFrame(int)));
72 bool Pointer::setAttribute(QString const& name, QString const& value)
77 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
79 Attribute attr = static_cast<Attribute>(attrId);
90 element_->setZValue(intVal);
93 setVisibleWhen(strToVisibleWhen(value));
96 return loadImage(value);
111 xRotationPoint_ = intVal;
114 yRotationPoint_ = intVal;
117 qDebug() << "Unknown attribute: " << attr;
129 void Pointer::addToScene(GraphicsScene* scene)
136 element_->setTransformOriginPoint(xRotationPoint_, yRotationPoint_);
137 element_->setX(x_ - xRotationPoint_);
138 element_->setY(y_ - yRotationPoint_);
140 bool animateVal = animate_;
143 animate_ = animateVal;
145 scene->addItem(element_);
148 void Pointer::update()
150 double speed = Odometer::instance().getLatestFix().kmSpeed;
152 if(speed < zeroSpeed_)
154 speed = static_cast<double>(zeroSpeed_);
156 else if(speed > fullSpeed_)
158 speed = static_cast<double>(fullSpeed_);
161 double multiplier = static_cast<double>(speed - zeroSpeed_) / (fullSpeed_ - zeroSpeed_);
162 double angle = static_cast<double>(zeroAngle_) + ((fullAngle_ - zeroAngle_) * multiplier);
164 int rounded = static_cast<int>(round(angle));
166 if(rounded == angle_)
175 element_->setRotation(rounded);
179 targetAngle_ = rounded;
180 startAngle_ = element_->rotation();
182 if(timer_->state() == QTimeLine::Running)
191 void Pointer::setFrame(int frame)
193 element_->setRotation(startAngle_ + ((static_cast<double>(frame) / ANIMATION_FRAMES) * (targetAngle_ - startAngle_)));
196 bool Pointer::loadImage(QString const& name)
201 if(!readFile(name, data))
206 if(!pixmap.loadFromData(data))
208 setError("Invalid image file: " + name);
212 element_->setPixmap(pixmap);
218 QGraphicsItem* Pointer::getElement() const