~openhommdev/openhomm/gui

« back to all changes in this revision

Viewing changes to src/gui/hrUI.cpp

  • Committer: Roman Fomin
  • Date: 2010-02-19 13:39:11 UTC
  • Revision ID: rfomin@gmail.com-20100219133911-hx8u6wxar0m7dtvz
missing files added

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// openhomm - open source clone of Heroes of Might and Magic III
 
2
// Copyright (C) 2009-2010 openhomm developers team (see AUTHORS)
 
3
//
 
4
// This program is free software: you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation, either version 3 of the License, or
 
7
// (at your option) any later version.
 
8
//
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
//
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
//
 
17
#include "precompiled.hpp"
 
18
#include "hrUI.hpp"
 
19
 
 
20
Q_SCRIPT_DECLARE_QMETAOBJECT(hrPushButton, QObject*)
 
21
 
 
22
hrUI::hrUI(QObject *parent) : QObject(parent)
 
23
{
 
24
    QScriptValue button = engine.scriptValueFromQMetaObject<hrPushButton>();
 
25
    engine.globalObject().setProperty("Button", button);
 
26
 
 
27
    addGlobalObject(this, "UI");
 
28
}
 
29
 
 
30
void hrUI::addGlobalObject(QObject *obj, QString name)
 
31
{
 
32
    QScriptValue val = engine.newQObject(obj);
 
33
    engine.globalObject().setProperty(name, val);
 
34
}
 
35
 
 
36
void hrUI::addWidget(hrWidget *widget)
 
37
{
 
38
    qDebug() << "add widget" << widget->getX() << widget->getY()
 
39
             << "name" << dynamic_cast<hrPushButton*>(widget)->getName();
 
40
    widgets.append(widget);
 
41
}
 
42
 
 
43
void hrUI::onMousePress(const QPoint &p)
 
44
{
 
45
    for (int i = 0; i < widgets.size(); i++)
 
46
    {
 
47
        hrWidget *widget = widgets.at(i);
 
48
        if (widget->getRect().contains(p))
 
49
        {
 
50
            hrPushButton *button  = dynamic_cast<hrPushButton*>(widget);
 
51
            if (button)
 
52
            {
 
53
                button->onMousePress();
 
54
            }
 
55
        }
 
56
    }
 
57
}
 
58
 
 
59
void hrUI::onMouseRelease(const QPoint &p)
 
60
{
 
61
    for (int i = 0; i < widgets.size(); i++)
 
62
    {
 
63
        hrWidget *widget = widgets.at(i);
 
64
        if (widget->getRect().contains(p))
 
65
        {
 
66
            hrPushButton *button  = dynamic_cast<hrPushButton*>(widget);
 
67
            if (button)
 
68
            {
 
69
                button->onMouseRelease();
 
70
            }
 
71
        }
 
72
    }
 
73
 
 
74
}
 
75
 
 
76
QListIterator<hrGraphicsItem> hrUI::getItems()
 
77
{
 
78
    collect();
 
79
    return QListIterator<hrGraphicsItem>(items);
 
80
}
 
81
 
 
82
void hrUI::collect()
 
83
{
 
84
    items.clear();
 
85
    for (int i = 0; i < widgets.size(); i++)
 
86
    {
 
87
        hrWidget *widget = widgets.at(i);
 
88
        QVectorIterator<hrGraphicsItem> it = widget->getItems();
 
89
        while (it.hasNext())
 
90
        {
 
91
            hrGraphicsItem item = it.next();
 
92
            item.setPoint(widget->getRect().topLeft());
 
93
            items.append(item);
 
94
        }
 
95
    }
 
96
}
 
97
 
 
98
void hrUI::script(QString path)
 
99
{
 
100
    QFile file(path);
 
101
    file.open(QIODevice::ReadOnly);
 
102
    if (file.error())
 
103
    {
 
104
        qDebug() << file.errorString();
 
105
    }
 
106
    QTextStream stream(&file);
 
107
    QString contents = stream.readAll();
 
108
    file.close();
 
109
 
 
110
    widgets.clear();
 
111
 
 
112
    QScriptValue result = engine.evaluate(contents, path);
 
113
 
 
114
    QScriptValueIterator it(result);
 
115
    while (it.hasNext())
 
116
    {
 
117
        it.next();
 
118
        qDebug() << it.name() << ": " << it.value().toString();
 
119
    }
 
120
}