18 #include "NCLTreeWidget.h"
19 #include "NCLStructure.h"
21 using namespace composer::language;
23 #include <QInputDialog>
26 #include <core/util/ComposerSettings.h>
27 using namespace composer::core::util;
31 setContextMenuPolicy(Qt::ActionsContextMenu);
37 labels << QObject::tr(
"Element") << QObject::tr(
"Attributes")
38 << QObject::tr(
"Element UId") << QObject::tr(
"Element Id")
39 << QObject::tr(
"Tagname");
40 setHeaderLabels(labels);
42 setHeaderHidden(
true);
43 setColumnHidden(1,
true);
44 setColumnHidden(2,
true);
45 setColumnHidden(3,
true);
46 setColumnHidden(4,
true);
51 defaultFont = QFont();
61 void NCLTreeWidget::createActions ()
63 insertNodeAct =
new QAction( QIcon(
":/images/save.png"),
67 connect( insertNodeAct, SIGNAL(triggered()),
68 this, SLOT(userAddNewElement()));
69 addAction(insertNodeAct);
71 removeNodeAct =
new QAction( QIcon(
":/images/delete.png"),
72 tr(
"&Remove Selected element"),
75 connect( removeNodeAct, SIGNAL(triggered()),
76 this, SLOT(userRemoveElement()));
77 removeNodeAct->setShortcutContext(Qt::WidgetShortcut);
78 removeNodeAct->setShortcut(QKeySequence::Delete);
79 addAction(removeNodeAct);
81 expandAllAct =
new QAction(tr(
"&Expand All"),
this);
82 expandAllAct->setCheckable(
true);
83 expandAllAct->setChecked(
true);
84 connect(expandAllAct, SIGNAL(triggered()),
this, SLOT(expandAll()));
85 addAction(expandAllAct);
89 void NCLTreeWidget::createMenus()
91 elementMenu =
new QMenu(
this);
92 elementMenu->addAction(insertNodeAct);
93 elementMenu->addAction(removeNodeAct);
94 elementMenu->addSeparator();
95 elementMenu->addAction(expandAllAct);
102 labels << QObject::tr(
"Element") << QObject::tr(
"Attributes")
103 << QObject::tr(
"id");
105 QXmlInputSource inputSource;
106 inputSource.setData( text );
107 QXmlSimpleReader reader;
109 this->setStyleSheet(
"/*background-color: #ffffff;*/ font-size: 11px;");
111 setHeaderLabels(labels);
120 SIGNAL(fatalErrorFound(QString, QString,
int,
int,
int)),
124 reader.setContentHandler(handler);
125 reader.setErrorHandler(handler);
126 bool ret = reader.parse(inputSource);
137 QMap <QString, QString> &attrs,
142 QTreeWidgetItem *child;
146 child =
new QTreeWidgetItem(0);
149 p = father->childCount();
151 father->insertChild(p, child);
155 child =
new QTreeWidgetItem(
this);
158 this->insertTopLevelItem(pos, child);
162 child->setText(2,
id);
164 child->setText(4, tagname);
173 void NCLTreeWidget::userAddNewElement()
176 QList<QTreeWidgetItem*> selecteds = this->selectedItems();
178 QTreeWidgetItem *item ;
179 QString parentId =
"", tagname =
"";
184 item = selecteds.at(0);
185 parentId = item->text(2);
186 tagname = item->text(4);
188 map <QString, char> *
189 children = NCLStructure::getInstance()->getChildren(tagname);
193 map <QString, char>::iterator it;
194 for(it = children->begin(); it != children->end(); ++it)
196 strlist << it->first;
203 QString element = QInputDialog::getItem(
this,
213 QMap<QString,QString> attr;
215 attr.insert(
"id",
"myNCLID");
222 QList <QTreeWidgetItem*> items = findItems( itemId,
224 Qt::MatchRecursive, 2);
228 qDebug() <<
"NCLTreeWidget::getItemById Warning - You have more than "
229 <<
"one item with id='" << itemId
230 <<
"' - All them will be deleted!";
233 for (
int i = 0; i < items.size(); i++)
235 if(items.at(i)->text(2) == itemId)
246 QList <QTreeWidgetItem*> items = this->findItems(itemId, Qt::MatchExactly
247 | Qt::MatchRecursive, 2);
248 QTreeWidgetItem *item;
252 qDebug() <<
"NCLTreeWidget::removeItem Warning - You have more than "
253 <<
"one item with id='"<< itemId
254 <<
"' - All them will be deleted!";
256 else if(items.size() == 0)
258 qDebug() <<
"NCLTreeWidget::removeItem Warning! Item with id ='"
259 << itemId <<
"' was not found!";
262 for (
int i = 0; i < items.size(); i++)
265 if (item->parent() != NULL)
267 item->parent()->removeChild(item);
268 qDeleteAll(item->takeChildren());
274 int index = indexOfTopLevelItem(item);
275 QTreeWidgetItem *item = takeTopLevelItem(index);
276 qDebug() <<
"index=" << index <<
"item=" << item;
283 void NCLTreeWidget::userRemoveElement()
285 QList<QTreeWidgetItem*> selecteds = this->selectedItems();
286 QTreeWidgetItem *item = selecteds.at (0);
291 QString
id = item->text(2);
292 QString name = item->text(0);
295 int resp = QMessageBox::question(
297 tr(
"Deleting Element"),
298 tr(
"Do you really want delete the \"%1\" element ?").arg(name),
302 if(resp == QMessageBox::Yes) {
310 QMap <QString, QString> &attrs)
316 if(tagname ==
"media")
320 if(attrs.contains(
"type") && !attrs.values(
"type").empty())
322 type = attrs.value(
"type");
324 else if(attrs.contains(
"src") && !attrs.values(
"src").empty())
326 QString src = attrs.value(
"src");
327 QString ext = src.mid(src.lastIndexOf(
".") + 1);
337 if(ext ==
"png" || ext ==
"jpg" || ext ==
"jpeg" || ext ==
"gif")
341 else if(ext ==
"mp4" || ext ==
"avi" || ext ==
"mpeg4" || ext ==
"mpeg"
342 || ext ==
"mpg" || ext ==
"mov" || ext ==
"ts")
346 else if(ext ==
"mp3" || ext ==
"wav" || ext ==
"ac3" || ext ==
"mpa"
351 else if(ext ==
"htm" || ext ==
"html")
355 else if(ext ==
"ncl")
357 type =
"application/x-ginga-ncl";
359 else if(ext ==
"txt")
363 else if(ext ==
"lua")
365 type =
"application/x-ginga-NCLua";
371 if(type.startsWith(
"audio"))
372 icon = QIcon(
":/icon/audio");
373 else if(type.startsWith(
"image"))
374 icon = QIcon(
":/icon/image");
375 else if(type.startsWith(
"video"))
376 icon = QIcon(
":/icon/video");
377 else if(type.startsWith(
"text/html"))
378 icon = QIcon(
":/icon/html");
379 else if(type.startsWith(
"text"))
380 icon = QIcon(
":/icon/text");
381 else if(type.startsWith(
"application/x-ginga-NCLua"))
382 icon = QIcon(
":/icon/script");
383 else if(type.startsWith(
"application/x-ginga-ncl"))
384 icon = QIcon(
":/icon/ncl");
385 else if(type.startsWith(
"application/x-ncl-settings") ||
386 type.startsWith(
"application/x-ginga-settings"))
387 icon = QIcon(
":/icon/settings");
388 else icon = QIcon (
":/icon/media");
391 icon = QIcon (
":/icon/media");
393 else if(tagname ==
"context" || tagname ==
"body")
394 icon = QIcon (
":/icon/context");
395 else if(tagname ==
"meta" || tagname ==
"metadata")
396 icon = QIcon (
":/icon/metadata");
397 else if(tagname ==
"switch")
398 icon = QIcon (
":/icon/switch");
399 else if(tagname ==
"descriptor")
400 icon = QIcon (
":/icon/descriptor");
401 else if(tagname ==
"link")
402 icon = QIcon (
":/icon/link");
403 else if(tagname ==
"port")
404 icon = QIcon (
":/icon/port");
405 else if(tagname ==
"property")
406 icon = QIcon (
":/icon/property");
408 icon = QIcon (
":/icon/element");
410 QString strAttrList =
"";
413 foreach (key, attrs.keys())
416 strAttrList += key +
"=\"" + attrs[key] +
"\"";
417 if(key ==
"id" || key ==
"name") {
422 item->setIcon(0, icon);
423 item->setText(4, tagname);
427 item->setText(0, tagname +
" (" + name +
")");
428 item->setText(3, name);
431 item->setText(0, tagname);
437 QTreeWidget::expandAll();
452 void NCLTreeWidget::decreaseFont()
454 unsigned int newPointSize = font().pointSize()-1;
455 QFont newFont(font());
456 newFont.setPointSize(newPointSize);
460 void NCLTreeWidget::increaseFont()
462 unsigned int newPointSize = font().pointSize()+1;
463 QFont newFont(font());
464 newFont.setPointSize(newPointSize);
470 setFont(defaultFont);
475 this->defaultFont = defaultFont;
478 void NCLTreeWidget::wheelEvent(QWheelEvent *event)
480 if(event->modifiers() == Qt::ControlModifier){
481 if(event->delta() > 0)
488 QTreeWidget::wheelEvent(event);
492 void NCLTreeWidget::keyPressEvent(QKeyEvent *event)
494 if(event->modifiers() & Qt::ControlModifier)
496 if(event->key() == Qt::Key_Plus)
501 else if(event->key() == Qt::Key_Minus)
506 else if(event->key() == Qt::Key_0)
513 QTreeWidget::keyPressEvent(event);
516 void NCLTreeWidget::expandAll()
520 isExpandedAll =
true;
521 QTreeWidget::expandAll();
523 else isExpandedAll =
false;
545 QTreeWidgetItem *selectedItem = currentItem();
548 if (!(event->buttons() & Qt::LeftButton))
return;
553 QDrag *drag =
new QDrag(
this);
554 QMimeData *mimeData =
new QMimeData;
555 mimeData->setColorData(Qt::green);
556 mimeData->setData(
"nclcomposer/mediaid", currentItem()->text(3).toAscii());
557 mimeData->setData(
"nclcomposer/qnstuid", currentItem()->text(2).toAscii());
559 drag->setMimeData(mimeData);
561 drag->start(Qt::CopyAction | Qt::MoveAction);