Fixes to speed alarm and poi alerts. Added flicker effect. Some new fields to text...
[jspeed] / src / flickereffect.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 <QtCore/QTimer>
22 #include <QtGui/QGraphicsItem>
23 #include <math.h>
24 #include "flickereffect.h"
25
26 namespace
27 {
28     Effect::AttributeDetails ATTRIBUTES[FlickerEffect::ATTRIBUTE_COUNT] =
29     {
30      {"interval", true}
31     };
32 }
33
34 FlickerEffect::FlickerEffect(): QObject(0), Effect(), interval_(500), timer_(0), item_(0)
35 {
36 }
37
38 FlickerEffect::~FlickerEffect()
39 {
40     delete timer_;
41 }
42
43 bool FlickerEffect::setAttribute(QString const& name, QString const& value)
44 {
45     qreal realVal = 0;
46     int attrId = -1;
47
48     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, realVal)) != -1)
49     {
50         Attribute attr = static_cast<Attribute>(attrId);
51
52         switch(attr)
53         {
54         case INTERVAL:
55             interval_ = round(realVal);
56             break;
57         default:
58             qDebug() << "Unknown attribute: " << attr;
59             return false;
60         }
61
62         return true;
63     }
64     else
65     {
66         return false;
67     }
68 }
69
70 void FlickerEffect::apply(QGraphicsItem* item)
71 {
72     if(interval_ > 0)
73     {
74         item_ = item;
75         timer_ = new QTimer;
76         timer_->setInterval(interval_);
77         connect(timer_, SIGNAL(timeout()), this, SLOT(updateVisibility()));
78         timer_->start();
79     }
80 }
81
82 void FlickerEffect::updateVisibility()
83 {
84     if(item_)
85     {
86         item_->setVisible(!item_->isVisible());
87     }
88 }