added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffscrollingcheckbox.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 ffscrollingcheckbox.cpp
76  * @brief Implementation of the FFScrollingCheckBox class.
77  *
78  * @author ComArch S.A.
79  * @date 2009.08.03
80  * @version 1.0
81  */
82
83 #include "ffscrollingcheckbox.h"
84
85
86 /*Static FFViewCaches -improve performance*/
87 FFViewCache* FFScrollingCheckBox::stateTrue = NULL;
88 FFViewCache* FFScrollingCheckBox::stateFalse = NULL;
89 /**
90  * Constructs a FFScrollingCheckBox with a parent.
91  * Sets variable to initial values and set graphics used in widget.
92  */
93 FFScrollingCheckBox::FFScrollingCheckBox(QWidget* parent) :
94         FFScrollingButton(parent)
95 {
96         path = SCROLLINGCHECKBOX_PATH;
97         init();
98 }
99 /**
100  * Constructs a FFScrollingCheckBox with a parent and new path to
101  * False/Normal Graphics.
102  * Set variable to initial values and set graphics used in widget.
103  */
104 FFScrollingCheckBox::FFScrollingCheckBox(const QString& path, QWidget* parent ) :
105         FFScrollingButton(parent)
106 {
107         this->path = path;
108         init();
109 }
110
111 /**
112  * A virtual destructor.
113  */
114 FFScrollingCheckBox::~FFScrollingCheckBox()
115 {
116         ;
117 }
118
119 /**
120  * Repaint the view of checkbox button. Overrides the virtual method from QWidget.
121  * @param event Contains all informations about event.
122  */
123 void FFScrollingCheckBox::paintEvent(QPaintEvent* event)
124 {
125         //calls parental paint event
126         FFAbstractButton::paintEvent(event);
127
128         //sets values for drawing
129         int x=0,y=0;
130         if(width() > height())
131         {
132                 x = leftMargin() + indent();
133                 y = topMargin() ;
134         }
135         else
136         {
137                 x = leftMargin() + indent();
138                 y = topMargin() ;
139         }
140
141         QPainter painter;
142         painter.begin(this);
143
144         //draws image.
145         if( isChecked())
146         {
147                 painter.drawPixmap(x,y,stateTrue->pixmap());
148         }
149         else
150         {
151                 painter.drawPixmap(x,y,stateFalse->pixmap());
152         }
153         painter.end();
154 }
155
156 /**
157  * Updates button`s view after changing size of the widget.
158  * Update size of all pixmap which need it (without corners)
159  * @param event Contains all informations about event.
160  */
161 void FFScrollingCheckBox::resizeEvent(QResizeEvent* event)
162 {
163         FFAbstractButton::resizeEvent(event);
164         calculateRatio();
165         updateView();
166 }
167
168 /**
169  * Support mouse press event.
170  * @param event Contains all informations about event.
171  */
172 void FFScrollingCheckBox::mousePressEvent(QMouseEvent* event)
173 {
174         FFScrollingButton::mousePressEvent(event);
175 }
176
177 /**
178  * Support mouse release event.
179  * @param event Contains all informations about event.
180  */
181 void FFScrollingCheckBox::mouseReleaseEvent(QMouseEvent* event)
182 {
183         if(isClicked_)
184         {
185                 if(!signalsBlocked())
186                 {
187                         toggle();
188                 }
189         }
190         FFScrollingButton::mouseReleaseEvent(event);
191 }
192 /**
193  * Paints icon if it was set.
194  * @param painter is a tool to paint icon on widget.
195  */
196 void FFScrollingCheckBox::paintIcon(QPainter* painter)
197 {
198         // if icon was set paint it
199         if (!icon().isNull())
200         {
201                 QRect rect(0+leftMargin() + qMin(width() - leftMargin() - rightMargin(),
202                                                  height() - topMargin() - bottomMargin()),
203                            0+topMargin(),
204                            geometry().width()-rightMargin()-leftMargin(),
205                            geometry().height()-bottomMargin()-topMargin());
206
207                 //paint icon in button center
208                 icon().paint(painter,rect,iconAlignment(),QIcon::Normal, QIcon::Off);
209         }
210 }
211 /**
212  * Returns text icon space.
213  */
214 int FFScrollingCheckBox::textIconSpace()
215 {
216         return textIconSpace_;
217 }
218
219 /**
220  * Initiates an object of FFScrollingCheckBox. Sets all needed fields.
221  */
222 void FFScrollingCheckBox::init()
223 {
224
225
226         setCheckable(true);
227         setChecked(false);
228         textIconSpace_ = DEFAULT_TEXTICON_SPACE;
229
230         if(NULL == stateFalse)
231         {
232                 stateFalse = new FFViewCache;
233                 stateFalse->init(path + "_false.svg");
234                 stateFalse->updateView(stateFalse->defaultSize());
235         }
236
237         if(NULL == stateTrue)
238         {
239
240                 stateTrue = new FFViewCache;
241                 stateTrue->init(path + "_true.svg");
242                 stateTrue->updateView(stateTrue->defaultSize());
243         }
244         setTitleIndent(0);
245         setDescriptionIndent(0);
246         setMinimumWidth(minimumHeight() + leftMargin() + rightMargin() +  indent());
247         setMargins(0,0,0,0);
248 }
249
250 /**
251  * Sets text icon space
252  */
253 void FFScrollingCheckBox::setTextIconSpace(int textIconSpace)
254 {
255         textIconSpace_=textIconSpace;
256 }
257 /**
258  * Updates button's view parameters
259  */
260 void FFScrollingCheckBox::updateView()
261 {
262         int margins = topMargin() + bottomMargin() + hSpacing();
263         int checkboxEdge = qMin(width() - leftMargin() - rightMargin(),
264                                 height() - topMargin() - bottomMargin());
265         QSize size= QSize(0,0);
266         if(width() > height())
267         {
268                 titleWidget()->setVisible(true);
269                 descriptionWidget()->setVisible(true);
270                 //sets button's labels geometry
271                 titleWidget()->setGeometry(vSpacing() + checkboxEdge + leftMargin() + iconSpace + textIconSpace_
272                                 + titleIndent() + indent(),
273                                 topMargin(),
274                                 width() - leftMargin() - rightMargin() - titleIndent() - checkboxEdge - hSpacing() - iconSpace - textIconSpace_ - indent(),
275                                 (height() - margins) * tRatio);
276
277                 descriptionWidget()->setGeometry(vSpacing() + checkboxEdge + leftMargin() + descriptionIndent() + iconSpace + textIconSpace_ + indent(),
278                                                  topMargin() + titleWidget()->size().height() + hSpacing(),
279                                                  width() - checkboxEdge - leftMargin() - descriptionIndent() - rightMargin() - hSpacing() - iconSpace - textIconSpace_ - indent(),
280                                                  (height() - margins) * (dRatio));
281                 size = QSize(checkboxEdge, checkboxEdge);
282         }
283         else
284         {
285                 size = QSize(width()-(leftMargin() + rightMargin() + 2* indent()),checkboxEdge-(topMargin()+bottomMargin()));
286         }
287
288         if(minimumSize().width() >= width() )
289         {
290                 titleWidget()->setVisible(false);
291                 descriptionWidget()->setVisible(false);
292         }
293
294         stateFalse->updateView(size);
295         stateTrue->updateView(size);
296
297         update();
298
299         //calls update view from parent
300         FFAbstractButton::updateView();
301 }
302 /**
303  * Sets icon alignment
304  * @param iconAlignment contains new alignemnt for icon
305  */
306 void FFScrollingCheckBox::setIconAlignment(Qt::Alignment iconAlignment)
307 {
308         //Sets only possible alignment options
309         switch(iconAlignment)
310         {
311                 case  Qt::AlignLeft:
312                         FFAbstractButton::setIconAlignment(iconAlignment);
313                         break;
314                 default:
315                         qDebug() << "You can only use Qt::AlignLeft or Qt::AlignRight";
316                         break;
317         }
318 }