1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "mainwindow.h"
42 #include "gameengine.h"
43 #include "plugins/levelplugininterface.h"
47 #include <QMessageBox>
48 #include <QLibraryInfo>
49 #include <QDeclarativeEngine>
50 #include <QDesktopWidget>
52 MainWindow::MainWindow(QWidget *parent)
53 : QDeclarativeView(parent)
57 window()->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
61 m_gameEngine = new GameEngine(this);
63 // Load all levels plugins
68 engine()->addImportPath("./imports");
69 setResizeMode(QDeclarativeView::SizeRootObjectToView);
71 // Set game engine visible to QML
72 rootContext()->setContextProperty("GameEngine", m_gameEngine);
75 setSource(QUrl("qrc:/Game.qml"));
76 //setSource(QUrl("../QuickHit/Game.qml"));
78 // Store QML root object for game engine
79 QObject *ro = static_cast<QObject*>(rootObject());
80 m_gameEngine->setGameQml(ro);
81 m_gameEngine->findQmlObjects();
83 // Application foreground / background event filter for filterin incoming call (window)
84 // when game will be paused
85 m_eventFilter = new MyEventFilter(this);
86 QObject::connect(m_eventFilter,SIGNAL(activationChangeFiltered()),this,SLOT(activationChangeFiltered()));
87 qApp->installEventFilter(m_eventFilter);
89 // Remove context menu from the all widgets
90 QWidgetList widgets = QApplication::allWidgets();
93 w->setContextMenuPolicy(Qt::NoContextMenu);
97 MainWindow::~MainWindow()
99 for (int i=0;i<m_plugins.count();i++) {
100 m_plugins[i]->unload();
106 void MainWindow::activationChangeFiltered()
108 m_gameEngine->pauseGame();
111 void MainWindow::levelActivated(int index)
113 // Set level for the game engine
115 rootContext()->setContextProperty("LevelPlugin", m_levelPlugin);
116 m_gameEngine->setGameLevel(m_levelPlugin);
119 void MainWindow::loadLevelPlugins()
121 #if defined (Q_OS_SYMBIAN)
122 bool exists_c = loadPlugins("c", "quickhitlevels");
123 bool exists_e = loadPlugins("e", "quickhitlevels");
124 bool exists_f = loadPlugins("f", "quickhitlevels");
125 if (exists_c || exists_e || exists_f) {
126 m_gameEngine->setPluginList(m_plugins);
130 //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
133 if (loadPlugins("c", "quickhitlevels")) {
134 m_gameEngine->setPluginList(m_plugins);
138 //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
145 bool MainWindow::loadPlugins(QString drive, QString pluginDir)
147 #if defined (Q_OS_SYMBIAN)
148 QDir pluginsDir(drive + ":" + QLibraryInfo::location(QLibraryInfo::PluginsPath));
149 #elif defined Q_OS_WIN32
150 QDir pluginsDir = QDir::currentPath();
152 QDir pluginsDir(QLibraryInfo::location(QLibraryInfo::PluginsPath));
154 pluginsDir.cd(pluginDir);
156 qDebug() << "Loads plugins from : " << pluginsDir.path();
158 bool newPluginsLoaded = false;
160 foreach (QString fileName, pluginsDir.entryList(QDir::Files))
162 // Accept only plugin files
163 #if defined (Q_OS_SYMBIAN)
164 if (fileName.contains(".qtplugin",Qt::CaseInsensitive)) {
165 #elif defined (Q_WS_MAEMO_5)
166 if (fileName.contains(".so",Qt::CaseInsensitive)) {
168 if (fileName.contains(".dll",Qt::CaseInsensitive)) {
171 // Create plugin loader
172 QPluginLoader* pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
174 bool ret = pluginLoader->load();
177 qDebug() << "Could not load plugin " << fileName;
180 // Test creating plugin
182 LevelPluginInterface* pluginIF = 0;
183 plugin = pluginLoader->instance();
184 pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
186 qDebug() << "Plugin can be created";
187 // Store loader to array
188 m_plugins.append(pluginLoader);
189 newPluginsLoaded = true;
191 pluginLoader->unload();
192 qDebug() << "Plugin can NOT be created!";
198 return newPluginsLoaded;
202 void MainWindow::createPlugin(int index)
210 // Try to create plugin instance
211 QPluginLoader* pluginLoader = m_plugins[index];
212 QObject *plugin = pluginLoader->instance();
214 // Plugin instance created
215 // Cast plugin to LevelPluginInterface, that is common for all plugins
216 LevelPluginInterface* pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
217 m_levelPlugin = pluginIF;
218 qDebug() << "Plugin created: " << index;
221 qDebug() << "Creating plugin failed!";
222 QMessageBox::information(this, "QuickHit", "Could not create quickhitlevels");
226 void MainWindow::printObjectTree(QObject* parent)
229 qDebug() << "className:" << parent->metaObject()->className();
230 qDebug() << "objectName:" << parent->objectName();
232 QObjectList list = parent->children();
234 foreach (item,list) {
235 if (item->children().count()>0) {
236 qDebug() << "--Childrens found--";
237 printObjectTree(item);
239 qDebug() << "className:" << item->metaObject()->className();
240 qDebug() << "objectName:" << item->objectName();
244 qDebug() << "object NULL";