NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnlycomposerplugin.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 "qnlycomposerplugin.h"
19 
20 #include <QAbstractButton>
21 #include <QPushButton>
22 #include <QMessageBox>
23 #include <QInputDialog>
24 
25 QnlyComposerPlugin::QnlyComposerPlugin(QObject* parent)
26 {
27  setParent(parent);
28 
29  createView();
30  createConnections();
31 
32  selectedId = NULL;
33 }
34 
35 QnlyComposerPlugin::~QnlyComposerPlugin()
36 {
37  if(selectedId != NULL)
38  delete selectedId;
39  delete(view);
40 }
41 
42 void QnlyComposerPlugin::createView()
43 {
44  view = new QnlyView();
45 }
46 
47 void QnlyComposerPlugin::createConnections()
48 {
49  connect(view,
50  SIGNAL(regionAdded(QString,QString,QString,QMap<QString,QString>)),
51  SLOT(addRegion(QString,QString,QString,QMap<QString,QString>)));
52 
53  connect(view,
54  SIGNAL(regionRemoved(QString,QString)),
55  SLOT(removeRegion(QString,QString)));
56 
57  connect(view,
58  SIGNAL(regionSelected(QString,QString)),
59  SLOT(selectRegion(QString,QString)));
60 
61  connect(view,
62  SIGNAL(regionChanged(QString,QString,QMap<QString,QString>)),
63  SLOT(changeRegion(QString,QString,QMap<QString,QString>)));
64 
65  connect(view,
66  SIGNAL(regionBaseAdded(QString,QMap<QString,QString>)),
67  SLOT(addRegionBase(QString,QMap<QString,QString>)));
68 
69  connect(view,
70  SIGNAL(regionBaseChanged(QString,QMap<QString,QString>)),
71  SLOT(changeRegionBase(QString,QMap<QString,QString>)));
72 
73  connect(view,
74  SIGNAL(regionBaseSelected(QString)),
75  SLOT(selectRegionBase(QString)));
76 
77  connect(view,
78  SIGNAL(regionBaseRemoved(QString)),
79  SLOT(removeRegionBase(QString)));
80 
81  connect(view,
82  SIGNAL(mediaOverRegionAction(QString, QString)),
83  SLOT(performMediaOverRegionAction(QString, QString)));
84 }
85 
86 void QnlyComposerPlugin::updateFromModel()
87 {
88  QRectF previousRect;
89  bool isGridVisible = false;
90 
91  QnlyGraphicsRegionBase *currentRegionBase = view->getSelectedRegionBase();
92  if(currentRegionBase != NULL)
93  {
94  previousRect = currentRegionBase->sceneRect();
95  isGridVisible = currentRegionBase->isGridVisible();
96  }
97 
98  clear();
99 
100  loadRegionbase();
101 
102  currentRegionBase = view->getSelectedRegionBase();
103  if(currentRegionBase != NULL)
104  {
105  currentRegionBase->changeResolution(previousRect.width(),
106  previousRect.height());
107  currentRegionBase->setGridVisible(isGridVisible);
108  }
109 }
110 
111 void QnlyComposerPlugin::loadRegionbase()
112 {
113  QList<Entity*> regionbaseList = getProject()->getEntitiesbyType("regionBase");
114 
115  if (!regionbaseList.isEmpty())
116  {
117  foreach(Entity* regionbaseEntity, regionbaseList)
118  {
119  addRegionBaseToView(regionbaseEntity);
120 
121  QVector<Entity*> children = regionbaseEntity->getChildren();
122 
123  foreach(Entity* child, children)
124  {
125  loadRegion(child);
126  }
127  }
128  }
129 }
130 
131 void QnlyComposerPlugin::loadRegion(Entity* region)
132 {
133  if (region != NULL)
134  {
135  addRegionToView(region);
136 
137  QVector<Entity*> children = region->getChildren();
138 
139  foreach(Entity* child, children)
140  {
141  loadRegion(child);
142  }
143  }
144 }
145 
147 {
148  return view;
149 }
150 
152 {
153  QByteArray data;
154  QString line;
155  QVariant gridVisible(QVariant::Bool),
156  resolutionWidth(QVariant::Int),
157  resolutionHeight(QVariant::Int);
158 
159  if(view->getSelectedRegionBase() != NULL)
160  {
161  gridVisible.setValue(view->getSelectedRegionBase()->isGridVisible());
162  resolutionWidth.setValue(view->getSelectedRegionBase()->sceneRect().width());
163  resolutionHeight.setValue(view->getSelectedRegionBase()->sceneRect().height());
164  }
165  else
166  {
167  gridVisible.setValue(false);
168  resolutionWidth.setValue(0);
169  resolutionHeight.setValue(0);
170  }
171 
172  line += QString("gridVisible=") + gridVisible.toString() + "\n";
173  line += QString("resolutionWidth=") + resolutionWidth.toString() + "\n";
174  line += QString("resolutionHeight=") + resolutionHeight.toString() + "\n";
175  data.append(line);
176  qDebug() << "[QNLY] saveSubsession() data is " << data;
177  emit setPluginData(data);
178  return true;
179 }
180 
182 {
183  // \todo Load specific contents.
184  QString data = project->getPluginData("br.puc-rio.telemidia.qncllayout");
185  qDebug() << "[QNLY] data = " << data;
186  QStringList lines = data.split("\n");
187  bool gridVisible = false, ok = true;
188  int resolutionWidth, resolutionHeight;
189 
190  for(int i = 0; i < lines.size(); i++)
191  {
192  QStringList list = lines[i].split("=");
193  for(int j = 0; !(list.size()%2) && j < list.size(); j += 2)
194  {
195  QString key = list[j];
196  QString value = list[j+1];
197  if(key == "gridVisible")
198  {
199  qDebug() << "[QNLY] gridVisible = " << value;
200  if(value == "true")
201  gridVisible = true;
202  }
203  else if(key == "resolutionWidth")
204  {
205  resolutionWidth = value.toInt(&ok);
206  if(!ok)
207  resolutionWidth = 0;
208  }
209  else if(key == "resolutionHeight")
210  {
211  resolutionHeight = value.toInt(&ok);
212  if(!ok)
213  resolutionHeight = 0;
214  }
215  }
216  }
217 
218  qDebug() << resolutionWidth << resolutionHeight;
219  // Update layout model from core model.
220  QStack <Entity*> stack;
221  stack.push(project);
222 
223  while(stack.size())
224  {
225  Entity *current = stack.top();
226  stack.pop();
227 
228  if(current->getType() == "regionBase")
229  {
230  addRegionBaseToView(current);
231 
232  // TODO: In the future we should support saving individual resolutions
233  // foreach regionBase
234  view->getSelectedRegionBase()->changeResolution(resolutionWidth, resolutionHeight);
235  }
236  else if(current->getType() == "region")
237  {
238  addRegionToView(current);
239  }
240 
241  QVector <Entity *> children = current->getChildren();
242  for(int i = 0; i < children.size(); i++)
243  {
244  stack.push(children.at(i));
245  }
246  }
247 
248  view->setGridVisible(gridVisible);
249 }
250 
251 void QnlyComposerPlugin::errorMessage(QString error)
252 {
253  //TODO: void QnlyComposerPlugin::errorMessage(QString error)
254 }
255 
256 void QnlyComposerPlugin::onEntityAdded(QString pluginID, Entity *entity)
257 {
258  if (entity != NULL)
259  {
260  if (entity->getType() == "region")
261  {
262  addRegionToView(entity);
263  }
264  else if (entity->getType() == "regionBase")
265  {
266  addRegionBaseToView(entity);
267  }
268  }
269 }
270 
271 void QnlyComposerPlugin::onEntityRemoved(QString pluginID, QString entityID)
272 {
273  if (!entityID.isEmpty())
274  {
275  if (regions.contains(entityID))
276  {
277  removeRegionFromView(entityID);
278  }
279  else if (regionbases.contains(entityID))
280  {
281  removeRegionBaseFromView(entityID);
282  }
283  }
284 }
285 
286 void QnlyComposerPlugin::onEntityChanged(QString pluginID, Entity *entity)
287 {
288  if (entity != NULL)
289  {
290  if (entity->getType() == "region")
291  {
292  changeRegionInView(entity);
293  }
294  else if (entity->getType() == "regionBase")
295  {
296  changeRegionBaseInView(entity);
297  }
298  }
299 }
300 
301 void QnlyComposerPlugin::changeSelectedEntity (QString pluginID, void* param)
302 {
303  //if(pluginID != this->pluginInstanceID)
304  // {
305  QString* entityUID = (QString*) param;
306 
307  if(entityUID != NULL)
308  {
309  if (regions.contains(*entityUID))
310  selectRegionInView(*entityUID);
311  else if (regionbases.contains(*entityUID))
312  selectRegionBaseInView(*entityUID);
313  }
314  // }
315 }
316 
317 void QnlyComposerPlugin::addRegionToView(Entity* entity)
318 {
319  if (entity != NULL)
320  {
321  if (entity->getType() == "region")
322  {
323  // setting
324  QString entityUID;
325 
326  if (!entity->getUniqueId().isEmpty())
327  {
328  entityUID = entity->getUniqueId();
329  }
330  else
331  {
332  qWarning() << "QnlyComposerPlugin::addRegion:"
333  << "Tried to add a region with empty UID.";
334 
335  return; // abort addition
336  }
337 
338  QString regionUID;
339 
340  regionUID = entityUID;
341 
342  QString parentUID;
343 
344  if (regions.contains(entity->getParentUniqueId()))
345  {
346  parentUID = entity->getParentUniqueId();
347  }
348  else
349  {
350  parentUID = "";
351  }
352 
353  QString regionbaseUID;
354 
355  if (regions.contains(parentUID))
356  {
357  regionbaseUID = relations[parentUID];
358  }
359  else if (regionbases.contains(entity->getParentUniqueId()))
360  {
361  regionbaseUID = entity->getParentUniqueId();
362 
363  }
364  else
365  {
366  qWarning() << "QnlyComposerPlugin::addRegion:"
367  << "Tried to add a region without regionbase UID.";
368 
369  return; // abort addition
370  }
371 
372  QMap<QString, QString> attributes;
373 
374  if (!entity->getAttribute("id").isEmpty())
375  {
376  attributes["id"] = entity->getAttribute("id");
377  }
378 
379  if (!entity->getAttribute("title").isEmpty())
380  {
381  attributes["title"] = entity->getAttribute("title");
382  }
383 
384  if (!entity->getAttribute("zIndex").isEmpty())
385  {
386  attributes["zIndex"] = entity->getAttribute("zIndex");
387  }
388 
389  if (!entity->getAttribute("top").isEmpty())
390  {
391  attributes["top"] = entity->getAttribute("top");
392  }
393 
394  if (!entity->getAttribute("left").isEmpty())
395  {
396  attributes["left"] = entity->getAttribute("left");
397  }
398 
399  if (!entity->getAttribute("bottom").isEmpty())
400  {
401  attributes["bottom"] = entity->getAttribute("bottom");
402  }
403 
404  if (!entity->getAttribute("right").isEmpty())
405  {
406  attributes["right"] = entity->getAttribute("right");
407  }
408 
409  if (!entity->getAttribute("width").isEmpty())
410  {
411  attributes["width"] = entity->getAttribute("width");
412  }
413 
414  if (!entity->getAttribute("height").isEmpty())
415  {
416  attributes["height"] = entity->getAttribute("height");
417  }
418 
419  // adding
420  regions[regionUID] = entity;
421 
422  relations[regionUID] = regionbaseUID;
423 
424  view->addRegion(regionUID,
425  parentUID,
426  regionbaseUID,
427  attributes);
428  }
429  }
430 }
431 
432 void QnlyComposerPlugin::removeRegionFromView(QString entityUID)
433 {
434  if (!entityUID.isEmpty())
435  {
436  if (regions.contains(entityUID))
437  {
438  // setting
439  QString regionUID;
440 
441  regionUID = entityUID;
442 
443  QString regionbaseUID;
444 
445  if (relations.contains(regionUID))
446  {
447  regionbaseUID = relations[regionUID];
448  }
449  else
450  {
451  qWarning() << "QnlyComposerPlugin::removeRegion:"
452  << "Tried to remove a region without"
453  << "regionbase UID.";
454 
455  return; // abort remotion
456  }
457 
458  regions.remove(regionUID);
459 
460  relations.remove(entityUID);
461 
462  // removing
463  view->removeRegion(regionUID, regionbaseUID);
464  }
465  }
466 }
467 
468 void QnlyComposerPlugin::changeRegionInView(Entity* entity)
469 {
470  if (entity != NULL)
471  {
472  if (entity->getType() == "region")
473  {
474  // setting
475  QString entityUID;
476 
477  if (!entity->getUniqueId().isEmpty())
478  {
479  entityUID = entity->getUniqueId();
480  }
481  else
482  {
483  qWarning() << "QnlyComposerPlugin::addRegion:"
484  << "Tried to add an region with empty UID.";
485 
486  return; // abort addition
487  }
488 
489  QString regionUID;
490 
491  regionUID = entityUID;
492 
493  QString parentUID;
494 
495  if (regions.contains(entity->getParentUniqueId()))
496  {
497  parentUID = entity->getParentUniqueId();
498  }
499  else
500  {
501  parentUID = "";
502  }
503 
504  QString regionbaseUID;
505 
506  if (regions.contains(parentUID))
507  {
508  regionbaseUID = relations[parentUID];
509 
510  }
511  else if (regionbases.contains(entity->getParentUniqueId()))
512  {
513  regionbaseUID = entity->getParentUniqueId();
514 
515  }
516  else
517  {
518  qWarning() << "QnlyComposerPlugin::addRegion:"
519  << "Tried to add an region without"
520  << "regionbase UID.";
521 
522  return; // abort addition
523  }
524 
525  QMap<QString, QString> attributes;
526 
527  if (!entity->getAttribute("id").isEmpty())
528  {
529  attributes["id"] = entity->getAttribute("id");
530  }
531 
532  if (!entity->getAttribute("title").isEmpty())
533  {
534  attributes["title"] = entity->getAttribute("title");
535  }
536 
537  if (!entity->getAttribute("zIndex").isEmpty())
538  {
539  attributes["zIndex"] = entity->getAttribute("zIndex");
540  }
541 
542  if (!entity->getAttribute("top").isEmpty())
543  {
544  attributes["top"] = entity->getAttribute("top");
545  }
546 
547  if (!entity->getAttribute("left").isEmpty())
548  {
549  attributes["left"] = entity->getAttribute("left");
550  }
551 
552  if (!entity->getAttribute("bottom").isEmpty())
553  {
554  attributes["bottom"] = entity->getAttribute("bottom");
555  }
556 
557  if (!entity->getAttribute("right").isEmpty())
558  {
559  attributes["right"] = entity->getAttribute("right");
560  }
561 
562  if (!entity->getAttribute("width").isEmpty())
563  {
564  attributes["width"] = entity->getAttribute("width");
565  }
566 
567  if (!entity->getAttribute("height").isEmpty())
568  {
569  attributes["height"] = entity->getAttribute("height");
570  }
571 
572  // change
573  view->changeRegion(regionUID,
574  regionbaseUID,
575  attributes);
576  }
577  }
578 }
579 
580 void QnlyComposerPlugin::selectRegionInView(QString entityUID)
581 {
582  if (!entityUID.isEmpty())
583  {
584  if (regions.contains(entityUID))
585  {
586  // setting
587  QString regionUID;
588 
589  regionUID = entityUID;
590 
591  QString regionbaseUID;
592 
593  if (relations.contains(regionUID))
594  {
595  regionbaseUID = relations[regionUID];
596  }
597  else
598  {
599  qWarning() << "QnlyComposerPlugin::selectRegion:"
600  << "Tried to select a region without"
601  << "regionbase UID.";
602 
603  return; // abort remotion
604  }
605 
606  // select
607  view->selectRegion(regionUID, regionbaseUID);
608  }
609  }
610 }
611 
612 void QnlyComposerPlugin::addRegionBaseToView(Entity* entity)
613 {
614  if (entity != NULL)
615  {
616  if (entity->getType() == "regionBase")
617  {
618  // setting
619  QString entityUID;
620 
621  if (!entity->getUniqueId().isEmpty())
622  {
623  entityUID = entity->getUniqueId();
624  }
625  else
626  {
627  qWarning() << "QnlyComposerPlugin::addRegion:"
628  << "Tried to add a region with empty UID.";
629 
630  return; // abort addition
631  }
632 
633  QString regionbaseUID;
634 
635  regionbaseUID = entityUID;
636 
637  QMap<QString, QString> attributes;
638 
639  if (!entity->getAttribute("id").isEmpty())
640  {
641  attributes["id"] = entity->getAttribute("id");
642  }
643 
644  if (!entity->getAttribute("region").isEmpty())
645  {
646  attributes["region"] = entity->getAttribute("region");
647  }
648 
649  if (!entity->getAttribute("device").isEmpty())
650  {
651  attributes["device"] = entity->getAttribute("device");
652  }
653 
654  // adding
655  regionbases[regionbaseUID] = entity;
656 
657  view->addRegionBase(regionbaseUID, attributes);
658  }
659  }
660 }
661 
662 void QnlyComposerPlugin::removeRegionBaseFromView(QString entityUID)
663 {
664  if (!entityUID.isEmpty())
665  {
666  if (regionbases.contains(entityUID))
667  {
668  // setting
669  QString regionbaseUID;
670 
671  regionbaseUID = entityUID;
672 
673  // select
674  view->removeRegionBase(regionbaseUID);
675 
676  regionbases.remove(regionbaseUID);
677 
678  relations.remove(entityUID);
679 
680  }
681  }
682 }
683 
684 void QnlyComposerPlugin::changeRegionBaseInView(Entity* entity)
685 {
686  if (entity != NULL)
687  {
688  if (entity->getType() == "regionBase")
689  {
690  // setting
691  QString entityUID;
692 
693  if (!entity->getUniqueId().isEmpty())
694  {
695  entityUID = entity->getUniqueId();
696  }
697  else
698  {
699  qWarning() << "QnlyComposerPlugin::addRegion:"
700  << "Tried to add a region with empty UID.";
701  return; // abort addition
702  }
703 
704  QString regionbaseUID;
705  regionbaseUID = entityUID;
706 
707  QMap<QString, QString> attributes;
708 
709  if (!entity->getAttribute("id").isEmpty())
710  {
711  attributes["id"] = entity->getAttribute("id");
712  }
713 
714  if (!entity->getAttribute("region").isEmpty())
715  {
716  attributes["region"] = entity->getAttribute("region");
717  }
718 
719  if (!entity->getAttribute("device").isEmpty())
720  {
721  attributes["device"] = entity->getAttribute("device");
722  }
723 
724  // adding
725  regionbases[regionbaseUID] = entity;
726 
727  view->changeRegionBase(regionbaseUID,
728  attributes);
729  }
730  }
731 }
732 
733 void QnlyComposerPlugin::selectRegionBaseInView(QString entityUID)
734 {
735  if (!entityUID.isEmpty())
736  {
737  if (regionbases.contains(entityUID))
738  {
739  // setting
740  QString regionbaseUID;
741 
742  regionbaseUID = entityUID;
743 
744  // select
745  view->selectRegionBase(regionbaseUID);
746  }
747  }
748 }
749 
750 void QnlyComposerPlugin::addRegion(const QString regionUID,
751  const QString parentUID,
752  const QString regionbaseUID,
753  const QMap<QString, QString> attributes)
754 {
755  // setting
756  QMap<QString, QString> standard;
757 
758  if (attributes.contains("id"))
759  standard["id"] = attributes["id"];
760  else
761  standard["id"] = project->generateUniqueNCLId("region");
762 
763  if (attributes.contains("title"))
764  standard["title"] = attributes["title"];
765 
766  if (attributes.contains("top"))
767  standard["top"] = attributes["top"];
768 
769  if (attributes.contains("left"))
770  standard["left"] = attributes["left"];
771 
772  if (attributes.contains("bottom"))
773  standard["bottom"] = attributes["bottom"];
774 
775  if (attributes.contains("right"))
776  standard["right"] = attributes["right"];
777 
778  if (attributes.contains("width"))
779  standard["width"] = attributes["width"];
780 
781  if (attributes.contains("height"))
782  standard["height"] = attributes["height"];
783 
784  if (attributes.contains("zIndex"))
785  standard["zIndex"] = attributes["zIndex"];
786 
787  // emitting
788  if (!parentUID.isEmpty())
789  emit addEntity("region", parentUID, standard, false);
790  else
791  emit addEntity("region", regionbaseUID, standard, false);
792 }
793 
794 void QnlyComposerPlugin::removeRegion(const QString regionUID,
795  const QString regionbaseUID)
796 {
797  if (regions.contains(regionUID))
798  {
799  emit removeEntity(regions[regionUID], false);
800  }
801 }
802 
803 void QnlyComposerPlugin::changeRegion(const QString regionUID,
804  const QString regionbaseUID,
805  const QMap<QString, QString> attributes)
806 {
807  if (regions.contains(regionUID))
808  {
809  // setting
810  QMap<QString, QString> standard;
811 
812  if (attributes.contains("id"))
813  standard["id"] = attributes["id"];
814 
815  if (attributes.contains("title"))
816  standard["title"] = attributes["title"];
817 
818  if (attributes.contains("top"))
819  standard["top"] = attributes["top"];
820 
821  if (attributes.contains("left"))
822  standard["left"] = attributes["left"];
823 
824  if (attributes.contains("bottom"))
825  standard["bottom"] = attributes["bottom"];
826 
827  if (attributes.contains("right"))
828  standard["right"] = attributes["right"];
829 
830  if (attributes.contains("width"))
831  standard["width"] = attributes["width"];
832 
833  if (attributes.contains("height"))
834  standard["height"] = attributes["height"];
835 
836  if (attributes.contains("zIndex"))
837  standard["zIndex"] = attributes["zIndex"];
838 
839  // emitting
840  emit setAttributes(regions[regionUID], standard, false);
841  }
842 }
843 
844 void QnlyComposerPlugin::selectRegion(const QString regionUID,
845  const QString regionbaseUID)
846 {
847  if(selectedId != NULL)
848  {
849  delete selectedId;
850  selectedId = NULL;
851  }
852 
853  if (!regionUID.isEmpty())
854  {
855  selectedId = new QString(regionUID);
856  emit sendBroadcastMessage("changeSelectedEntity", selectedId);
857  }
858 }
859 
860 void QnlyComposerPlugin::addRegionBase(const QString regionbaseUID,
861  const QMap<QString, QString> attributes)
862 {
863  // setting
864  QMap<QString, QString> standard;
865 
866  if (attributes.contains("id"))
867  standard["id"] = attributes["id"];
868 
869  if (attributes.contains("region"))
870  standard["region"] = attributes["region"];
871 
872  if (attributes.contains("device"))
873  standard["device"] = attributes["device"];
874 
875  QString headUID = getHeadUid();
876  // emitting
877  emit addEntity("regionBase", headUID, standard, false);
878 }
879 
880 void QnlyComposerPlugin::removeRegionBase(const QString regionbaseUID)
881 {
882  if (regionbases.contains(regionbaseUID))
883  {
884  emit removeEntity(regionbases[regionbaseUID], false);
885  }
886 }
887 
888 void QnlyComposerPlugin::changeRegionBase(const QString regionbaseUID,
889  const QMap<QString, QString> attributes)
890 {
891  if (regionbases.contains(regionbaseUID))
892  {
893  // setting
894  QMap<QString, QString> standard;
895 
896  if (attributes.contains("id"))
897  standard["id"] = attributes["id"];
898 
899  if (attributes.contains("title"))
900  standard["title"] = attributes["title"];
901 
902  if (attributes.contains("top"))
903  standard["top"] = attributes["top"];
904 
905  if (attributes.contains("left"))
906  standard["left"] = attributes["left"];
907 
908  if (attributes.contains("bottom"))
909  standard["bottom"] = attributes["bottom"];
910 
911  if (attributes.contains("right"))
912  standard["right"] = attributes["right"];
913 
914  if (attributes.contains("width"))
915  standard["width"] = attributes["width"];
916 
917  if (attributes.contains("height"))
918  standard["height"] = attributes["height"];
919 
920  if (attributes.contains("zIndex"))
921  standard["zIndex"] = attributes["zIndex"];
922 
923  // emitting
924  emit setAttributes(regionbases[regionbaseUID],standard, false);
925  }
926 }
927 
928 void QnlyComposerPlugin::clear()
929 {
930  while(!regions.empty())
931  {
932  Entity* regionEntity = regions.begin().value();
933 
934  qDebug() << regionEntity->getAttribute("id");
935 
936  removeRegionFromView(regionEntity->getUniqueId());
937  }
938 
939  while(!regionbases.empty())
940  {
941  Entity* regionbaseEntity = regionbases.begin().value();
942 
943  removeRegionBaseFromView(regionbaseEntity->getUniqueId());
944  }
945 
946  regions.clear();
947  regionbases.clear();
948  relations.clear();
949 }
950 
951 void QnlyComposerPlugin::selectRegionBase(const QString regionbaseUID)
952 {
953  if(selectedId != NULL)
954  {
955  delete selectedId;
956  selectedId = NULL;
957  }
958 
959  if (!regionbaseUID.isEmpty())
960  {
961  selectedId = new QString(regionbaseUID);
962  emit sendBroadcastMessage("changeSelectedEntity", selectedId);
963  }
964 }
965 
966 QString QnlyComposerPlugin::getHeadUid()
967 {
968  if (getProject()->getEntitiesbyType("head").isEmpty())
969  {
970  if (getProject()->getEntitiesbyType("ncl").isEmpty())
971  {
972  QMap<QString, QString> atts;
973 
974  emit addEntity("ncl",
975  getProject()->getUniqueId(),
976  atts,
977  false);
978  }
979 
980  QString nclUID =
981  getProject()->getEntitiesbyType("ncl").at(0)->getUniqueId();
982 
983  QMap<QString, QString> atts;
984 
985  emit addEntity("head", nclUID, atts, false);
986  }
987 
988  return getProject()->getEntitiesbyType("head").at(0)->getUniqueId();
989 }
990 
991 QMap <QString, QString> QnlyComposerPlugin::getRegionAttributes(Entity *region)
992 {
993  QMap <QString, QString>::iterator begin, end, it;
994  QVector <double> widths;
995  QVector <double> heights;
996  QVector <double> tops;
997  QVector <double> lefts;
998 
999  double top = 1.0, left = 1.0;
1000  double cvalue = 0.0;
1001 
1002  Entity *currentRegion = region;
1003 
1004  // Here, we will compose all the hierachy values to get the final values of
1005  // left, top, width and height.
1006  while(currentRegion != NULL && currentRegion->getType() == "region")
1007  {
1008  currentRegion->getAttributeIterator(begin, end);
1009  for (it = begin; it != end; ++it)
1010  {
1011  QString name = it.key();
1012  QString value = it.value();
1013  if(value.endsWith("%"))
1014  cvalue = value.mid(0, value.indexOf("%")).toDouble();
1015  else
1016  cvalue = 100.0;
1017 
1018  cvalue /= 100.0;
1019 
1020  if(name == "width") widths.push_back(cvalue);
1021  else if(name == "height") heights.push_back(cvalue);
1022  else if(name == "top") tops.push_back(cvalue);
1023  else if(name == "left") lefts.push_back(cvalue);
1024  }
1025  currentRegion = currentRegion->getParent();
1026  }
1027 
1028  int i;
1029  //width
1030  heights.push_back(1.0f);
1031  widths.push_back(1.0f);
1032 
1033  for(i = widths.size()-1; i > 0; --i)
1034  widths[i-1] *= widths[i];
1035  //height
1036  for(i = heights.size()-1; i > 0; --i)
1037  heights[i-1] *= heights[i];
1038 
1039  //left
1040  left = top = 0.0;
1041  for(i = lefts.size()-1; i >= 0; --i)
1042  left += lefts[i] * widths[i+1];
1043 
1044  qDebug() << tops;
1045  for(i = tops.size()-1; i >= 0; --i)
1046  top += tops[i] * heights[i+1];
1047 
1048  QMap <QString, QString> attrs;
1049  attrs.insert("width", QString::number(widths[0]*100.0)+"%");
1050  attrs.insert("height", QString::number(heights[0]*100.0)+"%");
1051  attrs.insert("left", QString::number(left*100.0)+"%");
1052  attrs.insert("top", QString::number(top*100.0)+"%");
1053 
1054  // Add also the zIndex to the list of properties.
1055  if(region->hasAttribute("zIndex"))
1056  attrs.insert("zIndex", region->getAttribute("zIndex"));
1057  else
1058  attrs.insert("zIndex", "0");
1059 
1060  return attrs;
1061 }
1062 
1063 void QnlyComposerPlugin::performMediaOverRegionAction(const QString mediaId,
1064  const QString regionUID)
1065 {
1066  bool error = false;
1067  Entity *region = project->getEntityById(regionUID);
1068  Entity *media = NULL;
1069  QList <Entity*> medias = project->getEntitiesbyType("media");
1070 
1071  const bool askDescOrProperties = true; // \todo This must be a preference.
1072 
1073  for(int i = 0; i < medias.size(); i++)
1074  {
1075  if(medias.at(i)->hasAttribute("id") &&
1076  medias.at(i)->getAttribute("id") == mediaId)
1077  {
1078  media = medias.at(i);
1079  break;
1080  }
1081  }
1082 
1083  if(region == NULL)
1084  {
1085  qWarning() << "QnlyComposerPlugin::performMediaOverRegionAction Region does not exists. Nothing will be done." ;
1086  error = 1;
1087  }
1088 
1089  if(media == NULL)
1090  {
1091  qWarning() << "QnlyComposerPlugin::performMediaOverRegionAction Media was not found! Nothing will be done." ;
1092  error = 1;
1093  }
1094 
1095  if(!error)
1096  {
1097  if(askDescOrProperties)
1098  {
1099  QMessageBox msgBox;
1100  msgBox.setText(tr("Please, tell what do you want to do"));
1101  // \todo Enable detailed text.
1102  // msgBox.setDetailedText(tr("Detailed text"));
1103 
1104  QPushButton *createDescButton =
1105  msgBox.addButton( tr("Use a descriptor"),
1106  QMessageBox::ActionRole);
1107 
1108  QPushButton *importPropButton =
1109  msgBox.addButton(tr("Import region properties to media object"),
1110  QMessageBox::ActionRole);
1111  // importPropButton->setEnabled(false);
1112 
1113  QPushButton *cancelButton =
1114  msgBox.addButton(tr("Nothing!"),
1115  QMessageBox::ActionRole);
1116 
1117  // msgBox.setIcon(QMessageBox::Question);
1118  msgBox.exec();
1119  if (msgBox.clickedButton() == createDescButton) // create a new descriptor
1120  {
1121  qDebug() << "Creating a new descriptor";
1122  QMap <QString,QString> attrs;
1123  QList <Entity*> descritorBases =
1124  project->getEntitiesbyType("descriptorBase");
1125 
1126  // create the descriptor
1127  QString newDescriptorID = project->generateUniqueNCLId("descriptor");
1128  QStringList alredyExistentsDescriptorsIds, descriptorsIds;
1129  descriptorsIds << newDescriptorID;
1130 
1131  QList <Entity*> descriptors = project->getEntitiesbyType("descriptor");
1132  foreach (Entity *desc, descriptors) {
1133  if(desc != NULL)
1134  {
1135  if(desc->getAttribute("region") == region->getAttribute("id"))
1136  {
1137  alredyExistentsDescriptorsIds << desc->getAttribute("id");
1138  descriptorsIds << desc->getAttribute("id");
1139  }
1140  }
1141  }
1142 
1143  bool ok;
1144  newDescriptorID = QInputDialog::getItem(NULL,
1145  tr("Descriptor id:"),
1146  tr("Please, enter the "
1147  "descriptor id"),
1148  descriptorsIds,
1149  0,
1150  true,
1151  &ok);
1152 
1153  if(ok)
1154  {
1155  if(!descritorBases.size()) // if does not exists any descriptorBase
1156  // create one
1157  emit addEntity("descriptorBase", getHeadUid(), attrs, false);
1158 
1159  // If we choose the new descriptor (create it)
1160  if(!alredyExistentsDescriptorsIds.contains(newDescriptorID))
1161  {
1162  Entity *descriptorBase =
1163  project->getEntitiesbyType("descriptorBase").at(0);
1164  attrs.insert("id", newDescriptorID);
1165  attrs.insert("region", region->getAttribute("id"));
1166 
1167  emit addEntity("descriptor", descriptorBase->getUniqueId(), attrs,
1168  false);
1169  }
1170 
1171  //update the media to refer to this descriptor
1172  attrs.clear();
1173  QMap <QString, QString>::iterator begin, end, it;
1174  media->getAttributeIterator(begin, end);
1175  for (it = begin; it != end; ++it)
1176  {
1177  attrs[it.key()] = it.value();
1178  }
1179  attrs["descriptor"] = newDescriptorID;
1180  emit setAttributes(media, attrs, false);
1181  }
1182  }
1183  // import properties from region to media element
1184  else if (msgBox.clickedButton() == importPropButton)
1185  {
1186  QMap <QString, QString> propertyNameToUID;
1187  QVector <Entity *> currentProperties = media->getChildren();
1188  for(int i = 0; i < currentProperties.size(); i++)
1189  {
1190  Entity *propEntity = currentProperties.at(i);
1191  if(propEntity->hasAttribute("name"))
1192  {
1193  propertyNameToUID.insert(propEntity->getAttribute("name"),
1194  propEntity->getUniqueId());
1195  }
1196  }
1197 
1198  qDebug() << "Import attributes as properties of media element.";
1199  QMap <QString, QString> newAttrs = getRegionAttributes(region);
1200  QString key;
1201  foreach(key, newAttrs.keys())
1202  {
1203  QMap <QString, QString> attrs;
1204  attrs.insert("name", key);
1205  attrs.insert("value", newAttrs.value(key));
1206 
1207  if(propertyNameToUID.keys().contains(key))
1208  {
1209  QString propUID = propertyNameToUID.value(key);
1210  emit setAttributes(project->getEntityById(propUID), attrs, false);
1211  }
1212  else
1213  emit addEntity("property", media->getUniqueId(), attrs, false);
1214  }
1215  }
1216  }
1217  else
1218  {
1219  // Does not ask the descriptor or media properties
1220  qDebug() << "Creating a new descriptor";
1221 
1222  QMap <QString,QString> attrs;
1223  QList <Entity*> descritorBases =
1224  project->getEntitiesbyType("descriptorBase");
1225 
1226  // create the descriptor
1227  QString newDescriptorID = project->generateUniqueNCLId("descriptor");
1228 
1229  if(!descritorBases.size()) // if does not exists any descriptorBase
1230  // create one
1231  emit addEntity("descriptorBase", getHeadUid(), attrs, false);
1232 
1233  Entity *descriptorBase =
1234  project->getEntitiesbyType("descriptorBase").at(0);
1235  attrs.insert("id", newDescriptorID);
1236  attrs.insert("region", region->getAttribute("id"));
1237 
1238  emit addEntity("descriptor", descriptorBase->getUniqueId(), attrs, false);
1239 
1240  //update the media to refer to this descriptor
1241  attrs.clear();
1242  QMap <QString, QString>::iterator begin, end, it;
1243  media->getAttributeIterator(begin, end);
1244  for (it = begin; it != end; ++it)
1245  {
1246  attrs[it.key()] = it.value();
1247  }
1248  attrs["descriptor"] = newDescriptorID;
1249  emit setAttributes(media, attrs, false);
1250 
1251  if(media->hasAttribute("type") &&
1252  media->getAttribute("type") == "text/html")
1253  {
1254  attrs.clear();
1255  QMap <QString, QString>::iterator begin, end, it;
1256  QList <Entity*> descriptors = project->getEntityByAttrId(newDescriptorID);
1257 
1258  if(descriptors.size())
1259  {
1260  Entity *desc = descriptors.at(0);
1261  desc->getAttributeIterator(begin, end);
1262  for (it = begin; it != end; ++it)
1263  {
1264  attrs[it.key()] = it.value();
1265  }
1266  attrs["focusIndex"] = "1";
1267  if(descriptors.size())
1268  emit setAttributes(desc, attrs, false);
1269  }
1270  }
1271  }
1272  }
1273 }