add 'view' global JS object to main frame
Stefan Bund [Wed, 11 Apr 2012 20:44:27 +0000 (22:44 +0200)]
Viewer.cc
Viewer.hh

index 89b0335..03e8452 100644 (file)
--- a/Viewer.cc
+++ b/Viewer.cc
@@ -19,6 +19,8 @@
 #include <QPrinter>
 #include <QPrintDialog>
 #include <QWebInspector>
+#include <QWebFrame>
+#include <QWebHistory>
 
 //#include "Viewer.mpp"
 #define prefix_
@@ -114,6 +116,15 @@ prefix_ pykit::Viewer::Viewer(QUrl const & url, Publisher * publisher, QWidget *
     load(url);
     inspector_ = new QWebInspector();
     inspector_->setPage(page());
+    connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
+            this, SLOT(initJSObjects()));
+    initJSObjects();
+}
+
+prefix_ pykit::Viewer::~Viewer()
+{
+    if (inspector_)
+        delete inspector_;
 }
 
 prefix_ pykit::Viewer * pykit::Viewer::instance()
@@ -121,10 +132,19 @@ prefix_ pykit::Viewer * pykit::Viewer::instance()
     return instance_;
 }
 
-prefix_ pykit::Viewer::~Viewer()
+prefix_ bool pykit::Viewer::canGoBack()
 {
-    if (inspector_)
-        delete inspector_;
+    return page()->history()->canGoBack();
+}
+
+prefix_ bool pykit::Viewer::canGoForward()
+{
+    return page()->history()->canGoForward();
+}
+
+prefix_ void pykit::Viewer::initJSObjects()
+{
+    page()->mainFrame()->addToJavaScriptWindowObject("view", this);
 }
 
 prefix_ void pykit::Viewer::printRequested()
index 445d894..ff99a3f 100644 (file)
--- a/Viewer.hh
+++ b/Viewer.hh
@@ -32,7 +32,11 @@ namespace pykit {
 
         static Viewer * instance();
 
+        Q_INVOKABLE bool canGoBack();
+        Q_INVOKABLE bool canGoForward();
+
     private slots:
+        void initJSObjects();
         void printRequested();
 
     private: