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 `<a>` 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.
This commit is contained in:
Chris Johnston 2019-10-01 23:11:20 -07:00 committed by Jonathan White
parent 37c04f3e42
commit 1ceacdf636
3 changed files with 21 additions and 14 deletions

View File

@ -168,13 +168,12 @@ void EntryPreviewWidget::updateEntryTotp()
void EntryPreviewWidget::setPasswordVisible(bool state) void EntryPreviewWidget::setPasswordVisible(bool state)
{ {
const QString password = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password()); const QString password = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password());
auto flags = m_ui->entryPasswordLabel->textInteractionFlags();
if (state) { if (state) {
m_ui->entryPasswordLabel->setRawText(password); m_ui->entryPasswordLabel->setRawText(password);
m_ui->entryPasswordLabel->setToolTip(password); m_ui->entryPasswordLabel->setToolTip(password);
m_ui->entryPasswordLabel->setTextInteractionFlags(flags | Qt::TextSelectableByMouse); m_ui->entryPasswordLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
} else { } else {
m_ui->entryPasswordLabel->setTextInteractionFlags(flags & ~Qt::TextSelectableByMouse); m_ui->entryPasswordLabel->setTextInteractionFlags(Qt::NoTextInteraction);
m_ui->entryPasswordLabel->setToolTip({}); m_ui->entryPasswordLabel->setToolTip({});
if (password.isEmpty() && config()->get("security/passwordemptynodots").toBool()) { if (password.isEmpty() && config()->get("security/passwordemptynodots").toBool()) {
m_ui->entryPasswordLabel->setRawText(""); m_ui->entryPasswordLabel->setRawText("");
@ -194,21 +193,20 @@ void EntryPreviewWidget::setGroupNotesVisible(bool state)
setNotesVisible(m_ui->groupNotesLabel, m_currentGroup->notes(), 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) { if (state) {
notesLabel->setText(notes); // Add html hyperlinks to notes that start with XXXX://
notesLabel->setToolTip(notes); QString hyperlinkNotes = notes;
notesLabel->setTextInteractionFlags(flags | Qt::TextSelectableByMouse); notesLabel->setText(hyperlinkNotes.replace(QRegExp("(\\w+:\\/\\/\\S+)"), "<a href=\"\\1\">\\1</a>"));
notesLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
} else { } else {
if (notes.isEmpty()) { if (notes.isEmpty()) {
notesLabel->setText(""); notesLabel->setText("");
} else { } else {
notesLabel->setText(QString("\u25cf").repeated(6)); notesLabel->setText(QString("\u25cf").repeated(6));
} }
notesLabel->setToolTip({}); notesLabel->setTextInteractionFlags(Qt::NoTextInteraction);
notesLabel->setTextInteractionFlags(flags & ~Qt::TextSelectableByMouse);
} }
} }

View File

@ -54,7 +54,7 @@ private slots:
void setPasswordVisible(bool state); void setPasswordVisible(bool state);
void setEntryNotesVisible(bool state); void setEntryNotesVisible(bool state);
void setGroupNotesVisible(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 updateGroupHeaderLine();
void updateGroupGeneralTab(); void updateGroupGeneralTab();

View File

@ -463,7 +463,7 @@
<string>Notes</string> <string>Notes</string>
</property> </property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignTop|Qt::AlignRight</set> <set>Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing</set>
</property> </property>
</widget> </widget>
</item> </item>
@ -502,12 +502,21 @@
<property name="text"> <property name="text">
<string notr="true">notes</string> <string notr="true">notes</string>
</property> </property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignTop</set> <set>Qt::AlignTop</set>
</property> </property>
<property name="wordWrap"> <property name="wordWrap">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -828,7 +837,7 @@
<layout class="QVBoxLayout" name="verticalLayout_8"> <layout class="QVBoxLayout" name="verticalLayout_8">
<item> <item>
<widget class="QWidget" name="groupGeneralWidget" native="true"> <widget class="QWidget" name="groupGeneralWidget" native="true">
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,0,0"> <layout class="QGridLayout" name="gridLayout_2" columnstretch="0,0,0,0">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>
</property> </property>
@ -971,7 +980,7 @@
<string>Notes</string> <string>Notes</string>
</property> </property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignTop|Qt::AlignRight</set> <set>Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing</set>
</property> </property>
</widget> </widget>
</item> </item>