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