added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffcolorcombobox.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 ffcolorcombobox.cpp
76  * @brief Implementation of the FFColorComboBox class
77  *
78  * @author ComArch S.A.
79  * @date 2009.08.13
80  * @version 1.0
81  */
82
83 #include "ffcolorcombobox.h"
84
85 /**
86  * Constructs a FFColorComboBox with a parent.
87  */
88 FFColorComboBox::FFColorComboBox(QWidget* parent) :
89         FFAbstractComboBox(new FFColorButton, parent)
90 {
91
92 }
93
94 /**
95  * A virtual destructor.
96  */
97 FFColorComboBox::~FFColorComboBox()
98 {
99         ;
100 }
101
102 /**
103  * Adds item to combobox
104  * @param spec is a color for item
105  */
106 void FFColorComboBox::addItem(QColor spec)
107 {
108         addItem(QVariant(spec));
109 }
110 /**
111  * Adds item to combobox
112  * @param spec is a pen with color for item
113  */
114 void FFColorComboBox::addItem(QPen spec)
115 {
116         addItem(spec.color());
117 }
118 /**
119  * Adds items to combobox
120  * @param items is a list of colors for items
121  */
122 void FFColorComboBox::addItems(QList<QColor> items)
123 {
124         for(int i = 0; i < items.size(); ++i)
125         {
126                 // First step: you must prepare new button (button must be inherited from FFAbstractButton)
127                 FFColorButton* temp = new FFColorButton;
128                 temp->setColor(items.at(i));
129
130                 // Second step: You must call this function with previously prepared button
131                 insertItem(temp, false);
132         }
133         emit reset();
134 }
135 /**
136  * Adds items to combobox
137  * @param items is a list of QPen-s with colors for items
138  */
139 void FFColorComboBox::addItems(QList<QPen> items)
140 {
141         for(int i = 0; i < items.size(); ++i)
142         {
143                 // First step: you must prepare new button (button must be inherited from FFAbstractButton)
144                 FFColorButton* temp = new FFColorButton;
145                 temp->setColor(items.at(i).color());
146
147                 // Second step: You must call this function with previously prepared button
148                 insertItem(temp, false);
149         }
150         emit reset();
151 }
152
153 /**
154  * Sets actual color. If a color is not in colorcombobox nothing happens.
155  *@param color is a color to set
156  */
157 void FFColorComboBox::setCurrentColor(QColor color)
158 {
159         for(int i = 0; i < items_.size(); ++i)
160         {
161                 if(dynamic_cast<FFColorButton*>(items_[i])->color() == color)
162                 {
163                         setCurrentItem(i);
164                         return;
165                 }
166         }
167 }
168
169 /**
170  * Sets chosen value of FFColoroComboBox on activator
171  * @param item is a pointer to chosen item.
172  */
173
174 void FFColorComboBox::setActivatorSpecs(FFAbstractButton* item)
175 {
176         if(FFColorButton* but = dynamic_cast<FFColorButton*>(item))
177         {
178                 dynamic_cast<FFColorButton*>(activator_)->setColor(but->color());
179         }
180 }
181 /**
182  * Returns QVariant element containing value of chosen item
183  */
184 QVariant FFColorComboBox::activatorSpecs()
185 {
186         return QVariant(dynamic_cast<FFColorButton*>(activator_)->pen());
187 }
188
189 /**
190  * Adds new item to FFColorComboBox
191  */
192 void FFColorComboBox::addItem(QVariant spec)
193 {
194         // First step: you must prepare new button (button must be inherited from FFAbstractButton)
195         FFColorButton* temp = new FFColorButton;
196         temp->setColor(spec.value<QColor>());
197
198         // Second step: You must call this function with previously prepared button
199         insertItem(temp);
200 }
201
202 /**
203  * Constructs FFColorButton with given parent
204  */
205 FFColorButton::FFColorButton(QWidget* parent) :
206         FFAbstractButton(parent)
207 {
208         setMargins(15,15,15,15);
209 }
210 /**
211  * A virtual destructor
212  */
213 FFColorButton::~FFColorButton()
214 {
215         ;
216 }
217 /**
218  * Sets a color painted on the button
219  */
220 void FFColorButton::setColor(QColor color)
221 {
222         QBrush brush;
223         brush = pen_.brush();
224         brush.setStyle(Qt::SolidPattern);
225         brush.setColor(color);
226         pen_.setBrush(brush);
227         pen_.setColor(color);
228 }
229 /**
230  * Returns color painted on the button
231  */
232 QColor FFColorButton::color()
233 {
234         return pen_.color();
235 }
236 /**
237  * Serves paint event, draws rounded rectangle. Overrides parent's method
238  * @param event Contains all informations about event.
239  */
240 void FFColorButton::paintEvent(QPaintEvent* event)
241 {
242         FFAbstractButton::paintEvent(event);
243
244         QPainter painter;
245         painter.begin(this);
246
247         painter.setPen(pen_);
248         painter.setBrush(pen_.brush());
249
250         painter.setRenderHint(QPainter::Antialiasing,true);
251         painter.drawRoundedRect(leftMargin() + indent(),
252                          topMargin(),
253                          width() - leftMargin() - rightMargin() - 2 * indent(),
254                          height() - topMargin() - bottomMargin(),10,10);
255
256         painter.end();
257
258 }