def hyperlink_dialog(self):
        dialog = QDialog(self.parent_window)
        dialog.setWindowTitle("Create a hyperlink")
        dialog.resize(DIALOG_SIZE_X, DIALOG_SIZE_Y)

        ok_button_anchor = QPushButton("&OK", dialog)
        ok_button_anchor.setEnabled(False)
        ok_button_anchor.clicked.connect(
            lambda: self.insert_anchor(url_edit.text(), urltext_edit.text()))
        ok_button_anchor.clicked.connect(dialog.hide)

        ok_button_anchor.setAutoDefault(True)

        cancel_button_anchor = QPushButton("&Cancel", dialog)
        cancel_button_anchor.clicked.connect(dialog.hide)
        cancel_button_anchor.setAutoDefault(True)

        url_label = QLabel("Link to:")
        url_edit = QLineEdit()
        url_edit.setPlaceholderText("URL")
        url_edit.textChanged.connect(lambda: self.enable_ok_button(
            ok_button_anchor, url_edit.text(), urltext_edit.text()))

        urltext_label = QLabel("Text to display:")
        urltext_edit = QLineEdit()
        urltext_edit.setPlaceholderText("Text")
        urltext_edit.textChanged.connect(lambda: self.enable_ok_button(
            ok_button_anchor, url_edit.text(), urltext_edit.text()))

        button_box = QHBoxLayout()
        button_box.addStretch(1)
        button_box.addWidget(cancel_button_anchor)
        button_box.addWidget(ok_button_anchor)

        dialog_vbox = QVBoxLayout()
        dialog_vbox.addWidget(url_label)
        dialog_vbox.addWidget(url_edit)
        dialog_vbox.addWidget(urltext_label)
        dialog_vbox.addWidget(urltext_edit)
        dialog_vbox.addLayout(button_box)
        dialog.setLayout(dialog_vbox)
        # if user already selected text, put it in urltext_edit
        if self.selected_text:
            if self.selected_is_url:
                url_edit.setText(self.selected_text)
                urltext_edit.setFocus()
            else:
                urltext_edit.setText(self.selected_text)
                url_edit.setFocus()
        dialog.exec()
Exemplo n.º 2
0
class Hyperlink(QDialog):
    def __init__(self,
                 editor,
                 parent_window,
                 selected_text,
                 selected_is_url=False):
        QDialog.__init__(self, parent_window, Qt.Window)
        self.editor_instance = editor
        self.parent_window = parent_window
        self.selected_text = selected_text
        self.selected_is_url = selected_is_url
        self.setWindowTitle("Anki: Create a hyperlink")
        self.resize(DIALOG_SIZE_X, DIALOG_SIZE_Y)
        restoreGeom(self, "318752047__add_hyperlink")

        self.pb_ok = QPushButton("&OK", self)
        self.pb_ok.setEnabled(False)
        self.pb_ok.clicked.connect(
            lambda: self.insert_anchor(url_edit.text(), urltext_edit.text()))

        self.pb_cancel = QPushButton("&Cancel", self)
        self.pb_cancel.clicked.connect(self.reject)

        url_label = QLabel("Link to:")
        url_edit = QLineEdit()
        url_edit.setPlaceholderText("URL")
        url_edit.textChanged.connect(lambda: self.enable_ok_button(
            self.pb_ok, url_edit.text(), urltext_edit.text()))

        urltext_label = QLabel("Text to display:")
        urltext_edit = QLineEdit()
        urltext_edit.setPlaceholderText("Text")
        urltext_edit.textChanged.connect(lambda: self.enable_ok_button(
            self.pb_ok, url_edit.text(), urltext_edit.text()))

        self.button_bar = QHBoxLayout()
        self.button_bar.addStretch(1)
        self.button_bar.addWidget(self.pb_cancel)
        self.button_bar.addWidget(self.pb_ok)

        self.dialog_vbox = QVBoxLayout()
        self.dialog_vbox.addWidget(url_label)
        self.dialog_vbox.addWidget(url_edit)
        self.dialog_vbox.addWidget(urltext_label)
        self.dialog_vbox.addWidget(urltext_edit)
        self.dialog_vbox.addLayout(self.button_bar)
        self.setLayout(self.dialog_vbox)
        # if user already selected text, put it in urltext_edit
        if self.selected_text:
            if self.selected_is_url:
                url_edit.setText(self.selected_text)
                urltext_edit.setFocus()
            else:
                urltext_edit.setText(self.selected_text)
                url_edit.setFocus()

    @staticmethod
    def enable_ok_button(button, url, text):
        if url and text:
            button.setEnabled(True)
        else:
            button.setEnabled(False)

    @staticmethod
    def combine_to_hyperlink(url, text):
        # Create a hyperlink string, where `url` is the hyperlink reference
        # and `text` the content of the tag.
        text = escape_html_chars(text)
        if gc("remove whitespace from beginning and end of urls"):
            url = url.strip()
        if gc("encode_illegal_characters_in_links"):
            url = some_percent_encoding(url)
        out = "<a href=\"{0}\">{1}</a>".format(url, text)
        return out

    def reject(self):
        saveGeom(self, "318752047__add_hyperlink")
        QDialog.reject(self)

    def insert_anchor(self, url, text):
        # Inserts a HTML anchor `<a>` into the text field.
        self.replacement = self.combine_to_hyperlink(url, text)
        self.url = url
        self.text = text
        saveGeom(self, "318752047__add_hyperlink")
        self.accept()
