123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- /*
- * Copyright (c) 2011 Nokia Corporation.
- */
- #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";
- }
- }
|