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[Compass::ATTRIBUTE_COUNT] =
35 {"visiblewhen", false},
37 {"xrotationpoint", true},
38 {"yrotationpoint", true}
41 int const ANIMATION_TIME = 700;
42 int const ANIMATION_FRAMES = 80;
43 int const ANIMATION_UPDATEINTERVAL = 20;
44 double const COMPASS_IGNORE_THRESHOLD = 90.0;
47 Compass::Compass(Reader* reader, bool animate): GraphicsElement(reader),
48 xRotationPoint_(-1), yRotationPoint_(-1), x_(0), y_(0), targetAngle_(0),
49 startAngle_(0), angle_(-1), width_(0), height_(0), imageSet_(false), animate_(animate), timer_(0)
51 element_ = new QGraphicsPixmapItem();
55 timer_ = new QTimeLine(ANIMATION_TIME);
56 timer_->setFrameRange(0, ANIMATION_FRAMES);
57 timer_->setUpdateInterval(ANIMATION_UPDATEINTERVAL);
58 connect(timer_, SIGNAL(frameChanged(int)), this, SLOT(setFrame(int)));
68 bool Compass::setAttribute(QString const& name, QString const& value)
73 if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
75 Attribute attr = static_cast<Attribute>(attrId);
86 element_->setZValue(intVal);
89 setVisibleWhen(strToVisibleWhen(value));
92 return loadImage(value);
95 xRotationPoint_ = intVal;
98 yRotationPoint_ = intVal;
101 qDebug() << "Unknown attribute: " << attr;
113 void Compass::addToScene(GraphicsScene* scene)
120 if(xRotationPoint_ < 0)
122 xRotationPoint_ = width_ / 2;
124 if(yRotationPoint_ < 0)
126 yRotationPoint_ = height_ / 2;
129 element_->setTransformOriginPoint(xRotationPoint_, yRotationPoint_);
130 element_->setX(x_ - xRotationPoint_);
131 element_->setY(y_ - yRotationPoint_);
133 bool animateVal = animate_;
136 animate_ = animateVal;
138 scene->addItem(element_);
141 void Compass::update()
143 if(Odometer::instance().getLatestFix().epd > COMPASS_IGNORE_THRESHOLD)
148 double angle = Odometer::instance().getLatestFix().track * -1.0;
155 int rounded = static_cast<int>(round(angle));
157 if(rounded == angle_)
166 element_->setRotation(rounded);
170 targetAngle_ = rounded;
171 startAngle_ = element_->rotation();
173 if(timer_->state() == QTimeLine::Running)
182 void Compass::setFrame(int frame)
184 element_->setRotation(startAngle_ + ((static_cast<double>(frame) / ANIMATION_FRAMES) * (targetAngle_ - startAngle_)));
187 bool Compass::loadImage(QString const& name)
192 if(!readFile(name, data))
197 if(!pixmap.loadFromData(data))
199 setError("Invalid image file: " + name);
203 width_ = pixmap.width();
204 height_ = pixmap.height();
206 element_->setPixmap(pixmap);
212 QGraphicsItem* Compass::getElement() const