Update copyright headers
[qt:qt.git] / demos / mobile / quickhit / mainwindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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.
23 **
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "mainwindow.h"
42 #include "gameengine.h"
43 #include "plugins/levelplugininterface.h"
44
45 #include <QDebug>
46 #include <QDir>
47 #include <QMessageBox>
48 #include <QLibraryInfo>
49 #include <QDeclarativeEngine>
50 #include <QDesktopWidget>
51
52 MainWindow::MainWindow(QWidget *parent)
53     : QDeclarativeView(parent)
54 {
55
56 #ifdef Q_WS_MAEMO_5
57     window()->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
58 #endif
59
60     // Game engine
61     m_gameEngine = new GameEngine(this);
62
63     // Load all levels plugins
64     m_levelPlugin = 0;
65     loadLevelPlugins();
66
67     // QML main window
68     engine()->addImportPath("./imports");
69     setResizeMode(QDeclarativeView::SizeRootObjectToView);
70
71     // Set game engine visible to QML
72     rootContext()->setContextProperty("GameEngine", m_gameEngine);
73
74     // Set QML source
75     setSource(QUrl("qrc:/Game.qml"));
76     //setSource(QUrl("../QuickHit/Game.qml"));
77
78     // Store QML root object for game engine
79     QObject *ro = static_cast<QObject*>(rootObject());
80     m_gameEngine->setGameQml(ro);
81     m_gameEngine->findQmlObjects();
82
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);
88
89     // Remove context menu from the all widgets
90     QWidgetList widgets = QApplication::allWidgets();
91     QWidget* w = 0;
92     foreach (w,widgets){
93         w->setContextMenuPolicy(Qt::NoContextMenu);
94     }
95 }
96
97 MainWindow::~MainWindow()
98 {
99     for (int i=0;i<m_plugins.count();i++) {
100         m_plugins[i]->unload();
101     }
102     m_plugins.clear();
103
104 }
105
106 void MainWindow::activationChangeFiltered()
107 {
108     m_gameEngine->pauseGame();
109 }
110
111 void MainWindow::levelActivated(int index)
112 {
113     // Set level for the game engine
114     createPlugin(index);
115     rootContext()->setContextProperty("LevelPlugin", m_levelPlugin);
116     m_gameEngine->setGameLevel(m_levelPlugin);
117 }
118
119 void MainWindow::loadLevelPlugins()
120 {
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);
127         createPlugin();
128     }
129     else {
130         //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
131     }
132 #else
133     if (loadPlugins("c", "quickhitlevels")) {
134         m_gameEngine->setPluginList(m_plugins);
135         createPlugin();
136     }
137     else {
138         //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
139     }
140
141
142 #endif
143 }
144
145 bool MainWindow::loadPlugins(QString drive, QString pluginDir)
146 {
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();
151 #else
152     QDir pluginsDir(QLibraryInfo::location(QLibraryInfo::PluginsPath));
153 #endif
154     pluginsDir.cd(pluginDir);
155
156     qDebug() << "Loads plugins from : " << pluginsDir.path();
157
158     bool newPluginsLoaded = false;
159
160     foreach (QString fileName, pluginsDir.entryList(QDir::Files))
161         {
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)) {
167 #else
168             if (fileName.contains(".dll",Qt::CaseInsensitive)) {
169 #endif
170
171                 // Create plugin loader
172                 QPluginLoader* pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
173                 // Load plugin
174                 bool ret = pluginLoader->load();
175                 if (!ret) {
176                     // Loading failed
177                     qDebug() << "Could not load plugin " << fileName;
178                 } else {
179                     // Loading done
180                     // Test creating plugin
181                     QObject *plugin = 0;
182                     LevelPluginInterface* pluginIF = 0;
183                     plugin = pluginLoader->instance();
184                     pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
185                     if (pluginIF) {
186                         qDebug() << "Plugin can be created";
187                         // Store loader to array
188                         m_plugins.append(pluginLoader);
189                         newPluginsLoaded = true;
190                     } else {
191                         pluginLoader->unload();
192                         qDebug() << "Plugin can NOT be created!";
193                     }
194                 }
195             }
196         }
197
198     return newPluginsLoaded;
199 }
200
201
202 void MainWindow::createPlugin(int index)
203 {
204     if (index == -1) {
205         return;
206     }
207
208     m_levelPlugin = 0;
209
210     // Try to create plugin instance
211     QPluginLoader* pluginLoader = m_plugins[index];
212     QObject *plugin = pluginLoader->instance();
213     if (plugin) {
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;
219     }
220     else {
221         qDebug() << "Creating plugin failed!";
222         QMessageBox::information(this, "QuickHit", "Could not create quickhitlevels");
223     }
224 }
225
226 void MainWindow::printObjectTree(QObject* parent)
227 {
228    if (parent) {
229         qDebug() << "className:" << parent->metaObject()->className();
230         qDebug() << "objectName:" << parent->objectName();
231
232         QObjectList list = parent->children();
233         QObject* item;
234         foreach (item,list) {
235             if (item->children().count()>0) {
236                 qDebug() << "--Childrens found--";
237                 printObjectTree(item);
238             } else {
239                 qDebug() << "className:" << item->metaObject()->className();
240                 qDebug() << "objectName:" << item->objectName();
241             }
242         }
243     } else {
244         qDebug() << "object NULL";
245     }
246
247 }
248