Skip to content

Commit

Permalink
Use ElidedLabel in DetailsWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
frostasm committed Jan 7, 2018
1 parent 6d9bd7d commit bbcb82b
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 72 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ set(keepassx_SOURCES
gui/UnlockDatabaseWidget.cpp
gui/UnlockDatabaseDialog.cpp
gui/WelcomeWidget.cpp
gui/widgets/ElidedLabel.cpp
gui/csvImport/CsvImportWidget.cpp
gui/csvImport/CsvImportWizard.cpp
gui/csvImport/CsvParserModel.cpp
Expand Down
43 changes: 10 additions & 33 deletions src/gui/DetailsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

namespace {
constexpr int GeneralTabIndex = 0;
const QString hierarchySeparator(" / ");
}

DetailsWidget::DetailsWidget(QWidget* parent)
Expand Down Expand Up @@ -169,25 +168,25 @@ void DetailsWidget::updateEntryGeneralTab()

if (!config()->get("security/hidepassworddetails").toBool()) {
const QString password = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password());
m_ui->entryPasswordLabel->setText(shortPassword(password));
m_ui->entryPasswordLabel->setRawText(password);
m_ui->entryPasswordLabel->setToolTip(password);
} else {
m_ui->entryPasswordLabel->setText("****");
m_ui->entryPasswordLabel->setRawText("****");
m_ui->entryPasswordLabel->setToolTip(QString());
}

QString url = m_currentEntry->webUrl();
const QString url = m_currentEntry->webUrl();
if (!url.isEmpty()) {
// URL is well formed and can be opened in a browser
// create a new display url that masks password placeholders
// the actual link will use the password
url = QString("<a href=\"%1\">%2</a>").arg(url).arg(shortUrl(m_currentEntry->displayUrl()));
m_ui->entryUrlLabel->setOpenExternalLinks(true);
m_ui->entryUrlLabel->setRawText(m_currentEntry->displayUrl());
m_ui->entryUrlLabel->setUrl(url);
} else {
// Fallback to the raw url string
url = shortUrl(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->url()));
m_ui->entryUrlLabel->setOpenExternalLinks(false);
m_ui->entryUrlLabel->setRawText(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->url()));
m_ui->entryUrlLabel->setUrl(QString());
}
m_ui->entryUrlLabel->setText(url);

const TimeInfo entryTime = m_currentEntry->timeInfo();
const QString expires = entryTime.expires() ? entryTime.expiryTime().toString(Qt::DefaultLocaleShortDate)
Expand Down Expand Up @@ -313,33 +312,11 @@ QPixmap DetailsWidget::preparePixmap(const QPixmap& pixmap, int size)
return pixmap;
}


QString DetailsWidget::shortUrl(const QString& url)
{
// TODO: create elided text
QString newurl = "";
if (url.length() > 60) {
newurl.append(url.left(20));
newurl.append("");
newurl.append(url.right(20));
return newurl;
}
return url;
}

QString DetailsWidget::shortPassword(const QString& password)
{
// TODO: create elided text
if (password.length() > 60) {
return QString("%1…").arg(password.left(60));
}
return password;
}

QString DetailsWidget::hierarchy(const Group* group, const QString& title)
{
const QString separator(" / ");
QStringList hierarchy = group->hierarchy();
hierarchy.removeFirst();
hierarchy.append(title);
return QString("%1%2").arg(hierarchySeparator, hierarchy.join(hierarchySeparator));
return QString("%1%2").arg(separator, hierarchy.join(separator));
}
10 changes: 5 additions & 5 deletions src/gui/DetailsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
#ifndef KEEPASSX_DETAILSWIDGET_H
#define KEEPASSX_DETAILSWIDGET_H

#include "gui/DatabaseWidget.h"
#include <QWidget>

#include "gui/DatabaseWidget.h"

namespace Ui {
class DetailsWidget;
}
Expand Down Expand Up @@ -57,13 +58,12 @@ private slots:
void updateTotp();
void updateTabIndexes();


private:
void setTabEnabled(QTabWidget *tabWidget, QWidget* widget, bool enabled);

static QPixmap preparePixmap(const QPixmap& pixmap, int size);
static QString shortUrl(const QString& url);
static QString shortPassword(const QString& password);
static QString hierarchy(const Group *group, const QString &title);
static QString hierarchy(const Group* group, const QString& title);

