Fixed the permissions for all sources files
[kfluid:loureiro-clone.git] / src / fluidwidget.cpp
1 /*
2  *  Copyright (c) 2009 Zagaia Project - INdT/Fucapi
3  *  Website: http://www.zagaia.org
4  *
5  *  This file is part of kfluid.
6  *
7  *  kfluid is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  kfluid is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with kfluid. If not, see <http://www.gnu.org/licenses/>
19  *
20  */
21
22 #include "fluidwidget.h"
23
24 #include <QParallelAnimationGroup>
25 #include "fluidscene.h"
26 #include "framewidget.h"
27 #include "gwidget.h"
28 #include "xmlhandlercontrol.h"
29 #include "devicefinder.h"
30
31 /*
32  * FluidWidget constructor
33  */
34 FluidWidget::FluidWidget()
35 {
36     setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
37     _scene = new FluidScene(this, 429, 123);
38     setScene(_scene);
39
40     _xmlhandler = new XMLHandlerControl();
41     _devicelist = _xmlhandler->extractObjects();
42
43     _bground = new GWidget(429, 123, ":/images/ini_screen/more_area.png");
44     _bground->setPos(0, 0);
45     _scene->addItem(_bground);
46
47     AddFavoriteItems();
48     AddSearchingItems();
49     ShowFavoriteItems();
50
51     _finder = new DeviceFinder();
52
53     connect(_finder, SIGNAL(DeviceAdded()),
54             this, SLOT(UpdateDevList()));
55
56     _par_anim = new QParallelAnimationGroup(this);
57
58     _favindex = 0;
59     _devindex = 0;
60
61 }
62
63 /*
64  * FluidWidget destructor
65  */
66 FluidWidget::~FluidWidget()
67 {
68     delete _bground;
69
70     _xmlhandler->writeObjects(_devicelist);
71     delete _xmlhandler;
72     delete _finder;
73     delete _par_anim;
74
75     //Favorite Items
76     delete _bt_add;
77     delete _bt_left;
78     delete _bt_right;
79
80     DelFavorite();
81     delete _favorite;
82
83     //Searching Items
84     delete _bt_back;
85
86     delete _text1;
87     delete _text2;
88     delete _text3;
89         delete _text4;
90         delete _text5;
91
92     DelDevices();
93     delete _searching;
94 }
95
96 /*
97  * add favorites itens on screen
98  */
99 void FluidWidget::AddFavoriteItems()
100 {
101     _favorite = new FrameWidget(429, 123);
102     _favorite->setPos(0, 0);
103     _scene->addItem(_favorite);
104
105     _logo = new GWidget(326, 91, ":/images/ini_screen/kfluid_logo.png", _favorite);
106
107     _bt_add = new GWidget(37, 38, ":/images/favorites/bt_add.png", _favorite);
108     _bt_add->setPressedImagePath(":/images/favorites/bt_add-press.png");
109     _bt_add->setMouseMoveImagePath(":/images/favorites/bt_add-over.png");
110     connect(_bt_add, SIGNAL(Clicked()),
111             this, SLOT(Clicked_Add()));
112
113     _bt_left = new GWidget(11, 17, ":/images/favorites/arrow_left.png", _favorite);
114     connect(_bt_left, SIGNAL(Clicked()),
115             this, SLOT(Clicked_Left()));
116     _bt_left->setVisible(false);
117
118     _bt_right = new GWidget(11, 17, ":/images/favorites/arrow_right.png", _favorite);
119     connect(_bt_right, SIGNAL(Clicked()),
120             this, SLOT(Clicked_Right()));
121
122     _bt_add->setPos(16, 43);
123     _logo->setPos(75, 18);
124
125     _bt_left->setPos(69, 53);
126     _bt_right->setPos(195 + (104 * (MaxDevice - 1)), 53);
127
128     _bt_left->setVisible(false);
129     _bt_right->setVisible(false);
130
131 }
132
133 /*
134  * add searching itens on screen
135  */
136 void FluidWidget::AddSearchingItems()
137 {
138     _searching = new FrameWidget(429, 123);
139     _searching->setPos(0, 123);
140
141     _scene->addItem(_searching);
142
143     _bt_back = new GWidget(37, 38, ":/images/searching/bt_arrow-back.png", _searching);
144     _bt_back->setPressedImagePath(":/images/searching/bt_arrow-back_press.png");
145     _bt_back->setMouseMoveImagePath(":/images/searching/bt_arrow-back_over.png");
146     connect(_bt_back, SIGNAL(Clicked()),
147             this, SLOT(Clicked_Back()));
148
149     _bt_up = new GWidget(17, 11, ":/images/searching/arrow_up.png", _searching);
150     connect(_bt_up, SIGNAL(Clicked()),
151             this, SLOT(Clicked_Up()));
152
153     _bt_down = new GWidget(17, 11, ":/images/searching/arrow_down.png", _searching);
154     connect(_bt_down, SIGNAL(Clicked()),
155             this, SLOT(Clicked_Down()));
156
157     _text1 = new GWidget(100, 40, "SEARCHING DEVICES",
158                          QFont("Tahoma", 8, QFont::Bold),
159                          QColor(255, 255, 255, 255),
160                          QTextOption(Qt::AlignLeft),
161                          _searching);
162
163     _text2 = new GWidget(200, 20, "Check if your device is 'visible'",
164                          QFont("Tahoma", 7, QFont::Bold),
165                          QColor(245, 245, 245, 245),
166                          QTextOption(Qt::AlignLeft),
167                          _searching);
168     QString message;
169     message = "0 FOUND";
170
171     _text3 = new GWidget(200, 20, message,
172                          QFont("Tahoma", 8, QFont::Normal),
173                          QColor(255, 255, 255, 255),
174                          QTextOption(Qt::AlignLeft),
175                          _searching);
176
177         _text4 = new GWidget(200, 20, "Bluetooth is not connected.",
178                                                  QFont("Tahoma", 8, QFont::Bold),
179                                                  QColor(255, 255, 255, 255),
180                                                  QTextOption(Qt::AlignLeft),
181                                                  _searching);
182
183         _text5 = new GWidget(200, 20, "No device found.",
184                                                  QFont("Tahoma", 8, QFont::Bold),
185                                                  QColor(255, 255, 255, 255),
186                                                  QTextOption(Qt::AlignLeft),
187                                                  _searching);
188
189     _bt_back->setPos(16, 43);
190     _bt_up->setPos(410, 5);
191     _bt_down->setPos(410, 110);
192
193     _text1->setPos(62, 40);
194     _text2->setPos(16, 105);
195     _text3->setPos(62, 70);
196         _text4->setPos(62, 105);
197         _text5->setPos(62, 105);
198
199     _bt_up->setVisible(false);
200     _bt_down->setVisible(false);
201
202 }
203
204 /*
205  * refresh device list
206  */
207 void FluidWidget::UpdateDevList(){
208
209     QList<FluidDeviceModel *> List = _finder->getDevList();
210
211     if (!List.isEmpty())
212         {
213         int j = _devlist.size();
214                 FluidDeviceModel *lastDev = List.at(List.size()-1);
215             _text5->setVisible(false);
216                 _text2->setVisible(true);
217
218         if (!CheckDevice(lastDev->getAddress()))
219         {
220                 FoundDevice *_fd;
221             _fd = new FoundDevice(238, 40, lastDev, _searching);
222             connect(_fd, SIGNAL(DoubleClicked(FluidDeviceModel *)), this, SLOT(FavoriteAdded(FluidDeviceModel *)));
223
224             if (j < MaxDevice)
225                 _fd->setPosition(170, 2 + (40 * j));
226             else
227             {
228                 _fd->setPosition(170, 3 + (40 * (MaxDevice - 1)));
229                 _fd->setOpacity(0);
230                 _fd->setVisible(false);
231                 _bt_down->setVisible(true);
232             }
233             _devlist.append(_fd);
234          }
235     }
236         else
237         {
238                 _text2->setVisible(false);
239                 _text5->setVisible(true);
240         }
241
242     QLocale listSize;
243     QString message;
244
245     message = listSize.toString(_devlist.size());
246     message += " FOUND";
247
248     _text3->setText(message);
249 }
250
251 /*
252  * delete device pointers
253  */
254 void FluidWidget::DelDevices()
255 {
256     for (int i = 0; i < _devlist.size(); i++)
257     {
258         FoundDevice *_fd;
259         _fd = _devlist.at(i);
260
261         delete _fd;
262     }
263
264     _devlist.clear();
265 }
266
267 /*
268  * delete device pointers favorites
269  */
270 void FluidWidget::DelFavorite(){
271     for (int i = 0; i < _favlist.size(); i++)
272     {
273         FavDevice *_fdev;
274         _fdev = _favlist.at(i);
275
276         delete _fdev;
277     }
278
279     _favlist.clear();
280 }
281
282 /*
283  * favorites right navigation
284  */
285 void FluidWidget::Clicked_Right()
286 {
287     _par_anim->clear();
288
289     _favlist.at(_favindex + 3)->setVisible(true);
290     _par_anim->addAnimation(_favlist.at(_favindex)->getFadeOut());
291     _par_anim->addAnimation(_favlist.at(_favindex + 1)->getLeftMove());
292     _par_anim->addAnimation(_favlist.at(_favindex + 2)->getLeftMove());
293     _par_anim->addAnimation(_favlist.at(_favindex + 3)->getFadeIn());
294
295     _par_anim->start();
296
297     _favindex++;
298
299     _favlist.at(_favindex)->LeftMove();
300     _favlist.at(_favindex + 1)->LeftMove();
301
302     if ((_favindex + 3) == _favlist.size())
303         _bt_right->setVisible(false);
304
305     _bt_left->setVisible(true);
306
307 }
308
309 /*
310  * favorites left navigation
311  */
312 void FluidWidget::Clicked_Left()
313 {
314     _par_anim->clear();
315
316     _par_anim->addAnimation(_favlist.at(_favindex + 2)->getFadeOut());
317     _par_anim->addAnimation(_favlist.at(_favindex + 1)->getRightMove());
318     _par_anim->addAnimation(_favlist.at(_favindex)->getRightMove());
319     _par_anim->addAnimation(_favlist.at(_favindex - 1)->getFadeIn());
320
321     _par_anim->start();
322
323     _favindex--;
324
325     _favlist.at(_favindex + 1)->RightMove();
326     _favlist.at(_favindex + 2)->RightMove();
327
328     if (_favindex == 0)
329         _bt_left->setVisible(false);
330
331     _bt_right->setVisible(true);
332 }
333
334 /*
335  * down navigation of the list devices
336  */
337 void FluidWidget::Clicked_Down()
338 {
339     _par_anim->clear();
340
341     _devlist.at(_devindex + 3)->setVisible(true);
342
343     _par_anim->addAnimation(_devlist.at(_devindex)->getFadeOut());
344     _par_anim->addAnimation(_devlist.at(_devindex + 1)->getUpMove());
345     _par_anim->addAnimation(_devlist.at(_devindex + 2)->getUpMove());
346     _par_anim->addAnimation(_devlist.at(_devindex + 3)->getFadeIn());
347
348     _par_anim->start();
349
350     _devindex++;
351
352     _devlist.at(_devindex)->UpMove();
353     _devlist.at(_devindex + 1)->UpMove();
354
355     if ((_devindex + 3) == _devlist.size())
356         _bt_down->setVisible(false);
357
358     _bt_up->setVisible(true);
359
360 }
361
362 /*
363  * up navigation of the list devices
364  */
365 void FluidWidget::Clicked_Up()
366 {
367     _par_anim->clear();
368
369     _par_anim->addAnimation(_devlist.at(_devindex + 2)->getFadeOut());
370     _par_anim->addAnimation(_devlist.at(_devindex + 1)->getDownMove());
371     _par_anim->addAnimation(_devlist.at(_devindex)->getDownMove());
372     _par_anim->addAnimation(_devlist.at(_devindex - 1)->getFadeIn());
373
374     _par_anim->start();
375
376     _devindex--;
377
378     _devlist.at(_devindex + 1)->DownMove();
379     _devlist.at(_devindex + 2)->DownMove();
380
381     if (_devindex == 0)
382         _bt_up->setVisible(false);
383
384     _bt_down->setVisible(true);
385 }
386
387 /*
388  * start search
389  */
390 void FluidWidget::Clicked_Add()
391 {
392     DelDevices();
393
394     if(_finder->getBluetoothDevice())
395     {
396                 _text5->setVisible(false);
397                 _text2->setVisible(true);
398                 _text4->setVisible(false);
399                 _text3->setVisible(false);
400                 _text3->setText("0 FOUND");
401                 _text3->setVisible(true);
402                 _bt_down->setVisible(false);
403                 _bt_up->setVisible(false);
404                 _favorite->FadeOut();
405                 _searching->UpMove();
406                 _finder->SearchDevices();
407     }
408     else
409     {
410                 _favorite->FadeOut();
411                 _searching->UpMove();
412                 _text5->setVisible(false);
413                 _text4->setVisible(true);
414                 _text2->setVisible(false);
415                 _text3->setVisible(false);
416                 _text3->setText("0 FOUND");
417                 _text3->setVisible(true);
418                 _bt_up->setVisible(false);
419                 _bt_down->setVisible(false);
420     }
421  }
422
423 /*
424  * back to favorites screen
425  */
426 void FluidWidget::Clicked_Back()
427 {
428     _finder->StopSearching();
429     _searching->DownMove();
430     _favorite->FadeIn();
431 }
432
433 /*
434  * favorite added
435  */
436 void FluidWidget::FavoriteAdded(FluidDeviceModel *device)
437 {
438     device->markAsFavorite();
439     _devicelist.insert(0, device);
440
441     ShowFavoriteItems();
442     Clicked_Back();
443 }
444
445 /*
446  * show itens favorites
447  */
448 void FluidWidget::ShowFavoriteItems()
449 {
450     DelFavorite();
451
452     if (_devicelist.size() > 0)
453         _logo->setVisible(false);
454     else
455         _logo->setVisible(true);
456
457     for (int i = 0; i < _devicelist.size(); i++)
458     {
459         FavDevice *_fdev;
460         _fdev = new FavDevice(92, 96, _devicelist.at(i), _favorite);
461         connect(_fdev, SIGNAL(removeFavorite(FluidDeviceModel *)), this, SLOT(removeFavorite(FluidDeviceModel *)));
462                 
463                 _bt_left->setVisible(false);
464                 _bt_right->setVisible(false);
465
466         if (i < MaxDevice)
467                 {
468             _fdev->setPosition(91 + (104 * i), 16);
469                 }
470         else
471         {
472             _fdev->setPosition(91 + (104 * (MaxDevice - 1)), 16);
473             _fdev->setOpacity(0);
474             _fdev->setVisible(false);
475             _bt_right->setVisible(true);
476         }
477
478         _favlist.append(_fdev);
479     }
480 }
481
482 /*
483  * remove favorite slot
484  */
485 void FluidWidget::removeFavorite(FluidDeviceModel *device)
486 {
487     for (int i=0; i < _favlist.size(); i++)
488     {
489         if (_favlist.at(i)->_device->getAddress() == device->getAddress())
490         {
491             _favlist.removeAt(i);
492             break;
493         }
494     }
495
496     for (int i=0; i < _devicelist.size(); i++)
497     {
498         if (_devicelist.at(i)->getAddress() == device->getAddress())
499         {
500             _devicelist.removeAt(i);
501             break;
502         }
503     }
504
505     _xmlhandler->writeObjects(_devicelist);
506     ShowFavoriteItems();
507 }
508
509 /*
510  * check device address
511  */
512 bool FluidWidget::CheckDevice(QString address)
513 {
514     for (int i = 0; i < _devicelist.size(); i++)
515         if (_devicelist.at(i)->getAddress() == address)
516             return true;
517
518     return false;
519 }