Exemplo n.º 3
0
class Hyperlink(QDialog):
    def __init__(self,
                 editor,
                 parent_window,
                 selected_visible_text,
                 selected_is_url=False):
        QDialog.__init__(self, parent_window, Qt.Window)
        self.editor_instance = editor
        self.parent_window = parent_window
        self.visible_text = selected_visible_text
        self.selected_is_url = selected_is_url
        self.setWindowTitle("Anki: Create a hyperlink")
        self.resize(500, 200)
        restoreGeom(self, "318752047__add_hyperlink")

        self.pb_ok = QPushButton("&OK", self)
        self.pb_ok.setEnabled(False)
        self.pb_ok.clicked.connect(self.store_hyperlink_and_close)

        self.pb_cancel = QPushButton("&Cancel", self)
        self.pb_cancel.clicked.connect(self.reject)

        self.url_label = QLabel("Link to:")
        self.url_edit = QLineEdit()
        self.url_edit.setPlaceholderText("URL")
        self.url_edit.textChanged.connect(self.maybe_enable_ok_button)

        self.text_label = QLabel("Text to display:")
        self.text_edit = QLineEdit()
        self.text_edit.setPlaceholderText("Text")
        self.text_edit.textChanged.connect(self.maybe_enable_ok_button)

        self.button_bar = QHBoxLayout()
        self.button_bar.addStretch(1)
        self.button_bar.addWidget(self.pb_cancel)
        self.button_bar.addWidget(self.pb_ok)

        self.dialog_vbox = QVBoxLayout()
        self.dialog_vbox.addWidget(self.url_label)
        self.dialog_vbox.addWidget(self.url_edit)
        self.dialog_vbox.addWidget(self.text_label)
        self.dialog_vbox.addWidget(self.text_edit)
        self.dialog_vbox.addLayout(self.button_bar)
        self.setLayout(self.dialog_vbox)

        # if user already selected text, put it in self.text_edit
        if self.visible_text:
            if self.selected_is_url:
                self.url_edit.setText(self.visible_text)
                self.text_edit.setFocus()
            else:
                self.text_edit.setText(self.visible_text)
                self.url_edit.setFocus()

    def maybe_enable_ok_button(self):
        if self.url_edit.text() and self.text_edit.text():
            self.pb_ok.setEnabled(True)
        else:
            self.pb_ok.setEnabled(False)

    def reject(self):
        saveGeom(self, "318752047__add_hyperlink")
        QDialog.reject(self)

    def store_hyperlink_and_close(self):
        self.url = self.url_edit.text()
        self.text = self.text_edit.text()
        self.replacement = combine_to_hyperlink(self.url, self.text)
        saveGeom(self, "318752047__add_hyperlink")
        self.accept()