Use ElidedLabel in DetailsWidget

This commit is contained in:
frostasm 2017-12-29 10:46:16 +02:00
parent 6007e0de71
commit b264614b9d
6 changed files with 222 additions and 71 deletions

View File

@ -132,6 +132,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

View File

@ -30,7 +30,6 @@
namespace {
constexpr int GeneralTabIndex = 0;
const QString hierarchySeparator(" / ");
}
DetailsWidget::DetailsWidget(QWidget* parent)
@ -168,25 +167,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(QString("\u25cf").repeated(6));
m_ui->entryPasswordLabel->setRawText(QString("\u25cf").repeated(6));
m_ui->entryPasswordLabel->setToolTip({});
}
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({});
}
m_ui->entryUrlLabel->setText(url);
const TimeInfo entryTime = m_currentEntry->timeInfo();
const QString expires = entryTime.expires() ? entryTime.expiryTime().toString(Qt::DefaultLocaleShortDate)
@ -251,7 +250,7 @@ void DetailsWidget::updateEntryAutotypeTab()
void DetailsWidget::updateGroupHeaderLine()
{
Q_ASSERT(m_currentGroup);
m_ui->groupTitleLabel->setText(hierarchy(m_currentGroup, QString()));
m_ui->groupTitleLabel->setText(hierarchy(m_currentGroup, {}));
m_ui->groupIcon->setPixmap(preparePixmap(m_currentGroup->iconPixmap(), 32));
}
@ -326,33 +325,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));
}

View File

@ -62,9 +62,8 @@ private slots:
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);
const QScopedPointer<Ui::DetailsWidget> m_ui;

View File

@ -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">
@ -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>
@ -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">
@ -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>
@ -297,7 +310,7 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>10</height>
</size>
</property>
</spacer>
@ -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">
@ -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">
@ -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">
@ -611,7 +603,7 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>10</height>
</size>
</property>
</spacer>
@ -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/>

View File

@ -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);
}

View File

@ -0,0 +1,61 @@
/*
* 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/>.
*/
#ifndef KEEPASSX_ELIDEDLABEL_H
#define KEEPASSX_ELIDEDLABEL_H
#include <QLabel>
class QResizeEvent;
class ElidedLabel : public QLabel
{
Q_OBJECT
Q_PROPERTY(Qt::TextElideMode elideMode READ elideMode WRITE setElideMode NOTIFY elideModeChanged)
Q_PROPERTY(QString rawText READ rawText WRITE setRawText NOTIFY rawTextChanged)
Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
public:
explicit ElidedLabel(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
explicit ElidedLabel(const QString &text, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags());
Qt::TextElideMode elideMode() const;
QString rawText() const;
QString url() const;
public slots:
void setElideMode(Qt::TextElideMode elideMode);
void setRawText(const QString& rawText);
void setUrl(const QString& url);
void clear();
signals:
void elideModeChanged(Qt::TextElideMode elideMode);
void rawTextChanged(QString rawText);
void urlChanged(QString url);
private slots:
void updateElidedText();
private:
void resizeEvent(QResizeEvent* event);
Qt::TextElideMode m_elideMode;
QString m_rawText;
QString m_url;
};
#endif // KEEPASSX_ELIDEDLABEL_H