Browser inside task panel

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: Browser inside task panel

Post by easyw-fc »

bernd wrote: Fri Nov 03, 2017 9:39 pm less code lines ...

Code: Select all

import FreeCAD
import FreeCADGui
from PySide import QtCore, QtGui, QtWebKit


class TransWindow(QtGui.QDialog):
    HOME = "http://www.forum.freecadweb.org"
    def __init__(self):
        super(TransWindow, self).__init__()
        self.webView = QtWebKit.QWebView(self)
        self.webView.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addLayout(QtGui.QHBoxLayout())
        self.layout.addWidget(self.webView)

    def showEvent(self, event):
        super(TransWindow, self).showEvent(event)
        self.webView.load(QtCore.QUrl(self.HOME))


mw = FreeCADGui.getMainWindow()
d = QtGui.QDockWidget()
d.setWidget(TransWindow())
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea, d)
Hi @bernd
is there a way to set the user agent
i.e. to

Code: Select all

USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1"
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Browser inside task panel

Post by bernd »

I do not know how.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Browser inside task panel

Post by microelly2 »

Code: Select all

import sys
from PySide import QtGui
from PySide import QtWebKit

user_agent = QtWebKit.QWebPage()
user_agent.userAgentForUrl = lambda x: 'Firefox/17.0'

webkit = QtWebKit.QWebView()
webkit.setPage(user_agent)
webkit.load('https://www.freecadweb.org')
webkit.show()
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: Browser inside task panel

Post by easyw-fc »

microelly2 wrote: Fri Oct 26, 2018 1:54 pm

Code: Select all

...
many thanks
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Browser inside task panel

Post by bernd »

related because of deprecated QtWebKit https://forum.freecadweb.org/viewtopic. ... 24#p422371
User avatar
vanuan
Posts: 539
Joined: Wed Oct 24, 2018 9:49 pm

Re: Browser inside task panel

Post by vanuan »

Maybe it's much promising to consider an alternative docking system instead of what Qt provides: https://github.com/githubuser0xFFFF/Qt- ... ing-System

This way you could just use the standard Web workbench (with the hidden toolbar and static HTML content) and dock it where you need it.
To communicate with FreeCAD though, you need some kind of server, as it was already mentioned

Check out this thread with my proof of concept: https://forum.freecadweb.org/viewtopic.php?t=48611
Though it doesn't show how to open a browser in a toolbar, it shows that you might not need it. We just need to initialize multiple workbenches and make MDI windows dockable.

Alternatively, we can open the Web window in fullscreen and reimagine FreeCAD UI altogether. Though, it might be painfully slow, as WebGL is still not as optimized as the native version. But CADCloud is going somewhere: https://forum.freecadweb.org/viewtopic.php?t=43502
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Browser inside task panel

Post by bernd »

PySide2 and Qt > 5.6

Code: Select all

import FreeCADGui
from PySide2 import QtCore
from PySide2 import QtWidgets
from PySide2 import QtWebEngineWidgets

class TransWindow(QtWidgets.QDialog):
    HOME = "https://forum.freecadweb.org/viewtopic.php?f=10&t=25189"
    def __init__(self):
        super(TransWindow, self).__init__()
        self.webView = QtWebEngineWidgets.QWebEngineView()
        self.webView.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addLayout(QtWidgets.QHBoxLayout())
        self.layout.addWidget(self.webView)
    def showEvent(self, event):
        super(TransWindow, self).showEvent(event)
        self.webView.load(QtCore.QUrl(self.HOME))

mw = FreeCADGui.getMainWindow()
d = QtWidgets.QDockWidget()
d.setWidget(TransWindow())
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea, d)


Macro file:

import_part_from_web.FCMacro
(2.6 KiB) Downloaded 59 times
Screenshot_20200809_213731.png
Screenshot_20200809_213731.png (281.34 KiB) Viewed 1803 times
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Browser inside task panel

Post by bernd »

vanuan wrote: Fri Aug 07, 2020 9:56 pm Maybe it's much promising to consider an alternative docking system instead of what Qt provides: https://github.com/githubuser0xFFFF/Qt- ... ing-System
May be. But I will not spend time on this. For me this is just some small side project. Thus I am happy if it just works.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Browser inside task panel

Post by bernd »

bernd wrote: Wed Nov 29, 2017 9:34 pm A browser widget inside FreeCAD which includes the part above in FreeCAD by click on a text button.
https://forum.freecadweb.org/viewtopic. ... 89#p201936 there is the step part too

updated code for Qt > 5.6 and PySide2:

Code: Select all

