From 43e886a1b0fcbbcca5e57c278f1a42dec6351a24 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 26 Feb 2018 18:18:56 +0100 Subject: [PATCH] Add example hellopython Python extension --- src/CMakeLists.txt | 1 + src/scripts/CMakeLists.txt | 5 ++ src/scripts/hellopython/__init__.py | 18 ++++ src/scripts/hellopython/button.py | 40 +++++++++ src/scripts/hellopython/hellopython.py | 106 +++++++++++++++++++++++ src/scripts/hellopython/metadata.desktop | 11 +++ src/scripts/hellopython/sidebar.py | 39 +++++++++ 7 files changed, 220 insertions(+) create mode 100644 src/scripts/CMakeLists.txt create mode 100644 src/scripts/hellopython/__init__.py create mode 100644 src/scripts/hellopython/button.py create mode 100644 src/scripts/hellopython/hellopython.py create mode 100644 src/scripts/hellopython/metadata.desktop create mode 100644 src/scripts/hellopython/sidebar.py diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 990fed34a..f0f8c1614 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(lib) add_subdirectory(main) add_subdirectory(plugins) +add_subdirectory(scripts) diff --git a/src/scripts/CMakeLists.txt b/src/scripts/CMakeLists.txt new file mode 100644 index 000000000..d7264839b --- /dev/null +++ b/src/scripts/CMakeLists.txt @@ -0,0 +1,5 @@ +function(install_python_script name) + install(DIRECTORY ${name} DESTINATION "${FALKON_INSTALL_PLUGINDIR}/python") +endfunction() + +install_python_script(hellopython) diff --git a/src/scripts/hellopython/__init__.py b/src/scripts/hellopython/__init__.py new file mode 100644 index 000000000..ab27dc4ab --- /dev/null +++ b/src/scripts/hellopython/__init__.py @@ -0,0 +1,18 @@ +# ============================================================ +# HelloPython plugin for Falkon +# Copyright (C) 2018 David Rosca +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# ============================================================ +from .hellopython import * diff --git a/src/scripts/hellopython/button.py b/src/scripts/hellopython/button.py new file mode 100644 index 000000000..01c78ff5a --- /dev/null +++ b/src/scripts/hellopython/button.py @@ -0,0 +1,40 @@ +# ============================================================ +# HelloPython plugin for Falkon +# Copyright (C) 2018 David Rosca +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# ============================================================ +import Falkon +from PySide2 import QtGui, QtWidgets + +class HelloButton(Falkon.AbstractButtonInterface): + def __init__(self): + super().__init__() + self.setIcon(QtGui.QIcon(":icons/other/about.svg")) + self.setTitle("HelloPython") + self.setToolTip("Hello Python") + + self.clicked.connect(self.onClicked) + + def id(self): + return "hellopython-button" + + def name(self): + return "HelloPython button" + + def onClicked(self, controller): + self.menu = QtWidgets.QMenu() + self.menu.addAction("Hello Python", lambda: print("clicked")) + self.menu.popup(controller.callPopupPosition(self.menu.sizeHint())) + self.menu.aboutToHide.connect(controller.callPopupClosed) diff --git a/src/scripts/hellopython/hellopython.py b/src/scripts/hellopython/hellopython.py new file mode 100644 index 000000000..8bae45bac --- /dev/null +++ b/src/scripts/hellopython/hellopython.py @@ -0,0 +1,106 @@ +# ============================================================ +# HelloPython plugin for Falkon +# Copyright (C) 2018 David Rosca +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# ============================================================ +import Falkon +from PySide2 import QtCore, QtGui, QtWidgets +from hellopython import sidebar, button + +class HelloPlugin(Falkon.PluginInterface, QtCore.QObject): + buttons = {} + + def init(self, state, settingsPath): + print("{} {}".format(state, settingsPath)) + + plugins = Falkon.MainApplication.instance().plugins() + plugins.registerAppEventHandler(Falkon.PluginProxy.MousePressHandler, self) + + plugins.mainWindowCreated.connect(self.mainWindowCreated) + plugins.mainWindowDeleted.connect(self.mainWindowDeleted) + + self.sidebar = sidebar.HelloSidebar() + Falkon.SideBarManager.addSidebar("hellopython-sidebar", self.sidebar) + + if state == Falkon.PluginInterface.LateInitState: + for window in Falkon.MainApplication.instance().windows(): + self.mainWindowCreated(window) + + def unload(self): + print("unload") + + Falkon.SideBarManager.removeSidebar("hellopython-sidebar") + + for window in Falkon.MainApplication.instance().windows(): + self.mainWindowDeleted(window) + + def testPlugin(self): + return True + + def populateWebViewMenu(self, menu, view, r): + self.view = view + + title = "" + if not r.imageUrl().isEmpty(): + title += " on image" + + if not r.linkUrl().isEmpty(): + title += " on link" + + if r.isContentEditable(): + title += " on input" + + menu.addAction("My first plugin action" + title, self.actionSlot) + + def mousePress(self, type, obj, event): + print("mousePress {} {} {}".format(type, obj, event)) + return False + + def actionSlot(self): + QtWidgets.QMessageBox.information(self.view, "Hello", "First plugin action works :-)") + + def showSettings(self, parent): + self.settings = QtWidgets.QDialog(parent) + b = QtWidgets.QPushButton("Hello Python v0.0.1") + closeButton = QtWidgets.QPushButton("Close") + label = QtWidgets.QLabel() + label.setPixmap(QtGui.QPixmap(":icons/other/about.svg")) + + l = QtWidgets.QVBoxLayout(self.settings) + l.addWidget(label) + l.addWidget(b) + l.addWidget(closeButton) + + self.settings.setAttribute(QtCore.Qt.WA_DeleteOnClose) + self.settings.setWindowTitle("Hello Python Settings") + self.settings.setWindowIcon(QtGui.QIcon(":icons/falkon.svg")) + closeButton.clicked.connect(self.settings.close) + + self.settings.show() + + def mainWindowCreated(self, window): + b = button.HelloButton() + window.statusBar().addButton(b) + window.navigationBar().addToolButton(b) + self.buttons[window] = b + + def mainWindowDeleted(self, window): + if not window in self.buttons: return + b = self.buttons[window] + window.statusBar().removeButton(b) + window.navigationBar().removeToolButton(b) + del self.buttons[window] + +Falkon.registerPlugin(HelloPlugin()) diff --git a/src/scripts/hellopython/metadata.desktop b/src/scripts/hellopython/metadata.desktop new file mode 100644 index 000000000..246d04140 --- /dev/null +++ b/src/scripts/hellopython/metadata.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Name=Hello Python +Comment=Example Python extension + +Icon= +Type=Service + +X-Falkon-Author=David Rosca +X-Falkon-Email=nowrep@gmail.com +X-Falkon-Version=0.1.0 +X-Falkon-Settings=true diff --git a/src/scripts/hellopython/sidebar.py b/src/scripts/hellopython/sidebar.py new file mode 100644 index 000000000..1af6e2e8a --- /dev/null +++ b/src/scripts/hellopython/sidebar.py @@ -0,0 +1,39 @@ +# ============================================================ +# HelloPython plugin for Falkon +# Copyright (C) 2018 David Rosca +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# ============================================================ +import Falkon +from PySide2 import QtGui, QtWidgets + +class HelloSidebar(Falkon.SideBarInterface): + def title(self): + return "HelloPython Sidebar" + + def createMenuAction(self): + act = QtWidgets.QAction("Hello Python Sidebar") + act.setCheckable(True) + return act + + def createSideBarWidget(self, window): + w = QtWidgets.QWidget() + b = QtWidgets.QPushButton("Hello Python v0.0.1"); + label = QtWidgets.QLabel() + label.setPixmap(QtGui.QPixmap(":icons/other/about.svg")); + l = QtWidgets.QVBoxLayout(w); + l.addWidget(label); + l.addWidget(b); + w.setLayout(l); + return w