/**************************************************************************** ** ** Copyright (C) 2008-$THISYEAR$ $TROLLTECH$. All rights reserved. ** ** This file is part of the SCXML project on Trolltech Labs. ** ** $TROLLTECH_GPL_LICENSE$ ** ****************************************************************************/ #include "qscxmlgui.h" #include #include #include #include #include #include /* { "parent" : parentObject, "trackHovers" : true/false "children": {{"type": "action", "text": "",}, {"type": "menu"}, {"type": "separator"} }, */ namespace { void traverseMenu (QMenu* menu, QScriptValue value, QSignalMapper* clickMap, QSignalMapper* hoverMap, bool trackHover) { QScriptValueIterator it (value); while (it.hasNext()) { it.next(); if (it.name() == "trackHover") { trackHover = it.value().toBoolean(); } else if (it.name() == "parent") { } else if (it.name() == "children") { QScriptValueIterator cit (it.value()); while (cit.hasNext()) { cit.next(); QString type = cit.value().property("type").toString(); if (type == "action") { QAction* act = menu->addAction(""); QScriptValueIterator ait (cit.value()); while (ait.hasNext()) { ait.next(); if (ait.name() != "type") { act->setProperty(ait.name().toAscii().constData(),ait.value().toVariant()); } } QObject::connect(act,SIGNAL(triggered()),clickMap,SLOT(map())); clickMap->setMapping(act,QString("menu.action." + cit.value().property("id").toString())); if (trackHover) { QObject::connect(act,SIGNAL(hovered()),hoverMap,SLOT(map())); hoverMap->setMapping(act,QString("menu.hover." + cit.value().property("id").toString())); } } else if (type == "menu") { traverseMenu(menu->addMenu(""),it.value(),clickMap,hoverMap,trackHover); } else if (type == "separator") { menu->addSeparator(); } } } else { menu->setProperty(it.name().toAscii().constData(),it.value().toVariant()); } } } }; void QScxmlMenuInvoker::activate () { QScxmlEvent* ie = initEvent; QScriptValue v = ie->content(); QWidget* parent = qobject_cast(v.property("parent").toQObject()); menu = new QMenu(parent); QSignalMapper* clickMap = new QSignalMapper(this); QSignalMapper* hoverMap = new QSignalMapper(this); connect (clickMap,SIGNAL(mapped(QString)), this, SLOT(postParentEvent(QString))); connect (hoverMap,SIGNAL(mapped(QString)), this, SLOT(postParentEvent(QString))); traverseMenu(menu,v,clickMap,hoverMap,false); menu->setParent(parent,Qt::Widget); menu->move(QPoint(0,0)); menu->resize(parent->size()); menu->show(); } void QScxmlMenuInvoker::cancel () { if (menu) menu->deleteLater(); QScxmlInvoker::cancel(); } Q_SCRIPT_DECLARE_QMETAOBJECT(QMenu,QWidget*) Q_SCRIPT_DECLARE_QMETAOBJECT(QMessageBox,QWidget*) void QScxmlMenuInvoker::initInvokerFactory(QScxml* sm) { QScriptEngine* se = sm->scriptEngine(); se->globalObject().setProperty("QMenu",qScriptValueFromQMetaObject(se)); } void QScxmlMessageBoxInvoker::initInvokerFactory(QScxml* sm) { QScriptEngine* se = sm->scriptEngine(); se->globalObject().setProperty("QMessageBox",qScriptValueFromQMetaObject(se)); } void QScxmlMessageBoxInvoker::onFinished(int n) { postParentEvent(new QScxmlEvent("q-messagebox.finished",QStringList()<<"result",QVariantList()<content(); QWidget* parent = qobject_cast(v.property("parent").toQObject()); messageBox = new QMessageBox(parent); messageBox->setModal(false); QScriptValueIterator it (v); while (it.hasNext()) { it.next(); if (it.name() == "standardButtons") { messageBox->setStandardButtons((QMessageBox::StandardButtons)it.value().toInt32()); } else if (it.name() == "icon") { messageBox->setIcon((QMessageBox::Icon)it.value().toInt32()); } else if (it.name() != "parent") { messageBox->setProperty(it.name().toAscii().constData(),it.value().toVariant()); } } connect(messageBox,SIGNAL(finished(int)),this,SLOT(onFinished(int))); messageBox->show (); } void QScxmlMessageBoxInvoker::cancel() { messageBox->deleteLater(); QScxmlInvoker::cancel(); }