const QScopedPointer<Ui::DetailsWidget> m_ui;
bool m_locked;
Entry* m_currentEntry;
Expand Down
65 changes: 31 additions & 34 deletions src/gui/DetailsWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QWidget" name="entryGeneralWidget" native="true">
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0,1">
<layout class="QGridLayout" name="gridLayout" rowstretch="0,0,0,0,1" columnstretch="0,0,1">
<item row="0" column="0">
<spacer name="entryLeftHorizontalSpacer">
<property name="orientation">
Expand Down Expand Up @@ -196,6 +196,12 @@
</item>
<item row="0" column="2">
<widget class="QLabel" name="entryUsernameLabel">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse</set>
</property>
Expand Down Expand Up @@ -224,7 +230,14 @@
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="entryPasswordLabel"/>
<widget class="ElidedLabel" name="entryPasswordLabel">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="entryUrlTitleLabel">
Expand All @@ -249,12 +262,12 @@
</widget>
</item>
<item row="2" column="2">
<widget class="QLabel" name="entryUrlLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<widget class="ElidedLabel" name="entryUrlLabel">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
Expand Down Expand Up @@ -297,7 +310,7 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>10</height>
</size>
</property>
</spacer>
Expand Down Expand Up @@ -530,14 +543,7 @@
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="groupAutotypeLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
<widget class="QLabel" name="groupAutotypeLabel"/>
</item>
<item row="1" column="1">
<widget class="QLabel" name="groupSearchingTitleLabel">
Expand All @@ -562,14 +568,7 @@
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="groupSearchingLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
<widget class="QLabel" name="groupSearchingLabel"/>
</item>
<item row="2" column="1">
<widget class="QLabel" name="groupExpirationTitleLabel">
Expand All @@ -594,14 +593,7 @@
</widget>
</item>
<item row="2" column="2">
<widget class="QLabel" name="groupExpirationLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
<widget class="QLabel" name="groupExpirationLabel"/>
</item>
<item row="3" column="2">
<spacer name="groupBottomVerticalSpacer">
Expand All @@ -611,7 +603,7 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>10</height>
</size>
</property>
</spacer>
Expand Down Expand Up @@ -663,6 +655,11 @@
<header>gui/entry/EntryAttachmentsWidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>ElidedLabel</class>
<extends>QLabel</extends>
<header>gui/widgets/ElidedLabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down
116 changes: 116 additions & 0 deletions src/gui/widgets/ElidedLabel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "ElidedLabel.h"

#include <QResizeEvent>
#include <QDebug>

namespace {
const QString htmlLinkTemplate("<a href=\"%1\">%2</a>");
}

ElidedLabel::ElidedLabel(QWidget* parent, Qt::WindowFlags f)
: QLabel(parent, f)
, m_elideMode(Qt::ElideMiddle)
{
connect(this, SIGNAL(elideModeChanged(Qt::TextElideMode)), this, SLOT(updateElidedText()));
connect(this, SIGNAL(rawTextChanged(QString)), this, SLOT(updateElidedText()));
connect(this, SIGNAL(urlChanged(QString)), this, SLOT(updateElidedText()));
}

ElidedLabel::ElidedLabel(const QString& text, QWidget* parent, Qt::WindowFlags f)
: ElidedLabel(parent, f)
{
setText(text);
}

Qt::TextElideMode ElidedLabel::elideMode() const
{
return m_elideMode;
}

QString ElidedLabel::rawText() const
{
return m_rawText;
}

QString ElidedLabel::url() const
{
return m_url;
}

void ElidedLabel::setElideMode(Qt::TextElideMode elideMode)
{
if (m_elideMode == elideMode)
return;

if (m_elideMode != Qt::ElideNone) {
setWordWrap(false);
}

m_elideMode = elideMode;
emit elideModeChanged(m_elideMode);
}

void ElidedLabel::setRawText(const QString& elidedText)
{
if (m_rawText == elidedText)
return;

m_rawText = elidedText;
emit rawTextChanged(m_rawText);
}

void ElidedLabel::setUrl(const QString& url)
{
if (m_url == url)
return;

m_url = url;
emit urlChanged(m_url);
}

void ElidedLabel::clear()
{
setRawText(QString());
setElideMode(Qt::ElideMiddle);
setUrl(QString());
QLabel::clear();
}

void ElidedLabel::updateElidedText()
{
if (m_rawText.isEmpty()) {
QLabel::clear();
return;
}

QString displayText = m_rawText;
if (m_elideMode != Qt::ElideNone) {
const QFontMetrics metrix(font());
displayText = metrix.elidedText(m_rawText, m_elideMode, width() - 2);
}
setText(m_url.isEmpty() ? displayText : htmlLinkTemplate.arg(m_url, displayText));
setOpenExternalLinks(!m_url.isEmpty());
}

void ElidedLabel::resizeEvent(QResizeEvent* event)
{
updateElidedText();
QLabel::resizeEvent(event);
}
Loading

0 comments on commit bbcb82b

Please sign in to comment.