2 GNU GENERAL PUBLIC LICENSE
3 Version 3, 29 June 2007
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.
11 The GNU General Public License is a free, copyleft license for
12 software and other kinds of works.
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
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.
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.
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
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.
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.
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.
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.
69 The precise terms and conditions for copying, distribution and
72 http://www.gnu.org/licenses/gpl-3.0.txt
75 * @file ffscrollinglabel.cpp
76 * @brief Implementation of the FFScrollingLabel class.
78 * @author ComArch S.A.
83 #include "ffscrollinglabel.h"
86 * Constructs a FFScrollingLabel with a parent.
88 FFScrollingLabel::FFScrollingLabel(QWidget* parent) :
89 FFAbstractWidget(parent)
91 QFont font = QFont(FONT_TEXT_DEF, LABEL_SIZE_NORMAL);
92 init(DEFAULT_TEXT, font);
96 * Constructs a FFScrollingLabel with a given text and parent.
98 FFScrollingLabel::FFScrollingLabel(QString text, QWidget* parent) :
99 FFAbstractWidget(parent)
101 QFont font = QFont(FONT_TEXT_DEF, LABEL_SIZE_NORMAL);
106 * Constructs a FFScrollingLabel with a given text, font and parent.
108 FFScrollingLabel::FFScrollingLabel(QString text, QFont font, QWidget* parent) :
109 FFAbstractWidget(parent)
115 * A virtual destructor.
117 FFScrollingLabel::~FFScrollingLabel()
123 * Initiates an object of FFScrollingLabel. Sets all needed fields and calls
124 * update() method. It is called by all constructors.
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
129 void FFScrollingLabel::init(QString text, QFont font)
131 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
136 timer = new QTimer(this);
138 //connecting timer with method responsible for moving the text
139 connect(timer, SIGNAL(timeout()), this, SLOT(scrollSideToSide()));
141 //setting default values
142 scrollSpeed_ = -DEFAULT_SCROLL_SPEED;
143 endScrollDelay_ = DEFAULT_END_SCROLL_DELAY;
147 smoothDisappear_ = true;
148 painter = new QPainter();
149 timerDelay_ = TIMER_DELAY;
150 alignment_ = ALIGNMENT_LEFT;
152 //updating view of the label
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.
162 void FFScrollingLabel::updateView()
164 //for calculate width and height of the text using a given font
165 QFontMetrics textSize(font_);
167 //fitting the label's size to the font's size
170 resize(width(), textSize.size(0, text_).height());
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);
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())
185 //this pen will be used to drawing the text in paintEvent()
186 pen.setBrush(QBrush(gradient));
190 //this pen will be used to drawing the text in paintEvent()
191 pen.setBrush(color_);
194 //calculating length (in pixels) of the text
195 length_ = textSize.width(text_, text_.length());
197 //resetting text offset (used in text movement)
199 textOffset = textStartPos;
201 //resetting field responsible for freezing text movement at ends
202 endScrollDelayIterationL = 0;
203 endScrollDelayIterationR = 0;
205 //if the text width is greater than label width and the label is wider
207 if(length_ > width() && width() > 0)
209 if(timer->isActive() == false)
211 timer->start(timerDelay_);
221 * Calculates a position of the text's left edge.
223 void FFScrollingLabel::calcTextPos()
227 case ALIGNMENT_CENTER:
228 textStartPos = (width() - length_) / 2;
235 case ALIGNMENT_RIGHT:
236 textStartPos = width() - length_;
242 * Changes gradient using to draw a text.
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
247 void FFScrollingLabel::changeGradient(qreal left, qreal right)
249 gradient.setStart(0, 0);
250 gradient.setFinalStop(width(), 0);
252 gradient.setColorAt(0.0, Qt::transparent);
253 gradient.setColorAt(1.0, Qt::transparent);
254 gradient.setColorAt(left, color_);
255 gradient.setColorAt(right, color_);
257 pen.setBrush(QBrush(gradient));
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.
265 void FFScrollingLabel::scrollSideToSide()
267 textOffset += scrollSpeed_;
268 if(textOffset + length_ - scrollSpeed_ < width())
270 textOffset -= scrollSpeed_;
271 if(endScrollDelayIterationR > endScrollDelay_)
273 scrollSpeed_ = -scrollSpeed_;
274 textOffset += scrollSpeed_;
275 endScrollDelayIterationR = 0;
276 changeGradient(0.2, 0.8);
280 endScrollDelayIterationR++;
281 changeGradient(0.2, 1);
284 else if(textOffset > 0)
286 textOffset -= scrollSpeed_;
288 if(endScrollDelayIterationL > endScrollDelay_)
290 scrollSpeed_ = -scrollSpeed_;
291 textOffset += scrollSpeed_;
292 endScrollDelayIterationL = 0;
293 changeGradient(0.2, 0.8);
297 endScrollDelayIterationL++;
298 changeGradient(0, 0.8);
307 ///////////////////////////////////////////////////////OVERRIDDEN
310 * Repaints the view of this label. Overrides the virtual method from QWidget.
311 * @param event Contains all informations about event.
313 void FFScrollingLabel::paintEvent(QPaintEvent* event)
317 painter->begin(this);
318 painter->setPen(pen);
319 painter->setFont(font_);
320 painter->drawText(textOffset,
322 width() - textOffset,
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.
335 void FFScrollingLabel::resizeEvent(QResizeEvent* event)
339 font_.setPixelSize(event->size().height() * 0.85 <= 0 ? 1 : event->size().height() * 0.85);
351 * Returns the recommended size for the widget. Overrides the virtual
352 * method from QWidget.
354 QSize FFScrollingLabel::sizeHint() const
356 return QSize(length_, font_.pixelSize());
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.
364 void FFScrollingLabel::hideEvent(QHideEvent* event)
373 * Starts the label's timer when the label is showing.
374 * @param event Contains all informations about event.
376 void FFScrollingLabel::showEvent(QShowEvent* event)
380 if(timer->isActive() == false && length_ > width() && width() > 0)
382 timer->start(timerDelay_);
388 * Returns the font that is used to printing label-text.
390 QFont FFScrollingLabel::font() const
396 * Sets a font that will be used to print the text on the label.
397 * Calls update() method.
399 void FFScrollingLabel::setFont(QFont font)
407 * Sets parameters of the font used to print a text on the label.
408 * Calls update() method.
410 void FFScrollingLabel::setFont(int fontParam)
414 //search font size to set
415 switch(fontParam & SIZE_MASK)
418 fontSize = LABEL_SIZE_TINY;
422 fontSize = LABEL_SIZE_SMALL;
426 fontSize = LABEL_SIZE_NORMAL;
430 fontSize = LABEL_SIZE_LARGE;
434 fontSize = LABEL_SIZE_HUGE;
438 //search font type to set
439 switch(fontParam & FONT_MASK)
442 fontType = FONT_TITLE_DEF;
446 fontType = FONT_TEXT_DEF;
450 //search font color to set
451 switch(fontParam & COLOR_MASK)
454 color_ = COLOR_TITLE_DEF;
458 color_ = COLOR_TEXT_DEF;
461 case COLOR_BRIGHT_BACKGROUND:
462 color_ = COLOR_BRIGHT_BACKGROUND_DEF;
465 case COLOR_DARK_BACKGROUND:
466 color_ = COLOR_DARK_BACKGROUND_DEF;
469 case COLOR_BRIGHT_FOREGROUND:
470 color_ = COLOR_BRIGHT_FOREGROUND_DEF;
473 case COLOR_DARK_FOREGROUND:
474 color_ = COLOR_DARK_FOREGROUND_DEF;
477 //search label's alignment to set
478 alignment_ = Alignment(fontParam & ALIGNMENT_MASK);
481 case ALIGNMENT_CENTER:
482 textStartPos = (width() - length_) / 2;
489 case ALIGNMENT_RIGHT:
490 textStartPos = width() - length_;
494 setFont(QFont(fontType, fontSize));
500 * Returns the text printed on the label.
502 QString FFScrollingLabel::text() const
508 * Sets the text on the label. Calls update() method.
510 void FFScrollingLabel::setText(QString text)
518 * Returns a value that tells if the label is automatically fitting up
521 bool FFScrollingLabel::isResizable() const
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.
532 void FFScrollingLabel::setResizable(bool resizable)
534 if((this->resizable_ = resizable))
536 this->fitToFont_ = false;
541 this->fitToFont_ = true;
547 * Returns the speed of text scrolling.
549 int FFScrollingLabel::scrollSpeed() const
555 * Sets the speed of text scrolling.
557 void FFScrollingLabel::setScrollSpeed(int scrollSpeed)
559 this->scrollSpeed_ = scrollSpeed;
564 * Returns time that the text waits between changing move direction. The time is
565 * calculating as: timerDelay * endScrollDelay.
567 int FFScrollingLabel::endScrollDelay() const
569 return endScrollDelay_;
573 * Sets time that the text waits between changing move direction. The time is
574 * calculating as: timerDelay * endScrollDelay.
576 void FFScrollingLabel::setEndScrollDelay(int endScrollDelay)
578 this->endScrollDelay_ = endScrollDelay;
583 * Returns the color using which is printing a text.
585 QColor FFScrollingLabel::color() const
591 * Sets the color that is used to print a text.
593 void FFScrollingLabel::setColor(QColor color)
595 this->color_ = color;
601 * Returns true if the size of label is fitting up to the font size.
603 bool FFScrollingLabel::fitToFont() const
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.
612 void FFScrollingLabel::setFitToFont(bool fitToFont)
614 if((this->fitToFont_ = fitToFont))
626 * Sets the scrolling type.
628 void FFScrollingLabel::setScrollType(int type)
632 case SCROLL_SIDE_TO_SIDE:
636 previousScrollType.toAscii());
640 SLOT(scrollSpeed()));
641 previousScrollType = SLOT(scrollSpeed());
648 * Returns true if the gradient at ends is set up, otherwise false.
650 bool FFScrollingLabel::isSmoothDisappear() const
652 return smoothDisappear_;
656 * Turns on/off a gradient at the ends of the label. Calls update() method.
658 void FFScrollingLabel::setSmoothDisappear(bool smoothDisappear)
660 this->smoothDisappear_ = smoothDisappear;
666 * Returns time (in milliseconds) between movements of the text.
668 int FFScrollingLabel::timerDelay()
674 * Sets time (in milliseconds) between movements of the text.
676 void FFScrollingLabel::setTimerDelay(int delay)
680 timer->start(timerDelay_);
685 * Sets alignment of the text.
687 void FFScrollingLabel::setAlignment(FFScrollingLabel::Alignment alignment)
689 alignment_ = alignment;
696 * Returns alignment used in this label.
698 FFScrollingLabel::Alignment FFScrollingLabel::alignment()