Changed: #1206 Update core and example plugin. Plugin should delete your own objects from the pool.

This commit is contained in:
dnk-88 2011-02-16 20:06:52 +02:00
parent 0c61d64592
commit de37da056a
9 changed files with 121 additions and 63 deletions

View file

@ -36,13 +36,27 @@
using namespace Core; using namespace Core;
CorePlugin::CorePlugin()
{
}
CorePlugin::~CorePlugin()
{
Q_FOREACH(QObject *obj, _autoReleaseObjects)
{
_plugMan->removeObject(obj);
}
qDeleteAll(_autoReleaseObjects);
_autoReleaseObjects.clear();
}
bool CorePlugin::initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString) bool CorePlugin::initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString)
{ {
Q_UNUSED(errorString); Q_UNUSED(errorString);
_plugMan = pluginManager; _plugMan = pluginManager;
oldOVQT = false; _oldOVQT = false;
_plugMan->addObject(new CSearchPathsSettingsPage(this)); addAutoReleasedObject(new CSearchPathsSettingsPage(this));
return true; return true;
} }
@ -66,7 +80,7 @@ void CorePlugin::extensionsInitialized()
connect(newAction, SIGNAL(triggered()), this, SLOT(execSettings())); connect(newAction, SIGNAL(triggered()), this, SLOT(execSettings()));
connect(newAction2, SIGNAL(triggered()), _pluginView, SLOT(show())); connect(newAction2, SIGNAL(triggered()), _pluginView, SLOT(show()));
oldOVQT = true; _oldOVQT = true;
} }
else else
{ {
@ -93,7 +107,7 @@ void CorePlugin::extensionsInitialized()
void CorePlugin::shutdown() void CorePlugin::shutdown()
{ {
if (!oldOVQT) if (!_oldOVQT)
{ {
delete _mainWindow; delete _mainWindow;
delete _pluginView; delete _pluginView;
@ -102,7 +116,7 @@ void CorePlugin::shutdown()
void CorePlugin::execSettings() void CorePlugin::execSettings()
{ {
CSettingsDialog settingsDialog(_plugMan); CSettingsDialog settingsDialog(this);
settingsDialog.show(); settingsDialog.show();
settingsDialog.execDialog(); settingsDialog.execDialog();
} }
@ -142,6 +156,12 @@ QList<QString> CorePlugin::dependencies() const
return QList<QString>(); return QList<QString>();
} }
void CorePlugin::addAutoReleasedObject(QObject *obj)
{
_plugMan->addObject(obj);
_autoReleaseObjects.prepend(obj);
}
QObject* CorePlugin::objectByName(const QString &name) const QObject* CorePlugin::objectByName(const QString &name) const
{ {
Q_FOREACH (QObject *qobj, _plugMan->allObjects()) Q_FOREACH (QObject *qobj, _plugMan->allObjects())

View file

@ -44,6 +44,9 @@ class CorePlugin : public QObject, public ExtensionSystem::IPlugin
Q_INTERFACES(ExtensionSystem::IPlugin) Q_INTERFACES(ExtensionSystem::IPlugin)
public: public:
CorePlugin();
virtual ~CorePlugin();
bool initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString); bool initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString);
void extensionsInitialized(); void extensionsInitialized();
void shutdown(); void shutdown();
@ -56,6 +59,8 @@ public:
QString description() const; QString description() const;
QList<QString> dependencies() const; QList<QString> dependencies() const;
void addAutoReleasedObject(QObject *obj);
QObject *objectByName(const QString &name) const; QObject *objectByName(const QString &name) const;
ExtensionSystem::IPluginSpec *pluginByName(const QString &name) const; ExtensionSystem::IPluginSpec *pluginByName(const QString &name) const;
ExtensionSystem::IPluginManager *pluginManager() const ExtensionSystem::IPluginManager *pluginManager() const
@ -63,20 +68,36 @@ public:
return _plugMan; return _plugMan;
} }
template <typename Type> template <typename T>
QList<Type *> getObjects() const QList<T *> getObjects() const
{ {
QList<QObject *> all = _plugMan->allObjects(); QList<QObject *> all = _plugMan->allObjects();
QList<Type *> objects; QList<T *> objects;
Q_FOREACH(QObject *obj, all) Q_FOREACH(QObject *obj, all)
{ {
Type *typeObj = qobject_cast<Type *>(obj); T *tObj = qobject_cast<T *>(obj);
if (typeObj) if (tObj)
objects.append(typeObj); objects.append(tObj);
} }
return objects; return objects;
} }
template <typename T>
T *getObject() const
{
QList<QObject *> all = _plugMan->allObjects();
T *result = 0;
Q_FOREACH(QObject *obj, all)
{
T *tObj = qobject_cast<T *>(obj);
if (tObj)
{
result = tObj;
break;
}
}
}
protected: protected:
NLMISC::CLibraryContext *_LibContext; NLMISC::CLibraryContext *_LibContext;
@ -87,8 +108,8 @@ private:
ExtensionSystem::IPluginManager *_plugMan; ExtensionSystem::IPluginManager *_plugMan;
ExtensionSystem::CPluginView *_pluginView; ExtensionSystem::CPluginView *_pluginView;
CMainWindow *_mainWindow; CMainWindow *_mainWindow;
QList<QObject *> _autoReleaseObjects;
bool oldOVQT; bool _oldOVQT;
}; };
} // namespace Core } // namespace Core

