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