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
76 * @file ffabstractbutton.cpp
77 * @brief Implementation of the FFAbstractButton class.
79 * @author ComArch S.A.
83 * This widget have an inside rectangle (draw area). This rectangle is bounded by four margins (top, bottom, left, right).
84 * Function setMargins(int,int,int,int) provides interface to change this parameters.
87 #include "ffabstractbutton.h"
89 /*Static FFViewCaches -improve performance*/
90 FFViewCache* FFAbstractButton::topLeftNormal = NULL;
91 FFViewCache* FFAbstractButton::topRightNormal = NULL;
92 FFViewCache* FFAbstractButton::bottomLeftNormal = NULL;
93 FFViewCache* FFAbstractButton::bottomRightNormal = NULL;
94 FFViewCache* FFAbstractButton::topLeftClicked = NULL;
95 FFViewCache* FFAbstractButton::topRightClicked = NULL;
96 FFViewCache* FFAbstractButton::bottomLeftClicked = NULL;
97 FFViewCache* FFAbstractButton::bottomRightClicked = NULL;
100 bool FFAbstractButton::staticFFViewCacheUpdated = false;
101 int FFAbstractButton::minimumWidth = 0;
103 * Constructs a FFAbstractButton with a parent.
104 * Set variable to initial values and set graphics used in widget.
106 FFAbstractButton::FFAbstractButton(QWidget* parent) :
107 QAbstractButton(parent)
114 * A virtual destructor of FFAbstractButton.
116 FFAbstractButton::~FFAbstractButton()
122 * Returns the left margin of inside drawing rectangle.
124 int FFAbstractButton::leftMargin()
126 return insideRect.leftMargin_;
130 * Returns the left margin of inside drawing rectangle.
132 int FFAbstractButton::rightMargin()
134 return insideRect.rightMargin_;
138 * Returns the top margin of inside drawing rectangle.
140 int FFAbstractButton::topMargin()
142 return insideRect.topMargin_;
146 * Returns the bottom margin of inside drawing rectangle.
148 int FFAbstractButton::bottomMargin()
150 return insideRect.bottomMargin_;
154 * Returns the horizontal spacing between any elements in inside rectangle.
156 int FFAbstractButton::hSpacing()
158 return insideRect.hSpacing_;
162 * Returns the vertical spacing between any elements in inside rectangle.
164 int FFAbstractButton::vSpacing()
166 return insideRect.vSpacing_;
170 * Sets left margin of elements on a button
171 * @param leftMargin left margin of elements on a button
173 void FFAbstractButton::setLeftMargin(int leftMargin)
175 insideRect.leftMargin_ = leftMargin;
176 insideRect.leftMarginTemp = leftMargin;
182 * Sets right margin of elements on a button
183 * @param rightMargin right margin of elements on a button
185 void FFAbstractButton::setRightMargin(int rightMargin)
187 insideRect.rightMargin_ = rightMargin;
188 insideRect.rightMarginTemp = rightMargin;
194 * Sets top margin of elements on a button
195 * @param topMargin top margin of elements on a button
197 void FFAbstractButton::setTopMargin(int topMargin)
199 insideRect.topMargin_ = topMargin;
200 insideRect.topMarginTemp = topMargin;
206 * Sets bottom margin of elements on a button
207 * @param bottomMargin bottom margin of elements on a button
209 void FFAbstractButton::setBottomMargin(int bottomMargin)
211 insideRect.bottomMargin_ = bottomMargin;
212 insideRect.bottomMarginTemp = bottomMargin;
218 * Provides interface to change this parameters.
219 * @param leftMargin set margin between left edge of widget to left edge of inside rectangle
220 * @param rightMargin set margin between right edge of widget to right edge of inside rectangle
221 * @param topMargin set margin between top edge of widget to top edge of inside rectangle
222 * @param bottomMargin set margin between bottom edge of widget to bottom edge of inside rectangle
224 void FFAbstractButton::setMargins(int leftMargin,
229 insideRect.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
235 * Sets horizontal spacing between any elements in inside rectangle.
236 * @param hSpacing Horizontal spacing
238 void FFAbstractButton::setHSpacing(int hSpacing)
240 insideRect.hSpacing_ = hSpacing;
241 insideRect.hSpacingTemp = hSpacing;
246 * Sets vertical spacing between any elements in inside rectangle.
247 * @param spacing Vertical spacing
249 void FFAbstractButton::setVSpacing(int spacing)
251 insideRect.vSpacing_ = spacing;
252 insideRect.vSpacingTemp = spacing;
258 QPen FFAbstractButton::pen()
266 void FFAbstractButton::setPen(QPen pen)
271 * Repaints the view of this button. Overrides the virtual method from QWidget.
272 * If button is not clicked draw one type of pixmaps otherwise draw another.
274 * Composition of pixmaps:
275 * <table><tr><td> topLeft </td><td> topCenter </td><td> topRight </td></tr>
276 * <tr><td> centerLeft </td><td> center </td><td> centerRight </td></tr>
277 * <tr><td> bottomLeft </td><td> bottomCenter </td><td> bottomRight </td></tr></table>
278 * @param event Contains all informations about event.
280 void FFAbstractButton::paintEvent(QPaintEvent* event)
290 painter.drawPixmap(QPoint(0, 0), topLeftNormal->pixmap());
291 painter.drawPixmap(QPoint(topLeftNormal->defaultSize().width(),
292 0), topCenterNormal->pixmap());
293 painter.drawPixmap(QPoint(topLeftNormal->defaultSize().width()
294 + pixmapsPositionWidth_, 0),
295 topRightNormal->pixmap());
296 painter.drawPixmap(QPoint(0,
297 topLeftNormal->defaultSize().height()),
298 centerLeftNormal->pixmap());
299 painter.drawPixmap(QPoint(centerLeftNormal->defaultSize().width(),
300 topCenterNormal->defaultSize().height()),
301 centerNormal->pixmap());
302 painter.drawPixmap(QPoint(centerLeftNormal->defaultSize().width()
303 + pixmapsPositionWidth_,
304 topRightNormal->defaultSize().height()),
305 centerRightNormal->pixmap());
306 painter.drawPixmap(QPoint(0,
307 topLeftNormal->defaultSize().height()
308 + pixmapsPositionHeight_),
309 bottomLeftNormal->pixmap());
310 painter.drawPixmap(QPoint(bottomLeftNormal->defaultSize().width(),
311 topCenterNormal->defaultSize().height()
312 + pixmapsPositionHeight_),
313 bottomCenterNormal->pixmap());
314 painter.drawPixmap(QPoint(bottomLeftNormal->defaultSize().width()
315 + pixmapsPositionWidth_,
316 topRightNormal->defaultSize().height()
317 + pixmapsPositionHeight_),
318 bottomRightNormal->pixmap());
322 painter.drawPixmap(QPoint(0, 0), topLeftClicked->pixmap());
323 painter.drawPixmap(QPoint(topLeftClicked->defaultSize().width(),
325 topCenterClicked->pixmap());
326 painter.drawPixmap(QPoint(topLeftClicked->defaultSize().width()
327 + pixmapsPositionWidth_, 0),
328 topRightClicked->pixmap());
329 painter.drawPixmap(QPoint(0,
330 topLeftClicked->defaultSize().height()),
331 centerLeftClicked->pixmap());
332 painter.drawPixmap(QPoint(centerLeftClicked->defaultSize().width(),
333 topCenterClicked->defaultSize().height()),
334 centerClicked->pixmap());
335 painter.drawPixmap(QPoint(centerLeftClicked->defaultSize().width()
336 + pixmapsPositionWidth_,
337 topRightClicked->defaultSize().height()),
338 centerRightClicked->pixmap());
339 painter.drawPixmap(QPoint(0,
340 topLeftClicked->defaultSize().height()
341 + pixmapsPositionHeight_),
342 bottomLeftClicked->pixmap());
343 painter.drawPixmap(QPoint(bottomLeftClicked->defaultSize().width(),
344 topCenterClicked->defaultSize().height()
345 + pixmapsPositionHeight_),
346 bottomCenterClicked->pixmap());
347 painter.drawPixmap(QPoint(bottomLeftClicked->defaultSize().width()
348 + pixmapsPositionWidth_,
349 topRightClicked->defaultSize().height()
350 + pixmapsPositionHeight_),
351 bottomRightClicked->pixmap());
360 * Update button view after changing size of the widget.
361 * Update size of all pixmap which need it (without corners)
362 * @param event Contains all informations about event.
364 void FFAbstractButton::resizeEvent(QResizeEvent* event)
366 pixmapsPositionWidth_ = event->size().width()
367 - centerLeftNormal->defaultSize().width()
368 - centerRightNormal->defaultSize().width();
370 pixmapsPositionHeight_ = event->size().height()
371 - topCenterNormal->defaultSize().height()
372 - bottomCenterNormal->defaultSize().height();
374 if(event->oldSize().isValid())
376 insideRect.scaleMargins(event->oldSize(), event->size());
382 QRect rect(0 + leftMargin(),
384 geometry().width() - rightMargin() - leftMargin()
385 - 2 * insideRect.leftIndent_,
386 geometry().height() - bottomMargin() - topMargin());
388 iconSpace = icon().pixmap(rect.size()).size().width();
397 * Initiates an object of FFAbstractgButton. Sets all needed fields. It is called by all constructors.
399 void FFAbstractButton::init()
402 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
408 iconAlignment_ = DEFAULT_ICON_ALIGNMENT;
410 if(NULL == topLeftNormal)
412 topLeftNormal = new FFViewCache;
415 topCenterNormal = new FFViewCache;
417 if(NULL == topRightNormal)
419 topRightNormal = new FFViewCache;
422 centerLeftNormal = new FFViewCache;
423 centerNormal = new FFViewCache;
424 centerRightNormal = new FFViewCache;
426 if(NULL == bottomLeftNormal)
428 bottomLeftNormal = new FFViewCache;
431 bottomCenterNormal = new FFViewCache;
433 if(NULL == bottomRightNormal)
435 bottomRightNormal = new FFViewCache;
438 if(NULL == topLeftClicked)
440 topLeftClicked = new FFViewCache;
443 topCenterClicked = new FFViewCache;
445 if(NULL == topRightClicked)
447 topRightClicked = new FFViewCache;
450 centerLeftClicked = new FFViewCache;
451 centerClicked = new FFViewCache;
452 centerRightClicked = new FFViewCache;
454 if(NULL == bottomLeftClicked)
456 bottomLeftClicked = new FFViewCache;
459 bottomCenterClicked = new FFViewCache;
461 if(NULL == bottomRightClicked)
463 bottomRightClicked = new FFViewCache;
469 * setSkin initiate FFViewCaches, update their size to default and set minimum width and height of widget
471 void FFAbstractButton::setSkin()
474 // QSize sizeTmp = QSize(width(), height());
476 if(!topLeftNormal->isValid())
478 topLeftNormal->init(path + "_topleft_normal.svg");
481 topCenterNormal->init(path + "_topcenter_normal.svg");
483 if(!topRightNormal->isValid())
485 topRightNormal->init(path + "_topright_normal.svg");
488 centerLeftNormal->init(path + "_centerleft_normal.svg");
489 centerNormal->init(path + "_center_normal.svg");
490 centerRightNormal->init(path + "_centerright_normal.svg");
492 if(!bottomLeftNormal->isValid())
494 bottomLeftNormal->init(path + "_bottomleft_normal.svg");
497 bottomCenterNormal->init(path + "_bottomcenter_normal.svg");
499 if(!bottomRightNormal->isValid())
501 bottomRightNormal->init(path + "_bottomright_normal.svg");
504 if(!topLeftClicked->isValid())
506 topLeftClicked->init(path + "_topleft_clicked.svg");
509 topCenterClicked->init(path + "_topcenter_clicked.svg");
511 if(!topRightClicked->isValid())
513 topRightClicked->init(path + "_topright_clicked.svg");
516 centerLeftClicked->init(path + "_centerleft_clicked.svg");
517 centerClicked->init(path + "_center_clicked.svg");
518 centerRightClicked->init(path + "_centerright_clicked.svg");
520 if(!bottomLeftClicked->isValid())
522 bottomLeftClicked->init(path + "_bottomleft_clicked.svg");
525 bottomCenterClicked->init(path + "_bottomcenter_clicked.svg");
527 if(!bottomRightClicked->isValid())
529 bottomRightClicked->init(path + "_bottomright_clicked.svg");
532 pixmapsPositionWidth_ = centerNormal->defaultSize().width();
533 pixmapsPositionHeight_ = centerNormal->defaultSize().height();
535 if(!staticFFViewCacheUpdated)
537 //Update view once after skin was set
538 topLeftNormal->updateView(topLeftNormal->defaultSize());
539 topRightNormal->updateView(topRightNormal->defaultSize());
540 bottomLeftNormal->updateView(bottomLeftNormal->defaultSize());
541 bottomRightNormal->updateView(bottomRightNormal->defaultSize());
543 topLeftClicked->updateView(topLeftClicked->defaultSize());
544 topRightClicked->updateView(topRightClicked->defaultSize());
545 bottomLeftClicked->updateView(bottomLeftClicked->defaultSize());
546 bottomRightClicked->updateView(bottomRightClicked->defaultSize());
549 = qMax(topLeftNormal->defaultSize().width()
550 + topRightNormal->defaultSize().width(),
551 bottomLeftNormal->defaultSize().width()
552 + bottomRightNormal->defaultSize().width());
554 staticFFViewCacheUpdated = true;
557 pixmapsPositionWidth_ = centerNormal->defaultSize().width();
558 pixmapsPositionHeight_ = centerNormal->defaultSize().height();
560 insideRect.leftIndent_ = topLeftNormal->defaultSize().width();
562 setMinimumHeight(DEFAULT_BUTTON_HEIGHT);
563 setMinimumWidth(2 * minimumWidth);
567 * Support mouse press event.
568 * @param event Contains all informations about event.
570 void FFAbstractButton::mousePressEvent(QMouseEvent* event)
573 isClicked_ = true; // isClicked contains boolean true information of mouse button pressed
578 * Support mouse release event.
579 * @param event Contains all informations about event.
581 void FFAbstractButton::mouseReleaseEvent(QMouseEvent *event)
587 if(!signalsBlocked())
593 isClicked_ = false; // isClicked contains boolean false information of mouse button unpressed
598 * Recalculate geometry of elements on button
600 void FFAbstractButton::updateView()
602 if(pixmapsPositionHeight_ <= 0)
604 pixmapsPositionHeight_ = 1;
606 //Update view all the time resize event appear:
607 topCenterNormal->updateView(QSize(pixmapsPositionWidth_,
608 topCenterNormal->defaultSize().height()));
609 bottomCenterNormal->updateView(QSize(pixmapsPositionWidth_,
610 bottomCenterNormal->defaultSize().height()));
612 centerLeftNormal->updateView(QSize(centerLeftNormal->defaultSize().width(),
613 pixmapsPositionHeight_));
614 centerRightNormal->updateView(QSize(centerRightNormal->defaultSize().width(),
615 pixmapsPositionHeight_));
616 centerNormal->updateView(QSize(pixmapsPositionWidth_,
617 pixmapsPositionHeight_));
619 topCenterClicked->updateView(QSize(pixmapsPositionWidth_,
620 topCenterClicked->defaultSize().height()));
621 bottomCenterClicked->updateView(QSize(pixmapsPositionWidth_,
622 bottomCenterClicked->defaultSize().height()));
623 centerLeftClicked->updateView(QSize(centerLeftClicked->defaultSize().width(),
624 pixmapsPositionHeight_));
625 centerRightClicked->updateView(QSize(centerRightClicked->defaultSize().width(),
626 pixmapsPositionHeight_));
627 centerClicked->updateView(QSize(pixmapsPositionWidth_,
628 pixmapsPositionHeight_));
631 * Draws icon on button
633 void FFAbstractButton::paintIcon(QPainter* painter)
636 if(iconAlignment_ == Qt::AlignLeft)
638 move = insideRect.leftIndent_;
640 else if(iconAlignment_ == Qt::AlignRight)
642 move = -insideRect.leftIndent_;
648 //if icon was set paint it
651 QRect rect(0 + leftMargin() + move,
653 geometry().width() - rightMargin() - leftMargin(),
654 geometry().height() - bottomMargin() - topMargin());
656 //paint icon in button center
657 icon().paint(painter,
667 * Initiate FFAbstractButtonPrivate
669 FFAbstractButtonPrivate::FFAbstractButtonPrivate()
671 setMargins(0.0, 0.0, 0.0, 0.0);
680 * Dectructor of FFAbstractButtonPrivate
682 FFAbstractButtonPrivate::~FFAbstractButtonPrivate()
688 * scaleMargins scale margins eg. when resize event appear
689 * @param oldSize before eg. resize event
690 * @param newSize actual size after eg. resize event
692 void FFAbstractButtonPrivate::scaleMargins(QSize oldSize, QSize newSize)
694 float widthProportion = (float)newSize.width() / (float)oldSize.width();
695 float heightProportion = (float)newSize.height()
696 / (float)oldSize.height();
698 leftMarginTemp *= widthProportion;
699 rightMarginTemp *= widthProportion;
700 topMarginTemp *= heightProportion;
701 bottomMarginTemp *= heightProportion;
702 hSpacingTemp *= heightProportion;
703 vSpacingTemp *= widthProportion;
705 leftMargin_ = leftMarginTemp;
706 rightMargin_ = rightMarginTemp;
707 topMargin_ = topMarginTemp;
708 bottomMargin_ = bottomMarginTemp;
709 hSpacing_ = hSpacingTemp;
710 vSpacing_ = vSpacingTemp;
714 * setMargins set left, right, top and bottom margins
715 * @param leftMargin Size of left margin
716 * @param rightMargin Size of right margin
717 * @param topMargin Size of top margin
718 * @param bottomMargin Size of bottom margin
720 void FFAbstractButtonPrivate::setMargins(int leftMargin,
725 leftMargin_ = leftMargin;
726 rightMargin_ = rightMargin;
727 topMargin_ = topMargin;
728 bottomMargin_ = bottomMargin;
730 leftMarginTemp = leftMargin;
731 rightMarginTemp = rightMargin;
732 topMarginTemp = topMargin;
733 bottomMarginTemp = bottomMargin;
736 * Returns alignment of icon
738 Qt::Alignment FFAbstractButton::iconAlignment()
740 return iconAlignment_;
743 * Sets alignment of icon
744 * @param iconAlignment new alignment of icon
746 void FFAbstractButton::setIconAlignment(Qt::Alignment iconAlignment)
748 this->iconAlignment_ = iconAlignment;
751 * Returns default width of image used as button's corner
753 int FFAbstractButton::indent()
755 return insideRect.leftIndent_;
759 \fn void FFAbstractButton::clicked()
761 This signal is emitted when the button will be clicked.