| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** All rights reserved. |
| 5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 |
** |
| 7 |
** This file is part of SCXML on Qt labs |
| 8 |
** |
| 9 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 10 |
** No Commercial Usage |
| 11 |
** This file contains pre-release code and may not be distributed. |
| 12 |
** You may use this file in accordance with the terms and conditions |
| 13 |
** contained in the Technology Preview License Agreement accompanying |
| 14 |
** this package. |
| 15 |
** |
| 16 |
** GNU Lesser General Public License Usage |
| 17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
| 18 |
** General Public License version 2.1 as published by the Free Software |
| 19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
| 20 |
** packaging of this file. Please review the following information to |
| 21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
| 22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 23 |
** |
| 24 |
** In addition, as a special exception, Nokia gives you certain additional |
| 25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 27 |
** |
| 28 |
** If you have questions regarding the use of this file, please contact |
| 29 |
** Nokia at qt-info@nokia.com. |
| 30 |
** |
| 31 |
** |
| 32 |
** |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
#include "qscxmlgui.h" |
| 43 |
#include <QMenu> |
| 44 |
#include <QDebug> |
| 45 |
#include <QMessageBox> |
| 46 |
#include <QScriptValueIterator> |
| 47 |
#include <QScriptEngine> |
| 48 |
#include <QSignalMapper> |
| 49 |
/* |
| 50 |
|
| 51 |
{ "parent" : parentObject, |
| 52 |
"trackHovers" : true/false |
| 53 |
"children": {{"type": "action", "text": "",}, |
| 54 |
{"type": "menu"}, |
| 55 |
{"type": "separator"} }, |
| 56 |
*/ |
| 57 |
namespace |
| 58 |
{ |
| 59 |
void traverseMenu (QMenu* menu, QScriptValue value, QSignalMapper* clickMap, QSignalMapper* hoverMap, bool trackHover) |
| 60 |
{ |
| 61 |
QScriptValueIterator it (value); |
| 62 |
while (it.hasNext()) { |
| 63 |
it.next(); |
| 64 |
if (it.name() == "trackHover") { |
| 65 |
trackHover = it.value().toBoolean(); |
| 66 |
} else if (it.name() == "parent") { |
| 67 |
} else if (it.name() == "children") { |
| 68 |
QScriptValueIterator cit (it.value()); |
| 69 |
while (cit.hasNext()) { |
| 70 |
cit.next(); |
| 71 |
QString type = cit.value().property("type").toString(); |
| 72 |
if (type == "action") { |
| 73 |
QAction* act = menu->addAction(""); |
| 74 |
QScriptValueIterator ait (cit.value()); |
| 75 |
while (ait.hasNext()) { |
| 76 |
ait.next(); |
| 77 |
if (ait.name() != "type") { |
| 78 |
act->setProperty(ait.name().toAscii().constData(),ait.value().toVariant()); |
| 79 |
} |
| 80 |
} |
| 81 |
QObject::connect(act,SIGNAL(triggered()),clickMap,SLOT(map())); |
| 82 |
clickMap->setMapping(act,QString("menu.action." + cit.value().property("id").toString())); |
| 83 |
if (trackHover) { |
| 84 |
QObject::connect(act,SIGNAL(hovered()),hoverMap,SLOT(map())); |
| 85 |
hoverMap->setMapping(act,QString("menu.hover." + cit.value().property("id").toString())); |
| 86 |
} |
| 87 |
} else if (type == "menu") { |
| 88 |
traverseMenu(menu->addMenu(""),it.value(),clickMap,hoverMap,trackHover); |
| 89 |
} else if (type == "separator") { |
| 90 |
menu->addSeparator(); |
| 91 |
} |
| 92 |
} |
| 93 |
} else { |
| 94 |
menu->setProperty(it.name().toAscii().constData(),it.value().toVariant()); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
void QScxmlMenuInvoker::activate () |
| 101 |
{ |
| 102 |
QScxmlEvent* ie = initEvent; |
| 103 |
QScriptValue v = ie->content(); |
| 104 |
QWidget* parent = qobject_cast<QWidget*>(v.property("parent").toQObject()); |
| 105 |
menu = new QMenu(parent); |
| 106 |
QSignalMapper* clickMap = new QSignalMapper(this); |
| 107 |
QSignalMapper* hoverMap = new QSignalMapper(this); |
| 108 |
connect (clickMap,SIGNAL(mapped(QString)), this, SLOT(postParentEvent(QString))); |
| 109 |
connect (hoverMap,SIGNAL(mapped(QString)), this, SLOT(postParentEvent(QString))); |
| 110 |
traverseMenu(menu,v,clickMap,hoverMap,false); |
| 111 |
menu->setParent(parent,Qt::Widget); |
| 112 |
menu->move(QPoint(0,0)); |
| 113 |
menu->resize(parent->size()); |
| 114 |
menu->show(); |
| 115 |
} |
| 116 |
void QScxmlMenuInvoker::cancel () |
| 117 |
{ |
| 118 |
if (menu) |
| 119 |
menu->deleteLater(); |
| 120 |
QScxmlInvoker::cancel(); |
| 121 |
} |
| 122 |
|
| 123 |
Q_SCRIPT_DECLARE_QMETAOBJECT(QMenu,QWidget*) |
| 124 |
Q_SCRIPT_DECLARE_QMETAOBJECT(QMessageBox,QWidget*) |
| 125 |
void QScxmlMenuInvoker::initInvokerFactory(QScxml* sm) |
| 126 |
{ |
| 127 |
QScriptEngine* se = sm->scriptEngine(); |
| 128 |
se->globalObject().setProperty("QMenu",qScriptValueFromQMetaObject<QMenu>(se)); |
| 129 |
} |
| 130 |
void QScxmlMessageBoxInvoker::initInvokerFactory(QScxml* sm) |
| 131 |
{ |
| 132 |
QScriptEngine* se = sm->scriptEngine(); |
| 133 |
se->globalObject().setProperty("QMessageBox",qScriptValueFromQMetaObject<QMessageBox>(se)); |
| 134 |
} |
| 135 |
|
| 136 |
void QScxmlMessageBoxInvoker::onFinished(int n) { |
| 137 |
postParentEvent(new QScxmlEvent("q-messagebox.finished",QStringList()<<"result",QVariantList()<<QVariant(n))); |
| 138 |
} |
| 139 |
/* |
| 140 |
{ "parent": someWidget, "buttons": ...} |
| 141 |
*/ |
| 142 |
void QScxmlMessageBoxInvoker::activate() |
| 143 |
{ |
| 144 |
QScriptValue v = initEvent->content(); |
| 145 |
QWidget* parent = qobject_cast<QWidget*>(v.property("parent").toQObject()); |
| 146 |
messageBox = new QMessageBox(parent); |
| 147 |
messageBox->setModal(false); |
| 148 |
QScriptValueIterator it (v); |
| 149 |
while (it.hasNext()) { |
| 150 |
it.next(); |
| 151 |
if (it.name() == "standardButtons") { |
| 152 |
messageBox->setStandardButtons((QMessageBox::StandardButtons)it.value().toInt32()); |
| 153 |
} else if (it.name() == "icon") { |
| 154 |
messageBox->setIcon((QMessageBox::Icon)it.value().toInt32()); |
| 155 |
} else if (it.name() != "parent") { |
| 156 |
messageBox->setProperty(it.name().toAscii().constData(),it.value().toVariant()); |
| 157 |
} |
| 158 |
} |
| 159 |
connect(messageBox,SIGNAL(finished(int)),this,SLOT(onFinished(int))); |
| 160 |
messageBox->show (); |
| 161 |
} |
| 162 |
|
| 163 |
void QScxmlMessageBoxInvoker::cancel() |
| 164 |
{ |
| 165 |
messageBox->deleteLater(); |
| 166 |
QScxmlInvoker::cancel(); |
| 167 |
} |