|
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 import Helpviewer.HelpWindow |
|
70 if not link or "userscript.org" in link: |
|
71 # userscript.org is down, default to Greasy Fork. |
|
72 link = "https://greasyfork.org/" |
|
73 Helpviewer.HelpWindow.HelpWindow.mainWindow().newTab( |
|
74 QUrl(link)) |
|
75 self.close() |
|
76 |
|
77 @pyqtSlot(QListWidgetItem) |
|
78 def on_scriptsList_itemDoubleClicked(self, item): |
|
79 """ |
|
80 Private slot to show information about the selected script. |
|
81 |
|
82 @param item reference to the double clicked item (QListWidgetItem) |
|
83 """ |
|
84 script = self.__getScript(item) |
|
85 if script is not None: |
|
86 from .GreaseMonkeyConfigurationScriptInfoDialog import \ |
|
87 GreaseMonkeyConfigurationScriptInfoDialog |
|
88 infoDlg = GreaseMonkeyConfigurationScriptInfoDialog(script, self) |
|
89 infoDlg.exec_() |
|
90 |
|
91 def __loadScripts(self): |
|
92 """ |
|
93 Private method to load all the available scripts. |
|
94 """ |
|
95 for script in self.__manager.allScripts(): |
|
96 itm = QListWidgetItem( |
|
97 UI.PixmapCache.getIcon("greaseMonkeyScript.png"), |
|
98 script.name(), self.scriptsList) |
|
99 itm.setData( |
|
100 GreaseMonkeyConfigurationDialog.ScriptVersionRole, |
|
101 script.version()) |
|
102 itm.setData( |
|
103 GreaseMonkeyConfigurationDialog.ScriptDescriptionRole, |
|
104 script.description()) |
|
105 itm.setFlags(itm.flags() | Qt.ItemIsUserCheckable) |
|
106 if script.isEnabled(): |
|
107 itm.setCheckState(Qt.Checked) |
|
108 else: |
|
109 itm.setCheckState(Qt.Unchecked) |
|
110 itm.setData(GreaseMonkeyConfigurationDialog.ScriptRole, script) |
|
111 self.scriptsList.addItem(itm) |
|
112 |
|
113 self.scriptsList.sortItems() |
|
114 |
|
115 itemMoved = True |
|
116 while itemMoved: |
|
117 itemMoved = False |
|
118 for row in range(self.scriptsList.count()): |
|
119 topItem = self.scriptsList.item(row) |
|
120 bottomItem = self.scriptsList.item(row + 1) |
|
121 if topItem is None or bottomItem is None: |
|
122 continue |
|
123 |
|
124 if topItem.checkState() == Qt.Unchecked and \ |
|
125 bottomItem.checkState == Qt.Checked: |
|
126 itm = self.scriptsList.takeItem(row + 1) |
|
127 self.scriptsList.insertItem(row, itm) |
|
128 itemMoved = True |
|
129 |
|
130 def __getScript(self, itm): |
|
131 """ |
|
132 Private method to get the script for the given item. |
|
133 |
|
134 @param itm item to get script for (QListWidgetItem) |
|
135 @return reference to the script object (GreaseMonkeyScript) |
|
136 """ |
|
137 if itm is None: |
|
138 return None |
|
139 |
|
140 script = itm.data(GreaseMonkeyConfigurationDialog.ScriptRole) |
|
141 return script |
|
142 |
|
143 def __removeItem(self, itm): |
|
144 """ |
|
145 Private slot to remove a script item. |
|
146 |
|
147 @param itm item to be removed (QListWidgetItem) |
|
148 """ |
|
149 script = self.__getScript(itm) |
|
150 if script is None: |
|
151 return |
|
152 |
|
153 removeIt = E5MessageBox.yesNo( |
|
154 self, |
|
155 self.tr("Remove Script"), |
|
156 self.tr( |
|
157 """<p>Are you sure you want to remove <b>{0}</b>?</p>""") |
|
158 .format(script.name())) |
|
159 if removeIt and self.__manager.removeScript(script): |
|
160 self.scriptsList.takeItem(self.scriptsList.row(itm)) |
|
161 del itm |
|
162 |
|
163 def __itemChanged(self, itm): |
|
164 """ |
|
165 Private slot to handle changes of a script item. |
|
166 |
|
167 @param itm changed item (QListWidgetItem) |
|
168 """ |
|
169 script = self.__getScript(itm) |
|
170 if script is None: |
|
171 return |
|
172 |
|
173 if itm.checkState() == Qt.Checked: |
|
174 self.__manager.enableScript(script) |
|
175 else: |
|
176 self.__manager.disableScript(script) |