|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 |
|
7 """ |
|
8 Module implementing a dialog for adding GreaseMonkey scripts.. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 from PyQt4.QtCore import pyqtSlot, QDir, QFile |
|
14 from PyQt4.QtGui import QDialog |
|
15 |
|
16 from E5Gui import E5MessageBox |
|
17 |
|
18 from .Ui_GreaseMonkeyAddScriptDialog import Ui_GreaseMonkeyAddScriptDialog |
|
19 |
|
20 from QScintilla.MiniEditor import MiniEditor |
|
21 |
|
22 from Helpviewer import HelpUtilities |
|
23 |
|
24 import UI.PixmapCache |
|
25 |
|
26 |
|
27 class GreaseMonkeyAddScriptDialog(QDialog, Ui_GreaseMonkeyAddScriptDialog): |
|
28 """ |
|
29 Class implementing a dialog for adding GreaseMonkey scripts.. |
|
30 """ |
|
31 def __init__(self, manager, script, parent=None): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param manager reference to the GreaseMonkey manager (GreaseMonkeyManager) |
|
36 @param script GreaseMonkey script to be added (GreaseMonkeyScript) |
|
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 self.__script = script |
|
46 |
|
47 runsAt = "" |
|
48 doesNotRunAt = "" |
|
49 |
|
50 include = script.include() |
|
51 exclude = script.exclude() |
|
52 |
|
53 if include: |
|
54 runsAt = self.trUtf8("<p>runs at:<br/><i>{0}</i></p>").format( |
|
55 "<br/>".join(include)) |
|
56 |
|
57 if exclude: |
|
58 doesNotRunAt = self.trUtf8("<p>does not run at:<br/><i>{0}</i></p>").format( |
|
59 "<br/>".join(exclude)) |
|
60 |
|
61 scriptInfoTxt = "<p><b>{0}</b> {1}<br/>{2}</p>{3}{4}".format( |
|
62 script.name(), script.version(), script.description(), runsAt, doesNotRunAt) |
|
63 self.scriptInfo.setHtml(scriptInfoTxt) |
|
64 |
|
65 self.accepted.connect(self.__accepted) |
|
66 |
|
67 @pyqtSlot() |
|
68 def on_showScriptSourceButton_clicked(self): |
|
69 """ |
|
70 Private slot to show an editor window with the source code. |
|
71 """ |
|
72 tmpFileName = HelpUtilities.ensureUniqueFilename( |
|
73 os.path.join(QDir.tempPath(), "tmp-userscript.js")) |
|
74 if QFile.copy(self.__script.fileName(), tmpFileName): |
|
75 editor = MiniEditor(tmpFileName, "JavaScript", self) |
|
76 editor.show() |
|
77 |
|
78 def __accepted(self): |
|
79 """ |
|
80 Private slot handling the accepted signal. |
|
81 """ |
|
82 if self.__manager.addScript(self.__script): |
|
83 msg = self.trUtf8("<p><b>{0}</b> installed successfully.</p>").format( |
|
84 self.__script.name()) |
|
85 else: |
|
86 msg = self.trUtf8("<p>Cannot install script.</p>") |
|
87 |
|
88 E5MessageBox.information(self, |
|
89 self.trUtf8("GreaseMonkey Script Installation"), |
|
90 msg) |