socexplorerproxy.cpp
434 lines
| 13.1 KiB
| text/x-c
|
CppLexer
Jeandet Alexis
|
r0 | /*------------------------------------------------------------------------------ | ||
-- This file is a part of the SocExplorer Software | ||||
Jeandet Alexis
|
r13 | -- Copyright (C) 2011, Plasma Physics Laboratory - CNRS | ||
Jeandet Alexis
|
r0 | -- | ||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 2 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
-------------------------------------------------------------------------------*/ | ||||
/*-- Author : Alexis Jeandet | ||||
-- Mail : alexis.jeandet@lpp.polytechnique.fr | ||||
----------------------------------------------------------------------------*/ | ||||
#include "socexplorerproxy.h" | ||||
#include <pluginmanagerWDGT.h> | ||||
#include <socexplorerengine.h> | ||||
#include <pluginloader.h> | ||||
#include <socexplorerplugin.h> | ||||
socexplorerproxy* socexplorerproxy::_self=NULL; | ||||
QMainWindow* socexplorerproxy::mainWindow=NULL; | ||||
QList<socexplorerplugin*>* socexplorerproxy::drivers=NULL; | ||||
QList<socexplorerplugin*>* socexplorerproxy::linearDriverList=NULL; | ||||
socexplorerplugin* socexplorerproxy::root=NULL; | ||||
socexplorerplugin* socexplorerproxy::parent=NULL; | ||||
PluginsCache* socexplorerproxy::cache=NULL; | ||||
Jeandet Alexis
|
r94 | QStringList* socexplorerproxy::linearDriverPathList=NULL; | ||
Jeandet Alexis
|
r0 | |||
socexplorerproxy::socexplorerproxy(QObject *parent) : | ||||
QObject(parent) | ||||
{ | ||||
cache = new PluginsCache; | ||||
drivers = new QList<socexplorerplugin*>; | ||||
linearDriverList=new QList<socexplorerplugin*>; | ||||
Jeandet Alexis
|
r94 | linearDriverPathList=new QStringList; | ||
Jeandet Alexis
|
r0 | root = NULL; | ||
} | ||||
socexplorerproxy::socexplorerproxy(QMainWindow *Mainwindow, QObject *parent): | ||||
QObject(parent) | ||||
{ | ||||
mainWindow = Mainwindow; | ||||
cache = new PluginsCache; | ||||
drivers = new QList<socexplorerplugin*>; | ||||
linearDriverList=new QList<socexplorerplugin*>; | ||||
Jeandet Alexis
|
r94 | linearDriverPathList=new QStringList; | ||
Jeandet Alexis
|
r0 | root = NULL; | ||
} | ||||
void socexplorerproxy::init() | ||||
{ | ||||
if(!_self) | ||||
{ | ||||
_self = new socexplorerproxy(); | ||||
mainWindow = NULL; | ||||
} | ||||
} | ||||
void socexplorerproxy::setMainWindow(QMainWindow *Mainwindow) | ||||
{ | ||||
if(!_self)init(); | ||||
mainWindow=Mainwindow; | ||||
} | ||||
void socexplorerproxy::loadSysDriver(const QString name) | ||||
{ | ||||
if(!_self)init(); | ||||
if(pluginloader::libcanberoot(name)) | ||||
{ | ||||
socexplorerplugin* driver = pluginloader::newsocexplorerplugin(name); | ||||
QString driverName = _self->getinstanceName(driver->baseName()); | ||||
Jeandet Alexis
|
r94 | loadSysDriver(driver,driverName,name); | ||
Jeandet Alexis
|
r0 | } | ||
} | ||||
void socexplorerproxy::loadSysDriver(const QString name, const QString instanceName) | ||||
{ | ||||
if(!_self)init(); | ||||
if(pluginloader::libcanberoot(name) && !_self->instanceExists(instanceName)) | ||||
{ | ||||
socexplorerplugin* driver = pluginloader::newsocexplorerplugin(name); | ||||
Jeandet Alexis
|
r94 | loadSysDriver(driver,instanceName,name); | ||
Jeandet Alexis
|
r0 | } | ||
} | ||||
Jeandet Alexis
|
r94 | void socexplorerproxy::loadSysDriver(socexplorerplugin *driver, const QString instanceName, const QString path) | ||
Jeandet Alexis
|
r0 | { | ||
if(!_self)init(); | ||||
r10 | driver->setInstanceName(instanceName); | |||
Jeandet Alexis
|
r0 | driver->parent = NULL; | ||
drivers->append(driver); | ||||
linearDriverList->append(driver); | ||||
Jeandet Alexis
|
r94 | if(path.isEmpty()) | ||
linearDriverPathList->append(driver->baseName()); | ||||
else | ||||
linearDriverPathList->append(path); | ||||
Jeandet Alexis
|
r0 | connectChildToProxy(driver); | ||
emit _self->addPluginGUI(driver); | ||||
emit _self->clearMenu(); | ||||
emit _self->treeChanged(QList<socexplorerplugin*>(*drivers)); | ||||
SocExplorerEngine::addSOC(driver); | ||||
r78 | driver->postInstantiationTrigger(); | |||
Jeandet Alexis
|
r0 | } | ||
void socexplorerproxy::loadChildSysDriver(socexplorerplugin *parent, const QString child) | ||||
{ | ||||
if(!_self)init(); | ||||
if(pluginloader::libcanbechild(child)) | ||||
{ | ||||
socexplorerplugin* driver = pluginloader::newsocexplorerplugin(child); | ||||
QString driverName = _self->getinstanceName(driver->baseName()); | ||||
bool ok=true; | ||||
if(ok) | ||||
{ | ||||
Jeandet Alexis
|
r94 | if(parent!=NULL)_self->loadSysDriverToParent(driver,parent,driverName,child); | ||
Jeandet Alexis
|
r0 | } | ||
} | ||||
} | ||||
void socexplorerproxy::loadSysDriverToParent(const QString name,const QString ParentInst) | ||||
{ | ||||
if(!_self)init(); | ||||
if(pluginloader::libcanbechild(name)) | ||||
{ | ||||
socexplorerplugin* driver = pluginloader::newsocexplorerplugin(name); | ||||
QString driverName = _self->getinstanceName(driver->baseName()); | ||||
bool ok=true; | ||||
if(ok) | ||||
{ | ||||
socexplorerplugin* parent=_self->getSysDriver(ParentInst); | ||||
Jeandet Alexis
|
r94 | if(parent!=NULL)loadSysDriverToParent(driver,parent,driverName,name); | ||
Jeandet Alexis
|
r0 | } | ||
} | ||||
} | ||||
void socexplorerproxy::loadSysDriverToParent(const QString name,const QString instanceName,const QString ParentInst) | ||||
{ | ||||
if(!_self)init(); | ||||
if(pluginloader::libcanbechild(name) && !_self->instanceExists(instanceName)) | ||||
{ | ||||
socexplorerplugin* driver = pluginloader::newsocexplorerplugin(name); | ||||
bool ok=true; | ||||
if(ok) | ||||
{ | ||||
socexplorerplugin* parent=_self->getSysDriver(ParentInst); | ||||
Jeandet Alexis
|
r94 | if(parent!=NULL)loadSysDriverToParent(driver,parent,instanceName,name); | ||
Jeandet Alexis
|
r0 | } | ||
} | ||||
} | ||||
Jeandet Alexis
|
r94 | void socexplorerproxy::loadSysDriverToParent(socexplorerplugin *driver,socexplorerplugin *parent, const QString instanceName,const QString path) | ||
Jeandet Alexis
|
r0 | { | ||
if(!_self)init(); | ||||
linearDriverList->append(driver); | ||||
Jeandet Alexis
|
r94 | if(path.isEmpty()) | ||
linearDriverPathList->append(driver->baseName()); | ||||
else | ||||
linearDriverPathList->append(path); | ||||
Jeandet Alexis
|
r0 | driver->parent = parent; | ||
r10 | driver->setInstanceName(instanceName); | |||
Jeandet Alexis
|
r0 | parent->childs.append(driver); | ||
connectChildToProxy(driver); | ||||
connectChildToParent(parent,driver); | ||||
emit _self->clearMenu(); | ||||
emit _self->addPluginGUI(driver); | ||||
emit _self->treeChanged(QList<socexplorerplugin*>(*drivers)); | ||||
driver->postInstantiationTrigger(); | ||||
} | ||||
void socexplorerproxy::changeSysDriverInstName(const QString instanceName) | ||||
{ | ||||
Q_UNUSED(instanceName) | ||||
} | ||||
void socexplorerproxy::changeSysDriverInstName(const QString newinstanceName, const QString previnstanceName) | ||||
{ | ||||
if(!_self)init(); | ||||
socexplorerplugin*temp=_self->getSysDriver(previnstanceName); | ||||
if(temp!=NULL) | ||||
{ | ||||
if(NULL!=_self->getSysDriver(newinstanceName)) | ||||
{ | ||||
emit _self->treeChanged(QList<socexplorerplugin*>(*drivers)); | ||||
return; | ||||
} | ||||
temp->setInstanceName(newinstanceName); | ||||
} | ||||
emit _self->treeChanged(QList<socexplorerplugin*>(*drivers)); | ||||
} | ||||
void socexplorerproxy::connectChildToParent(socexplorerplugin *parent, socexplorerplugin *child) | ||||
{ | ||||
if(!_self)init(); | ||||
r10 | connect(parent,SIGNAL(activateSig(bool)),child,SLOT(activate(bool))); | |||
Jeandet Alexis
|
r0 | child->activate(parent->isConnected()); | ||
} | ||||
void socexplorerproxy::disconnectChildToParent(socexplorerplugin *child) | ||||
{ | ||||
if(!_self)init(); | ||||
r10 | disconnect(child->parent,SIGNAL(activateSig(bool)),child,SLOT(activate(bool))); | |||
Jeandet Alexis
|
r0 | } | ||
void socexplorerproxy::connectChildToProxy(socexplorerplugin *child) | ||||
{ | ||||
if(!_self)init(); | ||||
connect(child,SIGNAL(registerObject(QObject*,QString)),_self,SIGNAL(registerObject(QObject*,QString))); | ||||
connect(child,SIGNAL(closePlugin(socexplorerplugin*)),_self,SLOT(closeSysDriver(socexplorerplugin*))); | ||||
} | ||||
void socexplorerproxy::disconnectChildToProxy(socexplorerplugin *child) | ||||
{ | ||||
if(!_self)init(); | ||||
disconnect(child,SIGNAL(registerObject(QObject*,QString)),_self,SIGNAL(registerObject(QObject*,QString))); | ||||
disconnect(child,SIGNAL(closePlugin(socexplorerplugin*)),_self,SLOT(closeSysDriverFromDriver(socexplorerplugin*))); | ||||
} | ||||
QString socexplorerproxy::getinstanceName(const QString& baseName) | ||||
{ | ||||
if(!_self)init(); | ||||
int i=0; | ||||
QString name; | ||||
bool validName = false; | ||||
while(!validName) | ||||
{ | ||||
name.clear(); | ||||
name.append(baseName+QString::number(i)); | ||||
validName = instanceNameIsValid(name); | ||||
i++; | ||||
} | ||||
return name; | ||||
} | ||||
void socexplorerproxy::changeSysDriverInstName(socexplorerplugin* driver) | ||||
{ | ||||
if(!_self)init(); | ||||
r10 | QString prevName(driver->instanceName()); | |||
Jeandet Alexis
|
r0 | } | ||
bool socexplorerproxy::instanceNameIsValid(const QString& instanceName) | ||||
{ | ||||
if(!_self)init(); | ||||
for(int k=0;k<linearDriverList->count();k++) | ||||
{ | ||||
r10 | if(!linearDriverList->at(k)->instanceName().compare(instanceName)) | |||
Jeandet Alexis
|
r0 | return false; | ||
} | ||||
return true; | ||||
} | ||||
socexplorerplugin *socexplorerproxy::findPlugin(const QString &instanceName) | ||||
{ | ||||
if(!_self)init(); | ||||
for(int k=0;k<linearDriverList->count();k++) | ||||
{ | ||||
r73 | if(!linearDriverList->at(k)->instanceName().compare(instanceName)) | |||
Jeandet Alexis
|
r0 | return linearDriverList->at(k); | ||
} | ||||
return NULL; | ||||
} | ||||
Jeandet Alexis
|
r94 | QStringList socexplorerproxy::getPluginsList() | ||
{ | ||||
if(!_self)init(); | ||||
QStringList result; | ||||
socexplorerplugin* plugin; | ||||
for(int i=0; i<linearDriverList->count();i++) | ||||
{ | ||||
QString parent=""; | ||||
plugin=linearDriverList->at(i); | ||||
if(plugin->parent)parent=plugin->parent->instanceName(); | ||||
result.append(linearDriverList->at(i)->instanceName()+":"+parent+":"+linearDriverPathList->at(i)); | ||||
} | ||||
return result; | ||||
} | ||||
bool socexplorerproxy::loadPluginsList(QStringList plugins) | ||||
{ | ||||
if(!_self)init(); | ||||
QString plugin; | ||||
int lastLoadedPlugins=-1; | ||||
QStringList loadedPlugins; | ||||
while(plugins.count() && lastLoadedPlugins!=loadedPlugins.count()) | ||||
{ | ||||
lastLoadedPlugins=loadedPlugins.count(); | ||||
for (int i = 0; i < plugins.count(); i++) | ||||
{ | ||||
plugin=plugins[i]; | ||||
QStringList args=plugin.split(':',QString::KeepEmptyParts); | ||||
if(args.count()==3) | ||||
{ | ||||
if(args[1].isEmpty()) | ||||
{ | ||||
_self->loadSysDriver(args[2],args[0]); | ||||
loadedPlugins.append(args[0]); | ||||
plugins.removeAt(i); | ||||
} | ||||
else if(loadedPlugins.contains(args[1])) | ||||
{ | ||||
_self->loadSysDriverToParent(args[2],args[0],args[1]); | ||||
loadedPlugins.append(args[0]); | ||||
plugins.removeAt(i); | ||||
} | ||||
} | ||||
else | ||||
{ | ||||
plugins.removeAt(i); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
Jeandet Alexis
|
r0 | bool socexplorerproxy::instanceExists(const QString &instanceName) | ||
{ | ||||
return !socexplorerproxy::instanceNameIsValid(instanceName); | ||||
} | ||||
void socexplorerproxy::close() | ||||
{ | ||||
if(!_self)init(); | ||||
socexplorerplugin* tmpPtr; | ||||
while(drivers->count()>0) | ||||
{ | ||||
tmpPtr = drivers->last(); | ||||
drivers->removeLast(); | ||||
_self->closeSysDriver(tmpPtr); | ||||
} | ||||
if(root!=NULL) | ||||
{ | ||||
_self->closeSysDriver(root); | ||||
} | ||||
} | ||||
socexplorerplugin* socexplorerproxy::getSysDriver(const QString instanceName) | ||||
{ | ||||
if(!_self)init(); | ||||
for(int i=0;i<linearDriverList->count();i++) | ||||
{ | ||||
r10 | if(!linearDriverList->at(i)->instanceName().compare(instanceName)) | |||
Jeandet Alexis
|
r0 | return linearDriverList->at(i); | ||
} | ||||
return NULL; | ||||
} | ||||
void socexplorerproxy::closeSysDriver(const QString instanceName) | ||||
{ | ||||
if(!_self)init(); | ||||
closeSysDriver(getSysDriver(instanceName),false); | ||||
} | ||||
void socexplorerproxy::closeSysDriver(socexplorerplugin *driver, bool recursive) | ||||
{ | ||||
if(!_self)init(); | ||||
if(driver!=NULL) | ||||
{ | ||||
Jeandet Alexis
|
r12 | emit _self->removePluginGUI(driver); | ||
Jeandet Alexis
|
r0 | if(driver->parent==NULL)SocExplorerEngine::removeSOC(driver); | ||
while(driver->childs.count()!=0)closeSysDriver(driver->childs.first()); | ||||
Jeandet Alexis
|
r94 | linearDriverPathList->removeAt(linearDriverList->indexOf(driver)); | ||
Jeandet Alexis
|
r0 | linearDriverList->removeOne(driver); | ||
if(driver->parent!= NULL) | ||||
{ | ||||
driver->parent->childs.removeOne(driver); //Have parent so it's a child | ||||
disconnectChildToParent(driver); | ||||
disconnectChildToProxy(driver); | ||||
delete driver; | ||||
} | ||||
else | ||||
{ | ||||
drivers->removeOne(driver); | ||||
disconnectChildToProxy(driver); | ||||
delete driver; | ||||
} | ||||
if(!recursive) | ||||
{ | ||||
emit _self->clearMenu(); | ||||
Jeandet Alexis
|
r12 | emit _self->registermenu(mainWindow); | ||
Jeandet Alexis
|
r0 | emit _self->treeChanged(QList<socexplorerplugin*>(*drivers)); | ||
} | ||||
} | ||||
} | ||||
void socexplorerproxy::geteplugintree() | ||||
{ | ||||
if(!_self)init(); | ||||
emit _self->treeChanged(QList<socexplorerplugin*>(*drivers)); | ||||
} | ||||
void socexplorerproxy::closeSysDriverFromDriver(socexplorerplugin *driver) | ||||
{ | ||||
if(!_self)init(); | ||||
emit _self->closeSysDriverSig(driver); | ||||
} | ||||
void socexplorerproxy::updateText() | ||||
{ | ||||
if(!_self)init(); | ||||
emit _self->clearMenu(); | ||||
emit _self->registermenu(mainWindow); | ||||
} | ||||
void socexplorerproxy::makeMenu(QMenu* menu) | ||||
{ | ||||
if(!_self)init(); | ||||
for(int i=0;i<drivers->count();i++) | ||||
{ | ||||
drivers->at(i)->registermenu(menu); | ||||
} | ||||
} | ||||