+ simplify porting of Web module to Python3

This commit is contained in:
wmayer
2016-01-17 19:17:34 +01:00
parent 5aea3220c8
commit a467612b9f
6 changed files with 173 additions and 247 deletions

View File

@@ -24,13 +24,18 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Python.h>
# include <QMdiArea>
# include <QMdiSubWindow>
# include <QUrl>
#endif
#include <Base/Console.h>
#include <Base/Interpreter.h>
#include <Gui/Application.h>
#include <Gui/MainWindow.h>
#include <Gui/WorkbenchManager.h>
#include <Gui/Language/Translator.h>
#include "BrowserView.h"
#include "Workbench.h"
@@ -45,21 +50,83 @@ void loadWebResource()
Gui::Translator::instance()->refresh();
}
/* registration table */
extern struct PyMethodDef WebGui_Import_methods[];
namespace WebGui {
class Module : public Py::ExtensionModule<Module>
{
public:
Module() : Py::ExtensionModule<Module>("WebGui")
{
add_varargs_method("openBrowser",&Module::openBrowser
);
add_varargs_method("openBrowserHTML",&Module::openBrowserHTML
);
initialize("This module is the Web module."); // register with Python
}
virtual ~Module() {}
private:
Py::Object openBrowser(const Py::Tuple& args)
{
const char* url;
if (!PyArg_ParseTuple(args.ptr(), "s",&url))
throw Py::Exception();
WebGui::BrowserView* pcBrowserView;
pcBrowserView = new WebGui::BrowserView(Gui::getMainWindow());
pcBrowserView->setWindowTitle(QObject::tr("Browser"));
pcBrowserView->resize(400, 300);
pcBrowserView->load(url);
Gui::getMainWindow()->addWindow(pcBrowserView);
return Py::None();
}
Py::Object openBrowserHTML(const Py::Tuple& args)
{
const char* HtmlCode;
const char* BaseUrl;
const char* TabName = "Browser";
if (! PyArg_ParseTuple(args.ptr(), "ss|s",&HtmlCode,&BaseUrl,&TabName))
throw Py::Exception();
QMdiSubWindow* browserView = 0;
QMdiArea* mdiArea = Gui::getMainWindow()->findChild<QMdiArea*>();
QList<QMdiSubWindow *> mdiViews = mdiArea->subWindowList();
for (QList<QMdiSubWindow *>::iterator it = mdiViews.begin(); it != mdiViews.end(); ++it) {
if (qobject_cast<WebGui::BrowserView*>((*it)->widget())) {
browserView = *it;
break;
}
}
if (!browserView) {
WebGui::BrowserView* pcBrowserView = 0;
pcBrowserView = new WebGui::BrowserView(Gui::getMainWindow());
pcBrowserView->resize(400, 300);
pcBrowserView->setHtml(QString::fromUtf8(HtmlCode),QUrl(QString::fromLatin1(BaseUrl)),QString::fromUtf8(TabName));
Gui::getMainWindow()->addWindow(pcBrowserView);
}
else {
mdiArea->setActiveSubWindow(browserView);
}
return Py::None();
}
};
} // namespace WebGui
/* Python entry */
extern "C" {
void WebGuiExport initWebGui()
PyMODINIT_FUNC initWebGui()
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
}
(void) Py_InitModule("WebGui", WebGui_Import_methods); /* mod name, table ptr */
new WebGui::Module();
Base::Console().Log("Loading GUI of Web module... done\n");
// instantiating the commands
@@ -69,5 +136,3 @@ void WebGuiExport initWebGui()
// add resources and reloads the translators
loadWebResource();
}
} // extern "C" {