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