NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnlygraphicsregionbase.cpp
1 /*
2  * Copyright 2011 TeleMidia/PUC-Rio.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 #include "qnlygraphicsregionbase.h"
19 
20 QnlyGraphicsRegionBase::QnlyGraphicsRegionBase(QObject* parent,
21  QMenu* switchMenu)
22  : QGraphicsScene(parent)
23 {
24  this->switchMenu = switchMenu;
25 
26  this->switchMenu->setEnabled(true);
27 
28  createActions();
29  createMenus();
30  createConnections();
31 
32  graphicsRegionBaseId = NULL;
33 
34  selectedRegion = NULL;
35 
36  setSceneRect(0,0,854,480);
37 
38  bgrect = new QGraphicsRectItem(0,this);
39  bgrect->setRect(0,0,854,480);
40  //bgrect->setBrush(QBrush(QPixmap(":/bg/layout")));
41  bgrect->setBrush(QBrush(Qt::white));
42  bgrect->setPen(QPen(QColor("#BBBBBB")));
43  bgrect->setZValue(-1);
44 
45  grid = new QnlyGraphicsGrid(0,this);
46  grid->setStep(25);
47  grid->setPen(QPen(QBrush(Qt::gray), 1.5,Qt::DotLine));
48  grid->setRect(0,0,854,480);
49  grid->setZValue(1000);
50  grid->setVisible(false);
51 }
52 
53 QnlyGraphicsRegionBase::~QnlyGraphicsRegionBase()
54 {
55 
56 }
57 
58 QString QnlyGraphicsRegionBase::getId() const
59 {
60  return id;
61 }
62 
63 void QnlyGraphicsRegionBase::setId(const QString &id)
64 {
65  this->id = id;
66 
67  // if(graphicsRegionBaseId == NULL)
68  // graphicsRegionBaseId = addText("regionBaseId");
69  // graphicsRegionBaseId->setPos(10,10);
70  // graphicsRegionBaseId->setToolTip(id);
71  // graphicsRegionBaseId->setPlainText(id);
72 }
73 
74 QString QnlyGraphicsRegionBase::getUid() const
75 {
76  return uid;
77 }
78 
79 void QnlyGraphicsRegionBase::setUid(const QString &uid)
80 {
81  this->uid = uid;
82 }
83 
84 QString QnlyGraphicsRegionBase::getRegion() const
85 {
86  return region;
87 }
88 
89 void QnlyGraphicsRegionBase::setRegion(const QString &region)
90 {
91  this->region = region;
92 }
93 
94 QString QnlyGraphicsRegionBase::getDevice() const
95 {
96  return device;
97 }
98 
99 void QnlyGraphicsRegionBase::setDevice(const QString &device)
100 {
101  this->device = device;
102 }
103 
104 void QnlyGraphicsRegionBase::selectRegion(QnlyGraphicsRegion* region)
105 {
106  if (!region->isSelected())
107  {
108  if (selectedRegion != NULL)
109  {
110  selectedRegion->setSelected(false);
111  }
112 
113  region->setSelected(true);
114 
115  selectedRegion = region;
116  }
117 }
118 
119 void QnlyGraphicsRegionBase::changeRegion(QnlyGraphicsRegion* region,
120  const QMap<QString, QString> attributes)
121 {
122  if (region != NULL)
123  {
124  // if (!attributes["id"].isEmpty()){
125  region->setId(attributes["id"]);
126  // }
127 
128  // if (!attributes["title"].isEmpty()){
129  region->setTitle(attributes["title"]);
130  // }
131 
132  if (!attributes["color"].isEmpty())
133  region->setColor(attributes["color"]);
134 
135  if (!attributes["top"].isEmpty())
136  {
137  if (attributes["top"].contains(QRegExp("\\d+(.\\d+)?%")))
138  {
139  QString attribute = attributes["top"];
140  attribute.remove(attribute.length()-1,1); // removing '%'
141 
142  qreal top = attribute.toDouble();
143 
144  if (top >= 0 && top <= 100)
145  region->setRelativeTop(top/100);
146  }
147  else if (attributes["top"].contains(QRegExp("\\d+(.\\d+)?")))
148  {
149  QString attribute = attributes["top"];
150 
151  qreal top = attribute.toDouble();
152 
153  if (top >= 0 && top <= 1)
154  region->setRelativeTop(top);
155  }
156  }
157 
158  if (!attributes["left"].isEmpty())
159  {
160  if (attributes["left"].contains(QRegExp("\\d+(.\\d+)?%")))
161  {
162  QString attribute = attributes["left"];
163  attribute.remove(attribute.length()-1,1); // removing '%'
164 
165  qreal left = attribute.toDouble();
166 
167  if (left >= 0 && left <= 100)
168  region->setRelativeLeft(left/100);
169  }
170  else if (attributes["left"].contains(QRegExp("\\d+(.\\d+)?")))
171  {
172  QString attribute = attributes["left"];
173 
174  qreal left = attribute.toDouble();
175 
176  if (left >= 0 && left <= 1)
177  region->setRelativeLeft(left);
178  }
179  }
180 
181  if (!attributes["right"].isEmpty())
182  {
183  if (attributes["right"].contains(QRegExp("\\d+(.\\d+)?%")))
184  {
185  QString attribute = attributes["right"];
186  attribute.remove(attribute.length()-1,1); // removing '%'
187 
188  qreal right = attribute.toDouble();
189 
190  if (right >= 0 && right <= 100)
191  region->setRelativeRight(right/100);
192  }
193  else if (attributes["right"].contains(QRegExp("\\d+(.\\d+)?")))
194  {
195  QString attribute = attributes["right"];
196 
197  qreal right = attribute.toDouble();
198 
199  if (right >= 0 && right <= 1)
200  region->setRelativeRight(right);
201  }
202  }
203 
204  if (!attributes["bottom"].isEmpty())
205  {
206  if (attributes["bottom"].contains(QRegExp("\\d+(.\\d+)?%")))
207  {
208  QString attribute = attributes["bottom"];
209  attribute.remove(attribute.length()-1,1); // removing '%'
210 
211  qreal bottom = attribute.toDouble();
212 
213  if (bottom >= 0 && bottom <= 100)
214  region->setRelativeBottom(bottom/100);
215 
216  }
217  else if (attributes["bottom"].contains(QRegExp("\\d+(.\\d+)?")))
218  {
219  QString attribute = attributes["bottom"];
220 
221  qreal bottom = attribute.toDouble();
222 
223  if (bottom >= 0 && bottom <= 1)
224  region->setRelativeBottom(bottom);
225  }
226  }
227 
228  if (!attributes["width"].isEmpty())
229  {
230  if (attributes["width"].contains(QRegExp("\\d+(.\\d+)?%")))
231  {
232  QString attribute = attributes["width"];
233  attribute.remove(attribute.length()-1,1); // removing '%'
234 
235  qreal width = attribute.toDouble();
236 
237  if (width >= 0 && width <= 100)
238  region->setRelativeWidth(width/100);
239 
240  }
241  else if (attributes["width"].contains(QRegExp("\\d+(.\\d+)?")))
242  {
243  QString attribute = attributes["width"];
244 
245  qreal width = attribute.toDouble();
246 
247  if (width >= 0 && width <= 1)
248  region->setRelativeWidth(width);
249  }
250  }
251 
252  if (!attributes["height"].isEmpty())
253  {
254  if (attributes["height"].contains(QRegExp("\\d+(.\\d+)?%")))
255  {
256  QString attribute = attributes["height"];
257  attribute.remove(attribute.length()-1,1); // removing '%'
258 
259  qreal height = attribute.toDouble();
260 
261  if (height >= 0 && height <= 100)
262  region->setRelativeHeight(height/100);
263 
264  }
265  else if (attributes["height"].contains(QRegExp("\\d+(.\\d+)?")))
266  {
267  QString attribute = attributes["height"];
268 
269  qreal height = attribute.toDouble();
270 
271  if (height >= 0 && height <= 1)
272  region->setRelativeHeight(height);
273 
274  }
275  }
276 
277  if(attributes.contains("zIndex"))
278  region->setzIndex(attributes["zIndex"].toInt());
279 
280  region->adjust();
281  }
282 }
283 
284 void QnlyGraphicsRegionBase::requestRegionChange(QnlyGraphicsRegion* region,
285  QMap<QString, QString> attributes)
286 {
287  // setting
288  QMap<QString, QString> full;
289 
290  double value = 0.0;
291  if (attributes.contains("id"))
292  full["id"] = attributes["id"];
293  else if (!region->getId().isEmpty())
294  full["id"] = region->getId();
295 
296  // if (attributes.contains("title"))
297  // full["title"] = attributes["title"];
298  // else if (!region->getTitle().isEmpty())
299  // full["title"] = region->getTitle();
300 
301  if (attributes.contains("color"))
302  full["color"] = attributes["color"];
303  else if (!region->getColor().isEmpty())
304  full["color"] = region->getColor();
305 
306  if (attributes.contains("top"))
307  full["top"] = attributes["top"];
308  else
309  {
310  value = region->getRelativeTop()*100;
311  ROUND_DOUBLE(value);
312  full["top"] = QString::number(value, 'f', 2) + "%";
313  }
314 
315  if (attributes.contains("left"))
316  full["left"] = attributes["left"];
317  else
318  {
319  value = region->getRelativeLeft()*100;
320  ROUND_DOUBLE(value);
321  full["left"] = QString::number(value, 'f', 2) + "%";
322  }
323 
324  // if (attributes.contains("right"))
325  // full["right"] = attributes["right"];
326  // else
327  // {
328  // value = region->getRelativeRight()*100;
329  // ROUND_DOUBLE(value);
330  // full["right"] = QString::number(value, 'f', 2) + "%";
331  // }
332 
333  // if (attributes.contains("bottom"))
334  // full["bottom"] = attributes["bottom"];
335  // else
336  // {
337  // value = region->getRelativeBottom()*100;
338  // ROUND_DOUBLE(value);
339  // full["bottom"] = QString::number(value, 'f', 2) + "%";
340  // }
341 
342  if (attributes.contains("width"))
343  full["width"] = attributes["width"];
344  else
345  {
346  value = region->getRelativeWidth()*100;
347  ROUND_DOUBLE(value);
348  full["width"] = QString::number(value, 'f', 2) + "%";
349  }
350 
351  if (attributes.contains("height"))
352  full["height"] = attributes["height"];
353  else
354  {
355  value = region->getRelativeHeight()*100;
356  ROUND_DOUBLE(value);
357  full["height"] = QString::number(value, 'f', 2) + "%";
358  }
359 
360  // TODO: zIndex
361  if(attributes.contains("zIndex"))
362  {
363  full["zIndex"] = attributes["zIndex"];
364  }
365  else
366  {
367  value = region->getzIndex();
368  full["zIndex"] = QString::number(value);
369  }
370 
371  emit regionChangeRequested( region->getUid(), uid, full );
372 }
373 
374 void QnlyGraphicsRegionBase::requestRegionSelection(QnlyGraphicsRegion* region)
375 {
376  emit regionSelectionRequested(region->getUid(),uid);
377 }
378 
379 void QnlyGraphicsRegionBase::QnlyGraphicsRegionBase::createActions()
380 {
381  // help action
382  helpAction = new QAction(this);
383  helpAction->setText(tr("Help"));
384 
385  helpAction->setEnabled(true);
386  helpAction->setShortcut(QKeySequence("F1"));
387 
388  // undo action
389  undoAction = new QAction(this);
390  undoAction->setText(tr("Undo"));
391 
392  undoAction->setEnabled(false);
393  undoAction->setShortcut(QKeySequence("Ctrl+Z"));
394 
395  // redo action
396  redoAction = new QAction(this);
397  redoAction->setText(tr("Redo"));
398 
399  redoAction->setEnabled(false);
400  redoAction->setShortcut(QKeySequence("Ctrl+Shift+Z"));
401 
402  // cut action
403  cutAction = new QAction(this);
404  cutAction->setText(tr("Cut"));
405 
406  cutAction->setEnabled(false);
407  cutAction->setShortcut(QKeySequence("Ctrl+X"));
408 
409  // copy action
410  copyAction = new QAction(this);
411  copyAction->setText(tr("Copy"));
412 
413  copyAction->setEnabled(false);
414  copyAction->setShortcut(QKeySequence("Ctrl+C"));
415 
416  // paste action
417  pasteAction = new QAction(this);
418  pasteAction->setText(tr("Paste"));
419 
420  pasteAction->setEnabled(false);
421  pasteAction->setShortcut(QKeySequence("Ctrl+V"));
422 
423  // delete action
424  deleteAction = new QAction(this);
425  deleteAction->setText(tr("Delete"));
426 
427  deleteAction->setEnabled(true);
428  deleteAction->setShortcut(QKeySequence("Del"));
429 
430  // zoomin action
431  zoominAction = new QAction(this);
432  zoominAction->setText(tr("Zoom In"));
433 
434  zoominAction->setEnabled(true);
435  zoominAction->setShortcut(QKeySequence("Ctrl++"));
436 
437  // zoomout action
438  zoomoutAction = new QAction(this);
439  zoomoutAction->setText(tr("Zoom Out"));
440 
441  zoomoutAction->setEnabled(true);
442  zoomoutAction->setShortcut(QKeySequence("Ctrl+-"));
443 
444  // reset action
445  zoomresetAction = new QAction(this);
446  zoomresetAction->setText(tr("Reset"));
447 
448  zoomresetAction->setEnabled(true);
449  zoomresetAction->setShortcut(QKeySequence("Ctrl+0"));
450 
451  // fullscreen action
452  fullscreenAction = new QAction(this);
453  fullscreenAction->setText(tr("Full Screen"));
454 
455  fullscreenAction->setEnabled(true);
456  fullscreenAction->setShortcut(QKeySequence("F11"));
457 
458  // export action
459  exportAction = new QAction(this);
460  exportAction->setText(tr("Export..."));
461 
462  exportAction->setEnabled(true);
463 
464  // region action
465  regionAction = new QAction(this);
466  regionAction->setText(tr("Region"));
467 
468  regionAction->setEnabled(true);
469 
470  // regionbase action
471  regionbaseAction = new QAction(this);
472  regionbaseAction->setText(tr("Regionbase"));
473 
474  regionbaseAction->setEnabled(true);
475 
476  // bring to front action
477  bringfrontAction = new QAction(this);
478  bringfrontAction->setText(tr("Bring to Front"));
479 
480  bringfrontAction->setEnabled(false);
481  bringfrontAction->setShortcut(QKeySequence("Shift+Ctrl+]"));
482 
483  // bring forward action
484  bringforwardAction = new QAction(this);
485  bringforwardAction->setText(tr("Bring Forward"));
486 
487  bringforwardAction->setEnabled(false);
488  bringforwardAction->setShortcut(QKeySequence("Ctrl+]"));
489 
490  // send backward action
491  sendbackwardAction = new QAction(this);
492  sendbackwardAction->setText(tr("Send Backward"));
493 
494  sendbackwardAction->setEnabled(false);
495  sendbackwardAction->setShortcut(QKeySequence("Ctrl+["));
496 
497  // send to back action
498  sendbackAction = new QAction(this);
499  sendbackAction->setText(tr("Send to Back"));
500 
501  sendbackAction->setEnabled(false);
502  sendbackAction->setShortcut(QKeySequence("Shift+Ctrl+["));
503 
504  re640x480 = new QAction(this);
505  re640x480->setText(tr("640x480 (4:3)"));
506  re640x480->setCheckable(true);
507  re640x480->setChecked(false);
508 
509  re800x600 = new QAction(this);
510  re800x600->setText(tr("800x600 (4:3)"));
511  re800x600->setCheckable(true);
512  re800x600->setChecked(false);
513 
514  re1024x768 = new QAction(this);
515  re1024x768->setText(tr("1024x768 (4:3)"));
516  re1024x768->setCheckable(true);
517  re1024x768->setChecked(false);
518 
519  re854x480 = new QAction(this);
520  re854x480->setText(tr("854x480 (16:9)"));
521  re854x480->setCheckable(true);
522  re854x480->setChecked(true);
523 
524  re1280x720 = new QAction(this);
525  re1280x720->setText(tr("1280x720 (16:9)"));
526  re1280x720->setCheckable(true);
527  re1280x720->setChecked(false);
528 
529  re1920x1080 = new QAction(this);
530  re1920x1080->setText(tr("1920x1080 (16:9)"));
531  re1920x1080->setCheckable(true);
532  re1920x1080->setChecked(false);
533 
534  re320x400 = new QAction(this);
535  re320x400->setText(tr("320x400 (4:5)"));
536  re320x400->setCheckable(true);
537  re320x400->setChecked(false);
538 
539  // hide action
540  hideAction = new QAction(this);
541  hideAction->setText(tr("Hide"));
542 
543  hideAction->setEnabled(false);
544 
545  gridAction = new QAction(this);
546  gridAction->setText(tr("Grid"));
547 
548  gridAction->setEnabled(true);
549  gridAction->setCheckable(true);
550  gridAction->setChecked(false);
551 
552  // properties action
553  propertiesAction = new QAction(this);
554  propertiesAction->setText(tr("Properties"));
555 
556  propertiesAction->setEnabled(true);
557 
558  regionActionGroup = new QActionGroup(this);
559  regionActionGroup->setExclusive(false);
560 
561  screensizeGroup = new QActionGroup(this);
562  screensizeGroup->setExclusive(true);
563 
564  screensizeGroup->addAction(re640x480);
565  screensizeGroup->addAction(re800x600);
566  screensizeGroup->addAction(re1024x768);
567  screensizeGroup->addAction(re854x480);
568  screensizeGroup->addAction(re1280x720);
569  screensizeGroup->addAction(re1920x1080);
570  screensizeGroup->addAction(re320x400);
571 }
572 
573 void QnlyGraphicsRegionBase::createMenus()
574 {
575  // view menu
576  viewMenu = new QMenu();
577  viewMenu->setTitle(tr("View"));
578 
579  viewMenu->setEnabled(true);
580 
581  viewMenu->addAction(zoominAction);
582  viewMenu->addAction(zoomoutAction);
583  viewMenu->addAction(zoomresetAction);
584  viewMenu->addSeparator();
585  viewMenu->addAction(fullscreenAction);
586 
587  // insert menu
588  insertMenu = new QMenu();
589  insertMenu->setTitle(tr("Insert"));
590 
591  insertMenu->setEnabled(true);
592 
593  insertMenu->addAction(regionAction);
594  insertMenu->addAction(regionbaseAction);
595 
596  // show menu
597  showMenu = new QMenu();
598  showMenu->setTitle(tr("Show"));
599 
600  showMenu->addAction(gridAction);
601 
602  showMenu->setEnabled(true);
603 
604  // arrange menu
605  arrangeMenu = new QMenu();
606  arrangeMenu->setTitle(tr("Arrange"));
607 
608  arrangeMenu->setEnabled(false);
609 
610  arrangeMenu->addAction(bringfrontAction);
611  arrangeMenu->addAction(bringforwardAction);
612  arrangeMenu->addAction(sendbackwardAction);
613  arrangeMenu->addAction(sendbackAction);
614 
615  // screensize menu
616  screensizeMenu = new QMenu();
617  screensizeMenu->setTitle(tr("Screen Size"));
618 
619  screensizeMenu->setEnabled(true);
620 
621  screensizeMenu->addAction(re640x480);
622  screensizeMenu->addAction(re800x600);
623  screensizeMenu->addAction(re1024x768);
624  screensizeMenu->addAction(re854x480);
625  screensizeMenu->addAction(re1280x720);
626  screensizeMenu->addAction(re1920x1080);
627  screensizeMenu->addAction(re320x400);
628 
629 
630  // context menu
631  contextMenu = new QMenu();
632  // contextMenu->addAction(helpAction);
633  // contextMenu->addSeparator();
634  // contextMenu->addAction(undoAction);
635  // contextMenu->addAction(redoAction);
636  // contextMenu->addSeparator();
637  // contextMenu->addAction(cutAction);
638  // contextMenu->addAction(copyAction);
639  // contextMenu->addAction(pasteAction);
640  // contextMenu->addSeparator();
641  contextMenu->addAction(deleteAction);
642  contextMenu->addSeparator();
643  contextMenu->addAction(exportAction);
644  contextMenu->addSeparator();
645  // contextMenu->addMenu(viewMenu); //disabled for while
646  contextMenu->addMenu(insertMenu);
647  contextMenu->addMenu(showMenu);
648  contextMenu->addMenu(screensizeMenu);
649  // contextMenu->addMenu(arrangeMenu); //disabled for while
650  contextMenu->addSeparator();
651  contextMenu->addAction(hideAction);
652  contextMenu->addSeparator();
653  contextMenu->addMenu(switchMenu);
654  // contextMenu->addSeparator(); // disabled for while
655  // contextMenu->addAction(propertiesAction); //disabled for while
656 }
657 
658 void QnlyGraphicsRegionBase::createConnections()
659 {
660  connect(regionAction, SIGNAL(triggered()),
661  SLOT(performRegion()));
662 
663  connect(regionbaseAction, SIGNAL(triggered()),
664  SIGNAL(regionbasePerformed()));
665 
666  connect(regionActionGroup, SIGNAL(triggered(QAction*)),
667  SLOT(performShow(QAction*)));
668 
669  connect(deleteAction, SIGNAL(triggered()),
670  SLOT(performDelete()));
671 
672  re640x480->setData(QSize(640, 480));
673  connect(re640x480, SIGNAL(triggered()), SLOT(performChangeResolution()));
674 
675  re800x600->setData(QSize(800, 600));
676  connect(re800x600, SIGNAL(triggered()), SLOT(performChangeResolution()));
677 
678  re1024x768->setData(QSize(1024, 768));
679  connect(re1024x768, SIGNAL(triggered()), SLOT(performChangeResolution()));
680 
681  re854x480->setData(QSize(854, 480));
682  connect(re854x480, SIGNAL(triggered()), SLOT(performChangeResolution()));
683 
684  re1280x720->setData(QSize(1280, 720));
685  connect(re1280x720, SIGNAL(triggered()), SLOT(performChangeResolution()));
686 
687  re1920x1080->setData(QSize(1920, 1080));
688  connect(re1920x1080, SIGNAL(triggered()), SLOT(performChangeResolution()));
689 
690  re320x400->setData(QSize(320, 400));
691  connect(re320x400, SIGNAL(triggered()), SLOT(performChangeResolution()));
692 
693  connect(exportAction, SIGNAL(triggered()), SLOT(performExport()));
694 
695  connect(gridAction, SIGNAL(triggered()), SLOT(performGrid()));
696 }
697 
698 void QnlyGraphicsRegionBase::performShow(QAction* action)
699 {
700  if (!action->isChecked())
701  regions[regionActions.key(action)]->setVisible(false);
702  else
703  regions[regionActions.key(action)]->setVisible(true);
704 }
705 
706 void QnlyGraphicsRegionBase::requestAdditionRegion(QnlyGraphicsRegion* parent)
707 {
708  QMap<QString, QString> attributes;
709 
710  attributes["top"] = "10%";
711  attributes["left"] = "10%";
712  // attributes["right"] = "10%";
713  // attributes["bottom"] = "10%";
714  attributes["width"] = "80%";
715  attributes["height"] = "80%";
716 
717  //Make the zIndex to be the greater one in the regionBase
718  int zIndex = 0;
719  foreach(QnlyGraphicsRegion *region, regions.values())
720  {
721  zIndex = zIndex > region->getzIndex() ? zIndex : region->getzIndex() + 1;
722  }
723  attributes["zIndex"] = QString::number(zIndex);
724 
725  emit regionAdditionRequested("", parent->getUid(), uid, attributes);
726 }
727 
728 void QnlyGraphicsRegionBase::requestRegionDeletion(QnlyGraphicsRegion* region)
729 {
730  emit regionDeletionRequested(region->getUid(), uid);
731 }
732 
733 void QnlyGraphicsRegionBase::updateActionText(QnlyGraphicsRegion *region)
734 {
735  // Update Show Menu
736  if(regionActions.contains(region->getUid()))
737  regionActions[region->getUid()]->setText(region->getId());
738 }
739 
740 void QnlyGraphicsRegionBase::hideRegion(QnlyGraphicsRegion* region)
741 {
742  regionActions[region->getUid()]->trigger();
743 }
744 
745 void QnlyGraphicsRegionBase::removeRegion(QnlyGraphicsRegion* region)
746 {
747  if (region != NULL)
748  {
749  if (region->parentItem() != NULL)
750  {
751  QnlyGraphicsRegion* parent =
752  (QnlyGraphicsRegion*) region->parentItem();
753 
754  foreach(QGraphicsItem* item, region->childItems())
755  {
756  QnlyGraphicsRegion* child = dynamic_cast<QnlyGraphicsRegion*> (item);
757 
758  if(child != NULL)
759  regions.remove(child->getUid());
760  else
761  qWarning() << "Trying to remove an element that is not of the type QnlyGraphicsRegion " << __FILE__ << __LINE__;
762  }
763 
764  parent->removeRegion(region);
765  }
766  else
767  {
768  foreach(QGraphicsItem* item, region->childItems())
769  {
770  QnlyGraphicsRegion* child = dynamic_cast<QnlyGraphicsRegion*> (item);
771 
772  if(child != NULL)
773  regions.remove(child->getUid());
774  else
775  qWarning() << "Trying to remove an element that is not of the type QnlyGraphicsRegion " << __FILE__ << __LINE__;
776  }
777 
778  if(regionActions.contains(region->getUid()))
779  {
780  QAction *action = regionActions[region->getUid()];
781 
782  showMenu->removeAction(action);
783 
784  regionActionGroup->removeAction(action);
785 
786  regionActions.remove(region->getUid());
787  }
788  }
789 
790  removeItem(region);
791 
792  regions.remove(region->getUid());
793 
794  // delete(region);
795 
796  selectedRegion = NULL;
797 
798  emit regionBaseSelectionRequested(uid);
799  }
800 }
801 
802 QGraphicsItem* QnlyGraphicsRegionBase::getBackgroundItem()
803 {
804  return bgrect;
805 }
806 
807 void QnlyGraphicsRegionBase::performDelete()
808 {
809  emit regionBaseDeletionRequested(uid);
810 }
811 
812 void QnlyGraphicsRegionBase::performRegion()
813 {
814  QMap<QString, QString> attributes;
815 
816  attributes["top"] = "10%";
817  attributes["left"] = "10%";
818  attributes["right"] = "10%";
819  attributes["bottom"] = "10%";
820  attributes["width"] = "80%";
821  attributes["height"] = "80%";
822 
823  //Make the zIndex to be the greater one in the regionBase
824  int zIndex = 0;
825  foreach(QnlyGraphicsRegion *region, regions.values())
826  {
827  zIndex = zIndex > region->getzIndex() ? zIndex : region->getzIndex() + 1;
828  }
829  attributes["zIndex"] = QString::number(zIndex);
830 
831  emit regionAdditionRequested("", "", uid, attributes);
832 }
833 
834 void QnlyGraphicsRegionBase::addRegion(QnlyGraphicsRegion* region,
835  QnlyGraphicsRegion* parent,
836  const QMap<QString, QString> attributes)
837 {
838  if (region != NULL)
839  {
840  /* changing */
841  if (!attributes["id"].isEmpty())
842  region->setId(attributes["id"]);
843 
844  if (!attributes["title"].isEmpty())
845  region->setTitle(attributes["title"]);
846 
847  if (!attributes["color"].isEmpty())
848  region->setColor(attributes["color"]);
849 
850  if (!attributes["top"].isEmpty())
851  {
852  if (attributes["top"].contains(QRegExp("\\d+(.\\d+)?%")))
853  {
854  QString attribute = attributes["top"];
855  attribute.remove(attribute.length()-1,1); // removing '%'
856 
857  qreal top = attribute.toDouble();
858 
859  if (top >= 0 && top <= 100)
860  region->setRelativeTop(top/100);
861  }
862  else if (attributes["top"].contains(QRegExp("\\d+(.\\d+)?")))
863  {
864  QString attribute = attributes["top"];
865 
866  qreal top = attribute.toDouble();
867 
868  if (top >= 0 && top <= 1)
869  region->setRelativeTop(top);
870  }
871  }
872 
873  if (!attributes["left"].isEmpty())
874  {
875  if (attributes["left"].contains(QRegExp("\\d+(.\\d+)?%")))
876  {
877  QString attribute = attributes["left"];
878  attribute.remove(attribute.length()-1,1); // removing '%'
879 
880  qreal left = attribute.toDouble();
881 
882  if (left >= 0 && left <= 100)
883  region->setRelativeLeft(left/100);
884  }
885  else if (attributes["left"].contains(QRegExp("\\d+(.\\d+)?")))
886  {
887  QString attribute = attributes["left"];
888 
889  qreal left = attribute.toDouble();
890 
891  if (left >= 0 && left <= 1)
892  region->setRelativeLeft(left);
893  }
894  }
895 
896  if (!attributes["right"].isEmpty())
897  {
898  if (attributes["right"].contains(QRegExp("\\d+(.\\d+)?%")))
899  {
900  QString attribute = attributes["right"];
901  attribute.remove(attribute.length()-1,1); // removing '%'
902 
903  qreal right = attribute.toDouble();
904 
905  if (right >= 0 && right <= 100)
906  region->setRelativeRight(right/100);
907  }
908  else if (attributes["right"].contains(QRegExp("\\d+(.\\d+)?")))
909  {
910  QString attribute = attributes["right"];
911 
912  qreal right = attribute.toDouble();
913 
914  if (right >= 0 && right <= 1)
915  region->setRelativeRight(right);
916  }
917  }
918 
919  if (!attributes["bottom"].isEmpty())
920  {
921  if (attributes["bottom"].contains(QRegExp("\\d+(.\\d+)?%")))
922  {
923  QString attribute = attributes["bottom"];
924  attribute.remove(attribute.length()-1,1); // removing '%'
925 
926  qreal bottom = attribute.toDouble();
927 
928  if (bottom >= 0 && bottom <= 100)
929  region->setRelativeBottom(bottom/100);
930  }
931  else if (attributes["bottom"].contains(QRegExp("\\d+(.\\d+)?")))
932  {
933  QString attribute = attributes["bottom"];
934 
935  qreal bottom = attribute.toDouble();
936 
937  if (bottom >= 0 && bottom <= 1)
938  region->setRelativeBottom(bottom);
939  }
940  }
941 
942  if (!attributes["width"].isEmpty())
943  {
944  if (attributes["width"].contains(QRegExp("\\d+(.\\d+)?%")))
945  {
946  QString attribute = attributes["width"];
947  attribute.remove(attribute.length()-1,1); // removing '%'
948 
949  qreal width = attribute.toDouble();
950 
951  if (width >= 0.0 && width <= 100.0)
952  region->setRelativeWidth(width/100);
953  }
954  else if (attributes["width"].contains(QRegExp("\\d+(.\\d+)?")))
955  {
956  QString attribute = attributes["width"];
957 
958  qreal width = attribute.toDouble();
959 
960  if (width >= 0 && width <= 1)
961  region->setRelativeWidth(width);
962  }
963  }
964 
965  if (!attributes["height"].isEmpty()){
966  if (attributes["height"].contains(QRegExp("\\d+(.\\d+)?%")))
967  {
968  QString attribute = attributes["height"];
969  attribute.remove(attribute.length()-1,1); // removing '%'
970 
971  qreal height = attribute.toDouble();
972 
973  if (height >= 0 && height <= 100)
974  region->setRelativeHeight(height/100);
975 
976  }
977  else if (attributes["height"].contains(QRegExp("\\d+(.\\d+)?")))
978  {
979  QString attribute = attributes["height"];
980 
981  qreal height = attribute.toDouble();
982 
983  if (height >= 0 && height <= 1)
984  region->setRelativeHeight(height);
985  }
986  }
987 
988  if(attributes.contains("zIndex"))
989  region->setzIndex(attributes["zIndex"].toInt());
990 
991  region->setGridAction(gridAction);
992 
993  if (parent != NULL)
994  {
995  parent->addRegion(region);
996  }
997  else
998  {
999  addItem(region);
1000 
1001  action = new QAction(this);
1002  action->setText(region->getId());
1003 
1004  showMenu->insertAction(showMenu->actions().front(), action);
1005 
1006  if (showMenu->actions().size() <= 2)
1007  {
1008  showMenu->insertSeparator(showMenu->actions().back());
1009  }
1010 
1011  action->setCheckable(true);
1012  action->setChecked(true);
1013  action->setEnabled(true);
1014 
1015  regionActionGroup->addAction(action);
1016 
1017  regionActions[region->getUid()] = action;
1018  }
1019 
1020  region->adjust();
1021 
1022  regions[region->getUid()] = region;
1023 
1024  connect(region,
1025  SIGNAL(regionSelectionRequested(QnlyGraphicsRegion*)),
1026  SLOT(requestRegionSelection(QnlyGraphicsRegion*)));
1027 
1028  connect(region,
1029  SIGNAL(regionChangeRequested(QnlyGraphicsRegion*,QMap<QString,QString>)),
1030  SLOT(requestRegionChange(QnlyGraphicsRegion*,QMap<QString,QString>)));
1031 
1032  connect(region,
1033  SIGNAL(regionbasePerformed()),
1034  SIGNAL(regionbasePerformed()));
1035 
1036 
1037  connect(region,
1038  SIGNAL(regionAdditionRequested(QnlyGraphicsRegion*)),
1039  SLOT(requestAdditionRegion(QnlyGraphicsRegion*)));
1040 
1041  connect(region,
1042  SIGNAL(regionDeletionRequested(QnlyGraphicsRegion*)),
1043  SLOT(requestRegionDeletion(QnlyGraphicsRegion*)));
1044 
1045  connect(region,
1046  SIGNAL(dragMediaOverRegion(QString,QnlyGraphicsRegion*)),
1047  this,
1048  SLOT(requestMediaOverRegionAction(QString,QnlyGraphicsRegion*))
1049  );
1050 
1051  // \fixme seg fault on Outline
1052  QMap<QString, QString> noChangeAtts;
1053 
1054  requestRegionChange(region,noChangeAtts);
1055 
1056  emit requestRegionSelection(region);
1057  }
1058 }
1059 
1060 void QnlyGraphicsRegionBase::performChangeResolution()
1061 {
1062  QAction* action = dynamic_cast<QAction*> (QObject::sender());
1063  if(action != NULL)
1064  {
1065  QSize size = action->data().toSize();
1066  changeResolution(size.width(), size.height());
1067  }
1068 }
1069 
1070 void QnlyGraphicsRegionBase::changeResolution(int w, int h)
1071 {
1072  setSceneRect(0, 0, w, h);
1073  bgrect->setRect(0, 0, w, h);
1074  grid->setRect(0, 0, w, h);
1075 
1076  foreach(QnlyGraphicsRegion* r, regions.values())
1077  {
1078  r->adjust();
1079  }
1080 
1081  // TODO: This should be based on array!
1082  QSize size(w, h);
1083  if(re640x480->data().toSize() == size)
1084  {
1085  re640x480->setChecked(true);
1086  }
1087  else if(re800x600->data().toSize() == size)
1088  {
1089  re800x600->setChecked(true);
1090  }
1091  else if(re1024x768->data().toSize() == size)
1092  {
1093  re1024x768->setChecked(true);
1094  }
1095  else if(re854x480->data().toSize() == size)
1096  {
1097  re854x480->setChecked(true);
1098  }
1099  else if(re1280x720->data().toSize() == size)
1100  {
1101  re1280x720->setChecked(true);
1102  }
1103  else if(re1920x1080->data().toSize() == size)
1104  {
1105  re1920x1080->setChecked(true);
1106  }
1107  else if(re320x400->data().toSize() == size)
1108  {
1109  re320x400->setChecked(true);
1110  }
1111 }
1112 
1113 void QnlyGraphicsRegionBase::performGrid()
1114 {
1115  setGridVisible(!isGridVisible());
1116  emit gridVisibilityChanged(isGridVisible());
1117 }
1118 
1119 bool QnlyGraphicsRegionBase::isGridVisible()
1120 {
1121  return grid->isVisible();
1122 }
1123 
1124 void QnlyGraphicsRegionBase::setGridVisible(bool visible)
1125 {
1126  gridAction->setChecked(visible);
1127  grid->setVisible(visible);
1128 }
1129 
1130 void QnlyGraphicsRegionBase::performExport()
1131 {
1132  QString location =
1133  QFileDialog::getSaveFileName(NULL, "Export...", "", tr("Images (*.png)"));
1134 
1135  if (location != "")
1136  {
1137  QImage image(width(), height(), QImage::Format_ARGB32_Premultiplied);
1138 
1139  QPainter painter(&image);
1140 
1141  render(&painter, QRect(), QRect(0,0,width(),height()));
1142 
1143  painter.end();
1144 
1145  image.save(location, "PNG");
1146  }
1147 }
1148 
1149 void QnlyGraphicsRegionBase::mousePressEvent(QGraphicsSceneMouseEvent* event)
1150 {
1151  QGraphicsScene::mousePressEvent(event);
1152 
1153  if (!event->isAccepted())
1154  {
1155  if (event->button() == Qt::RightButton)
1156  {
1157  event->ignore();
1158 
1159  }
1160  else if (event->button() == Qt::LeftButton){
1161  emit regionBaseSelectionRequested(uid);
1162  }
1163 
1164  event->accept();
1165  }
1166 }
1167 
1168 void QnlyGraphicsRegionBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
1169 {
1170  if(selectedRegion != NULL)
1171  {
1172  selectedRegion->updateCursor(event);
1173  }
1174 
1175  QGraphicsScene::mouseMoveEvent(event);
1176 }
1177 
1178 void QnlyGraphicsRegionBase::contextMenuEvent(
1179  QGraphicsSceneContextMenuEvent* event)
1180 {
1181  QGraphicsScene::contextMenuEvent(event);
1182 
1183  if (!event->isAccepted())
1184  {
1185  emit regionBaseSelectionRequested(uid);
1186 
1187  contextMenu->exec(event->screenPos());
1188 
1189  event->accept();
1190  }
1191 }
1192 
1193 void QnlyGraphicsRegionBase::requestMediaOverRegionAction(QString mediaId,
1194  QnlyGraphicsRegion* region)
1195 {
1196  emit mediaOverRegion(mediaId, region->getUid());
1197 }