mainwindow.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include "mainwindow.h"
  5. #include "gameengine.h"
  6. #include "plugins/levelplugininterface.h"
  7. #include <QDebug>
  8. #include <QDir>
  9. #include <QMessageBox>
  10. #include <QLibraryInfo>
  11. #include <QDeclarativeEngine>
  12. #include <QDesktopWidget>
  13. MainWindow::MainWindow(QWidget *parent)
  14. : QDeclarativeView(parent)
  15. {
  16. #ifdef Q_WS_MAEMO_5
  17. window()->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
  18. #endif
  19. // Game engine
  20. m_gameEngine = new GameEngine(this);
  21. // Load all levels plugins
  22. m_levelPlugin = 0;
  23. loadLevelPlugins();
  24. // QML main window
  25. engine()->addImportPath("./imports");
  26. setResizeMode(QDeclarativeView::SizeRootObjectToView);
  27. // Set game engine visible to QML
  28. rootContext()->setContextProperty("GameEngine", m_gameEngine);
  29. // Set QML source
  30. setSource(QUrl("qrc:/Game.qml"));
  31. //setSource(QUrl("../QuickHit/Game.qml"));
  32. // Store QML root object for game engine
  33. QObject *ro = static_cast<QObject*>(rootObject());
  34. m_gameEngine->setGameQml(ro);
  35. m_gameEngine->findQmlObjects();
  36. // Application foreground / background event filter for filterin incoming call (window)
  37. // when game will be paused
  38. m_eventFilter = new MyEventFilter(this);
  39. QObject::connect(m_eventFilter,SIGNAL(activationChangeFiltered()),this,SLOT(activationChangeFiltered()));
  40. qApp->installEventFilter(m_eventFilter);
  41. // Remove context menu from the all widgets
  42. QWidgetList widgets = QApplication::allWidgets();
  43. QWidget* w = 0;
  44. foreach(w,widgets){
  45. w->setContextMenuPolicy(Qt::NoContextMenu);
  46. }
  47. }
  48. MainWindow::~MainWindow()
  49. {
  50. for (int i=0;i<m_plugins.count();i++) {
  51. m_plugins[i]->unload();
  52. }
  53. m_plugins.clear();
  54. }
  55. void MainWindow::activationChangeFiltered()
  56. {
  57. m_gameEngine->pauseGame();
  58. }
  59. void MainWindow::levelActivated(int index)
  60. {
  61. // Set level for the game engine
  62. createPlugin(index);
  63. rootContext()->setContextProperty("LevelPlugin", m_levelPlugin);
  64. m_gameEngine->setGameLevel(m_levelPlugin);
  65. }
  66. void MainWindow::loadLevelPlugins()
  67. {
  68. #if defined (Q_OS_SYMBIAN)
  69. bool exists_c = loadPlugins("c", "quickhitlevels");
  70. bool exists_e = loadPlugins("e", "quickhitlevels");
  71. bool exists_f = loadPlugins("f", "quickhitlevels");
  72. if (exists_c || exists_e || exists_f) {
  73. m_gameEngine->setPluginList(m_plugins);
  74. createPlugin();
  75. }
  76. else {
  77. //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
  78. }
  79. #else
  80. if (loadPlugins("c", "quickhitlevels")) {
  81. m_gameEngine->setPluginList(m_plugins);
  82. createPlugin();
  83. }
  84. else {
  85. //QMessageBox::information(this, "QuickHit", "Could not load any of the quickhitlevels");
  86. }
  87. #endif
  88. }
  89. bool MainWindow::loadPlugins(QString drive, QString pluginDir)
  90. {
  91. #if defined (Q_OS_SYMBIAN)
  92. QDir pluginsDir(drive + ":" + QLibraryInfo::location(QLibraryInfo::PluginsPath));
  93. #elif defined Q_OS_WIN32
  94. QDir pluginsDir = QDir::currentPath();
  95. #else
  96. QDir pluginsDir(QLibraryInfo::location(QLibraryInfo::PluginsPath));
  97. #endif
  98. pluginsDir.cd(pluginDir);
  99. qDebug() << "Loads plugins from : " << pluginsDir.path();
  100. bool newPluginsLoaded = false;
  101. foreach (QString fileName, pluginsDir.entryList(QDir::Files))
  102. {
  103. // Accept only plugin files
  104. #if defined (Q_OS_SYMBIAN)
  105. if (fileName.contains(".qtplugin",Qt::CaseInsensitive)) {
  106. #elif defined (Q_WS_MAEMO_5)
  107. if (fileName.contains(".so",Qt::CaseInsensitive)) {
  108. #else
  109. if (fileName.contains(".dll",Qt::CaseInsensitive)) {
  110. #endif
  111. // Create plugin loader
  112. QPluginLoader* pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
  113. // Load plugin
  114. bool ret = pluginLoader->load();
  115. if (!ret) {
  116. // Loading failed
  117. qDebug() << "Could not load plugin " << fileName;
  118. } else {
  119. // Loading done
  120. // Test creating plugin
  121. QObject *plugin = 0;
  122. LevelPluginInterface* pluginIF = 0;
  123. plugin = pluginLoader->instance();
  124. pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
  125. if (pluginIF) {
  126. qDebug() << "Plugin can be created";
  127. // Store loader to array
  128. m_plugins.append(pluginLoader);
  129. newPluginsLoaded = true;
  130. } else {
  131. pluginLoader->unload();
  132. qDebug() << "Plugin can NOT be created!";
  133. }
  134. }
  135. }
  136. }
  137. return newPluginsLoaded;
  138. }
  139. void MainWindow::createPlugin(int index)
  140. {
  141. if (index == -1) {
  142. return;
  143. }
  144. m_levelPlugin = 0;
  145. // Try to create plugin instance
  146. QPluginLoader* pluginLoader = m_plugins[index];
  147. QObject *plugin = pluginLoader->instance();
  148. if (plugin) {
  149. // Plugin instance created
  150. // Cast plugin to LevelPluginInterface, that is common for all plugins
  151. LevelPluginInterface* pluginIF = qobject_cast<LevelPluginInterface*> (plugin);
  152. m_levelPlugin = pluginIF;
  153. qDebug() << "Plugin created: " << index;
  154. }
  155. else {
  156. qDebug() << "Creating plugin failed!";
  157. QMessageBox::information(this, "QuickHit", "Could not create quickhitlevels");
  158. }
  159. }
  160. void MainWindow::printObjectTree(QObject* parent)
  161. {
  162. if (parent) {
  163. qDebug() << "className:" << parent->metaObject()->className();
  164. qDebug() << "objectName:" << parent->objectName();
  165. QObjectList list = parent->children();
  166. QObject* item;
  167. foreach(item,list) {
  168. if (item->children().count()>0) {
  169. qDebug() << "--Childrens found--";
  170. printObjectTree(item);
  171. } else {
  172. qDebug() << "className:" << item->metaObject()->className();
  173. qDebug() << "objectName:" << item->objectName();
  174. }
  175. }
  176. } else {
  177. qDebug() << "object NULL";
  178. }
  179. }