added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffscrollinglabel.cpp
diff --git a/libffqw-n810-1.0/sources/ffscrollinglabel.cpp b/libffqw-n810-1.0/sources/ffscrollinglabel.cpp
new file mode 100644 (file)
index 0000000..8bb7ae9
--- /dev/null
@@ -0,0 +1,701 @@
+/*
+         GNU GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+  The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works.  By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.  We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors.  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+  To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights.  Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received.  You must make sure that they, too, receive
+or can get the source code.  And you must show them these terms so they
+know their rights.
+
+  Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+  For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software.  For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+  Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so.  This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software.  The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable.  Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products.  If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+  Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary.  To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+http://www.gnu.org/licenses/gpl-3.0.txt
+*/
+/**
+ * @file ffscrollinglabel.cpp
+ * @brief Implementation of the FFScrollingLabel class.
+ *
+ * @author ComArch S.A.
+ * @date 2009.08.07
+ * @version 1.1
+ */
+
+#include "ffscrollinglabel.h"
+
+/**
+ * Constructs a FFScrollingLabel with a parent.
+ */
+FFScrollingLabel::FFScrollingLabel(QWidget* parent) :
+       FFAbstractWidget(parent)
+{
+       QFont font = QFont(FONT_TEXT_DEF, LABEL_SIZE_NORMAL);
+       init(DEFAULT_TEXT, font);
+}
+
+/**
+ * Constructs a FFScrollingLabel with a given text and parent.
+ */
+FFScrollingLabel::FFScrollingLabel(QString text, QWidget* parent) :
+       FFAbstractWidget(parent)
+{
+       QFont font = QFont(FONT_TEXT_DEF, LABEL_SIZE_NORMAL);
+       init(text, font);
+}
+
+/**
+ * Constructs a FFScrollingLabel with a given text, font and parent.
+ */
+FFScrollingLabel::FFScrollingLabel(QString text, QFont font, QWidget* parent) :
+       FFAbstractWidget(parent)
+{
+       init(text, font);
+}
+
+/**
+ * A virtual destructor.
+ */
+FFScrollingLabel::~FFScrollingLabel()
+{
+
+}
+
+/**
+ * Initiates an object of FFScrollingLabel. Sets all needed fields and calls
+ * update() method. It is called by all constructors.
+ *
+ * @param text a text that will be shown at the label
+ * @param font a font that will be used to print a text on the label
+ */
+void FFScrollingLabel::init(QString text, QFont font)
+{
+       setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
+
+       text_ = text;
+       font_ = font;
+
+       timer = new QTimer(this);
+
+       //connecting timer with method responsible for moving the text
+       connect(timer, SIGNAL(timeout()), this, SLOT(scrollSideToSide()));
+
+       //setting default values
+       scrollSpeed_ = -DEFAULT_SCROLL_SPEED;
+       endScrollDelay_ = DEFAULT_END_SCROLL_DELAY;
+       color_ = COLOR_TEXT;
+       resizable_ = true;
+       fitToFont_ = false;
+       smoothDisappear_ = true;
+       painter = new QPainter();
+       timerDelay_ = TIMER_DELAY;
+       alignment_ = ALIGNMENT_LEFT;
+
+       //updating view of the label
+       updateView();
+}
+
+/**
+ * Updates text, font and size of the label.
+ * Makes that changing text, font or/and s     painter->setFont(font_);ize takes effect. It is automatically
+ * called by resizeEvent(), setFont(), setText(), setSmoothDisappear(), so
+ * there is no need to call it by you.
+ */
+void FFScrollingLabel::updateView()
+{
+       //for calculate width and height of the text using a given font
+       QFontMetrics textSize(font_);
+
+       //fitting the label's size to the font's size
+       if(fitToFont_)
+       {
+               resize(width(), textSize.size(0, text_).height());
+       }
+
+       //creating the gradient used to drawing the text on the label
+       gradient.setStart(0, 0);
+       gradient.setFinalStop(width(), 0);
+       gradient.setColorAt(0.2, color_);
+       gradient.setColorAt(0.0, Qt::transparent);
+       gradient.setColorAt(0.8, color_);
+       gradient.setColorAt(1.0, Qt::transparent);
+
+       //if developer wants a gradient at ends of the label and if text width
+       //is greater than label width
+       if(smoothDisappear_ && textSize.size(0, text_).width() > width())
+       {
+               //this pen will be used to drawing the text in paintEvent()
+               pen.setBrush(QBrush(gradient));
+       }
+       else
+       {
+               //this pen will be used to drawing the text in paintEvent()
+               pen.setBrush(color_);
+       }
+
+       //calculating length (in pixels) of the text
+       length_ = textSize.width(text_, text_.length());
+
+       //resetting text offset (used in text movement)
+       calcTextPos();
+       textOffset = textStartPos;
+
+       //resetting field responsible for freezing text movement at ends
+       endScrollDelayIterationL = 0;
+       endScrollDelayIterationR = 0;
+
+       //if the text width is greater than label width and the label is wider
+       //than 0 pixels
+       if(length_ > width() && width() > 0)
+       {
+               if(timer->isActive() == false)
+               {
+                       timer->start(timerDelay_);
+               }
+       }
+       else
+       {
+               timer->stop();
+       }
+}
+
+/**
+ * Calculates a position of the text's left edge.
+ */
+void FFScrollingLabel::calcTextPos()
+{
+       switch(alignment_)
+       {
+               case ALIGNMENT_CENTER:
+                       textStartPos = (width() - length_) / 2;
+                       break;
+
+               case ALIGNMENT_LEFT:
+                       textStartPos = 0;
+                       break;
+
+               case ALIGNMENT_RIGHT:
+                       textStartPos = width() - length_;
+                       break;
+       }
+}
+
+/**
+ * Changes gradient using to draw a text.
+ *
+ * @param left point in which transparent color is changing into a solid color
+ * @param right point in which a solid color is changing into transparent color
+ */
+void FFScrollingLabel::changeGradient(qreal left, qreal right)
+{
+       gradient.setStart(0, 0);
+       gradient.setFinalStop(width(), 0);
+
+       gradient.setColorAt(0.0, Qt::transparent);
+       gradient.setColorAt(1.0, Qt::transparent);
+       gradient.setColorAt(left, color_);
+       gradient.setColorAt(right, color_);
+
+       pen.setBrush(QBrush(gradient));
+       update();
+}
+
+/**
+ * Method that implements algorithm to scroll text from side to side.
+ * It is created as a slot, thus it can be called by a timer.
+ */
+void FFScrollingLabel::scrollSideToSide()
+{
+       textOffset += scrollSpeed_;
+       if(textOffset + length_ - scrollSpeed_ < width())
+       {
+               textOffset -= scrollSpeed_;
+               if(endScrollDelayIterationR > endScrollDelay_)
+               {
+                       scrollSpeed_ = -scrollSpeed_;
+                       textOffset += scrollSpeed_;
+                       endScrollDelayIterationR = 0;
+                       changeGradient(0.2, 0.8);
+               }
+               else
+               {
+                       endScrollDelayIterationR++;
+                       changeGradient(0.2, 1);
+               }
+       }
+       else if(textOffset > 0)
+       {
+               textOffset -= scrollSpeed_;
+
+               if(endScrollDelayIterationL > endScrollDelay_)
+               {
+                       scrollSpeed_ = -scrollSpeed_;
+                       textOffset += scrollSpeed_;
+                       endScrollDelayIterationL = 0;
+                       changeGradient(0.2, 0.8);
+               }
+               else
+               {
+                       endScrollDelayIterationL++;
+                       changeGradient(0, 0.8);
+               }
+       }
+       else
+       {
+               update();
+       }
+}
+
+///////////////////////////////////////////////////////OVERRIDDEN
+
+/**
+ * Repaints the view of this label. Overrides the virtual method from QWidget.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingLabel::paintEvent(QPaintEvent* event)
+{
+       Q_UNUSED(event);
+
+       painter->begin(this);
+       painter->setPen(pen);
+       painter->setFont(font_);
+       painter->drawText(textOffset,
+                         0,
+                         width() - textOffset,
+                         height(),
+                         0,
+                         text_);
+       painter->end();
+}
+
+/**
+ * Updates label's view after changing size of this widget. It calls update().
+ * If resizable is set up it also fits the font size to the label's size.
+ * Overrides the virtual method from QWidget.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingLabel::resizeEvent(QResizeEvent* event)
+{
+       if(resizable_)
+       {
+               font_.setPixelSize(event->size().height() * 0.85 <= 0 ? 1 : event->size().height() * 0.85);
+               updateView();
+       }
+       if(fitToFont_)
+       {
+               fitToFont_ = false;
+               updateView();
+               fitToFont_  = true;
+       }
+}
+
+/**
+ * Returns the recommended size for the widget. Overrides the virtual
+ * method from QWidget.
+ */
+QSize FFScrollingLabel::sizeHint() const
+{
+       return QSize(length_, font_.pixelSize());
+}
+
+/**
+ * Stops the label's timer when the label is hiding.
+ * It means if the label is not visible there are no useless calculates.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingLabel::hideEvent(QHideEvent* event)
+{
+       Q_UNUSED(event);
+
+       timer->stop();
+
+}
+
+/**
+ * Starts the label's timer when the label is showing.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingLabel::showEvent(QShowEvent* event)
+{
+       Q_UNUSED(event);
+
+       if(timer->isActive() == false && length_ > width() && width() > 0)
+       {
+               timer->start(timerDelay_);
+       }
+
+}
+
+/**
+ * Returns the font that is used to printing label-text.
+ */
+QFont FFScrollingLabel::font() const
+{
+       return font_;
+}
+
+/**
+ * Sets a font that will be used to print the text on the label.
+ * Calls update() method.
+ */
+void FFScrollingLabel::setFont(QFont font)
+{
+       this->font_ = font;
+       updateView();
+       update();
+}
+
+/**
+ * Sets parameters of the font used to print a text on the label.
+ * Calls update() method.
+ */
+void FFScrollingLabel::setFont(int fontParam)
+{
+       int fontSize = 0;
+       QString fontType;
+       //search font size to set
+       switch(fontParam & SIZE_MASK)
+       {
+               case SIZE_TINY:
+                       fontSize = LABEL_SIZE_TINY;
+                       break;
+
+               case SIZE_SMALL:
+                       fontSize = LABEL_SIZE_SMALL;
+                       break;
+
+               case SIZE_NORMAL:
+                       fontSize = LABEL_SIZE_NORMAL;
+                       break;
+
+               case SIZE_LARGE:
+                       fontSize = LABEL_SIZE_LARGE;
+                       break;
+
+               case SIZE_HUGE:
+                       fontSize = LABEL_SIZE_HUGE;
+                       break;
+       }
+
+       //search font type to set
+       switch(fontParam & FONT_MASK)
+       {
+               case FONT_TITLE:
+                       fontType = FONT_TITLE_DEF;
+                       break;
+
+               case FONT_TEXT:
+                       fontType = FONT_TEXT_DEF;
+                       break;
+       }
+
+       //search font color to set
+       switch(fontParam & COLOR_MASK)
+       {
+               case COLOR_TITLE:
+                       color_ = COLOR_TITLE_DEF;
+                       break;
+
+               case COLOR_TEXT:
+                       color_ = COLOR_TEXT_DEF;
+                       break;
+
+               case COLOR_BRIGHT_BACKGROUND:
+                       color_ = COLOR_BRIGHT_BACKGROUND_DEF;
+                       break;
+
+               case COLOR_DARK_BACKGROUND:
+                       color_ = COLOR_DARK_BACKGROUND_DEF;
+                       break;
+
+               case COLOR_BRIGHT_FOREGROUND:
+                       color_ = COLOR_BRIGHT_FOREGROUND_DEF;
+                       break;
+
+               case COLOR_DARK_FOREGROUND:
+                       color_ = COLOR_DARK_FOREGROUND_DEF;
+                       break;
+       }
+       //search label's alignment to set
+       alignment_ = Alignment(fontParam & ALIGNMENT_MASK);
+       switch(alignment_)
+       {
+               case ALIGNMENT_CENTER:
+                       textStartPos = (width() - length_) / 2;
+                       break;
+
+               case ALIGNMENT_LEFT:
+                       textStartPos = 0;
+                       break;
+
+               case ALIGNMENT_RIGHT:
+                       textStartPos = width() - length_;
+                       break;
+       }
+
+       setFont(QFont(fontType, fontSize));
+       updateView();
+       update();
+}
+
+/**
+ * Returns the text printed on the label.
+ */
+QString FFScrollingLabel::text() const
+{
+       return text_;
+}
+
+/**
+ * Sets the text on the label. Calls update() method.
+ */
+void FFScrollingLabel::setText(QString text)
+{
+       this->text_ = text;
+       updateView();
+       update();
+}
+
+/**
+ * Returns a value that tells if the label is automatically fitting up
+ * to a layout.
+ */
+bool FFScrollingLabel::isResizable() const
+{
+       return resizable_;
+}
+
+/**
+ * Turns on/off auto-fitting the label size to the layout. If it is set to true
+ * then with every change of the label's size, the font size will be changed.
+ * If false, the font size will be fixed. If resizable is true, fitToFont
+ * property is setting up to false.
+ */
+void FFScrollingLabel::setResizable(bool resizable)
+{
+       if((this->resizable_ = resizable))
+       {
+               this->fitToFont_ = false;
+       }
+       else
+       {
+
+               this->fitToFont_ = true;
+       }
+       update();
+}
+
+/**
+ * Returns the speed of text scrolling.
+ */
+int FFScrollingLabel::scrollSpeed() const
+{
+       return scrollSpeed_;
+}
+
+/**
+ * Sets the speed of text scrolling.
+ */
+void FFScrollingLabel::setScrollSpeed(int scrollSpeed)
+{
+       this->scrollSpeed_ = scrollSpeed;
+       update();
+}
+
+/**
+ * Returns time that the text waits between changing move direction. The time is
+ * calculating as: timerDelay * endScrollDelay.
+ */
+int FFScrollingLabel::endScrollDelay() const
+{
+       return endScrollDelay_;
+}
+
+/**
+ * Sets time that the text waits between changing move direction. The time is
+ * calculating as: timerDelay * endScrollDelay.
+ */
+void FFScrollingLabel::setEndScrollDelay(int endScrollDelay)
+{
+       this->endScrollDelay_ = endScrollDelay;
+       update();
+}
+
+/**
+ * Returns the color using which is printing a text.
+ */
+QColor FFScrollingLabel::color() const
+{
+       return color_;
+}
+
+/**
+ * Sets the color that is used to print a text.
+ */
+void FFScrollingLabel::setColor(QColor color)
+{
+       this->color_ = color;
+       updateView();
+       update();
+}
+
+/**
+ * Returns true if the size of label is fitting up to the font size.
+ */
+bool FFScrollingLabel::fitToFont() const
+{
+       return fitToFont_;
+}
+
+/**
+ * Turns on/off fitting up size of label to the font size. If fitToFont is true
+ * resizable property is setting up to false.
+ */
+void FFScrollingLabel::setFitToFont(bool fitToFont)
+{
+       if((this->fitToFont_ = fitToFont))
+       {
+               resizable_ = false;
+       }
+       else
+       {
+               resizable_ = true;
+       }
+       update();
+}
+
+/**
+ * Sets the scrolling type.
+ */
+void FFScrollingLabel::setScrollType(int type)
+{
+       switch(type)
+       {
+               case SCROLL_SIDE_TO_SIDE:
+                       disconnect(timer,
+                                  SIGNAL(timeOut()),
+                                  this,
+                                  previousScrollType.toAscii());
+                       connect(timer,
+                               SIGNAL(timeOut()),
+                               this,
+                               SLOT(scrollSpeed()));
+                       previousScrollType = SLOT(scrollSpeed());
+                       break;
+       }
+       update();
+}
+
+/**
+ * Returns true if the gradient at ends is set up, otherwise false.
+ */
+bool FFScrollingLabel::isSmoothDisappear() const
+{
+       return smoothDisappear_;
+}
+
+/**
+ * Turns on/off a gradient at the ends of the label. Calls update() method.
+ */
+void FFScrollingLabel::setSmoothDisappear(bool smoothDisappear)
+{
+       this->smoothDisappear_ = smoothDisappear;
+       updateView();
+       update();
+}
+
+/**
+ * Returns time (in milliseconds) between movements of the text.
+ */
+int FFScrollingLabel::timerDelay()
+{
+       return timerDelay_;
+}
+
+/**
+ * Sets time (in milliseconds) between movements of the text.
+ */
+void FFScrollingLabel::setTimerDelay(int delay)
+{
+       timerDelay_ = delay;
+       timer->stop();
+       timer->start(timerDelay_);
+       update();
+}
+
+/**
+ * Sets alignment of the text.
+ */
+void FFScrollingLabel::setAlignment(FFScrollingLabel::Alignment alignment)
+{
+       alignment_ = alignment;
+       calcTextPos();
+       updateView();
+       update();
+}
+
+/**
+ * Returns alignment used in this label.
+ */
+FFScrollingLabel::Alignment FFScrollingLabel::alignment()
+{
+       return alignment_;
+}