summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-11 14:19:27 (GMT)
committerEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-11 14:19:27 (GMT)
commit65165a0a3c32843486ebd8901b6cb825d9403766 (patch)
tree1c09f5c806ca60601bab268dde14f2a1ad04af06 /examples
parentb2fc9501ad9c84aeb48d45faf42bc38783bc339c (diff)
downloadQt-65165a0a3c32843486ebd8901b6cb825d9403766.zip
Qt-65165a0a3c32843486ebd8901b6cb825d9403766.tar.gz
Qt-65165a0a3c32843486ebd8901b6cb825d9403766.tar.bz2
Make selecting plugins more user friendly and platform independent
Using *.dll was just a placeholder here. The code is mostly copied from the Plug&Paint example, and goes through all the plugins to find the compatible ones with some hacks to find out the application directory on windows and mac.
Diffstat (limited to 'examples')
-rw-r--r--examples/statemachine/errorstate/mainwindow.cpp76
-rw-r--r--examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h2
-rw-r--r--examples/statemachine/errorstateplugins/seek_ai/seek_ai.h2
-rw-r--r--examples/statemachine/errorstateplugins/spin_ai/spin_ai.h2
-rw-r--r--examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h2
5 files changed, 66 insertions, 18 deletions
diff --git a/examples/statemachine/errorstate/mainwindow.cpp b/examples/statemachine/errorstate/mainwindow.cpp
index 39b8663..07719bc 100644
--- a/examples/statemachine/errorstate/mainwindow.cpp
+++ b/examples/statemachine/errorstate/mainwindow.cpp
@@ -12,6 +12,9 @@
#include <QTimer>
#include <QFileDialog>
#include <QPluginLoader>
+#include <QApplication>
+#include <QInputDialog>
+#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), m_scene(0), m_machine(0), m_runningState(0), m_started(false)
@@ -193,25 +196,62 @@ void MainWindow::addTank()
{
Q_ASSERT(!m_spawns.isEmpty());
- QString fileName = QFileDialog::getOpenFileName(this, "Select plugin file", "plugins/", "*.dll");
- QPluginLoader loader(fileName);
+ QDir pluginsDir(qApp->applicationDirPath());
+#if defined(Q_OS_WIN)
+ if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
+ pluginsDir.cdUp();
+#elif defined(Q_OS_MAC)
+ if (pluginsDir.dirName() == "MacOS") {
+ pluginsDir.cdUp();
+ pluginsDir.cdUp();
+ pluginsDir.cdUp();
+ }
+#endif
+
+ pluginsDir.cd("plugins");
+
+ QStringList itemNames;
+ QList<Plugin *> items;
+ foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
+ QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
+ QObject *possiblePlugin = loader.instance();
+ if (Plugin *plugin = qobject_cast<Plugin *>(possiblePlugin)) {
+ QString objectName = possiblePlugin->objectName();
+ if (objectName.isEmpty())
+ objectName = fileName;
+
+ itemNames.append(objectName);
+ items.append(plugin);
+ }
+ }
+
+ if (items.isEmpty()) {
+ QMessageBox::information(this, "No tank types found", "Please build the errorstateplugins directory");
+ return;
+ }
+
+ bool ok;
+ QString selectedName = QInputDialog::getItem(this, "Select a tank type", "Tank types",
+ itemNames, 0, false, &ok);
- Plugin *plugin = qobject_cast<Plugin *>(loader.instance());
- if (plugin != 0) {
- TankItem *tankItem = m_spawns.takeLast();
- m_scene->addItem(tankItem);
- connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket()));
- if (m_spawns.isEmpty())
- emit mapFull();
-
- QState *region = new QState(m_runningState);
- QState *pluginState = plugin->create(region, tankItem);
- region->setInitialState(pluginState);
-
- // If the plugin has an error it is disabled
- QState *errorState = new QState(region);
- errorState->assignProperty(tankItem, "enabled", false);
- pluginState->setErrorState(errorState);
+ if (ok && !selectedName.isEmpty()) {
+ int idx = itemNames.indexOf(selectedName);
+ if (Plugin *plugin = idx >= 0 ? items.at(idx) : 0) {
+ TankItem *tankItem = m_spawns.takeLast();
+ m_scene->addItem(tankItem);
+ connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket()));
+ if (m_spawns.isEmpty())
+ emit mapFull();
+
+ QState *region = new QState(m_runningState);
+ QState *pluginState = plugin->create(region, tankItem);
+ region->setInitialState(pluginState);
+
+ // If the plugin has an error it is disabled
+ QState *errorState = new QState(region);
+ errorState->assignProperty(tankItem, "enabled", false);
+ pluginState->setErrorState(errorState);
+ }
}
}
diff --git a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h b/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h
index 3db464b..10e6f48 100644
--- a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h
+++ b/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h
@@ -56,6 +56,8 @@ class RandomAiPlugin: public QObject, public Plugin
Q_OBJECT
Q_INTERFACES(Plugin)
public:
+ RandomAiPlugin() { setObjectName("Random"); }
+
virtual QState *create(QState *parentState, QObject *tank);
};
diff --git a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.h b/examples/statemachine/errorstateplugins/seek_ai/seek_ai.h
index 34d203e..a1b5749 100644
--- a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.h
+++ b/examples/statemachine/errorstateplugins/seek_ai/seek_ai.h
@@ -196,6 +196,8 @@ class SeekAi: public QObject, public Plugin
Q_OBJECT
Q_INTERFACES(Plugin)
public:
+ SeekAi() { setObjectName("Seek and destroy"); }
+
virtual QState *create(QState *parentState, QObject *tank);
};
diff --git a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h b/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h
index 4b4629c..6e220ed 100644
--- a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h
+++ b/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h
@@ -38,6 +38,8 @@ class SpinAi: public QObject, public Plugin
Q_OBJECT
Q_INTERFACES(Plugin)
public:
+ SpinAi() { setObjectName("Spin and destroy"); }
+
virtual QState *create(QState *parentState, QObject *tank);
};
diff --git a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h b/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h
index 9a96a8b..d520455 100644
--- a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h
+++ b/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h
@@ -38,6 +38,8 @@ class SpinAiWithError: public QObject, public Plugin
Q_OBJECT
Q_INTERFACES(Plugin)
public:
+ SpinAiWithError() { setObjectName("Spin and destroy with runtime error in state machine"); }
+
virtual QState *create(QState *parentState, QObject *tank);
};