Esempio n. 1
0
class HTMLViewerDialog(QDialog):
    """This class implements a dialog to view a piece of HTML text."""

    def __init__(self, parent, config_name=None, buttons=[], *args):
        """Creates dialog.
        'config_name' is used to get/set default window size from Config object
        'buttons' can be a list of names or (QPixmapWrapper,name[,tooltip]) tuples to provide
        custom buttons at the bottom of the dialog. When a button is clicked, the dialog
        emits SIGNAL("name").
        A "Close" button is always provided, this simply hides the dialog.
        """
        QDialog.__init__(self, parent, *args)
        self.setModal(False)
        lo = QVBoxLayout(self)
        # create viewer
        self.label = QLabel(self)
        self.label.setMargin(5)
        self.label.setWordWrap(True)
        lo.addWidget(self.label)
        self.label.hide()
        self.viewer = QTextBrowser(self)
        lo.addWidget(self.viewer)
        # self.viewer.setReadOnly(True)
        self.viewer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        QObject.connect(self.viewer, SIGNAL("anchorClicked(const QUrl &)"), self._urlClicked)
        self._source = None
        lo.addSpacing(5)
        # create button bar
        btnfr = QFrame(self)
        btnfr.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
        # btnfr.setMargin(5)
        lo.addWidget(btnfr)
        lo.addSpacing(5)
        btnfr_lo = QHBoxLayout(btnfr)
        btnfr_lo.setMargin(5)
        # add user buttons
        self._user_buttons = {}
        for name in buttons:
            if isinstance(name, str):
                btn = QPushButton(name, btnfr)
            elif isinstance(name, (list, tuple)):
                if len(name) < 3:
                    pixmap, name = name
                    tip = None
                else:
                    pixmap, name, tip = name
                btn = QPushButton(pixmap.icon(), name, btnfr)
                if tip:
                    btn.setToolTip(tip)
            self._user_buttons[name] = btn
            btn._clicked = Kittens.utils.curry(self.emit, SIGNAL(name))
            self.connect(btn, SIGNAL("clicked()"), btn._clicked)
            btnfr_lo.addWidget(btn, 1)
        # add a Close button
        btnfr_lo.addStretch(100)
        closebtn = QPushButton(pixmaps.grey_round_cross.icon(), "Close", btnfr)
        self.connect(closebtn, SIGNAL("clicked()"), self.hide)
        btnfr_lo.addWidget(closebtn, 1)
        # resize selves
        self.config_name = config_name or "html-viewer"
        width = Config.getint('%s-width' % self.config_name, 512)
        height = Config.getint('%s-height' % self.config_name, 512)
        self.resize(QSize(width, height))

    def resizeEvent(self, ev):
        QDialog.resizeEvent(self, ev)
        sz = ev.size()
        Config.set('%s-width' % self.config_name, sz.width())
        Config.set('%s-height' % self.config_name, sz.height())

    def setDocument(self, filename, empty=""):
        """Sets the HTML text to be displayed. """
        self._source = QUrl.fromLocalFile(filename)
        if os.path.exists(filename):
            self.viewer.setSource(self._source)
        else:
            self.viewer.setText(empty)

    def _urlClicked(self, url):
        path = str(url.path())
        if path:
            self.emit(SIGNAL("viewPath"), path)
        # to make sure it keeps displaying the same thing
        self.viewer.setSource(self._source)

    def reload(self):
        self.viewer.reload()

    def setLabel(self, label=None):
        if label is None:
            self.label.hide()
        else:
            self.label.setText(label)
            self.label.show()
Esempio n. 2
0
class HTMLViewerDialog(QDialog):
    """This class implements a dialog to view a piece of HTML text."""
    def __init__(self, parent, config_name=None, buttons=[], *args):
        """Creates dialog.
        'config_name' is used to get/set default window size from Config object
        'buttons' can be a list of names or (QPixmapWrapper,name[,tooltip]) tuples to provide
        custom buttons at the bottom of the dialog. When a button is clicked, the dialog
        emits SIGNAL("name").
        A "Close" button is always provided, this simply hides the dialog.
        """
        QDialog.__init__(self, parent, *args)
        self.setModal(False)
        lo = QVBoxLayout(self)
        # create viewer
        self.label = QLabel(self)
        self.label.setMargin(5)
        self.label.setWordWrap(True)
        lo.addWidget(self.label)
        self.label.hide()
        self.viewer = QTextBrowser(self)
        lo.addWidget(self.viewer)
        # self.viewer.setReadOnly(True)
        self.viewer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        QObject.connect(self.viewer, SIGNAL("anchorClicked(const QUrl &)"),
                        self._urlClicked)
        self._source = None
        lo.addSpacing(5)
        # create button bar
        btnfr = QFrame(self)
        btnfr.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
        # btnfr.setMargin(5)
        lo.addWidget(btnfr)
        lo.addSpacing(5)
        btnfr_lo = QHBoxLayout(btnfr)
        btnfr_lo.setMargin(5)
        # add user buttons
        self._user_buttons = {}
        for name in buttons:
            if isinstance(name, str):
                btn = QPushButton(name, btnfr)
            elif isinstance(name, (list, tuple)):
                if len(name) < 3:
                    pixmap, name = name
                    tip = None
                else:
                    pixmap, name, tip = name
                btn = QPushButton(pixmap.icon(), name, btnfr)
                if tip:
                    btn.setToolTip(tip)
            self._user_buttons[name] = btn
            btn._clicked = Kittens.utils.curry(self.emit, SIGNAL(name))
            self.connect(btn, SIGNAL("clicked()"), btn._clicked)
            btnfr_lo.addWidget(btn, 1)
        # add a Close button
        btnfr_lo.addStretch(100)
        closebtn = QPushButton(pixmaps.grey_round_cross.icon(), "Close", btnfr)
        self.connect(closebtn, SIGNAL("clicked()"), self.hide)
        btnfr_lo.addWidget(closebtn, 1)
        # resize selves
        self.config_name = config_name or "html-viewer"
        width = Config.getint('%s-width' % self.config_name, 512)
        height = Config.getint('%s-height' % self.config_name, 512)
        self.resize(QSize(width, height))

    def resizeEvent(self, ev):
        QDialog.resizeEvent(self, ev)
        sz = ev.size()
        Config.set('%s-width' % self.config_name, sz.width())
        Config.set('%s-height' % self.config_name, sz.height())

    def setDocument(self, filename, empty=""):
        """Sets the HTML text to be displayed. """
        self._source = QUrl.fromLocalFile(filename)
        if os.path.exists(filename):
            self.viewer.setSource(self._source)
        else:
            self.viewer.setText(empty)

    def _urlClicked(self, url):
        path = str(url.path())
        if path:
            self.emit(SIGNAL("viewPath"), path)
        # to make sure it keeps displaying the same thing
        self.viewer.setSource(self._source)

    def reload(self):
        self.viewer.reload()

    def setLabel(self, label=None):
        if label is None:
            self.label.hide()
        else:
            self.label.setText(label)
            self.label.show()