summaryrefslogtreecommitdiffstats
path: root/src/qscxmlgui.cpp
blob: 387ef6729cb68d105a674d25f0bef092eab4545d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the SCXML project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qscxmlgui.h"
#include <QMenu>
#include <QDebug>
#include <QMessageBox>
#include <QScriptValueIterator>
#include <QScriptEngine>
#include <QSignalMapper>
/*

  { "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 ()
{
    qDebug() << "Activating menu";
    QScxmlEvent* ie = initEvent;
    QScriptValue v = ie->content();
    QWidget* parent = qobject_cast<QWidget*>(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 ()
{
    qDebug() << "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<QMenu>(se));
 }
 void QScxmlMessageBoxInvoker::initInvokerFactory(QScxml* sm)
 {
     QScriptEngine* se = sm->scriptEngine();
    se->globalObject().setProperty("QMessageBox",qScriptValueFromQMetaObject<QMessageBox>(se));
 }

void QScxmlMessageBoxInvoker::onFinished(int n) {
    postParentEvent(new QScxmlEvent("q-messagebox.finished",QStringList()<<"result",QVariantList()<<QVariant(n)));
}
/*
    { "parent": someWidget, "buttons": ...}
  */
void QScxmlMessageBoxInvoker::activate()
{
    QScriptValue v = initEvent->content();
    QWidget* parent = qobject_cast<QWidget*>(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();
}