summaryrefslogtreecommitdiffstats
path: root/src/qscxmlgui.cpp
blob: ca27692d0ca4a1bc95e0298c1474f60d1dce4fed (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/****************************************************************************
**
** This file is part of a Qt Solutions component.
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Solutions Commercial License Agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 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 the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** Please note Third Party Software included with Qt Solutions may impose
** additional restrictions and it is the user's responsibility to ensure
** that they have met the licensing requirements of the GPL, LGPL, or Qt
** Solutions Commercial license and the relevant license of the Third
** Party Software they are using.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
****************************************************************************/
#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();
}