added all files
[ffqwlibrary] / libffqw-1.0 / sources / ffscrollingbutton.cpp
diff --git a/libffqw-1.0/sources/ffscrollingbutton.cpp b/libffqw-1.0/sources/ffscrollingbutton.cpp
new file mode 100644 (file)
index 0000000..77b5d8d
--- /dev/null
@@ -0,0 +1,581 @@
+/*
+         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 ffscrollingbutton.h
+ * @brief Contains a necessary class declaration.
+ *
+ * @author ComArch S.A.
+ * @date 2009.08.03
+ * @version 1.0
+ */
+
+#include "ffscrollingbutton.h"
+
+/**
+ * Constructs a FFScrollingButton with a parent.
+ * Sets variable to initial values and set graphics used in widget.
+ */
+FFScrollingButton::FFScrollingButton(QWidget* parent) :
+       FFAbstractButton(parent)
+{
+       init();
+}
+
+/**
+ * Constructs a FFScrollingButton with a parent , title and description.
+ * Sets variable to initial values and sets graphics used in widget.
+ */
+FFScrollingButton::FFScrollingButton(QString title, QString description, QWidget* parent) :
+       FFAbstractButton(parent)
+{
+       init();
+       title_->setText(title);
+       description_->setText(description);
+}
+
+
+/**
+ * A virtual destructor.
+ */
+FFScrollingButton::~FFScrollingButton()
+{
+       ;
+}
+
+/**
+ * Initiates an object of FFScrollingButton. Sets all needed fields.
+ */
+void FFScrollingButton::init()
+{
+       //sets title's label
+       title_ = new FFScrollingLabel(this);
+       title_->setResizable(true);
+       title_->setColor(FF_TITLE_COLOR);
+       title_->show();
+
+       //sets desciption's label
+       description_ = new FFScrollingLabel(this);
+       description_->setResizable(true);
+       description_->setColor(FF_DESCRIPTION_COLOR);
+       description_->show();
+
+       //sets indents
+       titleIndent_ = indent();
+       descriptionIndent_ = indent();
+
+       titleIndentTemp = indent();
+       descriptionIndentTemp = indent();
+
+       //sets switches
+       titleSwitch_ = true;
+       descriptionSwitch_ = true;
+
+       //sets margins
+       setTopMargin(DEFAULT_TOPBOTTOM_MARGIN);
+       setBottomMargin(DEFAULT_TOPBOTTOM_MARGIN);
+
+       //sets ratio
+       ratio_ = DEFAULT_RATIO;
+}
+
+/**
+ * Returns the text from FFScrollingLabel named title
+ */
+QString FFScrollingButton::title() const
+{
+       return title_->text();
+}
+
+/**
+ * Returns alignment of title.
+ */
+FFScrollingLabel::Alignment FFScrollingButton::titleAlignment() const
+{
+       return titleWidget()->alignment();
+}
+
+/**
+ * Returns title`s color.
+ */
+QColor FFScrollingButton::titleColor() const
+{
+       return title_->color();
+}
+
+/**
+ * Returns title`s indent.
+ */
+int FFScrollingButton::titleIndent() const
+{
+       return titleIndent_;
+}
+
+/**
+ * Returns title`s font.
+ */
+QFont FFScrollingButton::titleFont() const
+{
+       return title_->font();
+}
+
+/**
+ * Returns the text from FFScrollingLabel named description.
+ */
+QString FFScrollingButton::description() const
+{
+       return description_->text();
+}
+
+/**
+ * Return alignment of description.
+ */
+FFScrollingLabel::Alignment FFScrollingButton::descriptionAlignment() const
+{
+       return descriptionWidget()->alignment();
+}
+
+/**
+ * Return description`s color.
+ */
+QColor FFScrollingButton::descriptionColor() const
+{
+       return description_->color();
+}
+
+/**
+ * Returns description`s indent.
+ */
+int FFScrollingButton::descriptionIndent() const
+{
+       return descriptionIndent_;
+}
+
+/**
+ * Returns description`s font.
+ */
+QFont FFScrollingButton::descriptionFont() const
+{
+       return description_->font();
+}
+
+/**
+ * Returns ratio.
+ */
+float FFScrollingButton::ratio() const
+{
+       return ratio_;
+}
+
+/**
+ * Sets a text of the title.
+ * @param title Title text.
+ */
+void FFScrollingButton::setTitle(const QString& title)
+{
+       title_->setText(title);
+       update();
+}
+
+/**
+ * Sets a alignment of the title.
+ * @param alignment Title text.
+ */
+void FFScrollingButton::setTitleAlignment(FFScrollingLabel::Alignment alignment)
+{
+       titleWidget()->setAlignment(alignment);
+       update();
+}
+
+/**
+ * Sets a color of text on title.
+ * @param color Color of title.
+ */
+void FFScrollingButton::setTitleColor(QColor color)
+{
+       title_->setColor(color);
+       update();
+}
+
+/**
+ * Sets a font that will be used to print the text of the title.
+ * @param font Font style.
+ */
+void FFScrollingButton::setTitleFont(QFont font)
+{
+       title_->setFont(font);
+       update();
+}
+
+/**
+ * Sets Indent of title.
+ * @param indent Indent of title.
+ */
+void FFScrollingButton::setTitleIndent(int indent)
+{
+       titleIndent_ = indent;
+       titleIndentTemp = indent;
+       updateView();
+}
+
+/**
+ * Sets a text of the description.
+ * @param text Description text
+ */
+void FFScrollingButton::setDescription(const QString& text)
+{
+       description_->setText(text);
+       update();
+}
+
+/**
+ * Sets a alignment of the title.
+ * @param alignment Title text.
+ */
+void FFScrollingButton::setDescriptionAlignment(FFScrollingLabel::Alignment alignment)
+{
+       descriptionWidget()->setAlignment(alignment);
+       update();
+}
+
+/**
+ * Sets a color of text on description.
+ * @param color Color of description.
+ */
+void FFScrollingButton::setDescriptionColor(QColor color)
+{
+       description_->setColor(color);
+       update();
+}
+
+/**
+ * Sets a font that will be used to print the text of the description.
+ * @param font Font style.
+ */
+void FFScrollingButton::setDescriptionFont(QFont font)
+{
+       description_->setFont(font);
+       update();
+}
+
+/**
+ * Sets Indent of description.
+ * @param indent Indent of description.
+ */
+void FFScrollingButton::setDescriptionIndent(int indent)
+{
+       descriptionIndent_ = indent;
+       descriptionIndentTemp = indent;
+       updateView();
+}
+
+/**
+ * Sets ratio.
+ * @param ratio Ratio between size of title and description.
+ */
+void FFScrollingButton::setRatio(float ratio)
+{
+       if(ratio >= 0.05 && ratio <= 0.95)
+       {
+               ratio_ = ratio;
+       }
+       else if(ratio < 0.05)
+       {
+               ratio_ = 0.05;
+       }
+       else
+       {
+               ratio_ = 0.95;
+       }
+       calculateRatio();
+       updateView();
+}
+
+/**
+ * Disables title. Show/hide title.
+ * @param state State true/false to set visible of description.
+ */
+void FFScrollingButton::disableTitle(bool state)
+{
+       titleSwitch_ = !state;
+       title_->setVisible(titleSwitch_);
+       update();
+}
+
+/**
+ * Disables description. Show/hide description.
+ * @param state State true/false to set visible of description.
+ */
+void FFScrollingButton::disableDescription(bool state)
+{
+       descriptionSwitch_ = !state;
+       description_->setVisible(descriptionSwitch_);
+       update();
+}
+
+/**
+ * Returns state of title visibility. If returned value is true visibility is on.
+ */
+bool FFScrollingButton::isDisabledTitle()
+{
+       return titleSwitch_;
+}
+
+/**
+ * Returns state of description visibility. If returned value is true visibility is on.
+ */
+bool FFScrollingButton::isDisabledDescription()
+{
+       return descriptionSwitch_;
+}
+
+/**
+ * Repaints the view of button. Overrides the virtual method from QWidget.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingButton::paintEvent(QPaintEvent* event)
+{
+       FFAbstractButton::paintEvent(event);
+}
+
+/**
+ * Updates button`s view after changing size of the widget.
+ * Updates size of all pixmaps which need it (without corners)
+ * @param event Contains all informations about event.
+ */
+void FFScrollingButton::resizeEvent(QResizeEvent* event)
+{
+       FFAbstractButton::resizeEvent(event);
+       calculateRatio();
+       updateView();
+}
+
+/**
+ * Support mouse press event.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingButton::mousePressEvent(QMouseEvent* event)
+{
+       FFAbstractButton::mousePressEvent(event);
+}
+
+/**
+ * Support mouse release event.
+ * @param event Contains all informations about event.
+ */
+void FFScrollingButton::mouseReleaseEvent(QMouseEvent* event)
+{
+       FFAbstractButton::mouseReleaseEvent(event);
+}
+
+/**
+ * Paints icon if it was set.
+ * @param painter is a tool to paint icon on widget.
+ */
+void FFScrollingButton::paintIcon(QPainter* painter)
+{
+       int move = 0;
+       if(iconAlignment_ == Qt::AlignLeft)
+       {
+               move = titleIndent_;
+       }
+       else
+       {
+               move = -titleIndent_;
+       }
+
+       // if icon was set paint it
+
+       if (!icon().isNull())
+       {
+               QRect rect(0+leftMargin() + move,
+                          0+topMargin(),
+                          geometry().width()-rightMargin()-leftMargin() ,
+                          geometry().height()-bottomMargin()-topMargin());
+
+               //paint icon in button center
+               icon().paint(painter,rect,iconAlignment_,QIcon::Normal, QIcon::Off);
+       }
+}
+
+/**
+ * Returns pointer to title FFScrolingLabel.
+ */
+FFScrollingLabel* FFScrollingButton::titleWidget() const
+{
+       return title_;
+}
+
+/**
+ * Returns pointer to description FFScrolingLabel.
+ */
+FFScrollingLabel* FFScrollingButton::descriptionWidget() const
+{
+       return description_;
+}
+
+/**
+ * Changes tRatio - title ratio and dRatio - description ratio to show/not show title/description.
+ */
+void FFScrollingButton::calculateRatio()
+{
+       if(titleSwitch_ && descriptionSwitch_) // show title and description
+       {
+               tRatio = ratio_;
+               dRatio = 1.0 - ratio_;
+       }
+       else if(!titleSwitch_ && descriptionSwitch_) // show only description
+       {
+               tRatio = 0;
+               dRatio = 1;
+       }
+       else if(titleSwitch_ && !descriptionSwitch_) // show only title
+       {
+               tRatio = 1;
+               dRatio = 0;
+       }
+}
+
+/**
+ * Scales title and description indent. When resize event appears and calls out scaleIndent it changes indents to fit to new size.
+ * @param oldSize Old size of widget.
+ * @param newSize New size of widget.
+ */
+void FFScrollingButton::scaleIndent(QSize oldSize, QSize newSize)
+{
+       float widthProportion = (float)newSize.width() / (float)oldSize.width();
+
+       titleIndentTemp *= widthProportion;
+       descriptionIndentTemp *= widthProportion;
+
+       titleIndent_ = titleIndentTemp;
+       descriptionIndent_ = descriptionIndentTemp;
+}
+
+/**
+ * Recalculates geometry of labels (indent, margins etc.)
+ */
+void FFScrollingButton::updateView()
+{
+       // sets neede values
+       int margins = topMargin() + bottomMargin() + hSpacing();
+
+       int xT = 0;
+       int xD = 0;
+
+       int w = 0;
+
+       w = width() - 2 * descriptionIndent_ - leftMargin() - rightMargin() - iconSpace;
+       //values depend from alignment
+       if(iconAlignment() == Qt::AlignLeft)
+       {
+               xT = leftMargin() + titleIndent_ + iconSpace;
+               xD = leftMargin() + descriptionIndent_ + iconSpace;
+
+       }
+       else
+       {
+               xT = leftMargin() + titleIndent_;
+               xD = leftMargin() + descriptionIndent_;
+       }
+       //sets geometry of labels with texts
+       title_->setGeometry(xT,
+                           topMargin(),
+                           w,
+                           (height() - margins) * tRatio);
+
+       description_->setGeometry(xD,
+                                 topMargin() + title_->size().height() + hSpacing(),
+                                 w ,
+                                 (height() - margins) * dRatio);
+
+       update();
+       FFAbstractButton::updateView();
+}
+/**
+ * Sets icon's alignment
+ */
+void FFScrollingButton::setIconAlignment(Qt::Alignment iconAlignment)
+{
+       switch(iconAlignment)
+       {
+               case  Qt::AlignLeft:
+               case  Qt::AlignRight:
+                       FFAbstractButton::setIconAlignment(iconAlignment);
+                       break;
+               default:
+                       qDebug() << "You can only use Qt::AlignLeft or Qt::AlignRight";
+                       break;
+       }
+}
+// ####################################################### PRIVATE