From 1ceacdf6363a233d3abd9bc815d3b0d032f1d0b4 Mon Sep 17 00:00:00 2001 From: Chris Johnston Date: Tue, 1 Oct 2019 23:11:20 -0700 Subject: [PATCH] Fix #3511 Add hyperlinks to EntryPreviewWidget notes field This change adds support for hyperlinks in the notes field of the EntryPreviewWidget. This is done by enabling the `openExternalLinks` property of `QLabel`, and modifying the value of the content of this field so that links are wrapped with `` tags. Links are assumed to use the format: `protocol://path`, since assumptions cannot/shoudn't be made about the specific format of a link. The text of the label uses the modified notes value. --- src/gui/EntryPreviewWidget.cpp | 18 ++++++++---------- src/gui/EntryPreviewWidget.h | 2 +- src/gui/EntryPreviewWidget.ui | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/gui/EntryPreviewWidget.cpp b/src/gui/EntryPreviewWidget.cpp index 7395f380d..c20c85be9 100644 --- a/src/gui/EntryPreviewWidget.cpp +++ b/src/gui/EntryPreviewWidget.cpp @@ -168,13 +168,12 @@ void EntryPreviewWidget::updateEntryTotp() void EntryPreviewWidget::setPasswordVisible(bool state) { const QString password = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password()); - auto flags = m_ui->entryPasswordLabel->textInteractionFlags(); if (state) { m_ui->entryPasswordLabel->setRawText(password); m_ui->entryPasswordLabel->setToolTip(password); - m_ui->entryPasswordLabel->setTextInteractionFlags(flags | Qt::TextSelectableByMouse); + m_ui->entryPasswordLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); } else { - m_ui->entryPasswordLabel->setTextInteractionFlags(flags & ~Qt::TextSelectableByMouse); + m_ui->entryPasswordLabel->setTextInteractionFlags(Qt::NoTextInteraction); m_ui->entryPasswordLabel->setToolTip({}); if (password.isEmpty() && config()->get("security/passwordemptynodots").toBool()) { m_ui->entryPasswordLabel->setRawText(""); @@ -194,21 +193,20 @@ void EntryPreviewWidget::setGroupNotesVisible(bool state) setNotesVisible(m_ui->groupNotesLabel, m_currentGroup->notes(), state); } -void EntryPreviewWidget::setNotesVisible(QLabel* notesLabel, const QString notes, bool state) +void EntryPreviewWidget::setNotesVisible(QLabel* notesLabel, const QString& notes, bool state) { - auto flags = notesLabel->textInteractionFlags(); if (state) { - notesLabel->setText(notes); - notesLabel->setToolTip(notes); - notesLabel->setTextInteractionFlags(flags | Qt::TextSelectableByMouse); + // Add html hyperlinks to notes that start with XXXX:// + QString hyperlinkNotes = notes; + notesLabel->setText(hyperlinkNotes.replace(QRegExp("(\\w+:\\/\\/\\S+)"), "\\1")); + notesLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); } else { if (notes.isEmpty()) { notesLabel->setText(""); } else { notesLabel->setText(QString("\u25cf").repeated(6)); } - notesLabel->setToolTip({}); - notesLabel->setTextInteractionFlags(flags & ~Qt::TextSelectableByMouse); + notesLabel->setTextInteractionFlags(Qt::NoTextInteraction); } } diff --git a/src/gui/EntryPreviewWidget.h b/src/gui/EntryPreviewWidget.h index 205b3cbbf..ddf17b295 100644 --- a/src/gui/EntryPreviewWidget.h +++ b/src/gui/EntryPreviewWidget.h @@ -54,7 +54,7 @@ private slots: void setPasswordVisible(bool state); void setEntryNotesVisible(bool state); void setGroupNotesVisible(bool state); - void setNotesVisible(QLabel* notesLabel, const QString notes, bool state); + void setNotesVisible(QLabel* notesLabel, const QString& notes, bool state); void updateGroupHeaderLine(); void updateGroupGeneralTab(); diff --git a/src/gui/EntryPreviewWidget.ui b/src/gui/EntryPreviewWidget.ui index b620da659..4d1941fbc 100644 --- a/src/gui/EntryPreviewWidget.ui +++ b/src/gui/EntryPreviewWidget.ui @@ -463,7 +463,7 @@ Notes - Qt::AlignTop|Qt::AlignRight + Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing @@ -502,12 +502,21 @@ notes + + Qt::RichText + Qt::AlignTop true + + true + + + Qt::NoTextInteraction + @@ -828,7 +837,7 @@ - + 0 @@ -971,7 +980,7 @@ Notes - Qt::AlignTop|Qt::AlignRight + Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing