#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
m_gameEngine = new GameEngine(this);
m_levelPlugin = 0;
loadLevelPlugins();
engine()->addImportPath("./imports");
setResizeMode(QDeclarativeView::SizeRootObjectToView);
rootContext()->setContextProperty("GameEngine", m_gameEngine);
setSource(QUrl("qrc:/Game.qml"));
QObject *ro = static_cast<QObject*>(rootObject());
m_gameEngine->setGameQml(ro);
m_gameEngine->findQmlObjects();
m_eventFilter = new MyEventFilter(this);
QObject::connect(m_eventFilter,SIGNAL(activationChangeFiltered()),this,SLOT(activationChangeFiltered()));
qApp->installEventFilter(m_eventFilter);
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)
{
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 {
}
#else
if (loadPlugins("c", "quickhitlevels")) {
m_gameEngine->setPluginList(m_plugins);
createPlugin();
}
else {
}
#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))
{
#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
QPluginLoader* pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
bool ret = pluginLoader->load();
if (!ret) {
qDebug() << "Could not load plugin " << fileName;
} else {
QObject *plugin = 0;
LevelPluginInterface* pluginIF = 0;
plugin = pluginLoader->instance();
pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
if (pluginIF) {
qDebug() << "Plugin can be created";
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;
QPluginLoader* pluginLoader = m_plugins[index];
QObject *plugin = pluginLoader->instance();
if (plugin) {
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";
}
}