Merge pull request #1338 from frostasm/refactor-details-widget

Refactor details widget
This commit is contained in:
Janek Bevendorff
2018-02-04 14:56:29 +01:00
committed by GitHub
12 changed files with 1048 additions and 764 deletions

View File

@@ -132,6 +132,7 @@ set(keepassx_SOURCES
gui/UnlockDatabaseWidget.cpp gui/UnlockDatabaseWidget.cpp
gui/UnlockDatabaseDialog.cpp gui/UnlockDatabaseDialog.cpp
gui/WelcomeWidget.cpp gui/WelcomeWidget.cpp
gui/widgets/ElidedLabel.cpp
gui/csvImport/CsvImportWidget.cpp gui/csvImport/CsvImportWidget.cpp
gui/csvImport/CsvImportWizard.cpp gui/csvImport/CsvImportWizard.cpp
gui/csvImport/CsvParserModel.cpp gui/csvImport/CsvParserModel.cpp

View File

@@ -48,7 +48,7 @@ bool EntryAttributes::hasKey(const QString& key) const
return m_attributes.contains(key); return m_attributes.contains(key);
} }
QList<QString> EntryAttributes::customKeys() QList<QString> EntryAttributes::customKeys() const
{ {
QList<QString> customKeys; QList<QString> customKeys;
const QList<QString> keyList = keys(); const QList<QString> keyList = keys();

View File

@@ -33,7 +33,7 @@ public:
explicit EntryAttributes(QObject* parent = nullptr); explicit EntryAttributes(QObject* parent = nullptr);
QList<QString> keys() const; QList<QString> keys() const;
bool hasKey(const QString& key) const; bool hasKey(const QString& key) const;
QList<QString> customKeys(); QList<QString> customKeys() const;
QString value(const QString& key) const; QString value(const QString& key) const;
bool contains(const QString& key) const; bool contains(const QString& key) const;
bool containsValue(const QString& value) const; bool containsValue(const QString& value) const;

View File

@@ -439,11 +439,11 @@ void Group::setParent(Database* db)
QObject::setParent(db); QObject::setParent(db);
} }
QStringList Group::hierarchy() QStringList Group::hierarchy() const
{ {
QStringList hierarchy; QStringList hierarchy;
Group* group = this; const Group* group = this;
Group* parent = m_parent; const Group* parent = m_parent;
hierarchy.prepend(group->name()); hierarchy.prepend(group->name());
while (parent) { while (parent) {

View File

@@ -117,7 +117,7 @@ public:
Group* parentGroup(); Group* parentGroup();
const Group* parentGroup() const; const Group* parentGroup() const;
void setParent(Group* parent, int index = -1); void setParent(Group* parent, int index = -1);
QStringList hierarchy(); QStringList hierarchy() const;
Database* database(); Database* database();
const Database* database() const; const Database* database() const;

View File

@@ -111,9 +111,13 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
"border-radius: 5px;"); "border-radius: 5px;");
m_detailsView = new DetailsWidget(this); m_detailsView = new DetailsWidget(this);
connect(m_detailsView, &DetailsWidget::errorOccurred, this, [this](const QString& error) { m_detailsView->hide();
showMessage(error, MessageWidget::MessageType::Error); connect(this, SIGNAL(pressedEntry(Entry*)), m_detailsView, SLOT(setEntry(Entry*)));
}); connect(this, SIGNAL(pressedGroup(Group*)), m_detailsView, SLOT(setGroup(Group*)));
connect(this, SIGNAL(currentModeChanged(DatabaseWidget::Mode)),
m_detailsView, SLOT(setDatabaseMode(DatabaseWidget::Mode)));
connect(m_detailsView, SIGNAL(errorOccurred(QString)), this, SLOT(showErrorMessage(QString)));
QVBoxLayout* vLayout = new QVBoxLayout(rightHandSideWidget); QVBoxLayout* vLayout = new QVBoxLayout(rightHandSideWidget);
vLayout->setMargin(0); vLayout->setMargin(0);
@@ -1488,6 +1492,11 @@ void DatabaseWidget::showMessage(const QString& text, MessageWidget::MessageType
m_messageWidget->showMessage(text, type, autoHideTimeout); m_messageWidget->showMessage(text, type, autoHideTimeout);
} }
void DatabaseWidget::showErrorMessage(const QString& errorMessage)
{
showMessage(errorMessage, MessageWidget::MessageType::Error);
}
void DatabaseWidget::hideMessage() void DatabaseWidget::hideMessage()
{ {
if (m_messageWidget->isVisible()) { if (m_messageWidget->isVisible()) {

View File

@@ -178,6 +178,7 @@ public slots:
void showMessage(const QString& text, MessageWidget::MessageType type, bool showClosebutton = true, void showMessage(const QString& text, MessageWidget::MessageType type, bool showClosebutton = true,
int autoHideTimeout = MessageWidget::DefaultAutoHideTimeout); int autoHideTimeout = MessageWidget::DefaultAutoHideTimeout);
void showErrorMessage(const QString& errorMessage);
void hideMessage(); void hideMessage();
private slots: private slots:

View File

@@ -28,153 +28,189 @@
#include "gui/Clipboard.h" #include "gui/Clipboard.h"
#include "entry/EntryAttachmentsModel.h" #include "entry/EntryAttachmentsModel.h"
namespace {
constexpr int GeneralTabIndex = 0;
}
DetailsWidget::DetailsWidget(QWidget* parent) DetailsWidget::DetailsWidget(QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_ui(new Ui::DetailsWidget()) , m_ui(new Ui::DetailsWidget())
, m_locked(false) , m_locked(false)
, m_currentEntry(nullptr) , m_currentEntry(nullptr)
, m_currentGroup(nullptr) , m_currentGroup(nullptr)
, m_timer(nullptr) , m_step(0)
, m_attributesTabWidget(nullptr) , m_totpTimer(nullptr)
, m_attachmentsTabWidget(nullptr)
, m_autotypeTabWidget(nullptr)
, m_selectedTabEntry(0) , m_selectedTabEntry(0)
, m_selectedTabGroup(0) , m_selectedTabGroup(0)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
connect(parent, SIGNAL(pressedEntry(Entry*)), SLOT(getSelectedEntry(Entry*))); // Entry
connect(parent, SIGNAL(pressedGroup(Group*)), SLOT(getSelectedGroup(Group*))); m_ui->entryTotpButton->setIcon(filePath()->icon("actions", "chronometer"));
connect(parent, SIGNAL(currentModeChanged(DatabaseWidget::Mode)), SLOT(setDatabaseMode(DatabaseWidget::Mode))); m_ui->entryCloseButton->setIcon(filePath()->icon("actions", "dialog-close"));
m_ui->totpButton->setIcon(filePath()->icon("actions", "chronometer")); m_ui->entryAttachmentsWidget->setReadOnly(true);
m_ui->closeButton->setIcon(filePath()->icon("actions", "dialog-close")); m_ui->entryAttachmentsWidget->setButtonsVisible(false);
connect(m_ui->totpButton, SIGNAL(toggled(bool)), SLOT(showTotp(bool))); connect(m_ui->entryTotpButton, SIGNAL(toggled(bool)), m_ui->entryTotpWidget, SLOT(setVisible(bool)));
connect(m_ui->closeButton, SIGNAL(toggled(bool)), SLOT(hideDetails())); connect(m_ui->entryCloseButton, SIGNAL(toggled(bool)), SLOT(hide()));
connect(m_ui->tabWidget, SIGNAL(tabBarClicked(int)), SLOT(updateTabIndex(int))); connect(m_ui->entryTabWidget, SIGNAL(tabBarClicked(int)), SLOT(updateTabIndexes()), Qt::QueuedConnection);
m_ui->attachmentsWidget->setReadOnly(true); // Group
m_ui->attachmentsWidget->setButtonsVisible(false); m_ui->groupCloseButton->setIcon(filePath()->icon("actions", "dialog-close"));
connect(m_ui->groupCloseButton, SIGNAL(toggled(bool)), SLOT(hide()));
m_attributesTabWidget = m_ui->tabWidget->widget(AttributesTab); connect(m_ui->groupTabWidget, SIGNAL(tabBarClicked(int)), SLOT(updateTabIndexes()), Qt::QueuedConnection);
m_attachmentsTabWidget = m_ui->tabWidget->widget(AttachmentsTab);
m_autotypeTabWidget = m_ui->tabWidget->widget(AutotypeTab);
this->hide();
} }
DetailsWidget::~DetailsWidget() DetailsWidget::~DetailsWidget()
{ {
if (m_timer) { deleteTotpTimer();
delete m_timer;
}
} }
void DetailsWidget::getSelectedEntry(Entry* selectedEntry) void DetailsWidget::setEntry(Entry* selectedEntry)
{ {
if (!selectedEntry) { if (!selectedEntry) {
hideDetails(); hide();
return; return;
} }
m_currentEntry = selectedEntry; m_currentEntry = selectedEntry;
if (!config()->get("GUI/HideDetailsView").toBool()) { updateEntryHeaderLine();
this->show(); updateEntryTotp();
updateEntryGeneralTab();
updateEntryNotesTab();
updateEntryAttributesTab();
updateEntryAttachmentsTab();
updateEntryAutotypeTab();
setVisible(!config()->get("GUI/HideDetailsView").toBool());
m_ui->stackedWidget->setCurrentWidget(m_ui->pageEntry);
const int tabIndex = m_ui->entryTabWidget->isTabEnabled(m_selectedTabEntry) ? m_selectedTabEntry
: GeneralTabIndex;
Q_ASSERT(m_ui->entryTabWidget->isTabEnabled(GeneralTabIndex));
m_ui->entryTabWidget->setCurrentIndex(tabIndex);
}
void DetailsWidget::setGroup(Group* selectedGroup)
{
if (!selectedGroup) {
hide();
return;
} }
m_ui->stackedWidget->setCurrentIndex(EntryPreview); m_currentGroup = selectedGroup;
updateGroupHeaderLine();
updateGroupGeneralTab();
updateGroupNotesTab();
if (m_ui->tabWidget->count() < 5) { setVisible(!config()->get("GUI/HideDetailsView").toBool());
m_ui->tabWidget->insertTab(static_cast<int>(AttributesTab), m_attributesTabWidget, tr("Attributes"));
m_ui->tabWidget->insertTab(static_cast<int>(AttachmentsTab), m_attachmentsTabWidget, tr("Attachments")); m_ui->stackedWidget->setCurrentWidget(m_ui->pageGroup);
m_ui->tabWidget->insertTab(static_cast<int>(AutotypeTab), m_autotypeTabWidget, tr("Autotype")); const int tabIndex = m_ui->groupTabWidget->isTabEnabled(m_selectedTabGroup) ? m_selectedTabGroup
: GeneralTabIndex;
Q_ASSERT(m_ui->groupTabWidget->isTabEnabled(GeneralTabIndex));
m_ui->groupTabWidget->setCurrentIndex(tabIndex);
}
void DetailsWidget::setDatabaseMode(DatabaseWidget::Mode mode)
{
m_locked = mode == DatabaseWidget::LockedMode;
if (m_locked) {
return;
} }
m_ui->tabWidget->setTabEnabled(AttributesTab, false); if (mode == DatabaseWidget::ViewMode) {
m_ui->tabWidget->setTabEnabled(NotesTab, false); if (m_ui->stackedWidget->currentWidget() == m_ui->pageGroup) {
m_ui->tabWidget->setTabEnabled(AutotypeTab, false); setGroup(m_currentGroup);
} else {
m_ui->totpButton->hide(); setEntry(m_currentEntry);
m_ui->totpWidget->hide();
m_ui->totpButton->setChecked(false);
auto icon = m_currentEntry->iconPixmap();
if (icon.width() > 16 || icon.height() > 16) {
icon = icon.scaled(16, 16);
}
m_ui->entryIcon->setPixmap(icon);
QString title = QString(" / ");
Group* entry_group = m_currentEntry->group();
if (entry_group) {
QStringList hierarchy = entry_group->hierarchy();
hierarchy.removeFirst();
title += hierarchy.join(" / ");
if (hierarchy.size() > 0) {
title += " / ";
} }
} }
title.append(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->title())); }
m_ui->titleLabel->setText(title);
m_ui->usernameLabel->setText(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->username())); void DetailsWidget::updateEntryHeaderLine()
{
Q_ASSERT(m_currentEntry);
const QString title = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->title());
m_ui->entryTitleLabel->setText(hierarchy(m_currentEntry->group(), title));
m_ui->entryIcon->setPixmap(preparePixmap(m_currentEntry->iconPixmap(), 16));
}
void DetailsWidget::updateEntryTotp()
{
Q_ASSERT(m_currentEntry);
const bool hasTotp = m_currentEntry->hasTotp();
m_ui->entryTotpButton->setVisible(hasTotp);
m_ui->entryTotpWidget->hide();
m_ui->entryTotpButton->setChecked(false);
if (hasTotp) {
deleteTotpTimer();
m_totpTimer = new QTimer(m_currentEntry);
connect(m_totpTimer, SIGNAL(timeout()), this, SLOT(updateTotpLabel()));
m_totpTimer->start(1000);
m_step = m_currentEntry->totpStep();
updateTotpLabel();
} else {
m_ui->entryTotpLabel->clear();
stopTotpTimer();
}
}
void DetailsWidget::updateEntryGeneralTab()
{
Q_ASSERT(m_currentEntry);
m_ui->entryUsernameLabel->setText(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->username()));
if (!config()->get("security/hidepassworddetails").toBool()) { if (!config()->get("security/hidepassworddetails").toBool()) {
m_ui->passwordLabel->setText( const QString password = m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password());
shortPassword(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password()))); m_ui->entryPasswordLabel->setRawText(password);
m_ui->passwordLabel->setToolTip(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->password())); m_ui->entryPasswordLabel->setToolTip(password);
} else { } else {
m_ui->passwordLabel->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()) { if (!url.isEmpty()) {
// URL is well formed and can be opened in a browser // URL is well formed and can be opened in a browser
// create a new display url that masks password placeholders // create a new display url that masks password placeholders
// the actual link will use the password // the actual link will use the password
url = QString("<a href=\"%1\">%2</a>").arg(url).arg(shortUrl(m_currentEntry->displayUrl())); m_ui->entryUrlLabel->setRawText(m_currentEntry->displayUrl());
m_ui->urlLabel->setOpenExternalLinks(true); m_ui->entryUrlLabel->setUrl(url);
} else { } else {
// Fallback to the raw url string // Fallback to the raw url string
url = shortUrl(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->url())); m_ui->entryUrlLabel->setRawText(m_currentEntry->resolveMultiplePlaceholders(m_currentEntry->url()));
m_ui->urlLabel->setOpenExternalLinks(false); m_ui->entryUrlLabel->setUrl({});
}
m_ui->urlLabel->setText(url);
TimeInfo entryTime = m_currentEntry->timeInfo();
if (entryTime.expires()) {
m_ui->expirationLabel->setText(entryTime.expiryTime().toString(Qt::DefaultLocaleShortDate));
} else {
m_ui->expirationLabel->setText(tr("Never"));
} }
if (m_currentEntry->hasTotp()) { const TimeInfo entryTime = m_currentEntry->timeInfo();
m_step = m_currentEntry->totpStep(); const QString expires = entryTime.expires() ? entryTime.expiryTime().toString(Qt::DefaultLocaleShortDate)
: tr("Never");
m_ui->entryExpirationLabel->setText(expires);
}
if (m_timer) { void DetailsWidget::updateEntryNotesTab()
delete m_timer; {
} Q_ASSERT(m_currentEntry);
m_timer = new QTimer(selectedEntry); const QString notes = m_currentEntry->notes();
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTotp())); setTabEnabled(m_ui->entryTabWidget, m_ui->entryNotesTab, !notes.isEmpty());
updateTotp(); m_ui->entryNotesEdit->setText(m_currentEntry->resolveMultiplePlaceholders(notes));
m_timer->start(m_step * 1000); }
m_ui->totpButton->show();
}
QString notes = m_currentEntry->notes(); void DetailsWidget::updateEntryAttributesTab()
if (!notes.isEmpty()) { {
m_ui->tabWidget->setTabEnabled(NotesTab, true); Q_ASSERT(m_currentEntry);
m_ui->notesEdit->setText(m_currentEntry->resolveMultiplePlaceholders(notes)); m_ui->entryAttributesEdit->clear();
} const EntryAttributes* attributes = m_currentEntry->attributes();
const QStringList customAttributes = attributes->customKeys();
QStringList customAttributes = m_currentEntry->attributes()->customKeys(); const bool haveAttributes = customAttributes.size() > 0;
if (customAttributes.size() > 0) { setTabEnabled(m_ui->entryTabWidget, m_ui->entryAttributesTab, haveAttributes);
m_ui->tabWidget->setTabEnabled(AttributesTab, true); if (haveAttributes) {
m_ui->attributesEdit->clear(); QString attributesText;
QString attributesText = QString();
for (const QString& key : customAttributes) { for (const QString& key : customAttributes) {
QString value = m_currentEntry->attributes()->value(key); QString value = m_currentEntry->attributes()->value(key);
if (m_currentEntry->attributes()->isProtected(key)) { if (m_currentEntry->attributes()->isProtected(key)) {
@@ -182,174 +218,118 @@ void DetailsWidget::getSelectedEntry(Entry* selectedEntry)
} }
attributesText.append(QString("<b>%1</b>: %2<br/>").arg(key, value)); attributesText.append(QString("<b>%1</b>: %2<br/>").arg(key, value));
} }
m_ui->attributesEdit->setText(attributesText); m_ui->entryAttributesEdit->setText(attributesText);
} }
}
void DetailsWidget::updateEntryAttachmentsTab()
{
Q_ASSERT(m_currentEntry);
const bool hasAttachments = !m_currentEntry->attachments()->isEmpty(); const bool hasAttachments = !m_currentEntry->attachments()->isEmpty();
m_ui->tabWidget->setTabEnabled(AttachmentsTab, hasAttachments); setTabEnabled(m_ui->entryTabWidget, m_ui->entryAttachmentsTab, hasAttachments);
m_ui->attachmentsWidget->setEntryAttachments(m_currentEntry->attachments()); m_ui->entryAttachmentsWidget->setEntryAttachments(m_currentEntry->attachments());
}
m_ui->autotypeTree->clear(); void DetailsWidget::updateEntryAutotypeTab()
AutoTypeAssociations* autotypeAssociations = m_currentEntry->autoTypeAssociations(); {
Q_ASSERT(m_currentEntry);
m_ui->entryAutotypeTree->clear();
QList<QTreeWidgetItem*> items; QList<QTreeWidgetItem*> items;
for (auto assoc : autotypeAssociations->getAll()) { const AutoTypeAssociations* autotypeAssociations = m_currentEntry->autoTypeAssociations();
QStringList association = QStringList() << assoc.window << assoc.sequence; const auto associations = autotypeAssociations->getAll();
if (association.at(1).isEmpty()) { for (const auto& assoc : associations) {
association.replace(1, m_currentEntry->effectiveAutoTypeSequence()); const QString sequence = assoc.sequence.isEmpty() ? m_currentEntry->effectiveAutoTypeSequence()
} : assoc.sequence;
items.append(new QTreeWidgetItem(m_ui->autotypeTree, association)); items.append(new QTreeWidgetItem(m_ui->entryAutotypeTree, {assoc.window, sequence}));
}
if (items.count() > 0) {
m_ui->autotypeTree->addTopLevelItems(items);
m_ui->tabWidget->setTabEnabled(AutotypeTab, true);
} }
if (m_ui->tabWidget->isTabEnabled(m_selectedTabEntry)) { m_ui->entryAutotypeTree->addTopLevelItems(items);
m_ui->tabWidget->setCurrentIndex(m_selectedTabEntry); setTabEnabled(m_ui->entryTabWidget, m_ui->entryAutotypeTab, !items.isEmpty());
}
void DetailsWidget::updateGroupHeaderLine()
{
Q_ASSERT(m_currentGroup);
m_ui->groupTitleLabel->setText(hierarchy(m_currentGroup, {}));
m_ui->groupIcon->setPixmap(preparePixmap(m_currentGroup->iconPixmap(), 32));
}
void DetailsWidget::updateGroupGeneralTab()
{
Q_ASSERT(m_currentGroup);
const QString searchingText = m_currentGroup->resolveSearchingEnabled() ? tr("Enabled") : tr("Disabled");
m_ui->groupSearchingLabel->setText(searchingText);
const QString autotypeText = m_currentGroup->resolveAutoTypeEnabled() ? tr("Enabled") : tr("Disabled");
m_ui->groupAutotypeLabel->setText(autotypeText);
const TimeInfo groupTime = m_currentGroup->timeInfo();
const QString expiresText = groupTime.expires() ? groupTime.expiryTime().toString(Qt::DefaultLocaleShortDate)
: tr("Never");
m_ui->groupExpirationLabel->setText(expiresText);
}
void DetailsWidget::updateGroupNotesTab()
{
Q_ASSERT(m_currentGroup);
const QString notes = m_currentGroup->notes();
setTabEnabled(m_ui->groupTabWidget, m_ui->groupNotesTab, !notes.isEmpty());
m_ui->groupNotesEdit->setText(notes);
}
void DetailsWidget::stopTotpTimer()
{
if (m_totpTimer) {
m_totpTimer->stop();
} }
} }
void DetailsWidget::getSelectedGroup(Group* selectedGroup) void DetailsWidget::deleteTotpTimer()
{ {
if (!selectedGroup) { if (m_totpTimer) {
hideDetails(); delete m_totpTimer;
return;
} }
}
m_currentGroup = selectedGroup; void DetailsWidget::updateTotpLabel()
{
if (!config()->get("GUI/HideDetailsView").toBool()) { if (!m_locked && m_currentEntry) {
this->show(); const QString totpCode = m_currentEntry->totp();
const QString firstHalf = totpCode.left(totpCode.size() / 2);
const QString secondHalf = totpCode.mid(totpCode.size() / 2);
m_ui->entryTotpLabel->setText(firstHalf + " " + secondHalf);
} else {
m_ui->entryTotpLabel->clear();
stopTotpTimer();
} }
}
m_ui->stackedWidget->setCurrentIndex(GroupPreview); void DetailsWidget::updateTabIndexes()
{
m_selectedTabEntry = m_ui->entryTabWidget->currentIndex();
m_selectedTabGroup = m_ui->groupTabWidget->currentIndex();
}
if (m_ui->tabWidget->count() > 2) { void DetailsWidget::setTabEnabled(QTabWidget* tabWidget, QWidget* widget, bool enabled)
m_ui->tabWidget->removeTab(AutotypeTab); {
m_ui->tabWidget->removeTab(AttachmentsTab); const int tabIndex = tabWidget->indexOf(widget);
m_ui->tabWidget->removeTab(AttributesTab); Q_ASSERT(tabIndex != -1);
tabWidget->setTabEnabled(tabIndex, enabled);
}
QPixmap DetailsWidget::preparePixmap(const QPixmap& pixmap, int size)
{
if (pixmap.width() > size || pixmap.height() > size) {
return pixmap.scaled(size, size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
} }
return pixmap;
}
m_ui->tabWidget->setTabEnabled(GroupNotesTab, false); QString DetailsWidget::hierarchy(const Group* group, const QString& title)
{
m_ui->totpButton->hide(); const QString separator(" / ");
m_ui->totpWidget->hide(); QStringList hierarchy = group->hierarchy();
auto icon = m_currentGroup->iconPixmap();
if (icon.width() > 32 || icon.height() > 32) {
icon = icon.scaled(32, 32);
}
m_ui->entryIcon->setPixmap(icon);
QString title = " / ";
QStringList hierarchy = m_currentGroup->hierarchy();
hierarchy.removeFirst(); hierarchy.removeFirst();
title += hierarchy.join(" / "); hierarchy.append(title);
if (hierarchy.size() > 0) { return QString("%1%2").arg(separator, hierarchy.join(separator));
title += " / ";
}
m_ui->titleLabel->setText(title);
QString notes = m_currentGroup->notes();
if (!notes.isEmpty()) {
m_ui->tabWidget->setTabEnabled(GroupNotesTab, true);
m_ui->notesEdit->setText(notes);
}
QString searching = tr("Disabled");
if (m_currentGroup->resolveSearchingEnabled()) {
searching = tr("Enabled");
}
m_ui->searchingLabel->setText(searching);
QString autotype = tr("Disabled");
if (m_currentGroup->resolveAutoTypeEnabled()) {
autotype = tr("Enabled");
}
m_ui->autotypeLabel->setText(autotype);
TimeInfo groupTime = m_currentGroup->timeInfo();
if (groupTime.expires()) {
m_ui->groupExpirationLabel->setText(groupTime.expiryTime().toString(Qt::DefaultLocaleShortDate));
} else {
m_ui->groupExpirationLabel->setText(tr("Never"));
}
if (m_ui->tabWidget->isTabEnabled(m_selectedTabGroup)) {
m_ui->tabWidget->setCurrentIndex(m_selectedTabGroup);
}
}
void DetailsWidget::updateTotp()
{
if (!m_locked) {
QString totpCode = m_currentEntry->totp();
QString firstHalf = totpCode.left(totpCode.size() / 2);
QString secondHalf = totpCode.mid(totpCode.size() / 2);
m_ui->totpLabel->setText(firstHalf + " " + secondHalf);
} else if (m_timer) {
m_timer->stop();
}
}
void DetailsWidget::showTotp(bool visible)
{
if (visible) {
m_ui->totpWidget->show();
} else {
m_ui->totpWidget->hide();
}
}
QString DetailsWidget::shortUrl(QString url)
{
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(QString password)
{
QString newpassword = "";
if (password.length() > 60) {
newpassword.append(password.left(50));
newpassword.append("");
return newpassword;
}
return password;
}
void DetailsWidget::hideDetails()
{
this->hide();
}
void DetailsWidget::setDatabaseMode(DatabaseWidget::Mode mode)
{
m_locked = false;
if (mode == DatabaseWidget::LockedMode) {
m_locked = true;
return;
}
if (mode == DatabaseWidget::ViewMode) {
if (m_ui->stackedWidget->currentIndex() == GroupPreview) {
getSelectedGroup(m_currentGroup);
} else {
getSelectedEntry(m_currentEntry);
}
}
}
void DetailsWidget::updateTabIndex(int index)
{
if (m_ui->stackedWidget->currentIndex() == GroupPreview) {
m_selectedTabGroup = index;
} else {
m_selectedTabEntry = index;
}
} }

View File

@@ -19,8 +19,8 @@
#define KEEPASSX_DETAILSWIDGET_H #define KEEPASSX_DETAILSWIDGET_H
#include "gui/DatabaseWidget.h" #include "gui/DatabaseWidget.h"
#include <QWidget> #include <QWidget>
#include <QTimer>
namespace Ui { namespace Ui {
class DetailsWidget; class DetailsWidget;
@@ -34,48 +34,46 @@ public:
explicit DetailsWidget(QWidget* parent = nullptr); explicit DetailsWidget(QWidget* parent = nullptr);
~DetailsWidget(); ~DetailsWidget();
enum StackedWidgetIndex public slots:
{ void setEntry(Entry* selectedEntry);
EntryPreview = 0, void setGroup(Group* selectedGroup);
GroupPreview = 1, void setDatabaseMode(DatabaseWidget::Mode mode);
};
enum TabWidgetIndex
{
GeneralTab = 0,
AttributesTab = 1,
GroupNotesTab = 1,
AttachmentsTab = 2,
NotesTab = 3,
AutotypeTab = 4,
};
signals: signals:
void errorOccurred(const QString& error); void errorOccurred(const QString& error);
private slots: private slots:
void getSelectedEntry(Entry* selectedEntry); void updateEntryHeaderLine();
void getSelectedGroup(Group* selectedGroup); void updateEntryTotp();
void showTotp(bool visible); void updateEntryGeneralTab();
void updateTotp(); void updateEntryNotesTab();
void hideDetails(); void updateEntryAttributesTab();
void setDatabaseMode(DatabaseWidget::Mode mode); void updateEntryAttachmentsTab();
void updateTabIndex(int index); void updateEntryAutotypeTab();
void updateGroupHeaderLine();
void updateGroupGeneralTab();
void updateGroupNotesTab();
void stopTotpTimer();
void deleteTotpTimer();
void updateTotpLabel();
void updateTabIndexes();
private: private:
void setTabEnabled(QTabWidget* tabWidget, QWidget* widget, bool enabled);
static QPixmap preparePixmap(const QPixmap& pixmap, int size);
static QString hierarchy(const Group* group, const QString& title);
const QScopedPointer<Ui::DetailsWidget> m_ui; const QScopedPointer<Ui::DetailsWidget> m_ui;
bool m_locked; bool m_locked;
Entry* m_currentEntry; Entry* m_currentEntry;
Group* m_currentGroup; Group* m_currentGroup;
quint8 m_step; quint8 m_step;
QPointer<QTimer> m_timer = nullptr; QPointer<QTimer> m_totpTimer;
QWidget* m_attributesTabWidget;
QWidget* m_attachmentsTabWidget;
QWidget* m_autotypeTabWidget;
quint8 m_selectedTabEntry; quint8 m_selectedTabEntry;
quint8 m_selectedTabGroup; quint8 m_selectedTabGroup;
QString shortUrl(QString url);
QString shortPassword(QString password);
}; };
#endif // KEEPASSX_DETAILSWIDGET_H #endif // KEEPASSX_DETAILSWIDGET_H

File diff suppressed because it is too large Load Diff

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