
#include "qgscopepython.h"

#include "qgscopeworkbench.h"
#include "qgpythonsandbox.h"
#include "qgpythonconsoledockwidget.h"

#include "qglogger.h"
//#include "qgpython.h"

#include "qgscopechrono.h"

class QgScopePythonAdaptor: public QgScopeScriptSiteAdaptor
{
public:
    virtual void registerObject(const QString& name, QObject* obj)
    {
        QgScopeScriptSiteAdaptor::registerObject(name, obj);
    }
};

QgScopePython::QgScopePython(QObject *parent) : QObject(parent)
{
}

void QgScopePython::activate()
{
    QgPythonSandbox::enable(true);
    QgPythonSandbox::loadSettings();

    QgPythonSandbox sb;

    QgScopeWorkbench& wb=QgScopeWorkbench::instance();

    if (sb.isEnabled())
        wb.mainWindow()->addDockWidget(Qt::BottomDockWidgetArea, new QgPythonConsoleDockWidget);

    wb.setScriptSiteAdptor(pythonScriptAdaptor());

    //QgPython::instance().mainModule().addObject("chrono", &QgScopeChrono::instance());
    sb.addObject("chrono", &QgScopeChrono::instance());
    //QgPython::instance().mainModule().addObject("wb", &wb);
    sb.addObject("wb", &wb);
}

void QgScopePython::createApplicationMainModule(const QString& moduleName)
{
    //QgPython::instance().createMainApplicationModule(moduleName);
    QgPythonSandbox sb;
    sb.createAppModule(moduleName);
}


QgScopeScriptSiteAdaptor* QgScopePython::pythonScriptAdaptor()
{
    static QgScopePythonAdaptor pyAdaptor;
    return &pyAdaptor;
}


void QgScopeScriptSiteAdaptor::registerObject(const QString& name, QObject* obj)
{
    QgLogger logger("PYSCOPE");

    //QgPython::instance().mainModule().addObject(name, obj);
    QgPythonSandbox sb;
    sb.addObject(name, obj);
    logger.info("Registered object: "+name);
}

#if 0
#include "qgscopepython.h"

#include "qgscopeworkbench.h"
#include "qgpythonconsoledockwidget.h"

#include "qglogger.h"
#include "qgpython.h"

#include "qgscopechrono.h"

class QgScopePythonAdaptor: public QgScopeScriptSiteAdaptor
{
public:
    virtual void registerObject(const QString& name, QObject* obj)
    {
        QgScopeScriptSiteAdaptor::registerObject(name, obj);
    }
};

QgScopePython::QgScopePython(QObject *parent) : QObject(parent)
{
}

void QgScopePython::activate()
{
    QgScopeWorkbench& wb=QgScopeWorkbench::instance();
    wb.mainWindow()->addDockWidget(Qt::BottomDockWidgetArea, new QgPythonConsoleDockWidget);
    wb.setScriptSiteAdptor(pythonScriptAdaptor());
    QgPython::instance().mainModule().addObject("chrono", &QgScopeChrono::instance());
    QgPython::instance().mainModule().addObject("wb", &wb);
}

void QgScopePython::createApplicationMainModule(const QString& moduleName)
{
    QgPython::instance().createMainApplicationModule(moduleName);
}


QgScopeScriptSiteAdaptor* QgScopePython::pythonScriptAdaptor()
{
    static QgScopePythonAdaptor pyAdaptor;
    return &pyAdaptor;
}


void QgScopeScriptSiteAdaptor::registerObject(const QString& name, QObject* obj)
{
    QgLogger logger("PYSCOPE");

    QgPython::instance().mainModule().addObject(name, obj);
    logger.info("Registered object: "+name);
}
#endif
