Skip to content

Commit

Permalink
highlight reference field in Database view
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZ3ro committed Mar 7, 2017
1 parent e91a414 commit a03e354
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/core/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ bool Entry::isExpired() const
return m_data.timeInfo.expires() && m_data.timeInfo.expiryTime() < QDateTime::currentDateTimeUtc();
}

bool Entry::hasReferences() const
{
const QList<QString> keyList = EntryAttributes::DefaultAttributes;
for (const QString& key : keyList) {
if (m_attributes->isReference(key)) {
return true;
}
}
return false;
}

EntryAttributes* Entry::attributes()
{
return m_attributes;
Expand Down
1 change: 1 addition & 0 deletions src/core/Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Entry : public QObject
QString password() const;
QString notes() const;
bool isExpired() const;
bool hasReferences() const;
EntryAttributes* attributes();
const EntryAttributes* attributes() const;
EntryAttachments* attachments();
Expand Down
15 changes: 15 additions & 0 deletions src/core/EntryAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ bool EntryAttributes::isProtected(const QString& key) const
return m_protectedAttributes.contains(key);
}

bool EntryAttributes::isReference(const QString& key) const
{
if (!m_attributes.contains(key)) {
Q_ASSERT(false);
return false;
}

QString data = value(key);
QRegExp referenceRegExp("\\{REF:([TUPAN])@I:([^}]+)\\}", Qt::CaseInsensitive, QRegExp::RegExp2);
if (referenceRegExp.indexIn(data) != -1) {
return true;
}
return false;
}

void EntryAttributes::set(const QString& key, const QString& value, bool protect)
{
bool emitModified = false;
Expand Down
1 change: 1 addition & 0 deletions src/core/EntryAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class EntryAttributes : public QObject
QString value(const QString& key) const;
bool contains(const QString& key) const;
bool isProtected(const QString& key) const;
bool isReference(const QString& key) const;
void set(const QString& key, const QString& value, bool protect = false);
void remove(const QString& key);
void rename(const QString& oldKey, const QString& newKey);
Expand Down
27 changes: 24 additions & 3 deletions src/gui/entry/EntryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <QFont>
#include <QMimeData>
#include <QPalette>

#include "core/DatabaseIcons.h"
#include "core/Entry.h"
Expand Down Expand Up @@ -127,20 +128,34 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
}

Entry* entry = entryFromIndex(index);
EntryAttributes* attr = entry->attributes();

if (role == Qt::DisplayRole) {
QString result;
switch (index.column()) {
case ParentGroup:
if (entry->group()) {
return entry->group()->name();
}
break;
case Title:
return entry->title();
result = entry->resolvePlaceholder(entry->title());
if (attr->isReference(EntryAttributes::TitleKey)) {
result.prepend(tr("Ref: ","Reference abbreviation"));
}
return result;
case Username:
return entry->username();
result = entry->resolvePlaceholder(entry->username());
if (attr->isReference(EntryAttributes::UserNameKey)) {
result.prepend(tr("Ref: ","Reference abbreviation"));
}
return result;
case Url:
return entry->url();
result = entry->resolvePlaceholder(entry->url());
if (attr->isReference(EntryAttributes::URLKey)) {
result.prepend(tr("Ref: ","Reference abbreviation"));
}
return result;
}
}
else if (role == Qt::DecorationRole) {
Expand All @@ -166,6 +181,12 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
}
return font;
}
else if (role == Qt::TextColorRole) {
if (entry->hasReferences()) {
QPalette p;
return QVariant(p.color(QPalette::Active, QPalette::Mid));
}
}

return QVariant();
}
Expand Down

0 comments on commit a03e354

Please sign in to comment.