summaryrefslogtreecommitdiffstats
path: root/demos/mobile/quickhit/mainwindow.cpp
blob: 6c64c0c15cfab2bdcb9921d0baaaba681d625dbf (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.1, 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "mainwindow.h"
#include "gameengine.h"
#include "plugins/levelplugininterface.h"

#include <QDebug>
#include <QDir>
#include <QMessageBox>
#include <QLibraryInfo>
#include <QDeclarativeEngine>
#include <QDesktopWidget>

MainWindow::MainWindow(QWidget *parent)
    : QDeclarativeView(parent)
{

#ifdef Q_WS_MAEMO_5
    window()->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
#endif

    // Game engine
    m_gameEngine = new GameEngine(this);

    // Load all levels plugins
    m_levelPlugin = 0;
    loadLevelPlugins();

    // QML main window
    engine()->addImportPath("./imports");
    setResizeMode(QDeclarativeView::SizeRootObjectToView);

    // Set game engine visible to QML
    rootContext()->setContextProperty("GameEngine", m_gameEngine);

    // Set QML source
    setSource(QUrl("qrc:/Game.qml"));
    //setSource(QUrl("../QuickHit/Game.qml"));

    // Store QML root object for game engine
    QObject *ro = static_cast<QObject*>(rootObject());
    m_gameEngine->setGameQml(ro);
    m_gameEngine->findQmlObjects();

    // Application foreground / background event filter for filterin incoming call (window)
    // when game will be paused
    m_eventFilter = new MyEventFilter(this);
    QObject::connect(m_eventFilter,SIGNAL(activationChangeFiltered()),this,SLOT(activationChangeFiltered()));
    qApp->installEventFilter(m_eventFilter);

    // Remove context menu from the all widgets
    QWidgetList widgets = QApplication::allWidgets();
    QWidget* w = 0;
    foreach (w,widgets){
        w->setContextMenuPolicy(Qt::NoContextMenu);
    }
}

MainWindow::~MainWindow()
{
    for (int i=0;i<m_plugins.count();i++) {
        m_plugins[i]->unload();
    }
    m_plugins.clear();

}

void MainWindow::activationChangeFiltered()
{
    m_gameEngine->pauseGame();
}

void MainWindow::levelActivated(int index)
{
    // Set level for the game engine
    createPlugin(index);
    rootContext()->setContextProperty("LevelPlugin", m_levelPlugin);
    m_gameEngine->setGameLevel(m_levelPlugin);
}

void MainWindow::loadLevelPlugins()
{
#if defined (Q_OS_SYMBIAN)
    bool exists_c = loadPlugins("c", "quickhitlevels");
    bool exists_e = loadPlugins("e", "quickhitlevels");
    bool exists_f = loadPlugins("f", "quickhitlevels");
    if (exists_c || exists_e || exists_f) {
        m_gameEngine->setPluginList(m_plugins);
        createPlugin();
    }
    else {
        //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
    }
#else
    if (loadPlugins("c", "quickhitlevels")) {
        m_gameEngine->setPluginList(m_plugins);
        createPlugin();
    }
    else {
        //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
    }


#endif
}

bool MainWindow::loadPlugins(QString drive, QString pluginDir)
{
#if defined (Q_OS_SYMBIAN)
    QDir pluginsDir(drive + ":" + QLibraryInfo::location(QLibraryInfo::PluginsPath));
#elif defined Q_OS_WIN32
    QDir pluginsDir = QDir::currentPath();
#else
    QDir pluginsDir(QLibraryInfo::location(QLibraryInfo::PluginsPath));
#endif
    pluginsDir.cd(pluginDir);

    qDebug() << "Loads plugins from : " << pluginsDir.path();

    bool newPluginsLoaded = false;

    foreach (QString fileName, pluginsDir.entryList(QDir::Files))
        {
            // Accept only plugin files
#if defined (Q_OS_SYMBIAN)
            if (fileName.contains(".qtplugin",Qt::CaseInsensitive)) {
#elif defined (Q_WS_MAEMO_5)
            if (fileName.contains(".so",Qt::CaseInsensitive)) {
#else
            if (fileName.contains(".dll",Qt::CaseInsensitive)) {
#endif

                // Create plugin loader
                QPluginLoader* pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
                // Load plugin
                bool ret = pluginLoader->load();
                if (!ret) {
                    // Loading failed
                    qDebug() << "Could not load plugin " << fileName;
                } else {
                    // Loading done
                    // Test creating plugin
                    QObject *plugin = 0;
                    LevelPluginInterface* pluginIF = 0;
                    plugin = pluginLoader->instance();
                    pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
                    if (pluginIF) {
                        qDebug() << "Plugin can be created";
                        // Store loader to array
                        m_plugins.append(pluginLoader);
                        newPluginsLoaded = true;
                    } else {
                        pluginLoader->unload();
                        qDebug() << "Plugin can NOT be created!";
                    }
                }
            }
        }

    return newPluginsLoaded;
}


void MainWindow::createPlugin(int index)
{
    if (index == -1) {
        return;
    }

    m_levelPlugin = 0;

    // Try to create plugin instance
    QPluginLoader* pluginLoader = m_plugins[index];
    QObject *plugin = pluginLoader->instance();
    if (plugin) {
        // Plugin instance created
        // Cast plugin to LevelPluginInterface, that is common for all plugins
        LevelPluginInterface* pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
        m_levelPlugin = pluginIF;
        qDebug() << "Plugin created: " << index;
    }
    else {
        qDebug() << "Creating plugin failed!";
        QMessageBox::information(this, "QuickHit", "Could not create quickhitlevels");
    }
}

void MainWindow::printObjectTree(QObject* parent)
{
   if (parent) {
        qDebug() << "className:" << parent->metaObject()->className();
        qDebug() << "objectName:" << parent->objectName();

        QObjectList list = parent->children();
        QObject* item;
        foreach (item,list) {
            if (item->children().count()>0) {
                qDebug() << "--Childrens found--";
                printObjectTree(item);
            } else {
                qDebug() << "className:" << item->metaObject()->className();
                qDebug() << "objectName:" << item->objectName();
            }
        }
    } else {
        qDebug() << "object NULL";
    }

}