|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Python side for GreaseMonkey scripts. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot, QObject, QSettings |
|
11 from PyQt6.QtGui import QGuiApplication |
|
12 |
|
13 |
|
14 class GreaseMonkeyJsObject(QObject): |
|
15 """ |
|
16 Class implementing the Python side for GreaseMonkey scripts. |
|
17 """ |
|
18 def __init__(self, parent=None): |
|
19 """ |
|
20 Constructor |
|
21 |
|
22 @param parent reference to the parent object |
|
23 @type QObject |
|
24 """ |
|
25 super().__init__(parent) |
|
26 |
|
27 self.__settings = None |
|
28 |
|
29 def setSettingsFile(self, name): |
|
30 """ |
|
31 Public method to set the settings file for the GreaseMonkey parameters. |
|
32 |
|
33 @param name name of the settings file |
|
34 @type str |
|
35 """ |
|
36 if self.__settings is not None: |
|
37 self.__settings.sync() |
|
38 self.__settings = None |
|
39 |
|
40 self.__settings = QSettings(name, QSettings.Format.IniFormat) |
|
41 |
|
42 @pyqtSlot(str, str, str) |
|
43 def getValue(self, nspace, name, dValue): |
|
44 """ |
|
45 Public slot to get the value for the named variable for the identified |
|
46 script. |
|
47 |
|
48 @param nspace unique script id |
|
49 @type str |
|
50 @param name name of the variable |
|
51 @type str |
|
52 @param dValue default value |
|
53 @type str |
|
54 @return value for the named variable |
|
55 @rtype str |
|
56 """ |
|
57 vName = "GreaseMonkey-{0}/{1}".format(nspace, name) |
|
58 sValue = self.__settings.value(vName, dValue) |
|
59 if not sValue: |
|
60 return dValue |
|
61 |
|
62 return sValue |
|
63 |
|
64 @pyqtSlot(str, str, str) |
|
65 def setValue(self, nspace, name, value): |
|
66 """ |
|
67 Public slot to set the value for the named variable for the identified |
|
68 script. |
|
69 |
|
70 @param nspace unique script id |
|
71 @type str |
|
72 @param name name of the variable |
|
73 @type str |
|
74 @param value value to be set |
|
75 @type str |
|
76 @return flag indicating success |
|
77 @rtype bool |
|
78 """ |
|
79 vName = "GreaseMonkey-{0}/{1}".format(nspace, name) |
|
80 self.__settings.setValue(vName, value) |
|
81 self.__settings.sync() |
|
82 return True |
|
83 |
|
84 @pyqtSlot(str, str) |
|
85 def deleteValue(self, nspace, name): |
|
86 """ |
|
87 Public slot to set delete the named variable for the identified script. |
|
88 |
|
89 @param nspace unique script id |
|
90 @type str |
|
91 @param name name of the variable |
|
92 @type str |
|
93 @return flag indicating success |
|
94 @rtype bool |
|
95 """ |
|
96 vName = "GreaseMonkey-{0}/{1}".format(nspace, name) |
|
97 self.__settings.remove(vName) |
|
98 self.__settings.sync() |
|
99 return True |
|
100 |
|
101 @pyqtSlot(str) |
|
102 def listValues(self, nspace): |
|
103 """ |
|
104 Public slot to list the stored variables for the identified script. |
|
105 |
|
106 @param nspace unique script id |
|
107 @type str |
|
108 @return list of stored variables |
|
109 @rtype list of str |
|
110 """ |
|
111 nspaceName = "GreaseMonkey-{0}".format(nspace) |
|
112 self.__settings.beginGroup(nspaceName) |
|
113 keys = self.__settings.allKeys() |
|
114 self.__settings.endGroup() |
|
115 |
|
116 return keys |
|
117 |
|
118 @pyqtSlot(str) |
|
119 def setClipboard(self, text): |
|
120 """ |
|
121 Public slot to set some clipboard text. |
|
122 |
|
123 @param text text to be copied to the clipboard |
|
124 @type str |
|
125 """ |
|
126 QGuiApplication.clipboard().setText(text) |