added all files
[ffqwlibrary] / libffqw-1.0 / sources / ffdpad.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 ffdpad.cpp
76  * @brief Implementation of the FFDPad class
77  *
78  * @author ComArch S.A.
79  * @date 2009.08.11
80  * @version 1.0
81  */
82 #include "ffdpad.h"
83
84
85 /**
86  * Constructs a FFDPad with a parent
87  */
88 FFDPad::FFDPad(QWidget* parent) :
89         FFAbstractWidget(parent), size_(DEFAULT_SIZE, DEFAULT_SIZE)
90 {
91         init();
92 }
93
94 /**
95  * Constructs a FFDPad with given size and parent.
96  */
97 FFDPad::FFDPad(QSize size, QWidget* parent) :
98         FFAbstractWidget(parent)
99 {
100         this->size_ = QSize(size.width(),size.height());
101         init();
102 }
103
104 /** A virtual destructor */
105 FFDPad::~FFDPad()
106 {
107         ;
108 }
109 /**
110  * Initiates FFDPad class. Sets all needed fields, connections and calls.
111  * It is called by all constructors.
112  */
113 void FFDPad::init()
114 {
115
116         //sets size policy
117         setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
118         setMinimumSize(QSize(DEFAULT_MIN_SIZE, DEFAULT_MIN_SIZE));
119
120         //Sets default values
121
122
123         path = DPAD_PATH;
124         ax = 0;
125         ay = 0;
126
127         if(size_.height() <= 0 || size_.width() <= 0)
128         {
129                 this->size_ = QSize(DEFAULT_SIZE,DEFAULT_SIZE);
130         }
131
132         layout = new QGridLayout(this);
133                 layout->setMargin(0);
134                 layout->setSpacing(0);
135
136         background = new FFViewCache(this);
137
138         //creates FFDPadButtons
139         buttonUp = new FFDPadButton(path + "_button_up",
140                                     DPAD_UP,
141                                     this);
142         buttonDown = new FFDPadButton(path + "_button_down",
143                                       DPAD_DOWN,
144                                       this);
145         buttonLeft = new FFDPadButton(path + "_button_left",
146                                       DPAD_LEFT,
147                                       this);
148         buttonRight = new FFDPadButton(path + "_button_right",
149                                        DPAD_RIGHT,
150                                        this);
151         //connects
152         connect(buttonUp,SIGNAL(clicked(int)),this,SIGNAL(clicked(int)));
153         connect(buttonDown,SIGNAL(clicked(int)),this,SIGNAL(clicked(int)));
154         connect(buttonLeft,SIGNAL(clicked(int)),this,SIGNAL(clicked(int)));
155         connect(buttonRight,SIGNAL(clicked(int)),this,SIGNAL(clicked(int)));
156
157         //adds buttons to layout
158         layout->addWidget(buttonUp, 0, 0, 1, 1);
159         layout->addWidget(buttonDown, 0, 0, 1, 1);
160         layout->addWidget(buttonLeft, 0, 0, 1, 1);
161         layout->addWidget(buttonRight, 0, 0, 1, 1);
162
163         this->setSize(size_);
164
165         background->init(path + "_background.svg");
166         background->updateView(size_);
167
168 }
169
170
171 /**
172  * Returns size of FFDPad
173  */
174 QSize FFDPad::size() const
175 {
176         return this->geometry().size();
177 }
178
179 /**
180  * Sets FFDPad's size.
181  * If both edges are equal FFDPad have it`s maximum size cause it is always square.
182  * If size`s parameters are not equal then FFDPad is located in center of the widget.
183  * @param size Size of FFDPad widget
184  */
185 void FFDPad::setSize(QSize size)
186 {
187         calculateSize(size);
188         buttonUp -> setSize(size_);
189         buttonDown -> setSize(size_);
190         buttonLeft -> setSize(size_);
191         buttonRight -> setSize(size_);
192         setGeometry(geometry().x(), geometry().y(), size.width(), size.height());
193 }
194
195 /**
196  * Updates FFDPad view and events' mask after changing size of the widget,
197  * only when resizable_ is set up.
198  * Overrides the virtual method from QWidget.
199  * @param event Contains all informations about event.
200  */
201 void FFDPad::resizeEvent(QResizeEvent* event)
202 {
203         calculateSize(event->size());
204         buttonUp -> setSize(size_);
205         buttonDown -> setSize(size_);
206         buttonLeft -> setSize(size_);
207         buttonRight -> setSize(size_);
208         background -> updateView(size_);
209         ax = 0;
210         ay = 0;
211
212         if(event ->size().width() > event -> size().height())
213         {
214                 ax = (size_.width() - event -> size().width()) / 2;
215         }
216         else
217         {
218                 ay = (size_.height() - event -> size().height()) / 2;
219         }
220
221         buttonUp -> move(-ax, -ay);
222         buttonDown -> move(-ax, -ay);
223         buttonLeft -> move(-ax, -ay);
224         buttonRight -> move(-ax, -ay);
225
226 }
227
228 /**
229  * Repaints the view of the FFDPAd. Overrides the virtual method from QWidget.
230  * @param event Contains all informations about event.
231  */
232 void FFDPad::paintEvent(QPaintEvent* event)
233 {
234         Q_UNUSED(event)
235         QPainter paint(this);
236         paint.setOpacity(0.3);
237         paint.drawPixmap(-ax, -ay, background->pixmap());
238 }
239
240 /**
241  * Check and save size of the widget. Determine the size of the square.
242  * @param size Size to calculate square.
243  */
244 void FFDPad::calculateSize(QSize size)
245 {
246         if(size.height() <= 0 || size.width() <= 0)
247         {
248                 this->size_ = QSize(DEFAULT_SIZE, DEFAULT_SIZE);
249         }
250         else
251         {
252                 if(size.width() > size.height())
253                 {
254                         this->size_ = QSize(size.height(),size.height());
255                 }
256                 else
257                 {
258                         this->size_ = QSize(size.width(),size.width());
259                 }
260         }
261         update();
262 }
263
264 /**
265  \fn void FFDPad::clicked(int type)
266
267  This signal is emitted when the one of four buttons will be released.
268  */