|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the GreaseMonkey scripts configuration dialog. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt, QUrl |
|
13 from PyQt5.QtGui import QDesktopServices |
|
14 from PyQt5.QtWidgets import QDialog, QListWidgetItem |
|
15 |
|
16 from E5Gui import E5MessageBox |
|
17 |
|
18 from .Ui_GreaseMonkeyConfigurationDialog import \ |
|
19 Ui_GreaseMonkeyConfigurationDialog |
|
20 |
|
21 import UI.PixmapCache |
|
22 |
|
23 |
|
24 class GreaseMonkeyConfigurationDialog( |
|
25 QDialog, Ui_GreaseMonkeyConfigurationDialog): |
|
26 """ |
|
27 Class implementing the GreaseMonkey scripts configuration dialog. |
|
28 """ |
|
29 ScriptVersionRole = Qt.UserRole |
|
30 ScriptDescriptionRole = Qt.UserRole + 1 |
|
31 ScriptRole = Qt.UserRole + 2 |
|
32 |
|
33 def __init__(self, manager, parent=None): |
|
34 """ |
|
35 Constructor |
|
36 |
|
37 @param manager reference to the manager object (GreaseMonkeyManager) |
|
38 @param parent reference to the parent widget (QWidget) |
|
39 """ |
|
40 super(GreaseMonkeyConfigurationDialog, self).__init__(parent) |
|
41 self.setupUi(self) |
|
42 self.setWindowFlags(Qt.Window) |
|
43 |
|
44 self.iconLabel.setPixmap( |
|
45 UI.PixmapCache.getPixmap("greaseMonkey48.png")) |
|
46 |
|
47 self.__manager = manager |
|
48 |
|
49 self.__loadScripts() |
|
50 |
|
51 self.scriptsList.removeItemRequested.connect(self.__removeItem) |
|
52 self.scriptsList.itemChanged.connect(self.__itemChanged) |
|
53 |
|
54 @pyqtSlot() |
|
55 def on_openDirectoryButton_clicked(self): |
|
56 """ |
|
57 Private slot to open the GreaseMonkey scripts directory. |
|
58 """ |
|
59 QDesktopServices.openUrl( |
|
60 QUrl.fromLocalFile(self.__manager.scriptsDirectory())) |
|
61 |
|
62 @pyqtSlot(str) |
|
63 def on_downloadLabel_linkActivated(self, link): |
|
64 """ |
|
65 Private slot to open the greasyfork.org web site. |
|
66 |
|
67 @param link URL (string) |
|
68 """ |
|
69 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
70 if not link or "userscript.org" in link: |
|
71 # userscript.org is down, default to Greasy Fork. |
|
72 link = "https://greasyfork.org/" |
|
73 WebBrowserWindow.mainWindow().newTab(QUrl(link)) |
|
74 self.close() |
|
75 |
|
76 @pyqtSlot(QListWidgetItem) |
|
77 def on_scriptsList_itemDoubleClicked(self, item): |
|
78 """ |
|
79 Private slot to show information about the selected script. |
|
80 |
|
81 @param item reference to the double clicked item (QListWidgetItem) |
|
82 """ |
|
83 script = self.__getScript(item) |
|
84 if script is not None: |
|
85 from .GreaseMonkeyConfigurationScriptInfoDialog import \ |
|
86 GreaseMonkeyConfigurationScriptInfoDialog |
|
87 infoDlg = GreaseMonkeyConfigurationScriptInfoDialog(script, self) |
|
88 infoDlg.exec_() |
|
89 |
|
90 def __loadScripts(self): |
|
91 """ |
|
92 Private method to load all the available scripts. |
|
93 """ |
|
94 for script in self.__manager.allScripts(): |
|
95 itm = QListWidgetItem(self.scriptsList) |
|
96 itm.setText(script.name()) |
|
97 icon = script.icon() |
|
98 if icon.isNull: |
|
99 icon = UI.PixmapCache.getIcon("greaseMonkeyScript.png") |
|
100 itm.setIcon(icon) |
|
101 itm.setData( |
|
102 GreaseMonkeyConfigurationDialog.ScriptVersionRole, |
|
103 script.version()) |
|
104 itm.setData( |
|
105 GreaseMonkeyConfigurationDialog.ScriptDescriptionRole, |
|
106 script.description()) |
|
107 itm.setFlags(itm.flags() | Qt.ItemIsUserCheckable) |
|
108 if script.isEnabled(): |
|
109 itm.setCheckState(Qt.Checked) |
|
110 else: |
|
111 itm.setCheckState(Qt.Unchecked) |
|
112 itm.setData(GreaseMonkeyConfigurationDialog.ScriptRole, script) |
|
113 self.scriptsList.addItem(itm) |
|
114 |
|
115 self.scriptsList.sortItems() |
|
116 |
|
117 itemMoved = True |
|
118 while itemMoved: |
|
119 itemMoved = False |
|
120 for row in range(self.scriptsList.count()): |
|
121 topItem = self.scriptsList.item(row) |
|
122 bottomItem = self.scriptsList.item(row + 1) |
|
123 if topItem is None or bottomItem is None: |
|
124 continue |
|
125 |
|
126 if topItem.checkState() == Qt.Unchecked and \ |
|
127 bottomItem.checkState == Qt.Checked: |
|
128 itm = self.scriptsList.takeItem(row + 1) |
|
129 self.scriptsList.insertItem(row, itm) |
|
130 itemMoved = True |
|
131 |
|
132 def __getScript(self, itm): |
|
133 """ |
|
134 Private method to get the script for the given item. |
|
135 |
|
136 @param itm item to get script for (QListWidgetItem) |
|
137 @return reference to the script object (GreaseMonkeyScript) |
|
138 """ |
|
139 if itm is None: |
|
140 return None |
|
141 |
|
142 script = itm.data(GreaseMonkeyConfigurationDialog.ScriptRole) |
|
143 return script |
|
144 |
|
145 def __removeItem(self, itm): |
|
146 """ |
|
147 Private slot to remove a script item. |
|
148 |
|
149 @param itm item to be removed (QListWidgetItem) |
|
150 """ |
|
151 script = self.__getScript(itm) |
|
152 if script is None: |
|
153 return |
|
154 |
|
155 removeIt = E5MessageBox.yesNo( |
|
156 self, |
|
157 self.tr("Remove Script"), |
|
158 self.tr( |
|
159 """<p>Are you sure you want to remove <b>{0}</b>?</p>""") |
|
160 .format(script.name())) |
|
161 if removeIt and self.__manager.removeScript(script): |
|
162 self.scriptsList.takeItem(self.scriptsList.row(itm)) |
|
163 del itm |
|
164 |
|
165 def __itemChanged(self, itm): |
|
166 """ |
|
167 Private slot to handle changes of a script item. |
|
168 |
|
169 @param itm changed item (QListWidgetItem) |
|
170 """ |
|
171 script = self.__getScript(itm) |
|
172 if script is None: |
|
173 return |
|
174 |
|
175 if itm.checkState() == Qt.Checked: |
|
176 self.__manager.enableScript(script) |
|
177 else: |
|
178 self.__manager.disableScript(script) |