Falkon Develop
Cross-platform Qt-based web browser
hellopython.py
Go to the documentation of this file.
1# ============================================================
2# HelloPython plugin for Falkon
3# Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17# ============================================================
18import Falkon
19from PySide6 import QtCore, QtGui, QtWidgets
20from hellopython import sidebar, button
21from hellopython.i18n import i18n
22
23class HelloPlugin(Falkon.PluginInterface, QtCore.QObject):
24 buttons = {}
25
26 def init(self, state, settingsPath):
27 print("{} {}".format(state, settingsPath))
28
29 plugins = Falkon.MainApplication.instance().plugins()
30 plugins.registerAppEventHandler(Falkon.PluginProxy.MousePressHandler, self)
31
32 plugins.mainWindowCreated.connect(self.mainWindowCreated)
33 plugins.mainWindowDeleted.connect(self.mainWindowDeleted)
34
36 Falkon.SideBarManager.addSidebar("hellopython-sidebar", self.sidebar)
37
39 Falkon.MainApplication.instance().networkManager().registerExtensionSchemeHandler("hello", self.schemeHandler)
40
41 if state == Falkon.PluginInterface.LateInitState:
42 for window in Falkon.MainApplication.instance().windows():
43 self.mainWindowCreated(window)
44
45 def unload(self):
46 print("unload")
47
48 Falkon.SideBarManager.removeSidebar(self.sidebar)
49
50 for window in Falkon.MainApplication.instance().windows():
51 self.mainWindowDeleted(window)
52
53 def testPlugin(self):
54 return True
55
56 def populateWebViewMenu(self, menu, view, r):
57 self.view = view
58
59 title = ""
60 if not r.imageUrl().isEmpty():
61 title += " on image"
62
63 if not r.linkUrl().isEmpty():
64 title += " on link"
65
66 if r.isContentEditable():
67 title += " on input"
68
69 menu.addAction(i18n("My first plugin action") + title, self.actionSlot)
70
71 def mousePress(self, type, obj, event):
72 print("mousePress {} {} {}".format(type, obj, event))
73 return False
74
75 def actionSlot(self):
76 QtWidgets.QMessageBox.information(self.view, i18n("Hello"), i18n("First plugin action works :-)"))
77
78 def showSettings(self, parent):
79 self.settings = QtWidgets.QDialog(parent)
80 b = QtWidgets.QPushButton("Hello Python v0.0.1")
81 closeButton = QtWidgets.QPushButton(i18n("Close"))
82 label = QtWidgets.QLabel()
83 label.setPixmap(QtGui.QPixmap(":icons/other/about.svg"))
84
85 l = QtWidgets.QVBoxLayout(self.settings)
86 l.addWidget(label)
87 l.addWidget(b)
88 l.addWidget(closeButton)
89
90 self.settings.setAttribute(QtCore.Qt.WA_DeleteOnClose)
91 self.settings.setWindowTitle(i18n("Hello Python Settings"))
92 self.settings.setWindowIcon(QtGui.QIcon(":icons/falkon.svg"))
93 closeButton.clicked.connect(self.settings.close)
94
95 self.settings.show()
96
97 def mainWindowCreated(self, window):
99 window.statusBar().addButton(b)
100 window.navigationBar().addToolButton(b)
101 self.buttons[window] = b
102
103 def mainWindowDeleted(self, window):
104 if not window in self.buttons: return
105 b = self.buttons[window]
106 window.statusBar().removeButton(b)
107 window.navigationBar().removeToolButton(b)
108 del self.buttons[window]
109
110Falkon.registerPlugin(HelloPlugin())
111
112class HelloSchemeHandler(Falkon.ExtensionSchemeHandler):
113 def requestStarted(self, job):
114 print("req {}".format(job.requestUrl()))
115 self.setReply(job, "text/html", "<h1>TEST</h1>{}".format(job.requestUrl()))
def populateWebViewMenu(self, menu, view, r)
Definition: hellopython.py:56
def init(self, state, settingsPath)
Definition: hellopython.py:26
def mainWindowCreated(self, window)
Definition: hellopython.py:97
def mousePress(self, type, obj, event)
Definition: hellopython.py:71
Definition: i18n.py:1