Dialog for placing or editing a hyperlink annotation.
| 7 | |
| 8 | |
| 9 | class HyperlinkDialog(QDialog): |
| 10 | """Dialog for placing or editing a hyperlink annotation.""" |
| 11 | |
| 12 | def __init__(self, url: str = "", label: str = "", parent=None): |
| 13 | super().__init__(parent, Qt.Window) |
| 14 | self.setWindowTitle("Hyperlink") |
| 15 | self.setMinimumWidth(380) |
| 16 | |
| 17 | from .config import ( |
| 18 | HYPERLINK_FONT_FAMILY, HYPERLINK_FONT_SIZE, |
| 19 | HYPERLINK_COLOR, HYPERLINK_UNDERLINE, |
| 20 | ) |
| 21 | |
| 22 | outer = QVBoxLayout() |
| 23 | self.setLayout(outer) |
| 24 | |
| 25 | form = QFormLayout() |
| 26 | self._url_edit = QLineEdit(url) |
| 27 | self._url_edit.setPlaceholderText("https://…") |
| 28 | form.addRow("URL:", self._url_edit) |
| 29 | |
| 30 | self._label_edit = QLineEdit(label) |
| 31 | self._label_edit.setPlaceholderText("leave blank to display URL") |
| 32 | form.addRow("Label:", self._label_edit) |
| 33 | outer.addLayout(form) |
| 34 | |
| 35 | # Preview line showing appearance. |
| 36 | preview_font = QFont(HYPERLINK_FONT_FAMILY, HYPERLINK_FONT_SIZE) |
| 37 | preview_font.setUnderline(HYPERLINK_UNDERLINE) |
| 38 | self._preview = QLabel(label or url or "preview") |
| 39 | self._preview.setFont(preview_font) |
| 40 | self._preview.setStyleSheet( |
| 41 | f"color: {HYPERLINK_COLOR.name()}; padding: 4px;" |
| 42 | ) |
| 43 | outer.addWidget(self._preview) |
| 44 | |
| 45 | # Update preview as user types. |
| 46 | self._url_edit.textChanged.connect(self._update_preview) |
| 47 | self._label_edit.textChanged.connect(self._update_preview) |
| 48 | |
| 49 | buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) |
| 50 | buttons.accepted.connect(self.accept) |
| 51 | buttons.rejected.connect(self.reject) |
| 52 | outer.addWidget(buttons) |
| 53 | |
| 54 | self._url_edit.setFocus() |
| 55 | |
| 56 | def _update_preview(self) -> None: |
| 57 | display = self._label_edit.text().strip() or self._url_edit.text().strip() or "preview" |
| 58 | self._preview.setText(display) |
| 59 | |
| 60 | def url(self) -> str: |
| 61 | return self._url_edit.text().strip() |
| 62 | |
| 63 | def label(self) -> str: |
| 64 | return self._label_edit.text().strip() |
no outgoing calls
no test coverage detected