import os
import tempfile
from urllib.request import urlretrieve
from PySide2 import QtCore
from PySide2 import QtWidgets
from PySide2 import QtWebEngineWidgets
from PySide2 import QtWebChannel

import FreeCAD
import FreeCADGui
import Part


the_html_code = """
<html>
<head>
    <script src="qrc:///qtwebchannel/qwebchannel.js"></script>
</head>
<header><title>title</title></header>
<body>

<p id="output"></p>

<h2><a href="#" onclick="jsWebPartImporter.text('https://forum.freecadweb.org/download/file.php?id=48016')">Click to import a Part from Internet into FreeCAD</a></h2>
<h2><a href="#" onclick="alert('Javascript works!')">Click for Java Script Test</a></h2>

<script type="text/javascript">
    window.onload = function() {
        new QWebChannel(qt.webChannelTransport, function (channel) {
            window.jsWebPartImporter = channel.objects.jsWebPartImporter;
            console.log(jsWebPartImporter);
        });
    }
</script> 
</body>
</html>
"""


class WebPartImporter(QtCore.QObject):
    def __init__(self, parent=None):
        super(WebPartImporter, self).__init__(parent)

    @QtCore.Slot(str)
    def text(self, url_to_step_file):

        print(url_to_step_file)

        # get a temp file with ending step
        tmpstepfile_path = tempfile.NamedTemporaryFile(suffix=".step").name
        print(tmpstepfile_path)

        # download the step file
        urlreturn = urlretrieve(url_to_step_file, tmpstepfile_path)
        print(urlreturn[0])

        # read the step (the read method needs file ending step)
        # and add it to the active document
        # create a new doc if there is no active doc
        Part.show(Part.read(tmpstepfile_path))
        os.remove(tmpstepfile_path)
        print(FreeCAD.ActiveDocument.Name)

        # set the view
        FreeCADGui.ActiveDocument.activeView().viewAxonometric()
        FreeCADGui.SendMsgToActiveView("ViewFit")


class BrowserWidget(QtWidgets.QDialog):
    def __init__(self):
        super(BrowserWidget, self).__init__()
        self.web_view = QtWebEngineWidgets.QWebEngineView()

        channel = QtWebChannel.QWebChannel(self)
        channel.registerObject("jsWebPartImporter", WebPartImporter(self.web_view))
        self.web_view.page().setWebChannel(channel)

        # layout 
        self.web_view.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
        )
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addLayout(QtWidgets.QHBoxLayout())
        self.layout.addWidget(self.web_view)

    def showEvent(self, event):
        super(BrowserWidget, self).showEvent(event)
        self.web_view.setHtml(the_html_code)


mw = FreeCADGui.getMainWindow()
d = QtWidgets.QDockWidget()
d.setWidget(BrowserWidget())
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea, d)

These links have been very helpful:
https://stackoverflow.com/questions/510 ... ndowobject
https://stackoverflow.com/questions/395 ... 7/42740287


as FCMacro file
import_part_from_web.FCMacro
(4.74 KiB) Downloaded 50 times

- start FreeCAD
- open FCMacro file
- run the macro

Screenshot_20200809_224252.png
Screenshot_20200809_224252.png (251.05 KiB) Viewed 1780 times
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Browser inside task panel

Post by bernd »

I encoutered some very strange problem during use of browser inside task panel code ...

- start FreeCAD
- create a new document (important to do this, may be make a box)
- run Python code, a FreeCAD Forum login page will be displayed in the task panel
- try to input the numbers "0123" in the Username field, this does work
- try to input the numbers "0123" in the Password field, this does NOT work
- instead of put the numbers in the field the view in the scene changes (FreeCAD hotkey action for the keys 0, 1, 2, 3)

Code: Select all

import FreeCADGui
from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets
class TransWindow(QtWidgets.QDialog):
    def __init__(self):
        super(TransWindow, self).__init__()
        self.webView = QtWebEngineWidgets.QWebEngineView()
        self.webView.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.layout = QtWidgets.QVBoxLayout(self)
        self.layout.addWidget(self.webView)
    def showEvent(self, event):
        super(TransWindow, self).showEvent(event)
        self.webView.load(QtCore.QUrl("https://forum.freecadweb.org/ucp.php?mode=login"))

mw = FreeCADGui.getMainWindow()
d = QtWidgets.QDockWidget()
d.setWidget(TransWindow())
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea, d)


Code: Select all

OS: Windows 10 (10.0)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.22209 (Git)
Build type: Release
Branch: master
Hash: 9c3f9b72a82249d5fcf1f543dd69a78740251b26
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Switzerland (de_CH)

screen.png
screen.png (262.33 KiB) Viewed 1531 times
Post Reply