added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffscrollinglabel.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 ffscrollinglabel.cpp
76  * @brief Implementation of the FFScrollingLabel class.
77  *
78  * @author ComArch S.A.
79  * @date 2009.08.07
80  * @version 1.1
81  */
82
83 #include "ffscrollinglabel.h"
84
85 /**
86  * Constructs a FFScrollingLabel with a parent.
87  */
88 FFScrollingLabel::FFScrollingLabel(QWidget* parent) :
89         FFAbstractWidget(parent)
90 {
91         QFont font = QFont(FONT_TEXT_DEF, LABEL_SIZE_NORMAL);
92         init(DEFAULT_TEXT, font);
93 }
94
95 /**
96  * Constructs a FFScrollingLabel with a given text and parent.
97  */
98 FFScrollingLabel::FFScrollingLabel(QString text, QWidget* parent) :
99         FFAbstractWidget(parent)
100 {
101         QFont font = QFont(FONT_TEXT_DEF, LABEL_SIZE_NORMAL);
102         init(text, font);
103 }
104
105 /**
106  * Constructs a FFScrollingLabel with a given text, font and parent.
107  */
108 FFScrollingLabel::FFScrollingLabel(QString text, QFont font, QWidget* parent) :
109         FFAbstractWidget(parent)
110 {
111         init(text, font);
112 }
113
114 /**
115  * A virtual destructor.
116  */
117 FFScrollingLabel::~FFScrollingLabel()
118 {
119
120 }
121
122 /**
123  * Initiates an object of FFScrollingLabel. Sets all needed fields and calls
124  * update() method. It is called by all constructors.
125  *
126  * @param text a text that will be shown at the label
127  * @param font a font that will be used to print a text on the label
128  */
129 void FFScrollingLabel::init(QString text, QFont font)
130 {
131         setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
132
133         text_ = text;
134         font_ = font;
135
136         timer = new QTimer(this);
137
138         //connecting timer with method responsible for moving the text
139         connect(timer, SIGNAL(timeout()), this, SLOT(scrollSideToSide()));
140
141         //setting default values
142         scrollSpeed_ = -DEFAULT_SCROLL_SPEED;
143         endScrollDelay_ = DEFAULT_END_SCROLL_DELAY;
144         color_ = COLOR_TEXT;
145         resizable_ = true;
146         fitToFont_ = false;
147         smoothDisappear_ = true;
148         painter = new QPainter();
149         timerDelay_ = TIMER_DELAY;
150         alignment_ = ALIGNMENT_LEFT;
151
152         //updating view of the label
153         updateView();
154 }
155
156 /**
157  * Updates text, font and size of the label.
158  * Makes that changing text, font or/and s      painter->setFont(font_);ize takes effect. It is automatically
159  * called by resizeEvent(), setFont(), setText(), setSmoothDisappear(), so
160  * there is no need to call it by you.
161  */
162 void FFScrollingLabel::updateView()
163 {
164         //for calculate width and height of the text using a given font
165         QFontMetrics textSize(font_);
166
167         //fitting the label's size to the font's size
168         if(fitToFont_)
169         {
170                 resize(width(), textSize.size(0, text_).height());
171         }
172
173         //creating the gradient used to drawing the text on the label
174         gradient.setStart(0, 0);
175         gradient.setFinalStop(width(), 0);
176         gradient.setColorAt(0.2, color_);
177         gradient.setColorAt(0.0, Qt::transparent);
178         gradient.setColorAt(0.8, color_);
179         gradient.setColorAt(1.0, Qt::transparent);
180
181         //if developer wants a gradient at ends of the label and if text width
182         //is greater than label width
183         if(smoothDisappear_ && textSize.size(0, text_).width() > width())
184         {
185                 //this pen will be used to drawing the text in paintEvent()
186                 pen.setBrush(QBrush(gradient));
187         }
188         else
189         {
190                 //this pen will be used to drawing the text in paintEvent()
191                 pen.setBrush(color_);
192         }
193
194         //calculating length (in pixels) of the text
195         length_ = textSize.width(text_, text_.length());
196
197         //resetting text offset (used in text movement)
198         calcTextPos();
199         textOffset = textStartPos;
200
201         //resetting field responsible for freezing text movement at ends
202         endScrollDelayIterationL = 0;
203         endScrollDelayIterationR = 0;
204
205         //if the text width is greater than label width and the label is wider
206         //than 0 pixels
207         if(length_ > width() && width() > 0)
208         {
209                 if(timer->isActive() == false)
210                 {
211                         timer->start(timerDelay_);
212                 }
213         }
214         else
215         {
216                 timer->stop();
217         }
218 }
219
220 /**
221  * Calculates a position of the text's left edge.
222  */
223 void FFScrollingLabel::calcTextPos()
224 {
225         switch(alignment_)
226         {
227                 case ALIGNMENT_CENTER:
228                         textStartPos = (width() - length_) / 2;
229                         break;
230
231                 case ALIGNMENT_LEFT:
232                         textStartPos = 0;
233                         break;
234
235                 case ALIGNMENT_RIGHT:
236                         textStartPos = width() - length_;
237                         break;
238         }
239 }
240
241 /**
242  * Changes gradient using to draw a text.
243  *
244  * @param left point in which transparent color is changing into a solid color
245  * @param right point in which a solid color is changing into transparent color
246  */
247 void FFScrollingLabel::changeGradient(qreal left, qreal right)
248 {
249         gradient.setStart(0, 0);
250         gradient.setFinalStop(width(), 0);
251
252         gradient.setColorAt(0.0, Qt::transparent);
253         gradient.setColorAt(1.0, Qt::transparent);
254         gradient.setColorAt(left, color_);
255         gradient.setColorAt(right, color_);
256
257         pen.setBrush(QBrush(gradient));
258         update();
259 }
260
261 /**
262  * Method that implements algorithm to scroll text from side to side.
263  * It is created as a slot, thus it can be called by a timer.
264  */
265 void FFScrollingLabel::scrollSideToSide()
266 {
267         textOffset += scrollSpeed_;
268         if(textOffset + length_ - scrollSpeed_ < width())
269         {
270                 textOffset -= scrollSpeed_;
271                 if(endScrollDelayIterationR > endScrollDelay_)
272                 {
273                         scrollSpeed_ = -scrollSpeed_;
274                         textOffset += scrollSpeed_;
275                         endScrollDelayIterationR = 0;
276                         changeGradient(0.2, 0.8);
277                 }
278                 else
279                 {
280                         endScrollDelayIterationR++;
281                         changeGradient(0.2, 1);
282                 }
283         }
284         else if(textOffset > 0)
285         {
286                 textOffset -= scrollSpeed_;
287
288                 if(endScrollDelayIterationL > endScrollDelay_)
289                 {
290                         scrollSpeed_ = -scrollSpeed_;
291                         textOffset += scrollSpeed_;
292                         endScrollDelayIterationL = 0;
293                         changeGradient(0.2, 0.8);
294                 }
295                 else
296                 {
297                         endScrollDelayIterationL++;
298                         changeGradient(0, 0.8);
299                 }
300         }
301         else
302         {
303                 update();
304         }
305 }
306
307 ///////////////////////////////////////////////////////OVERRIDDEN
308
309 /**
310  * Repaints the view of this label. Overrides the virtual method from QWidget.
311  * @param event Contains all informations about event.
312  */
313 void FFScrollingLabel::paintEvent(QPaintEvent* event)
314 {
315         Q_UNUSED(event);
316
317         painter->begin(this);
318         painter->setPen(pen);
319         painter->setFont(font_);
320         painter->drawText(textOffset,
321                           0,
322                           width() - textOffset,
323                           height(),
324                           0,
325                           text_);
326         painter->end();
327 }
328
329 /**
330  * Updates label's view after changing size of this widget. It calls update().
331  * If resizable is set up it also fits the font size to the label's size.
332  * Overrides the virtual method from QWidget.
333  * @param event Contains all informations about event.
334  */
335 void FFScrollingLabel::resizeEvent(QResizeEvent* event)
336 {
337         if(resizable_)
338         {
339                 font_.setPixelSize(event->size().height() * 0.85 <= 0 ? 1 : event->size().height() * 0.85);
340                 updateView();
341         }
342         if(fitToFont_)
343         {
344                 fitToFont_ = false;
345                 updateView();
346                 fitToFont_  = true;
347         }
348 }
349
350 /**
351  * Returns the recommended size for the widget. Overrides the virtual
352  * method from QWidget.
353  */
354 QSize FFScrollingLabel::sizeHint() const
355 {
356         return QSize(length_, font_.pixelSize());
357 }
358
359 /**
360  * Stops the label's timer when the label is hiding.
361  * It means if the label is not visible there are no useless calculates.
362  * @param event Contains all informations about event.
363  */
364 void FFScrollingLabel::hideEvent(QHideEvent* event)
365 {
366         Q_UNUSED(event);
367
368         timer->stop();
369
370 }
371
372 /**
373  * Starts the label's timer when the label is showing.
374  * @param event Contains all informations about event.
375  */
376 void FFScrollingLabel::showEvent(QShowEvent* event)
377 {
378         Q_UNUSED(event);
379
380         if(timer->isActive() == false && length_ > width() && width() > 0)
381         {
382                 timer->start(timerDelay_);
383         }
384
385 }
386
387 /**
388  * Returns the font that is used to printing label-text.
389  */
390 QFont FFScrollingLabel::font() const
391 {
392         return font_;
393 }
394
395 /**
396  * Sets a font that will be used to print the text on the label.
397  * Calls update() method.
398  */
399 void FFScrollingLabel::setFont(QFont font)
400 {
401         this->font_ = font;
402         updateView();
403         update();
404 }
405
406 /**
407  * Sets parameters of the font used to print a text on the label.
408  * Calls update() method.
409  */
410 void FFScrollingLabel::setFont(int fontParam)
411 {
412         int fontSize = 0;
413         QString fontType;
414         //search font size to set
415         switch(fontParam & SIZE_MASK)
416         {
417                 case SIZE_TINY:
418                         fontSize = LABEL_SIZE_TINY;
419                         break;
420
421                 case SIZE_SMALL:
422                         fontSize = LABEL_SIZE_SMALL;
423                         break;
424
425                 case SIZE_NORMAL:
426                         fontSize = LABEL_SIZE_NORMAL;
427                         break;
428
429                 case SIZE_LARGE:
430                         fontSize = LABEL_SIZE_LARGE;
431                         break;
432
433                 case SIZE_HUGE:
434                         fontSize = LABEL_SIZE_HUGE;
435                         break;
436         }
437
438         //search font type to set
439         switch(fontParam & FONT_MASK)
440         {
441                 case FONT_TITLE:
442                         fontType = FONT_TITLE_DEF;
443                         break;
444
445                 case FONT_TEXT:
446                         fontType = FONT_TEXT_DEF;
447                         break;
448         }
449
450         //search font color to set
451         switch(fontParam & COLOR_MASK)
452         {
453                 case COLOR_TITLE:
454                         color_ = COLOR_TITLE_DEF;
455                         break;
456
457                 case COLOR_TEXT:
458                         color_ = COLOR_TEXT_DEF;
459                         break;
460
461                 case COLOR_BRIGHT_BACKGROUND:
462                         color_ = COLOR_BRIGHT_BACKGROUND_DEF;
463                         break;
464
465                 case COLOR_DARK_BACKGROUND:
466                         color_ = COLOR_DARK_BACKGROUND_DEF;
467                         break;
468
469                 case COLOR_BRIGHT_FOREGROUND:
470                         color_ = COLOR_BRIGHT_FOREGROUND_DEF;
471                         break;
472
473                 case COLOR_DARK_FOREGROUND:
474                         color_ = COLOR_DARK_FOREGROUND_DEF;
475                         break;
476         }
477         //search label's alignment to set
478         alignment_ = Alignment(fontParam & ALIGNMENT_MASK);
479         switch(alignment_)
480         {
481                 case ALIGNMENT_CENTER:
482                         textStartPos = (width() - length_) / 2;
483                         break;
484
485                 case ALIGNMENT_LEFT:
486                         textStartPos = 0;
487                         break;
488
489                 case ALIGNMENT_RIGHT:
490                         textStartPos = width() - length_;
491                         break;
492         }
493
494         setFont(QFont(fontType, fontSize));
495         updateView();
496         update();
497 }
498
499 /**
500  * Returns the text printed on the label.
501  */
502 QString FFScrollingLabel::text() const
503 {
504         return text_;
505 }
506
507 /**
508  * Sets the text on the label. Calls update() method.
509  */
510 void FFScrollingLabel::setText(QString text)
511 {
512         this->text_ = text;
513         updateView();
514         update();
515 }
516
517 /**
518  * Returns a value that tells if the label is automatically fitting up
519  * to a layout.
520  */
521 bool FFScrollingLabel::isResizable() const
522 {
523         return resizable_;
524 }
525
526 /**
527  * Turns on/off auto-fitting the label size to the layout. If it is set to true
528  * then with every change of the label's size, the font size will be changed.
529  * If false, the font size will be fixed. If resizable is true, fitToFont
530  * property is setting up to false.
531  */
532 void FFScrollingLabel::setResizable(bool resizable)
533 {
534         if((this->resizable_ = resizable))
535         {
536                 this->fitToFont_ = false;
537         }
538         else
539         {
540
541                 this->fitToFont_ = true;
542         }
543         update();
544 }
545
546 /**
547  * Returns the speed of text scrolling.
548  */
549 int FFScrollingLabel::scrollSpeed() const
550 {
551         return scrollSpeed_;
552 }
553
554 /**
555  * Sets the speed of text scrolling.
556  */
557 void FFScrollingLabel::setScrollSpeed(int scrollSpeed)
558 {
559         this->scrollSpeed_ = scrollSpeed;
560         update();
561 }
562
563 /**
564  * Returns time that the text waits between changing move direction. The time is
565  * calculating as: timerDelay * endScrollDelay.
566  */
567 int FFScrollingLabel::endScrollDelay() const
568 {
569         return endScrollDelay_;
570 }
571
572 /**
573  * Sets time that the text waits between changing move direction. The time is
574  * calculating as: timerDelay * endScrollDelay.
575  */
576 void FFScrollingLabel::setEndScrollDelay(int endScrollDelay)
577 {
578         this->endScrollDelay_ = endScrollDelay;
579         update();
580 }
581
582 /**
583  * Returns the color using which is printing a text.
584  */
585 QColor FFScrollingLabel::color() const
586 {
587         return color_;
588 }
589
590 /**
591  * Sets the color that is used to print a text.
592  */
593 void FFScrollingLabel::setColor(QColor color)
594 {
595         this->color_ = color;
596         updateView();
597         update();
598 }
599
600 /**
601  * Returns true if the size of label is fitting up to the font size.
602  */
603 bool FFScrollingLabel::fitToFont() const
604 {
605         return fitToFont_;
606 }
607
608 /**
609  * Turns on/off fitting up size of label to the font size. If fitToFont is true
610  * resizable property is setting up to false.
611  */
612 void FFScrollingLabel::setFitToFont(bool fitToFont)
613 {
614         if((this->fitToFont_ = fitToFont))
615         {
616                 resizable_ = false;
617         }
618         else
619         {
620                 resizable_ = true;
621         }
622         update();
623 }
624
625 /**
626  * Sets the scrolling type.
627  */
628 void FFScrollingLabel::setScrollType(int type)
629 {
630         switch(type)
631         {
632                 case SCROLL_SIDE_TO_SIDE:
633                         disconnect(timer,
634                                    SIGNAL(timeOut()),
635                                    this,
636                                    previousScrollType.toAscii());
637                         connect(timer,
638                                 SIGNAL(timeOut()),
639                                 this,
640                                 SLOT(scrollSpeed()));
641                         previousScrollType = SLOT(scrollSpeed());
642                         break;
643         }
644         update();
645 }
646
647 /**
648  * Returns true if the gradient at ends is set up, otherwise false.
649  */
650 bool FFScrollingLabel::isSmoothDisappear() const
651 {
652         return smoothDisappear_;
653 }
654
655 /**
656  * Turns on/off a gradient at the ends of the label. Calls update() method.
657  */
658 void FFScrollingLabel::setSmoothDisappear(bool smoothDisappear)
659 {
660         this->smoothDisappear_ = smoothDisappear;
661         updateView();
662         update();
663 }
664
665 /**
666  * Returns time (in milliseconds) between movements of the text.
667  */
668 int FFScrollingLabel::timerDelay()
669 {
670         return timerDelay_;
671 }
672
673 /**
674  * Sets time (in milliseconds) between movements of the text.
675  */
676 void FFScrollingLabel::setTimerDelay(int delay)
677 {
678         timerDelay_ = delay;
679         timer->stop();
680         timer->start(timerDelay_);
681         update();
682 }
683
684 /**
685  * Sets alignment of the text.
686  */
687 void FFScrollingLabel::setAlignment(FFScrollingLabel::Alignment alignment)
688 {
689         alignment_ = alignment;
690         calcTextPos();
691         updateView();
692         update();
693 }
694
695 /**
696  * Returns alignment used in this label.
697  */
698 FFScrollingLabel::Alignment FFScrollingLabel::alignment()
699 {
700         return alignment_;
701 }