added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffabstractcombobox.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 ffabstractcombobox.cpp
76  * @brief Implementation of the FFAbstractComboBox and FFComboPopUp class.
77  *
78  * @author  ComArch S.A.
79  * @date 2009.10.09
80  * @version 1.0
81  *
82  * This widget has activator(to open popup window) and popup window(to select item)
83  * FFAbstractComboBox and FFComboPopUp are friend classes in both directions
84  */
85
86 #include "ffabstractcombobox.h"
87
88 /**
89  * Rounds argument up.
90  * @param var is an argument to round up
91  * @return value of rounded up argument
92  */
93
94 float roundUp(float var)
95 {
96         if(0 > ((int)var - var  ))
97         {
98                 return (int)var + 1;
99         }
100         else
101         {
102                 return (int)var;
103         }
104 }
105
106 /**
107  * Constructs a FFAbstractButton with a parent.
108  * Sets variable to initial values.
109  */
110 FFAbstractComboBox::FFAbstractComboBox(FFAbstractButton* activator, QWidget* parent):
111         QWidget(parent)
112 {
113         cols_ = 1;
114
115         //sets activator
116         activator_ = activator;
117         activator_->setObjectName("Activator");
118
119         //sets layout of combobx
120         mainLayout = new QGridLayout();
121         mainLayout->setMargin(0);
122         mainLayout->setSpacing(0);
123         mainLayout->addWidget(activator_);
124         setLayout(mainLayout);
125
126         //sets popup
127         popUp = new FFComboPopUp(this);
128         popUp->setWindowFlags(Qt::SplashScreen);
129         popUp->setGeometry(QApplication::desktop()->geometry());
130         popUp->hide();
131         popUp->setWindowModality(Qt::WindowModal);
132
133         setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
134
135         //connects
136         connect(activator_, SIGNAL(clicked()), popUp, SLOT(exec()));
137         connect(this, SIGNAL(reset()),popUp,SLOT(recreatePopUp()));
138         connect(this, SIGNAL(itemsChanged(FFAbstractButton*)),popUp,SLOT(registerNewItem(FFAbstractButton*)));
139 }
140 /**
141  * A virtual destructor of  FFAbstractButton.
142  */
143 FFAbstractComboBox::~FFAbstractComboBox()
144 {
145         ;
146 }
147
148 /**
149  * Removes item from combobox
150  * @param index is a index of item
151  */
152 void FFAbstractComboBox::removeItem(int index)
153 {
154         if(items_.size()>index)
155         {
156                 disconnect(items_.at(index));
157                 delete items_.takeAt(index);
158         }
159         emit reset();
160 }
161 /**
162  * Calls setActivatorSpecs and emits signals activated(QVariant),activated(int)
163  * @param index is a index of item
164  */
165 void FFAbstractComboBox::setCurrentItem(int index)
166 {
167         if(items_.size()>index)
168         {
169                 setActivatorSpecs(items_.at(index));
170                 if(!signalsBlocked())
171                 {
172                         emit activated(activatorSpecs());
173                         emit activated(index);
174                 }
175         }
176 }
177 /**
178  * Returns pointer to activator
179  */
180 FFAbstractButton* FFAbstractComboBox::activator()
181 {
182         return activator_;
183 }
184 /**
185  * Returns number of columns
186  */
187 int FFAbstractComboBox::cols()
188 {
189         return cols_;
190 }
191 /**
192  * Returns list of items
193  */
194 QList<FFAbstractButton*> FFAbstractComboBox::items()
195 {
196         return items_;
197 }
198 /**
199  * Returns popup's layout spacing
200  */
201 int FFAbstractComboBox::spacing()
202 {
203         return popUp->scrollAreaLayout->spacing();
204 }
205 /**
206  * Sets columns number
207  * @param cols a number of columns
208  */
209 void FFAbstractComboBox::setCols(int cols)
210 {
211         cols_ = cols;
212         emit reset();
213 }
214 /**
215  * Sets popup's layout spacing
216  * @param spacing a size of spacing
217  */
218 void FFAbstractComboBox::setSpacing(int spacing)
219 {
220         popUp->scrollAreaLayout->setSpacing(spacing);
221         emit reset();
222 }
223 /**
224  * Adds item to combobox
225  * @param item a pointer to adding item
226  * @param autoRecreate defines if layout should be recreated automatically
227  */
228 void FFAbstractComboBox::insertItem(FFAbstractButton* item, bool autoRecreate)
229 {
230         items_.append(item);
231         emit itemsChanged(item);
232
233         if(autoRecreate)
234         {
235                 emit reset();
236         }
237 }
238
239 /**
240  * Constructs a FFComboPopUp with a parent.
241  * Sets variable to initial values.
242  */
243 FFComboPopUp::FFComboPopUp(QWidget* parent) : QDialog(parent)
244 {
245
246         parentCB = dynamic_cast<FFAbstractComboBox*>(parent);
247         //sets popup window's layout
248         popUpLayout = new QGridLayout();
249         popUpLayout->setMargin(0);
250         popUpLayout->setSpacing(0);
251
252         //sets scrollarea and scrollarea's widget
253         scrollArea = new FFScrollArea();
254         scrollArea->setWidgetResizable(true);
255
256         popUpLayout->addWidget(scrollArea,0,0);
257         setLayout(popUpLayout);
258
259         scrollAreaLayout = new QGridLayout();
260         scrollAreaLayout->setSpacing(5);
261         scrollAreaLayout->setMargin(0);
262
263         scrollAreaWidget = new QWidget();
264         scrollAreaWidget->setStyleSheet("background-color:black;");
265
266         scrollAreaWidget->setLayout(scrollAreaLayout);
267         scrollArea->setWidget(scrollAreaWidget);
268
269         //sets back button
270         backButton = new FFAbstractButton;
271         backButton->setMargins(0,0,5,5);
272         backButton->setIconAlignment(Qt::AlignRight);
273         backButton->setIcon(QIcon(QPixmap(":/standard/combo_colorarrow.svg")));
274         scrollAreaLayout->addWidget(backButton,0,0,1,parentCB->cols_);
275
276         //connect
277         connect(backButton,SIGNAL(clicked()),this,SLOT(reject()));
278 }
279 /**
280  * Serves show event. Override method from mother class.
281  */
282 void FFComboPopUp::showEvent(QShowEvent* event)
283 {
284         // The calculation of the size of the window so that it occupied little space as possible.
285
286         int itemsHeight = 0;
287
288         // for each row
289         for(int i=0; i<roundUp((float)parentCB->items_.size()/(float)parentCB->cols_); ++i)
290         {
291                 // height of an element
292                 itemsHeight += parentCB->items_.at(i)->minimumHeight();
293
294                 //height of an spacing
295                 itemsHeight += scrollAreaLayout->spacing();
296         }
297
298         itemsHeight += backButton->minimumHeight();
299
300         if(itemsHeight > QApplication::desktop()->geometry().height())
301           itemsHeight = QApplication::desktop()->geometry().height();
302
303         setGeometry(0,0,QApplication::desktop()->geometry().width(),itemsHeight);
304
305         QDialog::showEvent(event);
306
307 }
308 /**
309  * Refreshes popup layout at view event
310  */
311 void FFComboPopUp::recreatePopUp()
312 {
313         // Deleting old objects
314         QLayoutItem *child;
315         while((child = scrollAreaLayout->takeAt(0)) != 0)
316         {
317                 delete child;
318         }
319
320         // Arranging objects on the popup
321         scrollAreaLayout->addWidget(backButton,0,0,1,parentCB->cols_);
322         for(int i=0, cols=0, rows=1; i<parentCB->items_.size();++i,++cols)
323         {
324                 if(cols==parentCB->cols_)
325                 {
326                         ++rows;
327                 }
328                 cols%=parentCB->cols_;
329                 scrollAreaLayout->addWidget(parentCB->items_.at(i),rows,cols);
330         }
331
332         // Force refresh FFScrollArea (this line exist because FFScrollArea have a little imperfection - event filter problem)
333         scrollArea->setWidget(scrollAreaWidget);
334 }
335 /**
336  * Connects new item signal clicked to actual widget's slot accept
337  */
338 void FFComboPopUp::registerNewItem(FFAbstractButton* newItem)
339 {
340         connect(newItem,SIGNAL(clicked()),this,SLOT(accept()));
341 }
342 /**
343  * Calls setCurrentItem with widget index
344  */
345 void FFComboPopUp::accept()
346 {
347         parentCB->setCurrentItem(scrollAreaLayout->indexOf(dynamic_cast<QWidget*>(sender()))-1);
348         QDialog::accept();
349 }
350
351 /**
352  \fn void FFAbstractComboBox::reset()
353  *This signal is emitted when is necessary to refresh layout of popup window.
354  */
355
356 /**
357  \fn void FFAbstractComboBox::activated(QVariant spec)
358  *This signal is emitted when option on combobox was chosen.
359  *@param spec contains specific chosen value
360  */
361
362 /**
363  \fn void FFAbstractComboBox::activated(int index)
364  *This signal is emitted when option on combobox was chosen.
365  *@param index contains index of chosen item
366  */
367
368
369 /**
370  \fn void FFComboPopUp::selected(FFAbstractButton* selectedButton)
371  *This signal is emitted when item was selected
372  *@param selectedButton pointer to selected item
373  */