src/eric7/WebBrowser/GreaseMonkey/GreaseMonkeyAddScriptDialog.py

Fri, 04 Nov 2022 13:52:26 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 04 Nov 2022 13:52:26 +0100
branch
eric7
changeset 9473
3f23dbf37dbe
parent 9413
80c06d472826
child 9482
a2bc06a54d9d
permissions
-rw-r--r--

Resorted the import statements using isort.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#


"""
Module implementing a dialog for adding GreaseMonkey scripts..
"""

import os
import shutil

from PyQt6.QtCore import QDir, pyqtSlot
from PyQt6.QtWidgets import QDialog

from eric7.EricGui import EricPixmapCache
from eric7.UI.NotificationWidget import NotificationTypes

from .Ui_GreaseMonkeyAddScriptDialog import Ui_GreaseMonkeyAddScriptDialog


class GreaseMonkeyAddScriptDialog(QDialog, Ui_GreaseMonkeyAddScriptDialog):
    """
    Class implementing a dialog for adding GreaseMonkey scripts..
    """

    def __init__(self, manager, script, parent=None):
        """
        Constructor

        @param manager reference to the GreaseMonkey manager
            (GreaseMonkeyManager)
        @param script GreaseMonkey script to be added (GreaseMonkeyScript)
        @param parent reference to the parent widget (QWidget)
        """
        super().__init__(parent)
        self.setupUi(self)

        self.iconLabel.setPixmap(EricPixmapCache.getPixmap("greaseMonkey48"))

        self.__manager = manager
        self.__script = script

        runsAt = ""
        doesNotRunAt = ""

        include = script.include()
        exclude = script.exclude()

        if include:
            runsAt = self.tr("<p>runs at:<br/><i>{0}</i></p>").format(
                "<br/>".join(include)
            )

        if exclude:
            doesNotRunAt = self.tr("<p>does not run at:<br/><i>{0}</i></p>").format(
                "<br/>".join(exclude)
            )

        scriptInfoTxt = "<p><b>{0}</b> {1}<br/>{2}</p>{3}{4}".format(
            script.name(), script.version(), script.description(), runsAt, doesNotRunAt
        )
        self.scriptInfo.setHtml(scriptInfoTxt)

        self.accepted.connect(self.__accepted)

    @pyqtSlot()
    def on_showScriptSourceButton_clicked(self):
        """
        Private slot to show an editor window with the source code.
        """
        from eric7.WebBrowser.Tools import WebBrowserTools

        tmpFileName = WebBrowserTools.ensureUniqueFilename(
            os.path.join(QDir.tempPath(), "tmp-userscript.js")
        )
        if shutil.copy(self.__script.fileName(), tmpFileName):
            from eric7.QScintilla.MiniEditor import MiniEditor

            editor = MiniEditor(tmpFileName, "JavaScript", self)
            editor.show()

    def __accepted(self):
        """
        Private slot handling the accepted signal.
        """
        if self.__manager.addScript(self.__script):
            msg = self.tr("<p><b>{0}</b> installed successfully.</p>").format(
                self.__script.name()
            )
            success = True
        else:
            msg = self.tr("<p>Cannot install script.</p>")
            success = False

        from eric7.WebBrowser.WebBrowserWindow import WebBrowserWindow

        if success:
            WebBrowserWindow.showNotification(
                EricPixmapCache.getPixmap("greaseMonkey48"),
                self.tr("GreaseMonkey Script Installation"),
                msg,
            )
        else:
            WebBrowserWindow.showNotification(
                EricPixmapCache.getPixmap("greaseMonkey48"),
                self.tr("GreaseMonkey Script Installation"),
                msg,
                kind=NotificationTypes.CRITICAL,
                timeout=0,
            )

eric ide

mercurial