From ca30d1832de9640ae8f1d5c118f83143458a31d7 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Mon, 19 Feb 2018 14:59:04 -0500 Subject: [PATCH] Add support for foreground and background entry colors * Add foreground/background color elements to advanced tab * Center paperclip icon --- src/format/KdbxXmlReader.cpp | 3 +- src/gui/entry/EditEntryWidget.cpp | 67 ++++++++++++-- src/gui/entry/EditEntryWidget.h | 2 + src/gui/entry/EditEntryWidgetAdvanced.ui | 110 ++++++++++++++++++++--- src/gui/entry/EntryModel.cpp | 28 +++++- 5 files changed, 186 insertions(+), 24 deletions(-) diff --git a/src/format/KdbxXmlReader.cpp b/src/format/KdbxXmlReader.cpp index 3d08bb55e..f30dc64b4 100644 --- a/src/format/KdbxXmlReader.cpp +++ b/src/format/KdbxXmlReader.cpp @@ -700,7 +700,8 @@ Entry* KdbxXmlReader::parseEntry(bool history) entry->setIcon(uuid); } continue; - }if (m_xml.name() == "ForegroundColor") { + } + if (m_xml.name() == "ForegroundColor") { entry->setForegroundColor(readColor()); continue; } diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index ab2a85f81..e54ceb625 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "autotype/AutoType.h" #include "core/Config.h" @@ -156,6 +157,8 @@ void EditEntryWidget::setupAdvanced() connect(m_advancedUi->attributesView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(updateCurrentAttribute())); + connect(m_advancedUi->fgColorButton, SIGNAL(clicked()), SLOT(pickColor())); + connect(m_advancedUi->bgColorButton, SIGNAL(clicked()), SLOT(pickColor())); } void EditEntryWidget::setupIcon() @@ -593,6 +596,8 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore) editTriggers = QAbstractItemView::DoubleClicked; } m_advancedUi->attributesView->setEditTriggers(editTriggers); + setupColorButton(true, entry->foregroundColor()); + setupColorButton(false, entry->backgroundColor()); m_iconsWidget->setEnabled(!m_history); m_autoTypeUi->sequenceEdit->setReadOnly(m_history); m_autoTypeUi->windowTitleCombo->lineEdit()->setReadOnly(m_history); @@ -758,26 +763,35 @@ void EditEntryWidget::updateEntryData(Entry* entry) const entry->setNotes(m_mainUi->notesEdit->toPlainText()); + if (m_advancedUi->fgColorCheckBox->isChecked() && + m_advancedUi->fgColorButton->property("color").isValid()) { + entry->setForegroundColor(QColor(m_advancedUi->fgColorButton->property("color").toString())); + } else { + entry->setForegroundColor(QColor()); + } + + if (m_advancedUi->bgColorCheckBox->isChecked() && + m_advancedUi->bgColorButton->property("color").isValid()) { + entry->setBackgroundColor(QColor(m_advancedUi->bgColorButton->property("color").toString())); + } else { + entry->setBackgroundColor(QColor()); + } + IconStruct iconStruct = m_iconsWidget->state(); if (iconStruct.number < 0) { entry->setIcon(Entry::DefaultIconNumber); - } - else if (iconStruct.uuid.isNull()) { + } else if (iconStruct.uuid.isNull()) { entry->setIcon(iconStruct.number); - } - else { + } else { entry->setIcon(iconStruct.uuid); } entry->setAutoTypeEnabled(m_autoTypeUi->enableButton->isChecked()); if (m_autoTypeUi->inheritSequenceButton->isChecked()) { entry->setDefaultAutoTypeSequence(QString()); - } - else { - if (AutoType::verifyAutoTypeSyntax(m_autoTypeUi->sequenceEdit->text())) { - entry->setDefaultAutoTypeSequence(m_autoTypeUi->sequenceEdit->text()); - } + } else if (AutoType::verifyAutoTypeSyntax(m_autoTypeUi->sequenceEdit->text())) { + entry->setDefaultAutoTypeSequence(m_autoTypeUi->sequenceEdit->text()); } entry->autoTypeAssociations()->copyDataFrom(m_autoTypeAssoc); @@ -1122,3 +1136,38 @@ QMenu* EditEntryWidget::createPresetsMenu() expirePresetsMenu->addAction(tr("1 year"))->setData(QVariant::fromValue(TimeDelta::fromYears(1))); return expirePresetsMenu; } + +void EditEntryWidget::setupColorButton(bool foreground, QColor color) +{ + QWidget* button = m_advancedUi->fgColorButton; + QCheckBox* checkBox = m_advancedUi->fgColorCheckBox; + if (!foreground) { + button = m_advancedUi->bgColorButton; + checkBox = m_advancedUi->bgColorCheckBox; + } + + if (color.isValid()) { + button->setStyleSheet(QString("background-color:%1").arg(color.name())); + button->setProperty("color", color.name()); + checkBox->setChecked(true); + } else { + button->setStyleSheet(""); + button->setProperty("color", QVariant()); + checkBox->setChecked(false); + } +} + +void EditEntryWidget::pickColor() +{ + bool isForeground = (sender() == m_advancedUi->fgColorButton); + QColor oldColor = QColor(m_advancedUi->fgColorButton->property("color").toString()); + if (!isForeground) { + oldColor = QColor(m_advancedUi->bgColorButton->property("color").toString()); + } + + QColorDialog colorDialog; + QColor newColor = colorDialog.getColor(oldColor); + if (newColor.isValid()) { + setupColorButton(isForeground, newColor); + } +} diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index bd9f5cd0f..19c2ebe15 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -99,6 +99,7 @@ private slots: void updateHistoryButtons(const QModelIndex& current, const QModelIndex& previous); void useExpiryPreset(QAction* action); void toggleHideNotes(bool visible); + void pickColor(); #ifdef WITH_XC_SSHAGENT void updateSSHAgent(); void updateSSHAgentAttachment(); @@ -120,6 +121,7 @@ private: #endif void setupProperties(); void setupHistory(); + void setupColorButton(bool foreground, QColor color); bool passwordsEqual(); void setForms(const Entry* entry, bool restore = false); diff --git a/src/gui/entry/EditEntryWidgetAdvanced.ui b/src/gui/entry/EditEntryWidgetAdvanced.ui index 8c729fd7c..9556eee19 100644 --- a/src/gui/entry/EditEntryWidgetAdvanced.ui +++ b/src/gui/entry/EditEntryWidgetAdvanced.ui @@ -2,19 +2,15 @@ EditEntryWidgetAdvanced + + + 0 + 0 + 532 + 364 + + - - 0 - - - 0 - - - 0 - - - 0 - @@ -157,6 +153,96 @@ + + + + + + + Foreground Color: + + + + + + + + 0 + 0 + + + + + 25 + 25 + + + + + + + + + + + Qt::Horizontal + + + QSizePolicy::Maximum + + + + 30 + 20 + + + + + + + + Background Color: + + + + + + + + 0 + 0 + + + + + 25 + 25 + + + + + + + false + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + diff --git a/src/gui/entry/EntryModel.cpp b/src/gui/entry/EntryModel.cpp index 4a583b30e..71828c6ed 100644 --- a/src/gui/entry/EntryModel.cpp +++ b/src/gui/entry/EntryModel.cpp @@ -18,6 +18,7 @@ #include "EntryModel.h" #include +#include #include #include #include @@ -263,10 +264,26 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const font.setStrikeOut(true); } return font; - } else if (role == Qt::TextColorRole) { + } else if (role == Qt::ForegroundRole) { if (entry->hasReferences()) { QPalette p; return QVariant(p.color(QPalette::Active, QPalette::Mid)); + } else if (entry->foregroundColor().isValid()) { + return QVariant(entry->foregroundColor()); + } + } else if (role == Qt::BackgroundRole) { + if (entry->backgroundColor().isValid()) { + return QVariant(entry->backgroundColor()); + } + } else if (role == Qt::TextAlignmentRole) { + if (index.column() == Paperclip) { + return Qt::AlignCenter; + } + } else if (role == Qt::SizeHintRole) { + if (index.column() == Paperclip) { + QFont font; + QFontMetrics fm(font); + return fm.width(PaperClipDisplay) / 2; } } @@ -275,7 +292,9 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + Q_UNUSED(orientation); + + if (role == Qt::DisplayRole) { switch (section) { case ParentGroup: return tr("Group"); @@ -302,6 +321,11 @@ QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int ro case Attachments: return tr("Attachments"); } + } else if (role == Qt::TextAlignmentRole) { + switch (section) { + case Paperclip: + return Qt::AlignCenter; + } } return QVariant();