summaryrefslogtreecommitdiffstats
path: root/src/qscxmlgui.cpp
blob: cb6745fbb5d445eef0f6a26fd111a6a40a740137 (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
/****************************************************************************
**
** 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 <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 ()
{
    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 ()
{
    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();
}