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