Skip to content

Commit

Permalink
Fix #3511 Add hyperlinks to EntryPreviewWidget notes field
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Chris-Johnston authored and droidmonkey committed Oct 5, 2019
1 parent 37c04f3 commit 1ceacdf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
18 changes: 8 additions & 10 deletions src/gui/EntryPreviewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand All @@ -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+)"), "<a href=\"\\1\">\\1</a>"));
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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/EntryPreviewWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 12 additions & 3 deletions src/gui/EntryPreviewWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
<string>Notes</string>
</property>
<property name="alignment">
<set>Qt::AlignTop|Qt::AlignRight</set>
<set>Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -502,12 +502,21 @@
<property name="text">
<string notr="true">notes</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="alignment">
<set>Qt::AlignTop</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -828,7 +837,7 @@
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<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">
<number>0</number>
</property>
Expand Down Expand Up @@ -971,7 +980,7 @@
<string>Notes</string>
</property>
<property name="alignment">
<set>Qt::AlignTop|Qt::AlignRight</set>
<set>Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing</set>
</property>
</widget>
</item>
Expand Down

0 comments on commit 1ceacdf

Please sign in to comment.