View file

@ -25,6 +25,7 @@
// NeL includes // NeL includes
#include <nel/misc/debug.h> #include <nel/misc/debug.h>
// Qt includes // Qt includes
#include <QtGui/QtGui> #include <QtGui/QtGui>
@ -87,7 +88,7 @@ bool CMainWindow::showOptionsDialog(const QString &group,
{ {
if (!parent) if (!parent)
parent = this; parent = this;
CSettingsDialog _settingsDialog(_pluginManager, group, page, parent); CSettingsDialog _settingsDialog(_corePlugin, group, page, parent);
_settingsDialog.show(); _settingsDialog.show();
return _settingsDialog.execDialog(); return _settingsDialog.execDialog();
} }

View file

@ -29,7 +29,7 @@ namespace Core
CSearchPathsSettingsPage::CSearchPathsSettingsPage(QObject *parent) CSearchPathsSettingsPage::CSearchPathsSettingsPage(QObject *parent)
: QObject(parent), : QObject(parent),
_currentPage(NULL) _currentPage(0)
{ {
} }

View file

@ -16,7 +16,10 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// Project includes
#include "settings_dialog.h" #include "settings_dialog.h"
#include "core_plugin.h"
#include "ioptions_page.h"
// Qt includes // Qt includes
#include <QtGui/QHeaderView> #include <QtGui/QHeaderView>
@ -33,7 +36,7 @@ Q_DECLARE_METATYPE(PageData);
namespace Core namespace Core
{ {
CSettingsDialog::CSettingsDialog(ExtensionSystem::IPluginManager *pluginManager, CSettingsDialog::CSettingsDialog(CorePlugin *corePlugin,
const QString &categoryId, const QString &categoryId,
const QString &pageId, const QString &pageId,
QWidget *parent) QWidget *parent)
@ -42,7 +45,7 @@ CSettingsDialog::CSettingsDialog(ExtensionSystem::IPluginManager *pluginManager,
{ {
_ui.setupUi(this); _ui.setupUi(this);
_plugMan = pluginManager; _plugMan = corePlugin->pluginManager();
QString initialCategory = categoryId; QString initialCategory = categoryId;
QString initialPage = pageId; QString initialPage = pageId;
@ -59,14 +62,7 @@ CSettingsDialog::CSettingsDialog(ExtensionSystem::IPluginManager *pluginManager,
QMap<QString, QTreeWidgetItem *> categories; QMap<QString, QTreeWidgetItem *> categories;
QList<IOptionsPage *> pages; QList<IOptionsPage *> pages = corePlugin->getObjects<IOptionsPage>();
QList<QObject *> all = _plugMan->allObjects();
Q_FOREACH(QObject *obj, all)
{
IOptionsPage *page = qobject_cast<IOptionsPage *>(obj);
if (page)
pages.append(page);
}
int index = 0; int index = 0;
Q_FOREACH(IOptionsPage *page, pages) Q_FOREACH(IOptionsPage *page, pages)

View file

@ -25,11 +25,12 @@
#include <QtCore/QList> #include <QtCore/QList>
// Project includes // Project includes
#include "ioptions_page.h"
#include "../../extension_system/iplugin.h" #include "../../extension_system/iplugin.h"
namespace Core namespace Core
{ {
class CorePlugin;
class IOptionsPage;
/** /**
@class CSettingsDialog @class CSettingsDialog
@ -40,7 +41,7 @@ class CSettingsDialog: public QDialog
Q_OBJECT Q_OBJECT
public: public:
CSettingsDialog(ExtensionSystem::IPluginManager *pluginManager, CSettingsDialog(CorePlugin *corePlugin,
const QString &initialCategory = QString(), const QString &initialCategory = QString(),
const QString &initialPage = QString(), const QString &initialPage = QString(),
QWidget *parent = 0); QWidget *parent = 0);
@ -60,7 +61,7 @@ private Q_SLOTS:
void apply(); void apply();
private: private:
QList<Core::IOptionsPage*> _pages; QList<IOptionsPage *> _pages;
bool _applied; bool _applied;
QString _currentCategory; QString _currentCategory;
QString _currentPage; QString _currentPage;

View file

@ -18,15 +18,24 @@
namespace Plugin namespace Plugin
{ {
MyPlugin::~MyPlugin()
{
Q_FOREACH(QObject *obj, _autoReleaseObjects)
{
_plugMan->removeObject(obj);
}
qDeleteAll(_autoReleaseObjects);
_autoReleaseObjects.clear();
}
bool MyPlugin::initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString) bool MyPlugin::initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString)
{ {
Q_UNUSED(errorString); Q_UNUSED(errorString);
_plugMan = pluginManager; _plugMan = pluginManager;
_plugMan->addObject(new CExampleSettingsPage(this)); addAutoReleasedObject(new CExampleSettingsPage(this));
_plugMan->addObject(new CExampleAppPage(this)); addAutoReleasedObject(new CExampleAppPage(this));
_plugMan->addObject(new CCoreListener(this)); addAutoReleasedObject(new CCoreListener(this));
return true; return true;
} }
@ -69,6 +78,12 @@ QList<QString> MyPlugin::dependencies() const
return QList<QString>(); return QList<QString>();
} }
void MyPlugin::addAutoReleasedObject(QObject *obj)
{
_plugMan->addObject(obj);
_autoReleaseObjects.prepend(obj);
}
QObject* MyPlugin::objectByName(const QString &name) const QObject* MyPlugin::objectByName(const QString &name) const
{ {
Q_FOREACH (QObject *qobj, _plugMan->allObjects()) Q_FOREACH (QObject *qobj, _plugMan->allObjects())

View file

@ -32,6 +32,8 @@ class MyPlugin : public QObject, public ExtensionSystem::IPlugin
Q_INTERFACES(ExtensionSystem::IPlugin) Q_INTERFACES(ExtensionSystem::IPlugin)
public: public:
virtual ~MyPlugin();
bool initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString); bool initialize(ExtensionSystem::IPluginManager *pluginManager, QString *errorString);
void extensionsInitialized(); void extensionsInitialized();
@ -43,6 +45,8 @@ public:
QString description() const; QString description() const;
QList<QString> dependencies() const; QList<QString> dependencies() const;
void addAutoReleasedObject(QObject *obj);
QObject *objectByName(const QString &name) const; QObject *objectByName(const QString &name) const;
ExtensionSystem::IPluginSpec *pluginByName(const QString &name) const; ExtensionSystem::IPluginSpec *pluginByName(const QString &name) const;
@ -51,7 +55,7 @@ protected:
private: private:
ExtensionSystem::IPluginManager *_plugMan; ExtensionSystem::IPluginManager *_plugMan;
QList<QObject *> _autoReleaseObjects;
}; };
class CExampleAppPage: public QObject, public Core::IAppPage class CExampleAppPage: public QObject, public Core::IAppPage