예제 #1
0
class XEmbeddable(QWidget, _HalWidgetBase):
    def __init__(self, parent=None):
        super(XEmbeddable, self).__init__(parent)

    def _hal_init(self):
        pass

    def embed(self, command):
        if '-x {XID}' in command:
            # convert gladevcp to other type of embedding
            command = command.replace('-x {XID}', ' --xid ')
            self.embed_program(command)
        elif '{XID}' in command:
            self.let_program_embed(command)
        else:
            self.embed_program(command)

    # foreign program embeds it's self in our window
    def let_program_embed(self,command):
        self.w = QWindow()
        self.container = QWidget.createWindowContainer(self.w, self)

        xid = int(self.w.winId())
        cmd = command.replace('{XID}', str(xid))
        self.launch(cmd)
        self.show()
        # there seems to be a race - sometimes the foreign window doesn't embed
        time.sleep(.2)

    # we embed foreign program into our window 
    def embed_program(self, command): 
        try:
            self.external_id = self.launch_xid(command)
            window = QWindow.fromWinId(self.external_id)
            self.container = QWidget.createWindowContainer(window, self)
            self.show()
            # there seems to be a race - sometimes the foreign window doesn't embed
            time.sleep(.2)
            return True
        except  Exception, e:
            LOG.warning('Exception:{}'.format(command))
            LOG.warning('Exception:{}'.format( e))
            raise Exception(e)
            return False
예제 #2
0
파일: xembed.py 프로젝트: TurBoss/linuxcnc
class XEmbeddable(QWidget, _HalWidgetBase):
    def __init__(self, parent=None):
        super(XEmbeddable, self).__init__(parent)

    def _hal_init(self):
        pass

    def embed(self, command):
        if '-x {XID}' in command:
            # convert gladevcp to other type of embedding
            command = command.replace('-x {XID}', ' --xid ')
            self.embed_program(command)
        elif '{XID}' in command:
            self.let_program_embed(command)
        else:
            self.embed_program(command)

    # foreign program embeds it's self in our window
    def let_program_embed(self, command):
        self.w = QWindow()
        self.container = QWidget.createWindowContainer(self.w, self)

        xid = int(self.w.winId())
        cmd = command.replace('{XID}', str(xid))
        self.launch(cmd)
        self.show()
        # there seems to be a race - sometimes the foreign window doesn't embed
        time.sleep(.2)

    # we embed foreign program into our window
    def embed_program(self, command):
        try:
            self.external_id = self.launch_xid(command)
            window = QWindow.fromWinId(self.external_id)
            self.container = QWidget.createWindowContainer(window, self)
            self.show()
            # there seems to be a race - sometimes the foreign window doesn't embed
            time.sleep(.2)
            return True
        except Exception as e:
            LOG.warning('Exception:{}'.format(command))
            LOG.warning('Exception:{}'.format(e))
            raise Exception(e)
            return False

    def event(self, event):
        if event.type() == QEvent.Resize:
            w = QResizeEvent.size(event)
            try:
                self.container.resize(w.width(), w.height())
            except:
                pass
        elif event.type() == QEvent.Move:
            try:
                self.container.move(QMoveEvent.pos(event))
            except:
                pass
        return True

    # launches program that embeds it's self into our window
    # The foreign program never resizes as the window changes
    def launch(self, cmd):
        c = cmd.split()
        self.ob = subprocess.Popen(c,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE)

    # returns program's XID number so we can embed it into our window
    # This works the best - the window resizes properly
    # still not great qt4 worked so much better
    def launch_xid(self, cmd):
        c = cmd.split()
        self.ob = subprocess.Popen(c,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE)
        sid = self.ob.stdout.readline()
        LOG.debug('XID: {}'.format(sid))
        return int(sid)

    def _hal_cleanup(self):
        try:
            self.ob.terminate()
        except Exception as e:
            print(e)
예제 #3
0
class CefWidget(QWidget):
    """ CefPython Viewer"""
    browser = None

    def __init__(self, parent=None):
        super(CefWidget, self).__init__(parent)
        self.parent = parent
        self.browser = None
        self.hidden_window = None  # Required for PyQt5 on Linux
        self.show()

    def focusInEvent(self, _):
        if self.browser:
            if WINDOWS:
                WindowUtils.OnSetFocus(self.getHandle(), 0, 0, 0)
            self.browser.SetFocus(True)

    def focusOutEvent(self, _):
        if self.browser:
            self.browser.SetFocus(False)

    def embedBrowser(self):
        if LINUX:
            self.hidden_window = QWindow()
        windowInfo = cefpython.WindowInfo()
        rect = [0, 0, self.width(), self.height()]
        windowInfo.SetAsChild(self.getHandle(), rect)

        self.browser = cefpython.CreateBrowserSync(
            windowInfo, browserSettings={}, navigateUrl=config.DEFAULT_URL)

        # Add Handler
        self.browser.SetClientHandler(ClientHandler())

    def getHandle(self):
        if self.hidden_window:
            return int(self.hidden_window.winId())
        try:
            return int(self.winId())
        except Exception:
            ctypes.pythonapi.PyCapsule_GetPointer.restype = (ctypes.c_void_p)
            ctypes.pythonapi.PyCapsule_GetPointer.argtypes = ([
                ctypes.py_object
            ])
            return ctypes.pythonapi.PyCapsule_GetPointer(self.winId(), None)

    def moveEvent(self, _):
        if self.browser:
            if WINDOWS:
                WindowUtils.OnSize(self.getHandle(), 0, 0, 0)
            elif LINUX:
                self.browser.SetBounds(self.x, self.y, self.width(),
                                       self.height())
            self.browser.NotifyMoveOrResizeStarted()

    def resizeEvent(self, event):
        size = event.size()
        if self.browser:
            if WINDOWS:
                WindowUtils.OnSize(self.getHandle(), 0, 0, 0)
            elif LINUX:
                self.browser.SetBounds(self.x, self.y, size.width(),
                                       size.height())
            self.browser.NotifyMoveOrResizeStarted()
예제 #4
0
 def from_qwindow(cls, window: QWindow):
     return cls(window.winId())