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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -52,52 +52,52 @@ bool SheetBuilderPlugin::initialize(ExtensionSystem::IPluginManager *pluginManag
void SheetBuilderPlugin::extensionsInitialized()
{
QMenu *toolsMenu = qobject_cast<QMenu *>(objectByName("ovqt.Menu.Tools"));
nlassert(toolsMenu);
QMenu *toolsMenu = qobject_cast<QMenu *>(objectByName("ovqt.Menu.Tools"));
nlassert(toolsMenu);
toolsMenu->addSeparator();
toolsMenu->addSeparator();
QAction *actBuilder = toolsMenu->addAction("Sheet builder");
connect(actBuilder, SIGNAL(triggered()), this, SLOT(execBuilderDialog()));
QAction *actBuilder = toolsMenu->addAction("Sheet builder");
connect(actBuilder, SIGNAL(triggered()), this, SLOT(execBuilderDialog()));
}
void SheetBuilderPlugin::execBuilderDialog()
{
QMainWindow *wnd = qobject_cast<QMainWindow *>(objectByName("CMainWindow"));
nlassert(wnd);
QMainWindow *wnd = qobject_cast<QMainWindow *>(objectByName("CMainWindow"));
nlassert(wnd);
SheetBuilderDialog dlg(wnd);
dlg.exec();
SheetBuilderDialog dlg(wnd);
dlg.exec();
}
void SheetBuilderPlugin::setNelContext(NLMISC::INelContext *nelContext)
{
#ifdef NL_OS_WINDOWS
// Ensure that a context doesn't exist yet.
// This only applies to platforms without PIC, e.g. Windows.
nlassert(!NLMISC::INelContext::isContextInitialised());
// Ensure that a context doesn't exist yet.
// This only applies to platforms without PIC, e.g. Windows.
nlassert(!NLMISC::INelContext::isContextInitialised());
#endif // NL_OS_WINDOWS
_LibContext = new NLMISC::CLibraryContext(*nelContext);
_LibContext = new NLMISC::CLibraryContext(*nelContext);
}
QString SheetBuilderPlugin::name() const
{
return "Sheet builder";
return "Sheet builder";
}
QString SheetBuilderPlugin::version() const
{
return "1.0";
return "1.0";
}
QString SheetBuilderPlugin::vendor() const
{
return "kharvd";
return "kharvd";
}
QString SheetBuilderPlugin::description() const
{
return "make_sheet_id equivalent";
return "make_sheet_id equivalent";
}
QList<QString> SheetBuilderPlugin::dependencies() const
@ -107,18 +107,18 @@ QList<QString> SheetBuilderPlugin::dependencies() const
QObject* SheetBuilderPlugin::objectByName(const QString &name) const
{
Q_FOREACH (QObject *qobj, _plugMan->allObjects())
if (qobj->objectName() == name)
return qobj;
return 0;
Q_FOREACH (QObject *qobj, _plugMan->allObjects())
if (qobj->objectName() == name)
return qobj;
return 0;
}
ExtensionSystem::IPluginSpec *SheetBuilderPlugin::pluginByName(const QString &name) const
{
Q_FOREACH (ExtensionSystem::IPluginSpec *spec, _plugMan->plugins())
if (spec->name() == name)
return spec;
return 0;
Q_FOREACH (ExtensionSystem::IPluginSpec *spec, _plugMan->plugins())
if (spec->name() == name)
return spec;
return 0;
}
Q_EXPORT_PLUGIN(SheetBuilderPlugin)