added all files
[ffqwlibrary] / libffqw-1.0 / sources / ffchartbutton.cpp
1 /*
2           GNU GENERAL PUBLIC LICENSE
3                        Version 3, 29 June 2007
4
5  Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6  Everyone is permitted to copy and distribute verbatim copies
7  of this license document, but changing it is not allowed.
8
9                             Preamble
10
11   The GNU General Public License is a free, copyleft license for
12 software and other kinds of works.
13
14   The licenses for most software and other practical works are designed
15 to take away your freedom to share and change the works.  By contrast,
16 the GNU General Public License is intended to guarantee your freedom to
17 share and change all versions of a program--to make sure it remains free
18 software for all its users.  We, the Free Software Foundation, use the
19 GNU General Public License for most of our software; it applies also to
20 any other work released this way by its authors.  You can apply it to
21 your programs, too.
22
23   When we speak of free software, we are referring to freedom, not
24 price.  Our General Public Licenses are designed to make sure that you
25 have the freedom to distribute copies of free software (and charge for
26 them if you wish), that you receive source code or can get it if you
27 want it, that you can change the software or use pieces of it in new
28 free programs, and that you know you can do these things.
29
30   To protect your rights, we need to prevent others from denying you
31 these rights or asking you to surrender the rights.  Therefore, you have
32 certain responsibilities if you distribute copies of the software, or if
33 you modify it: responsibilities to respect the freedom of others.
34
35   For example, if you distribute copies of such a program, whether
36 gratis or for a fee, you must pass on to the recipients the same
37 freedoms that you received.  You must make sure that they, too, receive
38 or can get the source code.  And you must show them these terms so they
39 know their rights.
40
41   Developers that use the GNU GPL protect your rights with two steps:
42 (1) assert copyright on the software, and (2) offer you this License
43 giving you legal permission to copy, distribute and/or modify it.
44
45   For the developers' and authors' protection, the GPL clearly explains
46 that there is no warranty for this free software.  For both users' and
47 authors' sake, the GPL requires that modified versions be marked as
48 changed, so that their problems will not be attributed erroneously to
49 authors of previous versions.
50
51   Some devices are designed to deny users access to install or run
52 modified versions of the software inside them, although the manufacturer
53 can do so.  This is fundamentally incompatible with the aim of
54 protecting users' freedom to change the software.  The systematic
55 pattern of such abuse occurs in the area of products for individuals to
56 use, which is precisely where it is most unacceptable.  Therefore, we
57 have designed this version of the GPL to prohibit the practice for those
58 products.  If such problems arise substantially in other domains, we
59 stand ready to extend this provision to those domains in future versions
60 of the GPL, as needed to protect the freedom of users.
61
62   Finally, every program is threatened constantly by software patents.
63 States should not allow patents to restrict development and use of
64 software on general-purpose computers, but in those that do, we wish to
65 avoid the special danger that patents applied to a free program could
66 make it effectively proprietary.  To prevent this, the GPL assures that
67 patents cannot be used to render the program non-free.
68
69   The precise terms and conditions for copying, distribution and
70 modification follow.
71
72 http://www.gnu.org/licenses/gpl-3.0.txt
73 */
74 /**
75  * @file ffchartbutton.cpp
76  * @brief Implementation of the FFChartButton class.
77  *
78  * @author ComArch S.A.
79  * @date 2009.09.03
80  * @version 1.0
81  * Extend QGraphicsSvgItem to easy use 1 or 2 image and added functionality
82  * to decrease and increase item after click
83  */
84
85 #include "ffchartbutton.h"
86
87 /**
88  * Constructs a FFChartButton with a parent.
89  * Set variables to initial values.
90  */
91 FFChartButton::FFChartButton(QGraphicsItem* parent) :
92         QGraphicsSvgItem(parent)
93 {
94         init();
95 }
96
97 /**
98  * A virtual destructor of  FFChartButton.
99  */
100 FFChartButton::~FFChartButton()
101 {
102         ;
103 }
104
105 /**
106  * Set variables to initial values.
107  */
108 void FFChartButton::init()
109 {
110         //sets default values
111         state = false; //usefull when changing beetwen 2 images, it remember click state - what image should be display
112         twoImageON = false; //state to store if the button have 2 images
113         startedTimeLine = false; //state to store if time line ends
114
115         sizeRatio = 1;
116         frameRange_ = DEFAULT_FRAME_RANGE;
117         duration_ = DEFAULT_DURATION_TIME;
118         zoomRatio_ = DEFAULT_ZOOM_RATIO;
119         scaleWidth = 1;
120         scaleHeight = 1;
121
122         image = new FFViewCache;
123         image2 = new FFViewCache;
124
125         //sets animation's parameters
126         animation = new QTimeLine(duration_, this);
127         animation->setFrameRange(0, frameRange_);
128         animation->setUpdateInterval(duration_ / frameRange_);
129
130         //connects
131         connect(animation, SIGNAL(frameChanged(int)), this, SLOT(tick(int)));
132         connect(animation, SIGNAL(finished()), this, SLOT(animationFinished()));
133 }
134
135 /**
136  * Function responsible for animation (zooming)
137  * @param frame Number of the frame
138  */
139 void FFChartButton::tick(int frame)
140 {
141
142         qreal width = this->boundingRect().width();
143         qreal height = this->boundingRect().height();
144         qreal ratio = 1;
145         QGraphicsSvgItem::scale(1 / sizeRatio, 1 / sizeRatio);
146
147         //Setting scaling ratio
148         //first part of animation
149         if(frame <= frameRange_ / 2)
150         {
151                 ratio = ((zoomRatio_ - 1) * (frame)) / (frameRange_ / 2) + 1;
152         }
153         //second part of animation
154         else if(frame <= frameRange_)
155         {
156                 ratio = (zoomRatio_ - 1) * (frameRange_ - frame) / (frameRange_
157                                 / 2) + 1;
158         }
159
160         sizeRatio = ratio;
161         //change scale and posiotion of button
162         QGraphicsSvgItem::scale(ratio, ratio);
163         this->setPos(startingPosition.x() - ((ratio * width - width)
164                         * scaleWidth / 2), startingPosition.y() - ((ratio
165                         * height - height) * scaleHeight / 2));
166 }
167
168 /**
169  * Function to handle situation when animation reach end
170  */
171 void FFChartButton::animationFinished()
172 {
173         animation->stop();
174         sizeRatio = 1;
175         startedTimeLine = false;
176 }
177
178 /**
179  * Set image of item
180  * @param path Path to image
181  */
182 void FFChartButton::setImage(QString path)
183 {
184         image->init(path + ".svg");
185         image->updateView(image->defaultSize());
186         setSharedRenderer(image->renderer());
187
188         twoImageON = false;
189 }
190
191 /**
192  * Set images of item
193  * @param path Path to first image
194  * @param path2 Path to second image
195  */
196 void FFChartButton::setImage(QString path, QString path2)
197 {
198
199         image->init(path + ".svg");
200         image2->init(path2 + ".svg");
201
202         setSharedRenderer(image->renderer());
203         twoImageON = true;
204 }
205
206 /**
207  * Set size of item
208  * @param size New size of the item
209  */
210 void FFChartButton::setSize(const QSizeF& size)
211 {
212         qreal tmpScaleWidth = size.toSize().width() / boundingRect().width()
213                         * scaleWidth;
214         qreal tmpScaleHeight = size.toSize().height() / boundingRect().height()
215                         * scaleHeight;
216         scale(tmpScaleWidth, tmpScaleHeight);
217 }
218
219 /**
220  * Set if item will be increasing (zoomRatio>1) or will be decreasing (zoomRatio<1)
221  * @param zoomRatio How much size will be changing during animation
222  */
223 void FFChartButton::setZoomRatio(qreal zoomRatio)
224 {
225         zoomRatio_ = zoomRatio;
226 }
227
228 /**
229  * Set number of frame in animation
230  * @param frameRange Number of frames
231  */
232 void FFChartButton::setFrameRange(int frameRange)
233 {
234         frameRange_ = frameRange;
235         animation->setFrameRange(0, frameRange_);
236 }
237
238 /**
239  * Set how much time animation take
240  * @param duration Time of animation
241  */
242 void FFChartButton::setDuration(int duration)
243 {
244         duration_ = duration;
245         animation->setDuration(duration_);
246 }
247
248 /**
249  * Scale the item
250  * @param sx Size of scale in width
251  * @param sy Size of scale in height
252  */
253 void FFChartButton::scale(qreal sx, qreal sy)
254 {
255         scaleWidth = sx * scaleWidth;
256         scaleHeight = sy * scaleHeight;
257         QGraphicsSvgItem::scale(scaleWidth, scaleHeight);
258 }
259
260 /**
261  * Return if the item will be increasing (zoomRatio>1) or will be decreasing (zoomRatio<1)
262  */
263 qreal FFChartButton::zoomRatio()
264 {
265         return zoomRatio_;
266 }
267
268 /**
269  * Return number of frame in animation
270  */
271 int FFChartButton::frameRange()
272 {
273         return frameRange_;
274 }
275
276 /**
277  * Return how much time animation take
278  */
279 int FFChartButton::duration()
280 {
281         return duration_;
282 }
283
284 /**
285  * Support mouse release event.
286  * @param event Contains all informations about event.
287  */
288 void FFChartButton::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
289 {
290         Q_UNUSED(event)
291         //If button has two images they are toggled
292         if(twoImageON)
293         {
294                 if(state == false)
295                 {
296                         setSharedRenderer(image2->renderer());
297                 }
298                 else
299                 {
300                         setSharedRenderer(image->renderer());
301                 }
302                 state = !state;
303         }
304
305         //Starts animation
306         if(!startedTimeLine)
307         {
308                 startingPosition = this->pos();
309                 animation->start();
310                 startedTimeLine = true;
311         }
312         emit mouseRelease();
313 }
314
315 /**
316  * Support mouse press event.
317  * @param event Contains all informations about event.
318  */
319 void FFChartButton::mousePressEvent(QGraphicsSceneMouseEvent* event)
320 {
321         Q_UNUSED(event)
322         emit mousePress();
323 }
324
325 /**
326  * \fn void FFChartButton::mouseRelease()
327  * This signal is emitted when the item is released.
328  */
329
330 /**
331  * \fn void FFChartButton::mousePress()
332  * This signal is emitted when the item is pressed.
333  */