added all files
[ffqwlibrary] / libffqw-1.0 / sources / ffscrollarea.cpp
1 /*\r
2           GNU GENERAL PUBLIC LICENSE\r
3                        Version 3, 29 June 2007\r
4 \r
5  Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>\r
6  Everyone is permitted to copy and distribute verbatim copies\r
7  of this license document, but changing it is not allowed.\r
8 \r
9                             Preamble\r
10 \r
11   The GNU General Public License is a free, copyleft license for\r
12 software and other kinds of works.\r
13 \r
14   The licenses for most software and other practical works are designed\r
15 to take away your freedom to share and change the works.  By contrast,\r
16 the GNU General Public License is intended to guarantee your freedom to\r
17 share and change all versions of a program--to make sure it remains free\r
18 software for all its users.  We, the Free Software Foundation, use the\r
19 GNU General Public License for most of our software; it applies also to\r
20 any other work released this way by its authors.  You can apply it to\r
21 your programs, too.\r
22 \r
23   When we speak of free software, we are referring to freedom, not\r
24 price.  Our General Public Licenses are designed to make sure that you\r
25 have the freedom to distribute copies of free software (and charge for\r
26 them if you wish), that you receive source code or can get it if you\r
27 want it, that you can change the software or use pieces of it in new\r
28 free programs, and that you know you can do these things.\r
29 \r
30   To protect your rights, we need to prevent others from denying you\r
31 these rights or asking you to surrender the rights.  Therefore, you have\r
32 certain responsibilities if you distribute copies of the software, or if\r
33 you modify it: responsibilities to respect the freedom of others.\r
34 \r
35   For example, if you distribute copies of such a program, whether\r
36 gratis or for a fee, you must pass on to the recipients the same\r
37 freedoms that you received.  You must make sure that they, too, receive\r
38 or can get the source code.  And you must show them these terms so they\r
39 know their rights.\r
40 \r
41   Developers that use the GNU GPL protect your rights with two steps:\r
42 (1) assert copyright on the software, and (2) offer you this License\r
43 giving you legal permission to copy, distribute and/or modify it.\r
44 \r
45   For the developers' and authors' protection, the GPL clearly explains\r
46 that there is no warranty for this free software.  For both users' and\r
47 authors' sake, the GPL requires that modified versions be marked as\r
48 changed, so that their problems will not be attributed erroneously to\r
49 authors of previous versions.\r
50 \r
51   Some devices are designed to deny users access to install or run\r
52 modified versions of the software inside them, although the manufacturer\r
53 can do so.  This is fundamentally incompatible with the aim of\r
54 protecting users' freedom to change the software.  The systematic\r
55 pattern of such abuse occurs in the area of products for individuals to\r
56 use, which is precisely where it is most unacceptable.  Therefore, we\r
57 have designed this version of the GPL to prohibit the practice for those\r
58 products.  If such problems arise substantially in other domains, we\r
59 stand ready to extend this provision to those domains in future versions\r
60 of the GPL, as needed to protect the freedom of users.\r
61 \r
62   Finally, every program is threatened constantly by software patents.\r
63 States should not allow patents to restrict development and use of\r
64 software on general-purpose computers, but in those that do, we wish to\r
65 avoid the special danger that patents applied to a free program could\r
66 make it effectively proprietary.  To prevent this, the GPL assures that\r
67 patents cannot be used to render the program non-free.\r
68 \r
69   The precise terms and conditions for copying, distribution and\r
70 modification follow.\r
71 \r
72 http://www.gnu.org/licenses/gpl-3.0.txt\r
73 */\r
74 /**\r
75  * @file ffscrollarea.cpp\r
76  * @brief Implementation of the FFScrollArea class\r
77  *\r
78  * @author ComArch S.A.\r
79  * @date 2009.09.20\r
80  * @version 1.0\r
81  */\r
82 \r
83 #include "ffscrollarea.h"\r
84 #include <QDebug>\r
85 #include <QPen>\r
86 #include <QScrollBar>\r
87 #include <QPainter>\r
88 \r
89 //defines view of scrollbars\r
90 #define SCROLLBAR_STYLE " \\r
91         QScrollBar:vertical, QScrollBar:horizontal { \\r
92                 border: 0px solid transparent; \\r
93                 background: transparent; \\r
94                 width: 5px; \\r
95                 height: 5px; /* change this to 5px or more to see horizontal scrollbar */\\r
96                 margin: 2px 2px 2px 2px; \\r
97         } \\r
98         QScrollBar::sub-page, QScrollBar::add-page { \\r
99                 background: rgb(45,45,45); \\r
100         } \\r
101         QScrollBar::handle:vertical { \\r
102                 background: qlineargradient(x1:0, y1:0, x2:0, y2:1, \\r
103                                             stop:0 rgba(30,30,30,0), stop: 0.1 #1d1, stop:0.9 #2e2, stop: 1 rgba(30,30,30,0)); \\r
104                 min-height: 25px; \\r
105         } \\r
106         QScrollBar::handle:horizontal { \\r
107                 background: qlineargradient(x1:0, y1:0, x2:1, y2:0, \\r
108                                             stop:0 rgba(30,30,30,0), stop: 0.1 #1d1, stop:0.9 #2e2, stop: 1 rgba(30,30,30,0)); \\r
109                 min-width: 25px; \\r
110         } \\r
111         QScrollBar::add-line, QScrollBar::sub-line { \\r
112                 border: 0px solid transparent; \\r
113         } \\r
114         QScrollArea { \\r
115                 background-color: #222; color: #ddd; \\r
116         } \\r
117 "\r
118 \r
119 /**\r
120  * Constructs FFScrollArea with parent\r
121  */\r
122 FFScrollArea::FFScrollArea(QWidget* parent) :\r
123         QScrollArea(parent)\r
124 {\r
125         init();\r
126 }\r
127 /**\r
128  * A virtual destructor\r
129  */\r
130 FFScrollArea::~FFScrollArea()\r
131 {\r
132 \r
133 }\r
134 /**\r
135  * Initiates FFScrollArea class. Sets all needed fields, connections and calls.\r
136  * It is called by the constructor.\r
137  */\r
138 void FFScrollArea::init()\r
139 {\r
140         //sets FFScrollArea view properties\r
141         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);\r
142         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);\r
143         this->setFrameStyle(0);\r
144         setStyleSheet(SCROLLBAR_STYLE);\r
145         setStyleSheet("background-color: transparent");\r
146         //initiates all fields with default values\r
147         moved = false;\r
148         animate = false;\r
149 \r
150         oldPos = QPointF(0, 0);\r
151         prevPos = QPointF(0, 0);\r
152         pos = QPointF(0, 0);\r
153         speed = QPointF(0, 0);\r
154         movement = QPointF(0, 0);\r
155 \r
156         x = 0;\r
157         y = 0;\r
158 \r
159         slowingDownRate_ = DEFAULT_SLOWING_DOWN_RATE;\r
160         postionSumplingTime_ = DEFAULT_POSIOTION_SUMPLING_TIME;\r
161         kineticAnimationTime_ = DEFAULT_KINETIC_ANIMATION_TIME;\r
162         minMotionLength_ = DEFAULT_MIN_MOTION_LENGTH;\r
163 \r
164         timerPos.stop();\r
165         timerAnim.stop();\r
166 \r
167         //connects signals and slots\r
168         connect(&timerPos, SIGNAL(timeout()), this, SLOT(updatePos()));\r
169         connect(&timerAnim, SIGNAL(timeout()), this, SLOT(animation()));\r
170 }\r
171 \r
172 /**\r
173  * Method using to serve events. Overrides the virtual method from parent.\r
174  *\r
175  * @param object is a pointer handled object\r
176  * @param ev is a pointer to handled event\r
177  */\r
178 \r
179 bool FFScrollArea::eventFilter(QObject* object, QEvent* ev)\r
180 {\r
181         //Does nothing when QEvent type is Paint\r
182         if(ev->type() == QEvent::Paint)\r
183                 return false;\r
184 \r
185         if(QMouseEvent* event = dynamic_cast<QMouseEvent*>(ev))\r
186         {\r
187                 //Serves MouseButtonPressEvent\r
188                 if(event->type() == QEvent::MouseButtonPress)\r
189                 {\r
190 \r
191                         moved = false;\r
192                         movement = QPointF(0, 0);\r
193                         timerAnim.stop();\r
194                         if(event)\r
195                         {\r
196                                 //sets started values after each press event\r
197                                 x = event->globalX();\r
198                                 y = event->globalY();\r
199                                 prevPos = event->globalPos();\r
200                                 oldPos = event->globalPos();\r
201                                 speed = QPointF(0.0, 0.0);\r
202                                 pos = QPointF(0.0, 0.0);\r
203 \r
204                                 //starts position sampling timer\r
205                                 timerPos.start(postionSumplingTime_);\r
206                         }\r
207                         else\r
208                         {\r
209                                 return 0;\r
210                         }\r
211 \r
212                 }\r
213                 //Serves MouseMoveEvent\r
214                 if(event->type() == QEvent::MouseMove)\r
215                 {\r
216 \r
217                         //sets and calculates motion parameters\r
218                         int offsetY = event->globalY() - y;\r
219                         int offsetX = event->globalX() - x;\r
220 \r
221                         int valY = this->verticalScrollBar()->value() - offsetY;\r
222                         int valX = this->horizontalScrollBar()->value()\r
223                                         - offsetX;\r
224 \r
225                         y = event->globalY();\r
226                         x = event->globalX();\r
227 \r
228                         //If motion length is shorter then minMotionLength_\r
229                         //scrollbars postion will be not set.\r
230                         if(minMotionLength_ < qAbs(movement.y())\r
231                                         || minMotionLength_\r
232                                                         < qAbs(movement.x()))\r
233                         {\r
234                                 //sets new scrollBars' values\r
235                                 this->verticalScrollBar()->setValue(valY);\r
236                                 this->horizontalScrollBar()->setValue(valX);\r
237                                 moved = true;\r
238 \r
239                         }\r
240 \r
241                         //Updates parameters\r
242                         movement.setY(movement.y() + qAbs(prevPos.y()\r
243                                         - event->globalY()));\r
244                         movement.setX(movement.x() + qAbs(prevPos.x()\r
245                                         - event->globalX()));\r
246 \r
247                         prevPos = event->globalPos();\r
248                 }\r
249                 //Serves MouseButtonReleaseEvent\r
250                 if(event->type() == QEvent::MouseButtonRelease)\r
251                 {\r
252                         //Stops position's sampling\r
253                         timerPos.stop();\r
254 \r
255                         if(moved)\r
256                         {\r
257                                 //blocks signals\r
258                                 object->blockSignals(true);\r
259                                 object->event(event);\r
260                                 object->blockSignals(false);\r
261 \r
262                                 //sets cursor position after\r
263                                 //MouseButtonReleaseEvent\r
264                                 pos.setY((float)(this->verticalScrollBar()->value()));\r
265                                 pos.setX((float)(this->horizontalScrollBar()->value()));\r
266 \r
267                                 //starts animation\r
268                                 timerAnim.start(kineticAnimationTime_);\r
269                                 return true;\r
270                         }\r
271                         else\r
272                         {\r
273                                 //do nothing\r
274                                 return false;\r
275                         }\r
276                 }\r
277         }\r
278         //calls parent's event filter\r
279         return QScrollArea::eventFilter(object, ev);\r
280 }\r
281 \r
282 /**\r
283  * Sets widget on FFScrollArea\r
284  * Overrides the virtual method from parent.\r
285  *\r
286  * @param widget pointer to setting widget\r
287  */\r
288 \r
289 void FFScrollArea::setWidget(QWidget* widget)\r
290 {\r
291         //calls parent method\r
292         ((QScrollArea*)this)->setWidget(widget);\r
293         //calls method to install EventFilter\r
294         installFilterRecursive(widget);\r
295 }\r
296 /**\r
297  * Installs eventFilter to each widget on FFScrollArea\r
298  *\r
299  * @param wdg pointer to widget to which eventFilter is installing.\r
300  */\r
301 void FFScrollArea::installFilterRecursive(QWidget* wdg)\r
302 {\r
303         if(!wdg)\r
304                 return;\r
305         //Installs eventFilter to current widget\r
306         wdg->installEventFilter(this);\r
307 \r
308         //Calls installFilterRecursive to all children\r
309         const QList<QObject*> widgets = wdg->children();\r
310         for(int i = 0; i < widgets.length(); ++i)\r
311         {\r
312                 QWidget* wdg = dynamic_cast<QWidget*> (widgets.at(i));\r
313                 if(wdg)\r
314                         installFilterRecursive(wdg);\r
315         }\r
316 }\r
317 /**\r
318  * Updates motion's speed and cursor's position\r
319  */\r
320 void FFScrollArea::updatePos()\r
321 {\r
322         //sets motion's speed\r
323         speed.setY(oldPos.y() - this->cursor().pos().y());\r
324         speed.setX(oldPos.x() - this->cursor().pos().x());\r
325         //sets current cursor's position\r
326         oldPos = this->cursor().pos();\r
327 \r
328 }\r
329 \r
330 /**\r
331  * Method responsible for kinetic's animation.\r
332  */\r
333 \r
334 void FFScrollArea::animation()\r
335 {\r
336         //If speed is less then 1 animations is stopped\r
337         if(1 < qAbs(speed.x()) || 1 < qAbs(speed.y()))\r
338         {\r
339                 //updates position\r
340                 pos.setY(pos.y() + speed.y());\r
341                 pos.setX(pos.x() + speed.x());\r
342 \r
343                 //sets new position on scrollbars\r
344                 this->verticalScrollBar()->setValue((int)pos.y());\r
345                 this->horizontalScrollBar()->setValue((int)pos.x());\r
346 \r
347                 //updates speed\r
348                 speed.setY(speed.y() * slowingDownRate_);\r
349                 speed.setX(speed.x() * slowingDownRate_);\r
350         }\r
351         else\r
352         {\r
353                 //Stops animation\r
354                 timerAnim.stop();\r
355         }\r
356 }\r
357 \r
358 /**\r
359  * Returns rate responsible for kinetic speed slowing\r
360  */\r
361 float FFScrollArea::getSlowingDownRate() const\r
362 {\r
363         return slowingDownRate_;\r
364 }\r
365 /**\r
366  * Sets rate responsible for kinetic speed slowing\r
367  */\r
368 void FFScrollArea::setSlowingDownRate(float slowingDownRate_)\r
369 {\r
370         this->slowingDownRate_ = slowingDownRate_;\r
371 }\r
372 \r
373 /**\r
374  *  Returns kinetic animation frame time in ms.\r
375  */\r
376 int FFScrollArea::getKineticAnimationTime() const\r
377 {\r
378         return kineticAnimationTime_;\r
379 }\r
380 \r
381 /**\r
382  * Sets kinetic animation frame time in ms.\r
383  */\r
384 void FFScrollArea::setKineticAnimationTime(int kineticAnimationTime_)\r
385 {\r
386         this->kineticAnimationTime_ = kineticAnimationTime_;\r
387 }\r
388 \r
389 /**\r
390  * Returns cursor position sampling time in ms.\r
391  */\r
392 int FFScrollArea::getPostionSumplingTime() const\r
393 {\r
394         return postionSumplingTime_;\r
395 }\r
396 \r
397 /**\r
398  * Sets cursor position sampling time in ms.\r
399  */\r
400 void FFScrollArea::setPostionSumplingTime(int postionSumplingTime_)\r
401 {\r
402         this->postionSumplingTime_ = postionSumplingTime_;\r
403 }\r
404 \r
405 /**\r
406  * Sets value needed to blocking signals\r
407  *\r
408  */\r
409 void FFScrollArea::setMinMotionLength(int minMotionLength_)\r
410 {\r
411         this->minMotionLength_ = minMotionLength_;\r
412 }\r
413 \r
414 /**\r
415  * Returns value needed to blocking signals\r
416  */\r
417 int FFScrollArea::getMinMotionLength() const\r
418 {\r
419         return minMotionLength_;\r
420 }\r
421 \r
422 \r