added all files
[ffqwlibrary] / libffqw-1.0 / sources / ffscrollingbutton.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 ffscrollingbutton.h
76  * @brief Contains a necessary class declaration.
77  *
78  * @author ComArch S.A.
79  * @date 2009.08.03
80  * @version 1.0
81  */
82
83 #include "ffscrollingbutton.h"
84
85 /**
86  * Constructs a FFScrollingButton with a parent.
87  * Sets variable to initial values and set graphics used in widget.
88  */
89 FFScrollingButton::FFScrollingButton(QWidget* parent) :
90         FFAbstractButton(parent)
91 {
92         init();
93 }
94
95 /**
96  * Constructs a FFScrollingButton with a parent , title and description.
97  * Sets variable to initial values and sets graphics used in widget.
98  */
99 FFScrollingButton::FFScrollingButton(QString title, QString description, QWidget* parent) :
100         FFAbstractButton(parent)
101 {
102         init();
103         title_->setText(title);
104         description_->setText(description);
105 }
106
107
108 /**
109  * A virtual destructor.
110  */
111 FFScrollingButton::~FFScrollingButton()
112 {
113         ;
114 }
115
116 /**
117  * Initiates an object of FFScrollingButton. Sets all needed fields.
118  */
119 void FFScrollingButton::init()
120 {
121         //sets title's label
122         title_ = new FFScrollingLabel(this);
123         title_->setResizable(true);
124         title_->setColor(FF_TITLE_COLOR);
125         title_->show();
126
127         //sets desciption's label
128         description_ = new FFScrollingLabel(this);
129         description_->setResizable(true);
130         description_->setColor(FF_DESCRIPTION_COLOR);
131         description_->show();
132
133         //sets indents
134         titleIndent_ = indent();
135         descriptionIndent_ = indent();
136
137         titleIndentTemp = indent();
138         descriptionIndentTemp = indent();
139
140         //sets switches
141         titleSwitch_ = true;
142         descriptionSwitch_ = true;
143
144         //sets margins
145         setTopMargin(DEFAULT_TOPBOTTOM_MARGIN);
146         setBottomMargin(DEFAULT_TOPBOTTOM_MARGIN);
147
148         //sets ratio
149         ratio_ = DEFAULT_RATIO;
150 }
151
152 /**
153  * Returns the text from FFScrollingLabel named title
154  */
155 QString FFScrollingButton::title() const
156 {
157         return title_->text();
158 }
159
160 /**
161  * Returns alignment of title.
162  */
163 FFScrollingLabel::Alignment FFScrollingButton::titleAlignment() const
164 {
165         return titleWidget()->alignment();
166 }
167
168 /**
169  * Returns title`s color.
170  */
171 QColor FFScrollingButton::titleColor() const
172 {
173         return title_->color();
174 }
175
176 /**
177  * Returns title`s indent.
178  */
179 int FFScrollingButton::titleIndent() const
180 {
181         return titleIndent_;
182 }
183
184 /**
185  * Returns title`s font.
186  */
187 QFont FFScrollingButton::titleFont() const
188 {
189         return title_->font();
190 }
191
192 /**
193  * Returns the text from FFScrollingLabel named description.
194  */
195 QString FFScrollingButton::description() const
196 {
197         return description_->text();
198 }
199
200 /**
201  * Return alignment of description.
202  */
203 FFScrollingLabel::Alignment FFScrollingButton::descriptionAlignment() const
204 {
205         return descriptionWidget()->alignment();
206 }
207
208 /**
209  * Return description`s color.
210  */
211 QColor FFScrollingButton::descriptionColor() const
212 {
213         return description_->color();
214 }
215
216 /**
217  * Returns description`s indent.
218  */
219 int FFScrollingButton::descriptionIndent() const
220 {
221         return descriptionIndent_;
222 }
223
224 /**
225  * Returns description`s font.
226  */
227 QFont FFScrollingButton::descriptionFont() const
228 {
229         return description_->font();
230 }
231
232 /**
233  * Returns ratio.
234  */
235 float FFScrollingButton::ratio() const
236 {
237         return ratio_;
238 }
239
240 /**
241  * Sets a text of the title.
242  * @param title Title text.
243  */
244 void FFScrollingButton::setTitle(const QString& title)
245 {
246         title_->setText(title);
247         update();
248 }
249
250 /**
251  * Sets a alignment of the title.
252  * @param alignment Title text.
253  */
254 void FFScrollingButton::setTitleAlignment(FFScrollingLabel::Alignment alignment)
255 {
256         titleWidget()->setAlignment(alignment);
257         update();
258 }
259
260 /**
261  * Sets a color of text on title.
262  * @param color Color of title.
263  */
264 void FFScrollingButton::setTitleColor(QColor color)
265 {
266         title_->setColor(color);
267         update();
268 }
269
270 /**
271  * Sets a font that will be used to print the text of the title.
272  * @param font Font style.
273  */
274 void FFScrollingButton::setTitleFont(QFont font)
275 {
276         title_->setFont(font);
277         update();
278 }
279
280 /**
281  * Sets Indent of title.
282  * @param indent Indent of title.
283  */
284 void FFScrollingButton::setTitleIndent(int indent)
285 {
286         titleIndent_ = indent;
287         titleIndentTemp = indent;
288         updateView();
289 }
290
291 /**
292  * Sets a text of the description.
293  * @param text Description text
294  */
295 void FFScrollingButton::setDescription(const QString& text)
296 {
297         description_->setText(text);
298         update();
299 }
300
301 /**
302  * Sets a alignment of the title.
303  * @param alignment Title text.
304  */
305 void FFScrollingButton::setDescriptionAlignment(FFScrollingLabel::Alignment alignment)
306 {
307         descriptionWidget()->setAlignment(alignment);
308         update();
309 }
310
311 /**
312  * Sets a color of text on description.
313  * @param color Color of description.
314  */
315 void FFScrollingButton::setDescriptionColor(QColor color)
316 {
317         description_->setColor(color);
318         update();
319 }
320
321 /**
322  * Sets a font that will be used to print the text of the description.
323  * @param font Font style.
324  */
325 void FFScrollingButton::setDescriptionFont(QFont font)
326 {
327         description_->setFont(font);
328         update();
329 }
330
331 /**
332  * Sets Indent of description.
333  * @param indent Indent of description.
334  */
335 void FFScrollingButton::setDescriptionIndent(int indent)
336 {
337         descriptionIndent_ = indent;
338         descriptionIndentTemp = indent;
339         updateView();
340 }
341
342 /**
343  * Sets ratio.
344  * @param ratio Ratio between size of title and description.
345  */
346 void FFScrollingButton::setRatio(float ratio)
347 {
348         if(ratio >= 0.05 && ratio <= 0.95)
349         {
350                 ratio_ = ratio;
351         }
352         else if(ratio < 0.05)
353         {
354                 ratio_ = 0.05;
355         }
356         else
357         {
358                 ratio_ = 0.95;
359         }
360         calculateRatio();
361         updateView();
362 }
363
364 /**
365  * Disables title. Show/hide title.
366  * @param state State true/false to set visible of description.
367  */
368 void FFScrollingButton::disableTitle(bool state)
369 {
370         titleSwitch_ = !state;
371         title_->setVisible(titleSwitch_);
372         update();
373 }
374
375 /**
376  * Disables description. Show/hide description.
377  * @param state State true/false to set visible of description.
378  */
379 void FFScrollingButton::disableDescription(bool state)
380 {
381         descriptionSwitch_ = !state;
382         description_->setVisible(descriptionSwitch_);
383         update();
384 }
385
386 /**
387  * Returns state of title visibility. If returned value is true visibility is on.
388  */
389 bool FFScrollingButton::isDisabledTitle()
390 {
391         return titleSwitch_;
392 }
393
394 /**
395  * Returns state of description visibility. If returned value is true visibility is on.
396  */
397 bool FFScrollingButton::isDisabledDescription()
398 {
399         return descriptionSwitch_;
400 }
401
402 /**
403  * Repaints the view of button. Overrides the virtual method from QWidget.
404  * @param event Contains all informations about event.
405  */
406 void FFScrollingButton::paintEvent(QPaintEvent* event)
407 {
408         FFAbstractButton::paintEvent(event);
409 }
410
411 /**
412  * Updates button`s view after changing size of the widget.
413  * Updates size of all pixmaps which need it (without corners)
414  * @param event Contains all informations about event.
415  */
416 void FFScrollingButton::resizeEvent(QResizeEvent* event)
417 {
418         FFAbstractButton::resizeEvent(event);
419         calculateRatio();
420         updateView();
421 }
422
423 /**
424  * Support mouse press event.
425  * @param event Contains all informations about event.
426  */
427 void FFScrollingButton::mousePressEvent(QMouseEvent* event)
428 {
429         FFAbstractButton::mousePressEvent(event);
430 }
431
432 /**
433  * Support mouse release event.
434  * @param event Contains all informations about event.
435  */
436 void FFScrollingButton::mouseReleaseEvent(QMouseEvent* event)
437 {
438         FFAbstractButton::mouseReleaseEvent(event);
439 }
440
441 /**
442  * Paints icon if it was set.
443  * @param painter is a tool to paint icon on widget.
444  */
445 void FFScrollingButton::paintIcon(QPainter* painter)
446 {
447         int move = 0;
448         if(iconAlignment_ == Qt::AlignLeft)
449         {
450                 move = titleIndent_;
451         }
452         else
453         {
454                 move = -titleIndent_;
455         }
456
457         // if icon was set paint it
458
459         if (!icon().isNull())
460         {
461                 QRect rect(0+leftMargin() + move,
462                            0+topMargin(),
463                            geometry().width()-rightMargin()-leftMargin() ,
464                            geometry().height()-bottomMargin()-topMargin());
465
466                 //paint icon in button center
467                 icon().paint(painter,rect,iconAlignment_,QIcon::Normal, QIcon::Off);
468         }
469 }
470
471 /**
472  * Returns pointer to title FFScrolingLabel.
473  */
474 FFScrollingLabel* FFScrollingButton::titleWidget() const
475 {
476         return title_;
477 }
478
479 /**
480  * Returns pointer to description FFScrolingLabel.
481  */
482 FFScrollingLabel* FFScrollingButton::descriptionWidget() const
483 {
484         return description_;
485 }
486
487 /**
488  * Changes tRatio - title ratio and dRatio - description ratio to show/not show title/description.
489  */
490 void FFScrollingButton::calculateRatio()
491 {
492         if(titleSwitch_ && descriptionSwitch_) // show title and description
493         {
494                 tRatio = ratio_;
495                 dRatio = 1.0 - ratio_;
496         }
497         else if(!titleSwitch_ && descriptionSwitch_) // show only description
498         {
499                 tRatio = 0;
500                 dRatio = 1;
501         }
502         else if(titleSwitch_ && !descriptionSwitch_) // show only title
503         {
504                 tRatio = 1;
505                 dRatio = 0;
506         }
507 }
508
509 /**
510  * Scales title and description indent. When resize event appears and calls out scaleIndent it changes indents to fit to new size.
511  * @param oldSize Old size of widget.
512  * @param newSize New size of widget.
513  */
514 void FFScrollingButton::scaleIndent(QSize oldSize, QSize newSize)
515 {
516         float widthProportion = (float)newSize.width() / (float)oldSize.width();
517
518         titleIndentTemp *= widthProportion;
519         descriptionIndentTemp *= widthProportion;
520
521         titleIndent_ = titleIndentTemp;
522         descriptionIndent_ = descriptionIndentTemp;
523 }
524
525 /**
526  * Recalculates geometry of labels (indent, margins etc.)
527  */
528 void FFScrollingButton::updateView()
529 {
530         // sets neede values
531         int margins = topMargin() + bottomMargin() + hSpacing();
532
533         int xT = 0;
534         int xD = 0;
535
536         int w = 0;
537
538         w = width() - 2 * descriptionIndent_ - leftMargin() - rightMargin() - iconSpace;
539         //values depend from alignment
540         if(iconAlignment() == Qt::AlignLeft)
541         {
542                 xT = leftMargin() + titleIndent_ + iconSpace;
543                 xD = leftMargin() + descriptionIndent_ + iconSpace;
544
545         }
546         else
547         {
548                 xT = leftMargin() + titleIndent_;
549                 xD = leftMargin() + descriptionIndent_;
550         }
551         //sets geometry of labels with texts
552         title_->setGeometry(xT,
553                             topMargin(),
554                             w,
555                             (height() - margins) * tRatio);
556
557         description_->setGeometry(xD,
558                                   topMargin() + title_->size().height() + hSpacing(),
559                                   w ,
560                                   (height() - margins) * dRatio);
561
562         update();
563         FFAbstractButton::updateView();
564 }
565 /**
566  * Sets icon's alignment
567  */
568 void FFScrollingButton::setIconAlignment(Qt::Alignment iconAlignment)
569 {
570         switch(iconAlignment)
571         {
572                 case  Qt::AlignLeft:
573                 case  Qt::AlignRight:
574                         FFAbstractButton::setIconAlignment(iconAlignment);
575                         break;
576                 default:
577                         qDebug() << "You can only use Qt::AlignLeft or Qt::AlignRight";
578                         break;
579         }
580 }
581 // ####################################################### PRIVATE